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

index 9f27ff16058e970a46a737b2e8dd1a768a3eff30..abcfe4dc3622c3efe7d1dc79d5c41ae772e5a699 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -303,7 +303,9 @@ PROGS += $(T_PROGS)
 ifdef CONFIG_HAVE_CUNIT
 UT_OBJS = unittests/unittest.o
 UT_OBJS += unittests/lib/memalign.o
+UT_OBJS += unittests/lib/strntol.o
 UT_TARGET_OBJS = lib/memalign.o
+UT_TARGET_OBJS += lib/strntol.o
 UT_PROGS = unittests/unittest
 else
 UT_OBJS =
diff --git a/unittests/lib/strntol.c b/unittests/lib/strntol.c
new file mode 100644 (file)
index 0000000..14adde2
--- /dev/null
@@ -0,0 +1,59 @@
+#include "../unittest.h"
+
+#include "../../lib/strntol.h"
+
+static void test_strntol_1(void)
+{
+       char s[] = "12345";
+       char *endp = NULL;
+       long ret = strntol(s, strlen(s), &endp, 10);
+
+       CU_ASSERT_EQUAL(ret, 12345);
+       CU_ASSERT_NOT_EQUAL(endp, NULL);
+       CU_ASSERT_EQUAL(*endp, '\0');
+}
+
+static void test_strntol_2(void)
+{
+       char s[] = "     12345";
+       char *endp = NULL;
+       long ret = strntol(s, strlen(s), &endp, 10);
+
+       CU_ASSERT_EQUAL(ret, 12345);
+       CU_ASSERT_NOT_EQUAL(endp, NULL);
+       CU_ASSERT_EQUAL(*endp, '\0');
+}
+
+static void test_strntol_3(void)
+{
+       char s[] = "0x12345";
+       char *endp = NULL;
+       long ret = strntol(s, strlen(s), &endp, 16);
+
+       CU_ASSERT_EQUAL(ret, 0x12345);
+       CU_ASSERT_NOT_EQUAL(endp, NULL);
+       CU_ASSERT_EQUAL(*endp, '\0');
+}
+
+static struct fio_unittest_entry tests[] = {
+       {
+               .name   = "strntol/1",
+               .fn     = test_strntol_1,
+       },
+       {
+               .name   = "strntol/2",
+               .fn     = test_strntol_2,
+       },
+       {
+               .name   = "strntol/3",
+               .fn     = test_strntol_3,
+       },
+       {
+               .name   = NULL,
+       },
+};
+
+CU_ErrorCode fio_unittest_lib_strntol(void)
+{
+       return fio_unittest_add_suite("lib/strntol.c", NULL, NULL, tests);
+}
index 204897a3a1a591ff7b3fe713b97049fdcbbc1143..dc627b4e16ad1293ef6fa02254f8602749852088 100644 (file)
@@ -59,6 +59,7 @@ int main(void)
        }
 
        fio_unittest_register(fio_unittest_lib_memalign);
+       fio_unittest_register(fio_unittest_lib_strntol);
 
        CU_basic_set_mode(CU_BRM_VERBOSE);
        CU_basic_run_tests();
index 5d170af7ceea6972617bb2b96fd7c695d7b8c66b..e0121ec341b6901220327f0eb4aa79aae9ba43c1 100644 (file)
@@ -19,5 +19,6 @@ CU_ErrorCode fio_unittest_add_suite(const char*, CU_InitializeFunc,
        CU_CleanupFunc, struct fio_unittest_entry*);
 
 CU_ErrorCode fio_unittest_lib_memalign(void);
+CU_ErrorCode fio_unittest_lib_strntol(void);
 
 #endif