[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 <karthyuom@gmail.com>
This commit is contained in:
Karthick Ariyaratnam 2024-05-14 18:54:33 -04:00 committed by GitHub
parent 72f2a8743c
commit 741ee702ca
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 58 additions and 59 deletions

View File

@ -6932,7 +6932,6 @@ struct serverTest {
{"ziplist", ziplistTest},
{"quicklist", quicklistTest},
{"zipmap", zipmapTest},
{"zmalloc", zmalloc_test},
{"dict", dictTest},
{"listpack", listpackTest},
};

View File

@ -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},
};

53
src/unit/test_zmalloc.c Normal file
View File

@ -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;
}

View File

@ -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

View File

@ -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 */