unittests: add unittest suite for oslib/strsep.c
authorTomohiro Kusumi <kusumi.tomohiro@gmail.com>
Sun, 22 Dec 2019 05:26:25 +0000 (14:26 +0900)
committerTomohiro Kusumi <kusumi.tomohiro@gmail.com>
Sun, 22 Dec 2019 09:36:48 +0000 (18:36 +0900)
See b8b0e1eea7780a02ff67f0caeba446cc403f1b37
("unittests: add CUnit based unittest framework") for usage.

Signed-off-by: Tomohiro Kusumi <kusumi.tomohiro@gmail.com>
Makefile
unittests/oslib/strsep.c [new file with mode: 0644]
unittests/unittest.c
unittests/unittest.h

index 04fa351bfb2c122c9e1e573d5e51a5a086ebd67a..a77b6f440a98b2f3516b26a53b548cfc1e45cefb 100644 (file)
--- 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 (file)
index 0000000..7f645f4
--- /dev/null
@@ -0,0 +1,107 @@
+/*
+ * Copyright (C) 2019 Tomohiro Kusumi <tkusumi@netbsd.org>
+ */
+#include "../unittest.h"
+
+#ifndef CONFIG_STRSEP
+#include "../../oslib/strsep.h"
+#else
+#include <string.h>
+#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);
+}
index 2f253ba19caa59f55d8972c82355d02ab3de7990..66789e4fcc2f0c6f6c9cbc35f51a46e82f86353d 100644 (file)
@@ -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();
index 1e62afa64f3acc9257bc9df8e22d6ea5c9b2f8ac..bbc49613591156c88e974a66ec605d1670c08631 100644 (file)
@@ -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