From: Tomohiro Kusumi Date: Fri, 26 Oct 2018 16:35:41 +0000 (-0700) Subject: unittests: add unittest suite for lib/memalign.c X-Git-Tag: fio-3.12~7 X-Git-Url: https://git.kernel.dk/?a=commitdiff_plain;h=e37155efe2b60fe0acc5051e774d2eb49e4b7ca7;p=fio.git 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 --- 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