unittests: add unittest suite for oslib/strlcat.c
authorTomohiro Kusumi <kusumi.tomohiro@gmail.com>
Fri, 26 Oct 2018 16:35:43 +0000 (09:35 -0700)
committerJens Axboe <axboe@kernel.dk>
Fri, 26 Oct 2018 16:24:23 +0000 (10:24 -0600)
Add test cases for oslib/strlcat.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/strlcat.c [new file with mode: 0644]
unittests/unittest.c
unittests/unittest.h

index abcfe4dc3622c3efe7d1dc79d5c41ae772e5a699..dd02612af48b21f553ac50314eb61128cd426c03 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -304,8 +304,10 @@ ifdef CONFIG_HAVE_CUNIT
 UT_OBJS = unittests/unittest.o
 UT_OBJS += unittests/lib/memalign.o
 UT_OBJS += unittests/lib/strntol.o
+UT_OBJS += unittests/oslib/strlcat.o
 UT_TARGET_OBJS = lib/memalign.o
 UT_TARGET_OBJS += lib/strntol.o
+UT_TARGET_OBJS += oslib/strlcat.o
 UT_PROGS = unittests/unittest
 else
 UT_OBJS =
diff --git a/unittests/oslib/strlcat.c b/unittests/oslib/strlcat.c
new file mode 100644 (file)
index 0000000..8d35d41
--- /dev/null
@@ -0,0 +1,52 @@
+#include "../unittest.h"
+
+#ifndef CONFIG_STRLCAT
+#include "../../oslib/strlcat.h"
+#else
+#include <string.h>
+#endif
+
+static void test_strlcat_1(void)
+{
+       char dst[32];
+       char src[] = "test";
+       size_t ret;
+
+       dst[0] = '\0';
+       ret = strlcat(dst, src, sizeof(dst));
+
+       CU_ASSERT_EQUAL(strcmp(dst, "test"), 0);
+       CU_ASSERT_EQUAL(ret, 4); /* total length it tried to create */
+}
+
+static void test_strlcat_2(void)
+{
+       char dst[32];
+       char src[] = "test";
+       size_t ret;
+
+       dst[0] = '\0';
+       ret = strlcat(dst, src, strlen(dst));
+
+       CU_ASSERT_EQUAL(strcmp(dst, ""), 0);
+       CU_ASSERT_EQUAL(ret, 4); /* total length it tried to create */
+}
+
+static struct fio_unittest_entry tests[] = {
+       {
+               .name   = "strlcat/1",
+               .fn     = test_strlcat_1,
+       },
+       {
+               .name   = "strlcat/2",
+               .fn     = test_strlcat_2,
+       },
+       {
+               .name   = NULL,
+       },
+};
+
+CU_ErrorCode fio_unittest_oslib_strlcat(void)
+{
+       return fio_unittest_add_suite("oslib/strlcat.c", NULL, NULL, tests);
+}
index dc627b4e16ad1293ef6fa02254f8602749852088..20f92050d87015f1268a5e1d527e47fe59b3ac02 100644 (file)
@@ -60,6 +60,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);
 
        CU_basic_set_mode(CU_BRM_VERBOSE);
        CU_basic_run_tests();
index e0121ec341b6901220327f0eb4aa79aae9ba43c1..f45e193a7ae247a23170d229f720bb2f9e6820ab 100644 (file)
@@ -20,5 +20,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);
 
 #endif