unittests: add unittest suite for oslib/strndup.c
authorTomohiro Kusumi <kusumi.tomohiro@gmail.com>
Fri, 26 Oct 2018 16:35:44 +0000 (09:35 -0700)
committerJens Axboe <axboe@kernel.dk>
Fri, 26 Oct 2018 16:24:25 +0000 (10:24 -0600)
Add test cases for oslib/strndup.c as an example of unittest.

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

index dd02612af48b21f553ac50314eb61128cd426c03..5ac568e980b93bc1ad710331671af0b617eb6e92 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -305,9 +305,11 @@ UT_OBJS = unittests/unittest.o
 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_TARGET_OBJS = lib/memalign.o
 UT_TARGET_OBJS += lib/strntol.o
 UT_TARGET_OBJS += oslib/strlcat.o
+UT_TARGET_OBJS += oslib/strndup.o
 UT_PROGS = unittests/unittest
 else
 UT_OBJS =
diff --git a/unittests/oslib/strndup.c b/unittests/oslib/strndup.c
new file mode 100644 (file)
index 0000000..2d1baf1
--- /dev/null
@@ -0,0 +1,63 @@
+#include "../unittest.h"
+
+#ifndef CONFIG_HAVE_STRNDUP
+#include "../../oslib/strndup.h"
+#else
+#include <string.h>
+#endif
+
+static void test_strndup_1(void)
+{
+       char s[] = "test";
+       char *p = strndup(s, 3);
+
+       if (p) {
+               CU_ASSERT_EQUAL(strcmp(p, "tes"), 0);
+               CU_ASSERT_EQUAL(strlen(p), 3);
+       }
+}
+
+static void test_strndup_2(void)
+{
+       char s[] = "test";
+       char *p = strndup(s, 4);
+
+       if (p) {
+               CU_ASSERT_EQUAL(strcmp(p, s), 0);
+               CU_ASSERT_EQUAL(strlen(p), 4);
+       }
+}
+
+static void test_strndup_3(void)
+{
+       char s[] = "test";
+       char *p = strndup(s, 5);
+
+       if (p) {
+               CU_ASSERT_EQUAL(strcmp(p, s), 0);
+               CU_ASSERT_EQUAL(strlen(p), 4);
+       }
+}
+
+static struct fio_unittest_entry tests[] = {
+       {
+               .name   = "strndup/1",
+               .fn     = test_strndup_1,
+       },
+       {
+               .name   = "strndup/2",
+               .fn     = test_strndup_2,
+       },
+       {
+               .name   = "strndup/3",
+               .fn     = test_strndup_3,
+       },
+       {
+               .name   = NULL,
+       },
+};
+
+CU_ErrorCode fio_unittest_oslib_strndup(void)
+{
+       return fio_unittest_add_suite("oslib/strndup.c", NULL, NULL, tests);
+}
index 20f92050d87015f1268a5e1d527e47fe59b3ac02..1166e6efe05cffaa5a955cc3c5997488332b173d 100644 (file)
@@ -61,6 +61,7 @@ int main(void)
        fio_unittest_register(fio_unittest_lib_memalign);
        fio_unittest_register(fio_unittest_lib_strntol);
        fio_unittest_register(fio_unittest_oslib_strlcat);
+       fio_unittest_register(fio_unittest_oslib_strndup);
 
        CU_basic_set_mode(CU_BRM_VERBOSE);
        CU_basic_run_tests();
index f45e193a7ae247a23170d229f720bb2f9e6820ab..d3e3822f34a5264b91026e8205b2db4015518c88 100644 (file)
@@ -21,5 +21,6 @@ CU_ErrorCode fio_unittest_add_suite(const char*, CU_InitializeFunc,
 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);
 
 #endif