From e37155efe2b60fe0acc5051e774d2eb49e4b7ca7 Mon Sep 17 00:00:00 2001 From: Tomohiro Kusumi Date: Fri, 26 Oct 2018 09:35:41 -0700 Subject: [PATCH] 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 Signed-off-by: Jens Axboe --- Makefile | 3 ++- unittests/lib/memalign.c | 27 +++++++++++++++++++++++++++ unittests/unittest.c | 14 ++++++++++++-- unittests/unittest.h | 8 ++++++++ 4 files changed, 49 insertions(+), 3 deletions(-) create mode 100644 unittests/lib/memalign.c 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 + #include #include @@ -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 -- 2.25.1