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

A workaround code to emulate smalloc()/sfree() was needed since
3114b675fd("fio: enable cross-thread overlap checking with processes")
introduced dependency on smalloc()/sfree() which has dependency
on fio code.

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

index 461d784268ce442644bc0c8071d6e8179094b70a..9f27ff16058e970a46a737b2e8dd1a768a3eff30 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -302,7 +302,8 @@ PROGS += $(T_PROGS)
 
 ifdef CONFIG_HAVE_CUNIT
 UT_OBJS = unittests/unittest.o
-UT_TARGET_OBJS =
+UT_OBJS += unittests/lib/memalign.o
+UT_TARGET_OBJS = lib/memalign.o
 UT_PROGS = unittests/unittest
 else
 UT_OBJS =
diff --git a/unittests/lib/memalign.c b/unittests/lib/memalign.c
new file mode 100644 (file)
index 0000000..854c274
--- /dev/null
@@ -0,0 +1,27 @@
+#include "../unittest.h"
+
+#include "../../lib/memalign.h"
+
+static void test_memalign_1(void)
+{
+       size_t align = 4096;
+       void *p = fio_memalign(align, 1234, false);
+
+       if (p)
+               CU_ASSERT_EQUAL(((int)(uintptr_t)p) & (align - 1), 0);
+}
+
+static struct fio_unittest_entry tests[] = {
+       {
+               .name   = "memalign/1",
+               .fn     = test_memalign_1,
+       },
+       {
+               .name   = NULL,
+       },
+};
+
+CU_ErrorCode fio_unittest_lib_memalign(void)
+{
+       return fio_unittest_add_suite("lib/memalign.c", NULL, NULL, tests);
+}
index bc75bb6e0e930c3e864bbd4154c1e1b582f3419f..204897a3a1a591ff7b3fe713b97049fdcbbc1143 100644 (file)
@@ -8,6 +8,17 @@
 
 #include "./unittest.h"
 
+/* XXX workaround lib/memalign.c's dependency on smalloc.c */
+void *smalloc(size_t size)
+{
+       return malloc(size);
+}
+
+void sfree(void *ptr)
+{
+       free(ptr);
+}
+
 CU_ErrorCode fio_unittest_add_suite(const char *name, CU_InitializeFunc initfn,
        CU_CleanupFunc cleanfn, struct fio_unittest_entry *tvec)
 {
@@ -47,8 +58,7 @@ int main(void)
                exit(1);
        }
 
-       /* Register unittest suites. */
-       fio_unittest_register(NULL); /* prevent unused warning */
+       fio_unittest_register(fio_unittest_lib_memalign);
 
        CU_basic_set_mode(CU_BRM_VERBOSE);
        CU_basic_run_tests();
index 4ac6366bbbf364e740628f4dbc3e62bbff7c5e15..5d170af7ceea6972617bb2b96fd7c695d7b8c66b 100644 (file)
@@ -1,6 +1,8 @@
 #ifndef FIO_UNITTEST_H
 #define FIO_UNITTEST_H
 
+#include <sys/types.h>
+
 #include <CUnit/CUnit.h>
 #include <CUnit/Basic.h>
 
@@ -9,7 +11,13 @@ struct fio_unittest_entry {
        CU_TestFunc fn;
 };
 
+/* XXX workaround lib/memalign.c's dependency on smalloc.c */
+void *smalloc(size_t);
+void sfree(void*);
+
 CU_ErrorCode fio_unittest_add_suite(const char*, CU_InitializeFunc,
        CU_CleanupFunc, struct fio_unittest_entry*);
 
+CU_ErrorCode fio_unittest_lib_memalign(void);
+
 #endif