Skip to content

Commit

Permalink
add asserts for the expected values
Browse files Browse the repository at this point in the history
Signed-off-by: Shivshankar-Reddy <[email protected]>
  • Loading branch information
Shivshankar-Reddy committed May 9, 2024
1 parent 4f56dca commit d8ae919
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 6 deletions.
8 changes: 5 additions & 3 deletions src/unit/test_files.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,16 @@ 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_zipmap(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);

unitTest __test_crc64_c[] = {{"test_crc64", test_crc64}, {NULL, NULL}};
unitTest __test_crc64combine_c[] = {{"test_crc64combine", test_crc64combine}, {NULL, NULL}};
unitTest __test_intset_c[] = {{"test_intsetValueEncodings", test_intsetValueEncodings}, {"test_intsetBasicAdding", test_intsetBasicAdding}, {"test_intsetLargeNumberRandomAdd", test_intsetLargeNumberRandomAdd}, {"test_intsetUpgradeFromint16Toint32", test_intsetUpgradeFromint16Toint32}, {"test_intsetUpgradeFromint16Toint64", test_intsetUpgradeFromint16Toint64}, {"test_intsetUpgradeFromint32Toint64", test_intsetUpgradeFromint32Toint64}, {"test_intsetStressLookups", test_intsetStressLookups}, {"test_intsetStressAddDelete", test_intsetStressAddDelete}, {NULL, NULL}};
unitTest __test_kvstore_c[] = {{"test_kvstoreAdd16Keys", test_kvstoreAdd16Keys}, {"test_kvstoreIteratorRemoveAllKeysNoDeleteEmptyDict", test_kvstoreIteratorRemoveAllKeysNoDeleteEmptyDict}, {"test_kvstoreIteratorRemoveAllKeysDeleteEmptyDict", test_kvstoreIteratorRemoveAllKeysDeleteEmptyDict}, {"test_kvstoreDictIteratorRemoveAllKeysNoDeleteEmptyDict", test_kvstoreDictIteratorRemoveAllKeysNoDeleteEmptyDict}, {"test_kvstoreDictIteratorRemoveAllKeysDeleteEmptyDict", test_kvstoreDictIteratorRemoveAllKeysDeleteEmptyDict}, {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_zipmap_c[] = {{"test_zipmap", test_zipmap}, {NULL, NULL}};
unitTest __test_zipmap_c[] = {{"test_zipmapLookUpLargeKey", test_zipmapLookUpLargeKey}, {"test_zipmapPerformDirectLookup", test_zipmapPerformDirectLookup}, {"test_zipmapIterateThroughElements", test_zipmapIterateThroughElements}, {NULL, NULL}};

struct unitTestSuite {
char *filename;
Expand All @@ -45,5 +47,5 @@ struct unitTestSuite {
{"test_intset.c", __test_intset_c},
{"test_kvstore.c", __test_kvstore_c},
{"test_util.c", __test_util_c},
{"test_zipmap",__test_zipmap_c},
{"test_zipmap.c", __test_zipmap_c},
};
81 changes: 78 additions & 3 deletions src/unit/test_zipmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ static void zipmapRepr(unsigned char *p) {
printf("\n");
}

int test_zipmap(int argc, char *argv[], int flags) {
int test_zipmapLookUpLargeKey(int argc, char *argv[], int flags) {
unsigned char *zm;

UNUSED(argc);
Expand Down Expand Up @@ -69,10 +69,41 @@ int test_zipmap(int argc, char *argv[], int flags) {

zm = zipmapSet(zm,buf,512,(unsigned char*) "long",4,NULL);
if (zipmapGet(zm,buf,512,&value,&vlen)) {
printf(" <long key> is associated to the %d bytes value: %.*s\n",
vlen, vlen, value);
printf(" <long key> is associated to the %d bytes value: %.*s val:%s\n",
vlen, vlen, value,value);
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);

printf("\nPerform a direct lookup:\n");
{
Expand All @@ -82,16 +113,60 @@ int test_zipmap(int argc, char *argv[], int flags) {
if (zipmapGet(zm,(unsigned char*) "foo",3,&value,&vlen)) {
printf(" foo is associated to the %d bytes value: %.*s\n",
vlen, vlen, value);
TEST_ASSERT(5 == vlen);
TEST_ASSERT(strcmp("12345", (const char*)value));
}
}
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);

printf("\nIterate through elements:\n");
{
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) {
printf(" %d:%.*s => %d:%.*s\n", klen, klen, key, vlen, vlen, value);
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);
Expand Down

0 comments on commit d8ae919

Please sign in to comment.