#include "../Tests.h" #include "core/HashMap.h" #include "core/Utility.h" #define INT_MAP CORE_HASH_MAP(sizeof(int), sizeof(int), coreHash, coreEqual) static CoreHashMap getTestIntMap() { CoreHashMap map = INT_MAP; coreHashMapPut(&map, int, 1, int, 3); coreHashMapPut(&map, int, 2, int, 4); coreHashMapPut(&map, int, 3, int, 5); coreHashMapPut(&map, int, 0, int, 20); return map; } static void checkIntMap(CoreHashMap* map) { int* a = coreHashMapSearch(map, int, 1, int); int* b = coreHashMapSearch(map, int, 2, int); const int* c = coreHashMapSearchC(map, int, 3, int); const int* d = coreHashMapSearchC(map, int, 0, int); if(CORE_TEST_NOT_NULL(a) && CORE_TEST_NOT_NULL(b) && CORE_TEST_NOT_NULL(c) && CORE_TEST_NOT_NULL(d)) { CORE_TEST_INT(3, *a); CORE_TEST_INT(4, *b); CORE_TEST_INT(5, *c); CORE_TEST_INT(20, *d); } } static void testAdd() { CoreHashMap map = INT_MAP; coreHashMapPut(&map, int, 5, int, 4); int* value = coreHashMapSearch(&map, int, 5, int); if(CORE_TEST_NOT_NULL(value)) { CORE_TEST_INT(4, *value); } coreDestroyHashMap(&map); } static void testMultipleAdd() { CoreHashMap map = getTestIntMap(); CORE_TEST_TRUE(coreHashMapContains(&map, int, 0)); CORE_TEST_TRUE(coreHashMapContains(&map, int, 1)); CORE_TEST_TRUE(coreHashMapContains(&map, int, 2)); CORE_TEST_TRUE(coreHashMapContains(&map, int, 3)); checkIntMap(&map); coreDestroyHashMap(&map); } static void testSearch() { CoreHashMap map = getTestIntMap(); CORE_TEST_NULL(coreHashMapSearch(&map, int, 6, int)); coreHashMapPut(&map, int, 5, int, 4); coreHashMapPut(&map, int, 10, int, 3); coreHashMapPut(&map, int, 15, int, 2); CORE_TEST_NULL(coreHashMapSearch(&map, int, 6, int)); coreDestroyHashMap(&map); } static void testSearchEmpty() { CoreHashMap map = INT_MAP; CORE_TEST_NULL(coreHashMapSearch(&map, int, 6, int)); coreDestroyHashMap(&map); } static void testAddReplace() { CoreHashMap map = getTestIntMap(); coreHashMapPut(&map, int, 5, int, 4); coreHashMapPut(&map, int, 5, int, 10); CORE_TEST_TRUE(coreHashMapContains(&map, int, 5)); int* a = coreHashMapSearch(&map, int, 5, int); if(CORE_TEST_NOT_NULL(a)) { CORE_TEST_INT(10, *a); } coreDestroyHashMap(&map); } static void testClear() { CoreHashMap map = getTestIntMap(); coreHashMapPut(&map, int, 5, int, 4); coreHashMapPut(&map, int, 4, int, 10); coreClearHashMap(&map); CORE_TEST_FALSE(coreHashMapContains(&map, int, 5)); CORE_TEST_FALSE(coreHashMapContains(&map, int, 4)); coreDestroyHashMap(&map); } static void testClearEmpty() { CoreHashMap map = INT_MAP; coreClearHashMap(&map); coreDestroyHashMap(&map); } static void testOverflow(bool light) { int limit = light ? 10000 : 100000; CoreHashMap map = getTestIntMap(); for(int i = 0; i < limit; i++) { coreHashMapPut(&map, int, i, int, i); } for(int i = 0; i < limit; i++) { CORE_TEST_TRUE(coreHashMapContains(&map, int, i)); } coreDestroyHashMap(&map); } static void testToString() { CoreHashMap map = getTestIntMap(); char buffer[128]; size_t n = coreToStringHashMap(&map, buffer, sizeof(buffer), coreToStringInt, coreToStringInt); CORE_TEST_SIZE(29, n); CORE_TEST_STRING("[2 = 4, 1 = 3, 3 = 5, 0 = 20]", buffer); coreClearHashMap(&map); coreHashMapPut(&map, int, 1, int, 3); n = coreToStringHashMap(&map, buffer, sizeof(buffer), coreToStringInt, coreToStringInt); CORE_TEST_SIZE(7, n); CORE_TEST_STRING("[1 = 3]", buffer); coreClearHashMap(&map); n = coreToStringHashMap(&map, buffer, sizeof(buffer), coreToStringInt, coreToStringInt); CORE_TEST_SIZE(2, n); CORE_TEST_STRING("[]", buffer); coreDestroyHashMap(&map); } static void testEntryForEach() { CoreHashMap map = INT_MAP; coreHashMapPut(&map, int, 0, int, -1); coreHashMapPut(&map, int, 5, int, 4); coreHashMapPut(&map, int, 10, int, 3); coreHashMapPut(&map, int, 15, int, 2); int counter = 0; CoreHashMapIterator i = CORE_HASH_MAP_ITERATOR(&map); while(true) { CoreHashMapNode* n = coreHashMapNext(&i); if(n == nullptr) { break; } counter += coreHashMapKey(n, int) + coreHashMapValue(n, int); } CORE_TEST_INT(38, counter); coreDestroyHashMap(&map); } static void testInvalidPut() { CoreHashMap map = INT_MAP; char buffer[128]; coreToStringHashMap(&map, buffer, sizeof(buffer), coreToStringInt, coreToStringInt); CORE_TEST_STRING("[]", buffer); coreHashMapPut(&map, int, 0, int, 3); int* v = coreHashMapSearch(&map, int, 0, int); if(CORE_TEST_NOT_NULL(v)) { CORE_TEST_INT(3, *v); } coreToStringHashMap(&map, buffer, sizeof(buffer), coreToStringInt, coreToStringInt); CORE_TEST_STRING("[0 = 3]", buffer); coreClearHashMap(&map); CORE_TEST_NULL(coreHashMapSearch(&map, int, 0, int)); coreDestroyHashMap(&map); } static void testAddCollisions() { CoreHashMap map = getTestIntMap(); for(int i = 0; i < 16; i++) { coreHashMapPut(&map, int, i * 64, int, i); } coreDestroyHashMap(&map); } static void testRemove() { CoreHashMap map = INT_MAP; coreHashMapPut(&map, int, 1, int, 3); coreHashMapPut(&map, int, 2, int, 4); coreHashMapPut(&map, int, 3, int, 5); CORE_TEST_TRUE(coreHashMapRemove(&map, size_t, 2)); CORE_TEST_FALSE(coreHashMapRemove(&map, size_t, 7)); int* a = coreHashMapSearch(&map, int, 1, int); int* b = coreHashMapSearch(&map, int, 2, int); int* c = coreHashMapSearch(&map, int, 3, int); CORE_TEST_NULL(b); if(CORE_TEST_NOT_NULL(a) && CORE_TEST_NOT_NULL(c)) { CORE_TEST_INT(3, *a); CORE_TEST_INT(5, *c); } coreDestroyHashMap(&map); } static void testHash() { u32 buffer[] = {0xFFAA00BB, 0x00000000, 0x00FF00FF}; CORE_TEST_SIZE(0xFF550044, coreHash(buffer, sizeof(buffer))); const char* s = "wusi"; CORE_TEST_TRUE(coreHashString(&s, sizeof(&s)) != 0); CORE_TEST_SIZE(0, coreHashString(&s, 1)); } typedef struct { size_t a; size_t b; } A; static void testSearchStruct() { CoreHashMap map = CORE_HASH_MAP(sizeof(A), sizeof(int), coreHash, coreEqual); A a = {1, 2}; A b = {1, 3}; A c = {0, 0}; CORE_TEST_NULL(coreHashMapSearchPointer(&map, &a)); CORE_TEST_NULL(coreHashMapSearchPointer(&map, &b)); CORE_TEST_NULL(coreHashMapSearchPointer(&map, &c)); int v = 3; coreHashMapPutPointer(&map, &a, &v); int* ap = coreHashMapSearchPointer(&map, &a); if(CORE_TEST_NOT_NULL(ap)) { CORE_TEST_INT(3, *ap); } CORE_TEST_NULL(coreHashMapSearchPointer(&map, &b)); CORE_TEST_NULL(coreHashMapSearchPointer(&map, &c)); v = 4; coreHashMapPutPointer(&map, &c, &v); int* cp = coreHashMapSearchPointer(&map, &c); if(CORE_TEST_NOT_NULL(cp)) { CORE_TEST_INT(4, *cp); } coreDestroyHashMap(&map); } static void testSearchSize() { CoreHashMap map = CORE_HASH_MAP(sizeof(size_t), sizeof(int), coreHash, coreEqual); CORE_TEST_NULL(coreHashMapSearch(&map, size_t, 0, int)); CORE_TEST_NULL(coreHashMapSearch(&map, size_t, 1, int)); CORE_TEST_NULL(coreHashMapSearch(&map, size_t, 2, int)); coreHashMapPut(&map, size_t, 1, int, 3); int* ap = coreHashMapSearch(&map, size_t, 1, int); if(CORE_TEST_NOT_NULL(ap)) { CORE_TEST_INT(3, *ap); } CORE_TEST_NULL(coreHashMapSearch(&map, size_t, 0, int)); CORE_TEST_NULL(coreHashMapSearch(&map, size_t, 2, int)); coreHashMapPut(&map, size_t, 0, int, 4); int* cp = coreHashMapSearch(&map, size_t, 0, int); if(CORE_TEST_NOT_NULL(cp)) { CORE_TEST_INT(4, *cp); } coreDestroyHashMap(&map); } void coreTestHashMap(bool light) { testAdd(); testMultipleAdd(); testSearch(); testSearchEmpty(); testAddReplace(); testClear(); testClearEmpty(); testOverflow(light); testToString(); testEntryForEach(); testInvalidPut(); testAddCollisions(); testRemove(); testHash(); testSearchStruct(); testSearchSize(); }