summaryrefslogtreecommitdiff
path: root/unittests
diff options
context:
space:
mode:
authorTomohiro Kusumi <kusumi.tomohiro@gmail.com>2019-12-22 14:26:25 +0900
committerTomohiro Kusumi <kusumi.tomohiro@gmail.com>2019-12-22 18:36:48 +0900
commit4d3c2cc5668417ebc7c1779fc74be59976f8f8ba (patch)
tree2ff80f56d03342303d81404df08614164f11cc8b /unittests
parentd2292c7d8ce8a30788a38ff6dd0a8b9c0f930a63 (diff)
downloadfio-4d3c2cc5668417ebc7c1779fc74be59976f8f8ba.tar.gz
fio-4d3c2cc5668417ebc7c1779fc74be59976f8f8ba.tar.bz2
unittests: add unittest suite for oslib/strsep.c
See b8b0e1eea7780a02ff67f0caeba446cc403f1b37 ("unittests: add CUnit based unittest framework") for usage. Signed-off-by: Tomohiro Kusumi <kusumi.tomohiro@gmail.com>
Diffstat (limited to 'unittests')
-rw-r--r--unittests/oslib/strsep.c107
-rw-r--r--unittests/unittest.c1
-rw-r--r--unittests/unittest.h1
3 files changed, 109 insertions, 0 deletions
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 <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);
+}
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