From 4d3c2cc5668417ebc7c1779fc74be59976f8f8ba Mon Sep 17 00:00:00 2001 From: Tomohiro Kusumi Date: Sun, 22 Dec 2019 14:26:25 +0900 Subject: [PATCH] unittests: add unittest suite for oslib/strsep.c See b8b0e1eea7780a02ff67f0caeba446cc403f1b37 ("unittests: add CUnit based unittest framework") for usage. Signed-off-by: Tomohiro Kusumi --- Makefile | 2 + unittests/oslib/strsep.c | 107 +++++++++++++++++++++++++++++++++++++++ unittests/unittest.c | 1 + unittests/unittest.h | 1 + 4 files changed, 111 insertions(+) create mode 100644 unittests/oslib/strsep.c diff --git a/Makefile b/Makefile index 04fa351b..a77b6f44 100644 --- a/Makefile +++ b/Makefile @@ -323,11 +323,13 @@ 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_OBJS += unittests/oslib/strsep.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_TARGET_OBJS += oslib/strsep.o UT_PROGS = unittests/unittest else UT_OBJS = diff --git a/unittests/oslib/strsep.c b/unittests/oslib/strsep.c new file mode 100644 index 00000000..7f645f40 --- /dev/null +++ b/unittests/oslib/strsep.c @@ -0,0 +1,107 @@ +/* + * Copyright (C) 2019 Tomohiro Kusumi + */ +#include "../unittest.h" + +#ifndef CONFIG_STRSEP +#include "../../oslib/strsep.h" +#else +#include +#endif + +/* + * strsep(3) - "If *stringp is NULL, the strsep() function returns NULL and does + * nothing else." + */ +static void test_strsep_1(void) +{ + char *string = NULL; + const char *p; + + p = strsep(&string, ""); + CU_ASSERT_EQUAL(p, NULL); + CU_ASSERT_EQUAL(string, NULL); + + p = strsep(&string, "ABC"); + CU_ASSERT_EQUAL(p, NULL); + CU_ASSERT_EQUAL(string, NULL); +} + +/* + * strsep(3) - "In case no delimiter was found, the token is taken to be the + * entire string *stringp, and *stringp is made NULL." + */ +static void test_strsep_2(void) +{ + char src[] = "ABCDEFG"; + char *string = src; + const char *p; + + p = strsep(&string, ""); + CU_ASSERT_EQUAL(p, src); + CU_ASSERT_EQUAL(*p, 'A'); + CU_ASSERT_EQUAL(string, NULL); + + string = src; + p = strsep(&string, "@"); + CU_ASSERT_EQUAL(p, src); + CU_ASSERT_EQUAL(*p, 'A'); + CU_ASSERT_EQUAL(string, NULL); +} + +/* + * strsep(3) - "This token is terminated with a '\0' character (by overwriting + * the delimiter) and *stringp is updated to point past the token." + */ +static void test_strsep_3(void) +{ + char src[] = "ABCDEFG"; + char *string = src; + const char *p; + + p = strsep(&string, "ABC"); + CU_ASSERT_EQUAL(p, &src[0]); + CU_ASSERT_EQUAL(*p, '\0'); + CU_ASSERT_EQUAL(strcmp(string, "BCDEFG"), 0); + CU_ASSERT_EQUAL(*string, 'B'); + + p = strsep(&string, "ABC"); + CU_ASSERT_EQUAL(p, &src[1]); + CU_ASSERT_EQUAL(*p, '\0'); + CU_ASSERT_EQUAL(strcmp(string, "CDEFG"), 0); + CU_ASSERT_EQUAL(*string, 'C'); + + p = strsep(&string, "ABC"); + CU_ASSERT_EQUAL(p, &src[2]); + CU_ASSERT_EQUAL(*p, '\0'); + CU_ASSERT_EQUAL(strcmp(string, "DEFG"), 0); + CU_ASSERT_EQUAL(*string, 'D'); + + p = strsep(&string, "ABC"); + CU_ASSERT_EQUAL(p, &src[3]); + CU_ASSERT_EQUAL(*p, 'D'); + CU_ASSERT_EQUAL(string, NULL); +} + +static struct fio_unittest_entry tests[] = { + { + .name = "strsep/1", + .fn = test_strsep_1, + }, + { + .name = "strsep/2", + .fn = test_strsep_2, + }, + { + .name = "strsep/3", + .fn = test_strsep_3, + }, + { + .name = NULL, + }, +}; + +CU_ErrorCode fio_unittest_oslib_strsep(void) +{ + return fio_unittest_add_suite("oslib/strsep.c", NULL, NULL, tests); +} diff --git a/unittests/unittest.c b/unittests/unittest.c index 2f253ba1..66789e4f 100644 --- a/unittests/unittest.c +++ b/unittests/unittest.c @@ -63,6 +63,7 @@ int main(void) fio_unittest_register(fio_unittest_oslib_strlcat); fio_unittest_register(fio_unittest_oslib_strndup); fio_unittest_register(fio_unittest_oslib_strcasestr); + fio_unittest_register(fio_unittest_oslib_strsep); CU_basic_set_mode(CU_BRM_VERBOSE); CU_basic_run_tests(); diff --git a/unittests/unittest.h b/unittests/unittest.h index 1e62afa6..bbc49613 100644 --- a/unittests/unittest.h +++ b/unittests/unittest.h @@ -23,5 +23,6 @@ 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); +CU_ErrorCode fio_unittest_oslib_strsep(void); #endif -- 2.25.1