From 2b76c8fbe2ccadaee2149e4b9b7c7df7ff0d07b6 Mon Sep 17 00:00:00 2001 From: Shivshankar Date: Thu, 29 Aug 2024 11:17:53 -0400 Subject: [PATCH] Migrate zipmap unit test to new framework (#474) Migrate zipmap unit test to new unit test framework, parent ticket #428 . --------- Signed-off-by: Shivshankar-Reddy Signed-off-by: hwware Co-authored-by: hwware --- src/server.c | 1 - src/unit/test_files.h | 5 ++ src/unit/test_zipmap.c | 146 +++++++++++++++++++++++++++++++++++++++++ src/zipmap.c | 98 --------------------------- src/zipmap.h | 5 -- 5 files changed, 151 insertions(+), 104 deletions(-) create mode 100644 src/unit/test_zipmap.c diff --git a/src/server.c b/src/server.c index 63d2aa06df..f343ff1bba 100644 --- a/src/server.c +++ b/src/server.c @@ -6660,7 +6660,6 @@ struct serverTest { int failed; } serverTests[] = { {"quicklist", quicklistTest}, - {"zipmap", zipmapTest}, {"dict", dictTest}, {"listpack", listpackTest}, }; diff --git a/src/unit/test_files.h b/src/unit/test_files.h index a087e6fe44..fda367fb86 100644 --- a/src/unit/test_files.h +++ b/src/unit/test_files.h @@ -70,6 +70,9 @@ int test_BenchmarkziplistValidateIntegrity(int argc, char **argv, int flags); int test_BenchmarkziplistCompareWithString(int argc, char **argv, int flags); int test_BenchmarkziplistCompareWithNumber(int argc, char **argv, int flags); int test_ziplistStress__ziplistCascadeUpdate(int argc, char **argv, int flags); +int test_zipmapLookUpLargeKey(int argc, char *argv[], int flags); +int test_zipmapPerformDirectLookup(int argc, char *argv[], int flags); +int test_zipmapIterateThroughElements(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); @@ -83,6 +86,7 @@ unitTest __test_sds_c[] = {{"test_sds", test_sds}, {"test_typesAndAllocSize", te 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_version2num", test_version2num}, {"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_ziplistStressWithVariableSize", test_ziplistStressWithVariableSize}, {"test_BenchmarkziplistFind", test_BenchmarkziplistFind}, {"test_BenchmarkziplistIndex", test_BenchmarkziplistIndex}, {"test_BenchmarkziplistValidateIntegrity", test_BenchmarkziplistValidateIntegrity}, {"test_BenchmarkziplistCompareWithString", test_BenchmarkziplistCompareWithString}, {"test_BenchmarkziplistCompareWithNumber", test_BenchmarkziplistCompareWithNumber}, {"test_ziplistStress__ziplistCascadeUpdate", test_ziplistStress__ziplistCascadeUpdate}, {NULL, NULL}}; +unitTest __test_zipmap_c[] = {{"test_zipmapLookUpLargeKey", test_zipmapLookUpLargeKey}, {"test_zipmapPerformDirectLookup", test_zipmapPerformDirectLookup}, {"test_zipmapIterateThroughElements", test_zipmapIterateThroughElements}, {NULL, NULL}}; unitTest __test_zmalloc_c[] = {{"test_zmallocInitialUsedMemory", test_zmallocInitialUsedMemory}, {"test_zmallocAllocReallocCallocAndFree", test_zmallocAllocReallocCallocAndFree}, {"test_zmallocAllocZeroByteAndFree", test_zmallocAllocZeroByteAndFree}, {NULL, NULL}}; struct unitTestSuite { @@ -98,5 +102,6 @@ struct unitTestSuite { {"test_sha1.c", __test_sha1_c}, {"test_util.c", __test_util_c}, {"test_ziplist.c", __test_ziplist_c}, + {"test_zipmap.c", __test_zipmap_c}, {"test_zmalloc.c", __test_zmalloc_c}, }; diff --git a/src/unit/test_zipmap.c b/src/unit/test_zipmap.c new file mode 100644 index 0000000000..824815cba9 --- /dev/null +++ b/src/unit/test_zipmap.c @@ -0,0 +1,146 @@ +#include "../zipmap.c" +#include "test_help.h" + +static void zipmapRepr(unsigned char *p) { + unsigned int l; + + p++; + while (1) { + if (p[0] == ZIPMAP_END) { + break; + } else { + unsigned char e; + + l = zipmapDecodeLength(p); + p += zipmapEncodeLength(NULL, l); + if (l != 0 && fwrite(p, l, 1, stdout) == 0) perror("fwrite"); + p += l; + + l = zipmapDecodeLength(p); + p += zipmapEncodeLength(NULL, l); + e = *p++; + if (l != 0 && fwrite(p, l, 1, stdout) == 0) perror("fwrite"); + p += l + e; + if (e) { + while (e--); + } + } + } +} + +int test_zipmapLookUpLargeKey(int argc, char *argv[], int flags) { + unsigned char *zm; + UNUSED(argc); + UNUSED(argv); + UNUSED(flags); + zm = zipmapNew(); + + zm = zipmapSet(zm, (unsigned char *)"name", 4, (unsigned char *)"foo", 3, NULL); + zm = zipmapSet(zm, (unsigned char *)"surname", 7, (unsigned char *)"foo", 3, NULL); + zm = zipmapSet(zm, (unsigned char *)"age", 3, (unsigned char *)"foo", 3, NULL); + zipmapRepr(zm); + + zm = zipmapSet(zm, (unsigned char *)"hello", 5, (unsigned char *)"world!", 6, NULL); + zm = zipmapSet(zm, (unsigned char *)"foo", 3, (unsigned char *)"bar", 3, NULL); + zm = zipmapSet(zm, (unsigned char *)"foo", 3, (unsigned char *)"!", 1, NULL); + zipmapRepr(zm); + zm = zipmapSet(zm, (unsigned char *)"foo", 3, (unsigned char *)"12345", 5, NULL); + zipmapRepr(zm); + zm = zipmapSet(zm, (unsigned char *)"new", 3, (unsigned char *)"xx", 2, NULL); + zm = zipmapSet(zm, (unsigned char *)"noval", 5, (unsigned char *)"", 0, NULL); + zipmapRepr(zm); + zm = zipmapDel(zm, (unsigned char *)"new", 3, NULL); + zipmapRepr(zm); + + unsigned char buf[512]; + unsigned char *value; + unsigned int vlen, i; + for (i = 0; i < 512; i++) buf[i] = 'a'; + zm = zipmapSet(zm, buf, 512, (unsigned char *)"long", 4, NULL); + if (zipmapGet(zm, buf, 512, &value, &vlen)) { + TEST_ASSERT(4 == vlen); + TEST_ASSERT(strncmp("long", (const char *)value, vlen) == 0); + } + zfree(zm); + return 0; +} + +int test_zipmapPerformDirectLookup(int argc, char *argv[], int flags) { + unsigned char *zm; + UNUSED(argc); + UNUSED(argv); + UNUSED(flags); + zm = zipmapNew(); + + zm = zipmapSet(zm, (unsigned char *)"name", 4, (unsigned char *)"foo", 3, NULL); + zm = zipmapSet(zm, (unsigned char *)"surname", 7, (unsigned char *)"foo", 3, NULL); + zm = zipmapSet(zm, (unsigned char *)"age", 3, (unsigned char *)"foo", 3, NULL); + zipmapRepr(zm); + + zm = zipmapSet(zm, (unsigned char *)"hello", 5, (unsigned char *)"world!", 6, NULL); + zm = zipmapSet(zm, (unsigned char *)"foo", 3, (unsigned char *)"bar", 3, NULL); + zm = zipmapSet(zm, (unsigned char *)"foo", 3, (unsigned char *)"!", 1, NULL); + zipmapRepr(zm); + zm = zipmapSet(zm, (unsigned char *)"foo", 3, (unsigned char *)"12345", 5, NULL); + zipmapRepr(zm); + zm = zipmapSet(zm, (unsigned char *)"new", 3, (unsigned char *)"xx", 2, NULL); + zm = zipmapSet(zm, (unsigned char *)"noval", 5, (unsigned char *)"", 0, NULL); + zipmapRepr(zm); + zm = zipmapDel(zm, (unsigned char *)"new", 3, NULL); + zipmapRepr(zm); + unsigned char *value; + unsigned int vlen; + + if (zipmapGet(zm, (unsigned char *)"foo", 3, &value, &vlen)) { + TEST_ASSERT(5 == vlen); + TEST_ASSERT(!strncmp("12345", (const char *)value, vlen)); + } + zfree(zm); + return 0; +} + +int test_zipmapIterateThroughElements(int argc, char *argv[], int flags) { + unsigned char *zm; + UNUSED(argc); + UNUSED(argv); + UNUSED(flags); + zm = zipmapNew(); + + zm = zipmapSet(zm, (unsigned char *)"name", 4, (unsigned char *)"foo", 3, NULL); + zm = zipmapSet(zm, (unsigned char *)"surname", 7, (unsigned char *)"foo", 3, NULL); + zm = zipmapSet(zm, (unsigned char *)"age", 3, (unsigned char *)"foo", 3, NULL); + zipmapRepr(zm); + + zm = zipmapSet(zm, (unsigned char *)"hello", 5, (unsigned char *)"world!", 6, NULL); + zm = zipmapSet(zm, (unsigned char *)"foo", 3, (unsigned char *)"bar", 3, NULL); + zm = zipmapSet(zm, (unsigned char *)"foo", 3, (unsigned char *)"!", 1, NULL); + zipmapRepr(zm); + zm = zipmapSet(zm, (unsigned char *)"foo", 3, (unsigned char *)"12345", 5, NULL); + zipmapRepr(zm); + zm = zipmapSet(zm, (unsigned char *)"new", 3, (unsigned char *)"xx", 2, NULL); + zm = zipmapSet(zm, (unsigned char *)"noval", 5, (unsigned char *)"", 0, NULL); + zipmapRepr(zm); + zm = zipmapDel(zm, (unsigned char *)"new", 3, NULL); + zipmapRepr(zm); + + unsigned char *i = zipmapRewind(zm); + unsigned char *key, *value; + unsigned int klen, vlen; + char *expected_key[] = {"name", "surname", "age", "hello", "foo", "noval"}; + char *expected_value[] = {"foo", "foo", "foo", "world!", "12345", NULL}; + unsigned int expected_klen[] = {4, 7, 3, 5, 3, 5}; + unsigned int expected_vlen[] = {3, 3, 3, 6, 5, 0}; + int iter = 0; + + while ((i = zipmapNext(i, &key, &klen, &value, &vlen)) != NULL) { + char *tmp = expected_key[iter]; + TEST_ASSERT(klen == expected_klen[iter]); + TEST_ASSERT(strncmp((const char *)tmp, (const char *)key, klen) == 0); + tmp = expected_value[iter]; + TEST_ASSERT(vlen == expected_vlen[iter]); + TEST_ASSERT(strncmp((const char *)tmp, (const char *)value, vlen) == 0); + iter++; + } + zfree(zm); + return 0; +} diff --git a/src/zipmap.c b/src/zipmap.c index 25d8ab8465..19d8972164 100644 --- a/src/zipmap.c +++ b/src/zipmap.c @@ -432,101 +432,3 @@ int zipmapValidateIntegrity(unsigned char *zm, size_t size, int deep) { return 1; #undef OUT_OF_RANGE } - -#ifdef SERVER_TEST -static void zipmapRepr(unsigned char *p) { - unsigned int l; - - printf("{status %u}", *p++); - while (1) { - if (p[0] == ZIPMAP_END) { - printf("{end}"); - break; - } else { - unsigned char e; - - l = zipmapDecodeLength(p); - printf("{key %u}", l); - p += zipmapEncodeLength(NULL, l); - if (l != 0 && fwrite(p, l, 1, stdout) == 0) perror("fwrite"); - p += l; - - l = zipmapDecodeLength(p); - printf("{value %u}", l); - p += zipmapEncodeLength(NULL, l); - e = *p++; - if (l != 0 && fwrite(p, l, 1, stdout) == 0) perror("fwrite"); - p += l + e; - if (e) { - printf("["); - while (e--) printf("."); - printf("]"); - } - } - } - printf("\n"); -} - -#define UNUSED(x) (void)(x) -int zipmapTest(int argc, char *argv[], int flags) { - unsigned char *zm; - - UNUSED(argc); - UNUSED(argv); - UNUSED(flags); - - zm = zipmapNew(); - - zm = zipmapSet(zm, (unsigned char *)"name", 4, (unsigned char *)"foo", 3, NULL); - zm = zipmapSet(zm, (unsigned char *)"surname", 7, (unsigned char *)"foo", 3, NULL); - zm = zipmapSet(zm, (unsigned char *)"age", 3, (unsigned char *)"foo", 3, NULL); - zipmapRepr(zm); - - zm = zipmapSet(zm, (unsigned char *)"hello", 5, (unsigned char *)"world!", 6, NULL); - zm = zipmapSet(zm, (unsigned char *)"foo", 3, (unsigned char *)"bar", 3, NULL); - zm = zipmapSet(zm, (unsigned char *)"foo", 3, (unsigned char *)"!", 1, NULL); - zipmapRepr(zm); - zm = zipmapSet(zm, (unsigned char *)"foo", 3, (unsigned char *)"12345", 5, NULL); - zipmapRepr(zm); - zm = zipmapSet(zm, (unsigned char *)"new", 3, (unsigned char *)"xx", 2, NULL); - zm = zipmapSet(zm, (unsigned char *)"noval", 5, (unsigned char *)"", 0, NULL); - zipmapRepr(zm); - zm = zipmapDel(zm, (unsigned char *)"new", 3, NULL); - zipmapRepr(zm); - - printf("\nLook up large key:\n"); - { - unsigned char buf[512]; - unsigned char *value; - unsigned int vlen, i; - for (i = 0; i < 512; i++) buf[i] = 'a'; - - zm = zipmapSet(zm, buf, 512, (unsigned char *)"long", 4, NULL); - if (zipmapGet(zm, buf, 512, &value, &vlen)) { - printf(" is associated to the %d bytes value: %.*s\n", vlen, vlen, value); - } - } - - printf("\nPerform a direct lookup:\n"); - { - unsigned char *value; - unsigned int vlen; - - if (zipmapGet(zm, (unsigned char *)"foo", 3, &value, &vlen)) { - printf(" foo is associated to the %d bytes value: %.*s\n", vlen, vlen, value); - } - } - printf("\nIterate through elements:\n"); - { - unsigned char *i = zipmapRewind(zm); - unsigned char *key, *value; - unsigned int klen, vlen; - - while ((i = zipmapNext(i, &key, &klen, &value, &vlen)) != NULL) { - printf(" %d:%.*s => %d:%.*s\n", klen, klen, key, vlen, vlen, value); - } - } - zfree(zm); - return 0; -} -#endif diff --git a/src/zipmap.h b/src/zipmap.h index 0112ac2b1f..12cc5a134c 100644 --- a/src/zipmap.h +++ b/src/zipmap.h @@ -46,11 +46,6 @@ int zipmapGet(unsigned char *zm, unsigned char *key, unsigned int klen, unsigned int zipmapExists(unsigned char *zm, unsigned char *key, unsigned int klen); unsigned int zipmapLen(unsigned char *zm); size_t zipmapBlobLen(unsigned char *zm); -void zipmapRepr(unsigned char *p); int zipmapValidateIntegrity(unsigned char *zm, size_t size, int deep); -#ifdef SERVER_TEST -int zipmapTest(int argc, char *argv[], int flags); -#endif - #endif