Skip to content

Commit

Permalink
[New] Migrate zmalloc.c unit tests to new test framework. (#493)
Browse files Browse the repository at this point in the history
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
#428.

Signed-off-by: Karthick Ariyaratnam <[email protected]>
Signed-off-by: adetunjii <[email protected]>
  • Loading branch information
karthyuom authored and adetunjii committed May 15, 2024
1 parent ac01b70 commit 7e8380c
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 59 deletions.
1 change: 0 additions & 1 deletion src/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -6931,7 +6931,6 @@ struct serverTest {
{"ziplist", ziplistTest},
{"quicklist", quicklistTest},
{"zipmap", zipmapTest},
{"zmalloc", zmalloc_test},
{"dict", dictTest},
{"listpack", listpackTest},
};
Expand Down
5 changes: 5 additions & 0 deletions src/unit/test_files.h
Original file line number Diff line number Diff line change
Expand Up @@ -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}};
Expand All @@ -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;
Expand All @@ -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 changes: 53 additions & 0 deletions src/unit/test_zmalloc.c
Original file line number Diff line number Diff line change
@@ -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;
}
54 changes: 0 additions & 54 deletions src/zmalloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -906,57 +906,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
4 changes: 0 additions & 4 deletions src/zmalloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */

0 comments on commit 7e8380c

Please sign in to comment.