From 741ee702cacd402dedeef75c7b8cbd6a1f091b7a Mon Sep 17 00:00:00 2001 From: Karthick Ariyaratnam Date: Tue, 14 May 2024 18:54:33 -0400 Subject: [PATCH] [New] Migrate zmalloc.c unit tests to new test framework. (#493) This is the actual PR which is created to migrate all tests related to zmalloc into new test framework as part of the parent issue https://github.com/valkey-io/valkey/issues/428. Signed-off-by: Karthick Ariyaratnam --- src/server.c | 1 - src/unit/test_files.h | 5 ++++ src/unit/test_zmalloc.c | 53 ++++++++++++++++++++++++++++++++++++++++ src/zmalloc.c | 54 ----------------------------------------- src/zmalloc.h | 4 --- 5 files changed, 58 insertions(+), 59 deletions(-) create mode 100644 src/unit/test_zmalloc.c diff --git a/src/server.c b/src/server.c index a5f40c8991..fe54d9eb82 100644 --- a/src/server.c +++ b/src/server.c @@ -6932,7 +6932,6 @@ struct serverTest { {"ziplist", ziplistTest}, {"quicklist", quicklistTest}, {"zipmap", zipmapTest}, - {"zmalloc", zmalloc_test}, {"dict", dictTest}, {"listpack", listpackTest}, }; diff --git a/src/unit/test_files.h b/src/unit/test_files.h index 40361454e9..53fc0d2eb8 100644 --- a/src/unit/test_files.h +++ b/src/unit/test_files.h @@ -30,6 +30,9 @@ int test_ll2string(int argc, char **argv, int flags); int test_ld2string(int argc, char **argv, int flags); int test_fixedpoint_d2string(int argc, char **argv, int flags); int test_reclaimFilePageCache(int argc, char **argv, int flags); +int test_zmallocInitialUsedMemory(int argc, char **argv, int flags); +int test_zmallocAllocReallocCallocAndFree(int argc, char **argv, int flags); +int test_zmallocAllocZeroByteAndFree(int argc, char **argv, int flags); unitTest __test_crc64_c[] = {{"test_crc64", test_crc64}, {NULL, NULL}}; unitTest __test_crc64combine_c[] = {{"test_crc64combine", test_crc64combine}, {NULL, NULL}}; @@ -39,6 +42,7 @@ unitTest __test_kvstore_c[] = {{"test_kvstoreAdd16Keys", test_kvstoreAdd16Keys}, unitTest __test_sds_c[] = {{"test_sds", test_sds}, {NULL, NULL}}; unitTest __test_sha1_c[] = {{"test_sha1", test_sha1}, {NULL, NULL}}; unitTest __test_util_c[] = {{"test_string2ll", test_string2ll}, {"test_string2l", test_string2l}, {"test_ll2string", test_ll2string}, {"test_ld2string", test_ld2string}, {"test_fixedpoint_d2string", test_fixedpoint_d2string}, {"test_reclaimFilePageCache", test_reclaimFilePageCache}, {NULL, NULL}}; +unitTest __test_zmalloc_c[] = {{"test_zmallocInitialUsedMemory", test_zmallocInitialUsedMemory}, {"test_zmallocAllocReallocCallocAndFree", test_zmallocAllocReallocCallocAndFree}, {"test_zmallocAllocZeroByteAndFree", test_zmallocAllocZeroByteAndFree}, {NULL, NULL}}; struct unitTestSuite { char *filename; @@ -52,4 +56,5 @@ struct unitTestSuite { {"test_sds.c", __test_sds_c}, {"test_sha1.c", __test_sha1_c}, {"test_util.c", __test_util_c}, + {"test_zmalloc.c", __test_zmalloc_c}, }; diff --git a/src/unit/test_zmalloc.c b/src/unit/test_zmalloc.c new file mode 100644 index 0000000000..6c1d03e8e1 --- /dev/null +++ b/src/unit/test_zmalloc.c @@ -0,0 +1,53 @@ +#include "../zmalloc.h" +#include "test_help.h" + +int test_zmallocInitialUsedMemory(int argc, char **argv, int flags) { + UNUSED(argc); + UNUSED(argv); + UNUSED(flags); + + TEST_ASSERT(zmalloc_used_memory() == 0); + + return 0; +} + +int test_zmallocAllocReallocCallocAndFree(int argc, char **argv, int flags) { + UNUSED(argc); + UNUSED(argv); + UNUSED(flags); + + void *ptr, *ptr2; + + ptr = zmalloc(123); + TEST_PRINT_INFO("Allocated 123 bytes; used: %zu\n", zmalloc_used_memory()); + + ptr = zrealloc(ptr, 456); + TEST_PRINT_INFO("Reallocated to 456 bytes; used: %zu\n", zmalloc_used_memory()); + + ptr2 = zcalloc(123); + TEST_PRINT_INFO("Callocated 123 bytes; used: %zu\n", zmalloc_used_memory()); + + zfree(ptr); + zfree(ptr2); + TEST_PRINT_INFO("Freed pointers; used: %zu\n", zmalloc_used_memory()); + + TEST_ASSERT(zmalloc_used_memory() == 0); + + return 0; +} + +int test_zmallocAllocZeroByteAndFree(int argc, char **argv, int flags) { + UNUSED(argc); + UNUSED(argv); + UNUSED(flags); + + void *ptr; + + ptr = zmalloc(0); + TEST_PRINT_INFO("Allocated 0 bytes; used: %zu\n", zmalloc_used_memory()); + zfree(ptr); + + TEST_ASSERT(zmalloc_used_memory() == 0); + + return 0; +} diff --git a/src/zmalloc.c b/src/zmalloc.c index 550752240f..f6318d36d6 100644 --- a/src/zmalloc.c +++ b/src/zmalloc.c @@ -907,57 +907,3 @@ size_t zmalloc_get_memory_size(void) { return 0L; /* Unknown OS. */ #endif } - -#ifdef SERVER_TEST -#include "testhelp.h" -#include "serverassert.h" - -#define TEST(name) printf("test — %s\n", name); - -int zmalloc_test(int argc, char **argv, int flags) { - void *ptr, *ptr2; - - UNUSED(argc); - UNUSED(argv); - UNUSED(flags); - - printf("Malloc prefix size: %d\n", (int) PREFIX_SIZE); - - TEST("Initial used memory is 0") { - assert(zmalloc_used_memory() == 0); - } - - TEST("Allocated 123 bytes") { - ptr = zmalloc(123); - printf("Allocated 123 bytes; used: %zu\n", zmalloc_used_memory()); - } - - TEST("Reallocated to 456 bytes") { - ptr = zrealloc(ptr, 456); - printf("Reallocated to 456 bytes; used: %zu\n", zmalloc_used_memory()); - } - - TEST("Callocated 123 bytes") { - ptr2 = zcalloc(123); - printf("Callocated 123 bytes; used: %zu\n", zmalloc_used_memory()); - } - - TEST("Freed pointers") { - zfree(ptr); - zfree(ptr2); - printf("Freed pointers; used: %zu\n", zmalloc_used_memory()); - } - - TEST("Allocated 0 bytes") { - ptr = zmalloc(0); - printf("Allocated 0 bytes; used: %zu\n", zmalloc_used_memory()); - zfree(ptr); - } - - TEST("At the end used memory is 0") { - assert(zmalloc_used_memory() == 0); - } - - return 0; -} -#endif diff --git a/src/zmalloc.h b/src/zmalloc.h index 1cf4af96c4..559d27ac2b 100644 --- a/src/zmalloc.h +++ b/src/zmalloc.h @@ -168,8 +168,4 @@ __attribute__((alloc_size(2),noinline)) void *extend_to_usable(void *ptr, size_t int get_proc_stat_ll(int i, long long *res); -#ifdef SERVER_TEST -int zmalloc_test(int argc, char **argv, int flags); -#endif - #endif /* __ZMALLOC_H */