Skip to content

Commit

Permalink
[New] Migrate zmalloc.c unit tests to new test framework. (valkey-io#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
valkey-io#428.

Signed-off-by: Karthick Ariyaratnam <karthyuom@gmail.com>
  • Loading branch information
karthyuom authored and hallmason17 committed May 15, 2024
1 parent 73d26cc commit b86d0a1
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 67 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 {
} serverTests[] = {
{"quicklist", quicklistTest},
{"zipmap", zipmapTest},
{"zmalloc", zmalloc_test},
{"dict", dictTest},
{"listpack", listpackTest},
};
Expand Down
13 changes: 9 additions & 4 deletions src/unit/test_files.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ int test_ziplistGetLastElement(int argc, char **argv, int flags);
int test_ziplistGetFirstElement(int argc, char **argv, int flags);
int test_ziplistGetElementOutOfRangeReverse(int argc, char **argv, int flags);
int test_ziplistIterateThroughFullList(int argc, char **argv, int flags);
int test_ziplistIterateThroughListFrom1ToEnd(int argc, char **argv, int flags);
int test_ziplistIterateThroughListFrom2ToEnd(int argc, char **argv, int flags);
int test_ziplistIterateThroughStartOutOfRange(int argc, char **argv, int flags);
int test_ziplistInterateThroughListFrom1ToEnd(int argc, char **argv, int flags);
int test_ziplistInterateThroughListFrom2ToEnd(int argc, char **argv, int flags);
int test_ziplistInterateThroughStartOutOfRange(int argc, char **argv, int flags);
int test_ziplistIterateBackToFront(int argc, char **argv, int flags);
int test_ziplistIterateBackToFrontDeletingAllItems(int argc, char **argv, int flags);
int test_ziplistDeleteInclusiveRange0To0(int argc, char **argv, int flags);
Expand All @@ -60,6 +60,9 @@ int test_ziplistStressWithRandomPayloadsOfDifferentEncoding(int argc, char **arg
int test_ziplistCascadeUpdateEdgeCases(int argc, char **argv, int flags);
int test_ziplistInsertEdgeCase(int argc, char **argv, int flags);
int test_ziplistBenchmarks(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 @@ -69,7 +72,8 @@ 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_ziplist_c[] = {{"test_ziplistCreateIntList", test_ziplistCreateIntList}, {"test_ziplistPop", test_ziplistPop}, {"test_ziplistGetElementAtIndex3", test_ziplistGetElementAtIndex3}, {"test_ziplistGetElementOutOfRange", test_ziplistGetElementOutOfRange}, {"test_ziplistGetLastElement", test_ziplistGetLastElement}, {"test_ziplistGetFirstElement", test_ziplistGetFirstElement}, {"test_ziplistGetElementOutOfRangeReverse", test_ziplistGetElementOutOfRangeReverse}, {"test_ziplistIterateThroughFullList", test_ziplistIterateThroughFullList}, {"test_ziplistIterateThroughListFrom1ToEnd", test_ziplistIterateThroughListFrom1ToEnd}, {"test_ziplistIterateThroughListFrom2ToEnd", test_ziplistIterateThroughListFrom2ToEnd}, {"test_ziplistIterateThroughStartOutOfRange", test_ziplistIterateThroughStartOutOfRange}, {"test_ziplistIterateBackToFront", test_ziplistIterateBackToFront}, {"test_ziplistIterateBackToFrontDeletingAllItems", test_ziplistIterateBackToFrontDeletingAllItems}, {"test_ziplistDeleteInclusiveRange0To0", test_ziplistDeleteInclusiveRange0To0}, {"test_ziplistDeleteInclusiveRange0To1", test_ziplistDeleteInclusiveRange0To1}, {"test_ziplistDeleteInclusiveRange1To2", test_ziplistDeleteInclusiveRange1To2}, {"test_ziplistDeleteWithStartIndexOutOfRange", test_ziplistDeleteWithStartIndexOutOfRange}, {"test_ziplistDeleteWithNumOverflow", test_ziplistDeleteWithNumOverflow}, {"test_ziplistDeleteFooWhileIterating", test_ziplistDeleteFooWhileIterating}, {"test_ziplistReplaceWithSameSize", test_ziplistReplaceWithSameSize}, {"test_ziplistReplaceWithDifferentSize", test_ziplistReplaceWithDifferentSize}, {"test_ziplistRegressionTestForOver255ByteStrings", test_ziplistRegressionTestForOver255ByteStrings}, {"test_ziplistRegressionTestDeleteNextToLastEntries", test_ziplistRegressionTestDeleteNextToLastEntries}, {"test_ziplistCreateLongListAndCheckIndices", test_ziplistCreateLongListAndCheckIndices}, {"test_ziplistCompareStringWithZiplistEntries", test_ziplistCompareStringWithZiplistEntries}, {"test_ziplistMergeTest", test_ziplistMergeTest}, {"test_ziplistStressWithRandomPayloadsOfDifferentEncoding", test_ziplistStressWithRandomPayloadsOfDifferentEncoding}, {"test_ziplistCascadeUpdateEdgeCases", test_ziplistCascadeUpdateEdgeCases}, {"test_ziplistInsertEdgeCase", test_ziplistInsertEdgeCase}, {"test_ziplistBenchmarks", test_ziplistBenchmarks}, {NULL, NULL}};
unitTest __test_ziplist_c[] = {{"test_ziplistCreateIntList", test_ziplistCreateIntList}, {"test_ziplistPop", test_ziplistPop}, {"test_ziplistGetElementAtIndex3", test_ziplistGetElementAtIndex3}, {"test_ziplistGetElementOutOfRange", test_ziplistGetElementOutOfRange}, {"test_ziplistGetLastElement", test_ziplistGetLastElement}, {"test_ziplistGetFirstElement", test_ziplistGetFirstElement}, {"test_ziplistGetElementOutOfRangeReverse", test_ziplistGetElementOutOfRangeReverse}, {"test_ziplistIterateThroughFullList", test_ziplistIterateThroughFullList}, {"test_ziplistInterateThroughListFrom1ToEnd", test_ziplistInterateThroughListFrom1ToEnd}, {"test_ziplistInterateThroughListFrom2ToEnd", test_ziplistInterateThroughListFrom2ToEnd}, {"test_ziplistInterateThroughStartOutOfRange", test_ziplistInterateThroughStartOutOfRange}, {"test_ziplistIterateBackToFront", test_ziplistIterateBackToFront}, {"test_ziplistIterateBackToFrontDeletingAllItems", test_ziplistIterateBackToFrontDeletingAllItems}, {"test_ziplistDeleteInclusiveRange0To0", test_ziplistDeleteInclusiveRange0To0}, {"test_ziplistDeleteInclusiveRange0To1", test_ziplistDeleteInclusiveRange0To1}, {"test_ziplistDeleteInclusiveRange1To2", test_ziplistDeleteInclusiveRange1To2}, {"test_ziplistDeleteWithStartIndexOutOfRange", test_ziplistDeleteWithStartIndexOutOfRange}, {"test_ziplistDeleteWithNumOverflow", test_ziplistDeleteWithNumOverflow}, {"test_ziplistDeleteFooWhileIterating", test_ziplistDeleteFooWhileIterating}, {"test_ziplistReplaceWithSameSize", test_ziplistReplaceWithSameSize}, {"test_ziplistReplaceWithDifferentSize", test_ziplistReplaceWithDifferentSize}, {"test_ziplistRegressionTestForOver255ByteStrings", test_ziplistRegressionTestForOver255ByteStrings}, {"test_ziplistRegressionTestDeleteNextToLastEntries", test_ziplistRegressionTestDeleteNextToLastEntries}, {"test_ziplistCreateLongListAndCheckIndices", test_ziplistCreateLongListAndCheckIndices}, {"test_ziplistCompareStringWithZiplistEntries", test_ziplistCompareStringWithZiplistEntries}, {"test_ziplistMergeTest", test_ziplistMergeTest}, {"test_ziplistStressWithRandomPayloadsOfDifferentEncoding", test_ziplistStressWithRandomPayloadsOfDifferentEncoding}, {"test_ziplistCascadeUpdateEdgeCases", test_ziplistCascadeUpdateEdgeCases}, {"test_ziplistInsertEdgeCase", test_ziplistInsertEdgeCase}, {"test_ziplistBenchmarks", test_ziplistBenchmarks}, {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 @@ -84,4 +88,5 @@ struct unitTestSuite {
{"test_sha1.c", __test_sha1_c},
{"test_util.c", __test_util_c},
{"test_ziplist.c", __test_ziplist_c},
{"test_zmalloc.c", __test_zmalloc_c},
};
4 changes: 0 additions & 4 deletions src/unit/test_ziplist.c
Original file line number Diff line number Diff line change
Expand Up @@ -837,12 +837,8 @@ int test_ziplistBenchmarks(int argc, char **argv, int flags) {
if (argc >= 4)
srand(atoi(argv[3]));
int accurate = (flags & UNIT_TEST_ACCURATE);
<<<<<<< HEAD
printf("Stress with variable ziplist size:\n");
=======

printf("Stress with variable ziplist size:");
>>>>>>> 4a68d5ed8 (add pop and int list tests)
{
unsigned long long start = usec();
int maxsize = accurate ? 16384 : 16;
Expand Down
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 @@ -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
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 b86d0a1

Please sign in to comment.