summaryrefslogtreecommitdiff
path: root/unittests
diff options
context:
space:
mode:
authorTomohiro Kusumi <kusumi.tomohiro@gmail.com>2018-10-26 09:35:42 -0700
committerJens Axboe <axboe@kernel.dk>2018-10-26 10:24:22 -0600
commitce87503183fc2ecb71b105a56daf49ddc897b9f9 (patch)
tree83631d042961d0eed85b2f0c77eed1fb553430b1 /unittests
parente37155efe2b60fe0acc5051e774d2eb49e4b7ca7 (diff)
downloadfio-ce87503183fc2ecb71b105a56daf49ddc897b9f9.tar.gz
fio-ce87503183fc2ecb71b105a56daf49ddc897b9f9.tar.bz2
unittests: add unittest suite for lib/strntol.c
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>
Diffstat (limited to 'unittests')
-rw-r--r--unittests/lib/strntol.c59
-rw-r--r--unittests/unittest.c1
-rw-r--r--unittests/unittest.h1
3 files changed, 61 insertions, 0 deletions
diff --git a/unittests/lib/strntol.c b/unittests/lib/strntol.c
new file mode 100644
index 00000000..14adde2c
--- /dev/null
+++ b/unittests/lib/strntol.c
@@ -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);
+}
diff --git a/unittests/unittest.c b/unittests/unittest.c
index 204897a3..dc627b4e 100644
--- a/unittests/unittest.c
+++ b/unittests/unittest.c
@@ -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();
diff --git a/unittests/unittest.h b/unittests/unittest.h
index 5d170af7..e0121ec3 100644
--- a/unittests/unittest.h
+++ b/unittests/unittest.h
@@ -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