summaryrefslogtreecommitdiff
path: root/unittests
diff options
context:
space:
mode:
authorTomohiro Kusumi <kusumi.tomohiro@gmail.com>2018-10-26 09:35:44 -0700
committerJens Axboe <axboe@kernel.dk>2018-10-26 10:24:25 -0600
commit2f84d0c71475264d6e14d782ecb38f63a22bc3a7 (patch)
tree646543cc660e6f805f99f86446d2bd5bc978c668 /unittests
parentcdcd7e998f7b11371b73f646a750efc4e6eb378c (diff)
downloadfio-2f84d0c71475264d6e14d782ecb38f63a22bc3a7.tar.gz
fio-2f84d0c71475264d6e14d782ecb38f63a22bc3a7.tar.bz2
unittests: add unittest suite for oslib/strndup.c
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>
Diffstat (limited to 'unittests')
-rw-r--r--unittests/oslib/strndup.c63
-rw-r--r--unittests/unittest.c1
-rw-r--r--unittests/unittest.h1
3 files changed, 65 insertions, 0 deletions
diff --git a/unittests/oslib/strndup.c b/unittests/oslib/strndup.c
new file mode 100644
index 00000000..2d1baf1a
--- /dev/null
+++ b/unittests/oslib/strndup.c
@@ -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);
+}
diff --git a/unittests/unittest.c b/unittests/unittest.c
index 20f92050..1166e6ef 100644
--- a/unittests/unittest.c
+++ b/unittests/unittest.c
@@ -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();
diff --git a/unittests/unittest.h b/unittests/unittest.h
index f45e193a..d3e3822f 100644
--- a/unittests/unittest.h
+++ b/unittests/unittest.h
@@ -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