From d2292c7d8ce8a30788a38ff6dd0a8b9c0f930a63 Mon Sep 17 00:00:00 2001 From: Tomohiro Kusumi Date: Sun, 22 Dec 2019 01:46:28 +0900 Subject: [PATCH] unittests: add unittest suite for oslib/strcasestr.c See b8b0e1eea7780a02ff67f0caeba446cc403f1b37 ("unittests: add CUnit based unittest framework") for usage. Signed-off-by: Tomohiro Kusumi --- Makefile | 2 + unittests/oslib/strcasestr.c | 87 ++++++++++++++++++++++++++++++++++++ unittests/unittest.c | 1 + unittests/unittest.h | 1 + 4 files changed, 91 insertions(+) create mode 100644 unittests/oslib/strcasestr.c diff --git a/Makefile b/Makefile index 4a07fab3..04fa351b 100644 --- a/Makefile +++ b/Makefile @@ -322,10 +322,12 @@ UT_OBJS += unittests/lib/memalign.o UT_OBJS += unittests/lib/strntol.o UT_OBJS += unittests/oslib/strlcat.o UT_OBJS += unittests/oslib/strndup.o +UT_OBJS += unittests/oslib/strcasestr.o UT_TARGET_OBJS = lib/memalign.o UT_TARGET_OBJS += lib/strntol.o UT_TARGET_OBJS += oslib/strlcat.o UT_TARGET_OBJS += oslib/strndup.o +UT_TARGET_OBJS += oslib/strcasestr.o UT_PROGS = unittests/unittest else UT_OBJS = diff --git a/unittests/oslib/strcasestr.c b/unittests/oslib/strcasestr.c new file mode 100644 index 00000000..19a2de37 --- /dev/null +++ b/unittests/oslib/strcasestr.c @@ -0,0 +1,87 @@ +/* + * Copyright (C) 2019 Tomohiro Kusumi + */ +#include "../unittest.h" + +#ifndef CONFIG_STRCASESTR +#include "../../oslib/strcasestr.h" +#else +#include +#endif + +static void test_strcasestr_1(void) +{ + const char *haystack = "0123456789"; + const char *p; + + p = strcasestr(haystack, "012"); + CU_ASSERT_EQUAL(p, haystack); + + p = strcasestr(haystack, "12345"); + CU_ASSERT_EQUAL(p, haystack + 1); + + p = strcasestr(haystack, "1234567890"); + CU_ASSERT_EQUAL(p, NULL); + + p = strcasestr(haystack, ""); + CU_ASSERT_EQUAL(p, haystack); /* is this expected ? */ +} + +static void test_strcasestr_2(void) +{ + const char *haystack = "ABCDEFG"; + const char *p; + + p = strcasestr(haystack, "ABC"); + CU_ASSERT_EQUAL(p, haystack); + + p = strcasestr(haystack, "BCD"); + CU_ASSERT_EQUAL(p, haystack + 1); + + p = strcasestr(haystack, "ABCDEFGH"); + CU_ASSERT_EQUAL(p, NULL); + + p = strcasestr(haystack, ""); + CU_ASSERT_EQUAL(p, haystack); /* is this expected ? */ +} + +static void test_strcasestr_3(void) +{ + const char *haystack = "ABCDEFG"; + const char *p; + + p = strcasestr(haystack, "AbC"); + CU_ASSERT_EQUAL(p, haystack); + + p = strcasestr(haystack, "bCd"); + CU_ASSERT_EQUAL(p, haystack + 1); + + p = strcasestr(haystack, "AbcdEFGH"); + CU_ASSERT_EQUAL(p, NULL); + + p = strcasestr(haystack, ""); + CU_ASSERT_EQUAL(p, haystack); /* is this expected ? */ +} + +static struct fio_unittest_entry tests[] = { + { + .name = "strcasestr/1", + .fn = test_strcasestr_1, + }, + { + .name = "strcasestr/2", + .fn = test_strcasestr_2, + }, + { + .name = "strcasestr/3", + .fn = test_strcasestr_3, + }, + { + .name = NULL, + }, +}; + +CU_ErrorCode fio_unittest_oslib_strcasestr(void) +{ + return fio_unittest_add_suite("oslib/strcasestr.c", NULL, NULL, tests); +} diff --git a/unittests/unittest.c b/unittests/unittest.c index 1166e6ef..2f253ba1 100644 --- a/unittests/unittest.c +++ b/unittests/unittest.c @@ -62,6 +62,7 @@ int main(void) fio_unittest_register(fio_unittest_lib_strntol); fio_unittest_register(fio_unittest_oslib_strlcat); fio_unittest_register(fio_unittest_oslib_strndup); + fio_unittest_register(fio_unittest_oslib_strcasestr); CU_basic_set_mode(CU_BRM_VERBOSE); CU_basic_run_tests(); diff --git a/unittests/unittest.h b/unittests/unittest.h index d3e3822f..1e62afa6 100644 --- a/unittests/unittest.h +++ b/unittests/unittest.h @@ -22,5 +22,6 @@ CU_ErrorCode fio_unittest_lib_memalign(void); CU_ErrorCode fio_unittest_lib_strntol(void); CU_ErrorCode fio_unittest_oslib_strlcat(void); CU_ErrorCode fio_unittest_oslib_strndup(void); +CU_ErrorCode fio_unittest_oslib_strcasestr(void); #endif -- 2.25.1