summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTomohiro Kusumi <kusumi.tomohiro@gmail.com>2018-10-26 09:35:41 -0700
committerJens Axboe <axboe@kernel.dk>2018-10-26 10:24:21 -0600
commite37155efe2b60fe0acc5051e774d2eb49e4b7ca7 (patch)
tree1b75ab56cd8232e57ac09fffdc49fe34d80e6577
parentb8b0e1eea7780a02ff67f0caeba446cc403f1b37 (diff)
downloadfio-e37155efe2b60fe0acc5051e774d2eb49e4b7ca7.tar.gz
fio-e37155efe2b60fe0acc5051e774d2eb49e4b7ca7.tar.bz2
unittests: add unittest suite for lib/memalign.c
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>
-rw-r--r--Makefile3
-rw-r--r--unittests/lib/memalign.c27
-rw-r--r--unittests/unittest.c14
-rw-r--r--unittests/unittest.h8
4 files changed, 49 insertions, 3 deletions
diff --git a/Makefile b/Makefile
index 461d7842..9f27ff16 100644
--- 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
index 00000000..854c2744
--- /dev/null
+++ b/unittests/lib/memalign.c
@@ -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);
+}
diff --git a/unittests/unittest.c b/unittests/unittest.c
index bc75bb6e..204897a3 100644
--- a/unittests/unittest.c
+++ b/unittests/unittest.c
@@ -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();
diff --git a/unittests/unittest.h b/unittests/unittest.h
index 4ac6366b..5d170af7 100644
--- a/unittests/unittest.h
+++ b/unittests/unittest.h
@@ -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