2024-05-07 19:49:24 -04:00
|
|
|
#include "../kvstore.c"
|
|
|
|
#include "test_help.h"
|
|
|
|
|
|
|
|
uint64_t hashTestCallback(const void *key) {
|
2024-05-28 09:27:51 -07:00
|
|
|
return dictGenHashFunction((unsigned char *)key, strlen((char *)key));
|
2024-05-07 19:49:24 -04:00
|
|
|
}
|
|
|
|
|
2024-11-14 00:45:47 -08:00
|
|
|
void freeTestCallback(void *val) {
|
2024-05-07 19:49:24 -04:00
|
|
|
zfree(val);
|
|
|
|
}
|
|
|
|
|
2024-09-02 11:01:59 -07:00
|
|
|
dictType KvstoreDictTestType = {hashTestCallback,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
freeTestCallback,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
kvstoreDictRehashingStarted,
|
|
|
|
kvstoreDictRehashingCompleted,
|
|
|
|
kvstoreDictMetadataSize};
|
2024-05-07 19:49:24 -04:00
|
|
|
|
|
|
|
char *stringFromInt(int value) {
|
|
|
|
char buf[32];
|
|
|
|
int len;
|
|
|
|
char *s;
|
|
|
|
|
2024-05-28 09:27:51 -07:00
|
|
|
len = snprintf(buf, sizeof(buf), "%d", value);
|
|
|
|
s = zmalloc(len + 1);
|
2024-05-07 19:49:24 -04:00
|
|
|
memcpy(s, buf, len);
|
|
|
|
s[len] = '\0';
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
int test_kvstoreAdd16Keys(int argc, char **argv, int flags) {
|
|
|
|
UNUSED(argc);
|
|
|
|
UNUSED(argv);
|
|
|
|
UNUSED(flags);
|
|
|
|
|
|
|
|
int i;
|
|
|
|
dictEntry *de;
|
|
|
|
|
|
|
|
int didx = 0;
|
|
|
|
kvstore *kvs1 = kvstoreCreate(&KvstoreDictTestType, 0, KVSTORE_ALLOCATE_DICTS_ON_DEMAND);
|
|
|
|
kvstore *kvs2 = kvstoreCreate(&KvstoreDictTestType, 0, KVSTORE_ALLOCATE_DICTS_ON_DEMAND | KVSTORE_FREE_EMPTY_DICTS);
|
|
|
|
|
|
|
|
for (i = 0; i < 16; i++) {
|
|
|
|
de = kvstoreDictAddRaw(kvs1, didx, stringFromInt(i), NULL);
|
|
|
|
TEST_ASSERT(de != NULL);
|
|
|
|
de = kvstoreDictAddRaw(kvs2, didx, stringFromInt(i), NULL);
|
|
|
|
TEST_ASSERT(de != NULL);
|
|
|
|
}
|
|
|
|
TEST_ASSERT(kvstoreDictSize(kvs1, didx) == 16);
|
|
|
|
TEST_ASSERT(kvstoreSize(kvs1) == 16);
|
|
|
|
TEST_ASSERT(kvstoreDictSize(kvs2, didx) == 16);
|
|
|
|
TEST_ASSERT(kvstoreSize(kvs2) == 16);
|
|
|
|
|
|
|
|
kvstoreRelease(kvs1);
|
|
|
|
kvstoreRelease(kvs2);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int test_kvstoreIteratorRemoveAllKeysNoDeleteEmptyDict(int argc, char **argv, int flags) {
|
|
|
|
UNUSED(argc);
|
|
|
|
UNUSED(argv);
|
|
|
|
UNUSED(flags);
|
|
|
|
|
|
|
|
int i;
|
|
|
|
void *key;
|
|
|
|
dictEntry *de;
|
|
|
|
kvstoreIterator *kvs_it;
|
|
|
|
|
|
|
|
int didx = 0;
|
|
|
|
int curr_slot = 0;
|
|
|
|
kvstore *kvs1 = kvstoreCreate(&KvstoreDictTestType, 0, KVSTORE_ALLOCATE_DICTS_ON_DEMAND);
|
|
|
|
|
|
|
|
for (i = 0; i < 16; i++) {
|
|
|
|
de = kvstoreDictAddRaw(kvs1, didx, stringFromInt(i), NULL);
|
|
|
|
TEST_ASSERT(de != NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
kvs_it = kvstoreIteratorInit(kvs1);
|
2024-05-28 09:27:51 -07:00
|
|
|
while ((de = kvstoreIteratorNext(kvs_it)) != NULL) {
|
2024-05-07 19:49:24 -04:00
|
|
|
curr_slot = kvstoreIteratorGetCurrentDictIndex(kvs_it);
|
|
|
|
key = dictGetKey(de);
|
|
|
|
TEST_ASSERT(kvstoreDictDelete(kvs1, curr_slot, key) == DICT_OK);
|
|
|
|
}
|
|
|
|
kvstoreIteratorRelease(kvs_it);
|
|
|
|
|
|
|
|
dict *d = kvstoreGetDict(kvs1, didx);
|
|
|
|
TEST_ASSERT(d != NULL);
|
|
|
|
TEST_ASSERT(kvstoreDictSize(kvs1, didx) == 0);
|
|
|
|
TEST_ASSERT(kvstoreSize(kvs1) == 0);
|
|
|
|
|
|
|
|
kvstoreRelease(kvs1);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int test_kvstoreIteratorRemoveAllKeysDeleteEmptyDict(int argc, char **argv, int flags) {
|
|
|
|
UNUSED(argc);
|
|
|
|
UNUSED(argv);
|
|
|
|
UNUSED(flags);
|
|
|
|
|
|
|
|
int i;
|
|
|
|
void *key;
|
|
|
|
dictEntry *de;
|
|
|
|
kvstoreIterator *kvs_it;
|
|
|
|
|
|
|
|
int didx = 0;
|
|
|
|
int curr_slot = 0;
|
|
|
|
kvstore *kvs2 = kvstoreCreate(&KvstoreDictTestType, 0, KVSTORE_ALLOCATE_DICTS_ON_DEMAND | KVSTORE_FREE_EMPTY_DICTS);
|
|
|
|
|
|
|
|
for (i = 0; i < 16; i++) {
|
|
|
|
de = kvstoreDictAddRaw(kvs2, didx, stringFromInt(i), NULL);
|
|
|
|
TEST_ASSERT(de != NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
kvs_it = kvstoreIteratorInit(kvs2);
|
2024-05-28 09:27:51 -07:00
|
|
|
while ((de = kvstoreIteratorNext(kvs_it)) != NULL) {
|
2024-05-07 19:49:24 -04:00
|
|
|
curr_slot = kvstoreIteratorGetCurrentDictIndex(kvs_it);
|
|
|
|
key = dictGetKey(de);
|
|
|
|
TEST_ASSERT(kvstoreDictDelete(kvs2, curr_slot, key) == DICT_OK);
|
|
|
|
}
|
|
|
|
kvstoreIteratorRelease(kvs_it);
|
|
|
|
|
|
|
|
/* Make sure the dict was removed from the rehashing list. */
|
2024-05-28 09:27:51 -07:00
|
|
|
while (kvstoreIncrementallyRehash(kvs2, 1000)) {
|
|
|
|
}
|
2024-05-07 19:49:24 -04:00
|
|
|
|
|
|
|
dict *d = kvstoreGetDict(kvs2, didx);
|
|
|
|
TEST_ASSERT(d == NULL);
|
|
|
|
TEST_ASSERT(kvstoreDictSize(kvs2, didx) == 0);
|
|
|
|
TEST_ASSERT(kvstoreSize(kvs2) == 0);
|
|
|
|
|
|
|
|
kvstoreRelease(kvs2);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int test_kvstoreDictIteratorRemoveAllKeysNoDeleteEmptyDict(int argc, char **argv, int flags) {
|
|
|
|
UNUSED(argc);
|
|
|
|
UNUSED(argv);
|
|
|
|
UNUSED(flags);
|
|
|
|
|
|
|
|
int i;
|
|
|
|
void *key;
|
|
|
|
dictEntry *de;
|
|
|
|
kvstoreDictIterator *kvs_di;
|
|
|
|
|
|
|
|
int didx = 0;
|
|
|
|
kvstore *kvs1 = kvstoreCreate(&KvstoreDictTestType, 0, KVSTORE_ALLOCATE_DICTS_ON_DEMAND);
|
|
|
|
|
|
|
|
for (i = 0; i < 16; i++) {
|
|
|
|
de = kvstoreDictAddRaw(kvs1, didx, stringFromInt(i), NULL);
|
|
|
|
TEST_ASSERT(de != NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
kvs_di = kvstoreGetDictSafeIterator(kvs1, didx);
|
2024-05-28 09:27:51 -07:00
|
|
|
while ((de = kvstoreDictIteratorNext(kvs_di)) != NULL) {
|
2024-05-07 19:49:24 -04:00
|
|
|
key = dictGetKey(de);
|
|
|
|
TEST_ASSERT(kvstoreDictDelete(kvs1, didx, key) == DICT_OK);
|
|
|
|
}
|
|
|
|
kvstoreReleaseDictIterator(kvs_di);
|
|
|
|
|
|
|
|
dict *d = kvstoreGetDict(kvs1, didx);
|
|
|
|
TEST_ASSERT(d != NULL);
|
|
|
|
TEST_ASSERT(kvstoreDictSize(kvs1, didx) == 0);
|
|
|
|
TEST_ASSERT(kvstoreSize(kvs1) == 0);
|
|
|
|
|
|
|
|
kvstoreRelease(kvs1);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int test_kvstoreDictIteratorRemoveAllKeysDeleteEmptyDict(int argc, char **argv, int flags) {
|
|
|
|
UNUSED(argc);
|
|
|
|
UNUSED(argv);
|
|
|
|
UNUSED(flags);
|
|
|
|
|
|
|
|
int i;
|
|
|
|
void *key;
|
|
|
|
dictEntry *de;
|
|
|
|
kvstoreDictIterator *kvs_di;
|
|
|
|
|
|
|
|
int didx = 0;
|
|
|
|
kvstore *kvs2 = kvstoreCreate(&KvstoreDictTestType, 0, KVSTORE_ALLOCATE_DICTS_ON_DEMAND | KVSTORE_FREE_EMPTY_DICTS);
|
|
|
|
|
|
|
|
for (i = 0; i < 16; i++) {
|
|
|
|
de = kvstoreDictAddRaw(kvs2, didx, stringFromInt(i), NULL);
|
|
|
|
TEST_ASSERT(de != NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
kvs_di = kvstoreGetDictSafeIterator(kvs2, didx);
|
2024-05-28 09:27:51 -07:00
|
|
|
while ((de = kvstoreDictIteratorNext(kvs_di)) != NULL) {
|
2024-05-07 19:49:24 -04:00
|
|
|
key = dictGetKey(de);
|
|
|
|
TEST_ASSERT(kvstoreDictDelete(kvs2, didx, key) == DICT_OK);
|
|
|
|
}
|
|
|
|
kvstoreReleaseDictIterator(kvs_di);
|
|
|
|
|
|
|
|
dict *d = kvstoreGetDict(kvs2, didx);
|
|
|
|
TEST_ASSERT(d == NULL);
|
|
|
|
TEST_ASSERT(kvstoreDictSize(kvs2, didx) == 0);
|
|
|
|
TEST_ASSERT(kvstoreSize(kvs2) == 0);
|
|
|
|
|
|
|
|
kvstoreRelease(kvs2);
|
|
|
|
return 0;
|
|
|
|
}
|