#define IMPORT_CORE #include "../Tests.h" #include "core/HashMap.h" #include "core/ToString.h" #include "core/Utility.h" static HashMap createIntMap() { HashMap map; initHashMap(&map, sizeof(int), sizeof(int)); return map; } static HashMap getTestIntMap() { HashMap map = createIntMap(); putTypedHashMapPair(&map, int, 1, int, 3); putTypedHashMapPair(&map, int, 2, int, 4); putTypedHashMapPair(&map, int, 3, int, 5); putTypedHashMapPair(&map, int, 0, int, 20); return map; } static void checkIntMap(HashMap* map) { int* a = searchTypedHashMapKey(map, int, 1, int); int* b = searchTypedHashMapKey(map, int, 2, int); int* c = searchTypedHashMapKey(map, int, 3, int); int* d = searchTypedHashMapKey(map, int, 0, int); if(TEST_NOT_NULL(a) && TEST_NOT_NULL(b) && TEST_NOT_NULL(c) && TEST_NOT_NULL(d)) { TEST_INT(3, *a); TEST_INT(4, *b); TEST_INT(5, *c); TEST_INT(20, *d); } } static void testAdd() { HashMap map = createIntMap(); putTypedHashMapPair(&map, int, 5, int, 4); int* value = searchTypedHashMapKey(&map, int, 5, int); if(TEST_NOT_NULL(value)) { TEST_INT(4, *value); } destroyHashMap(&map); } static void testMultipleAdd() { HashMap map = getTestIntMap(); TEST_NOT_NULL(searchTypedHashMapKey(&map, int, 0, int)); TEST_NOT_NULL(searchTypedHashMapKey(&map, int, 1, int)); TEST_NOT_NULL(searchTypedHashMapKey(&map, int, 2, int)); TEST_NOT_NULL(searchTypedHashMapKey(&map, int, 3, int)); checkIntMap(&map); destroyHashMap(&map); } static void testSearch() { HashMap map = getTestIntMap(); TEST_NULL(searchTypedHashMapKey(&map, int, 6, int)); putTypedHashMapPair(&map, int, 5, int, 4); putTypedHashMapPair(&map, int, 10, int, 3); putTypedHashMapPair(&map, int, 15, int, 2); TEST_NULL(searchTypedHashMapKey(&map, int, 6, int)); destroyHashMap(&map); } static void testSearchEmpty() { HashMap map = createIntMap(); TEST_NULL(searchTypedHashMapKey(&map, int, 6, int)); destroyHashMap(&map); } static void testAddReplace() { HashMap map = getTestIntMap(); putTypedHashMapPair(&map, int, 5, int, 4); putTypedHashMapPair(&map, int, 5, int, 10); TEST_NOT_NULL(searchTypedHashMapKey(&map, int, 5, int)); int* a = searchTypedHashMapKey(&map, int, 5, int); if(TEST_NOT_NULL(a)) { TEST_INT(10, *a); } destroyHashMap(&map); } static void testClear() { HashMap map = getTestIntMap(); putTypedHashMapPair(&map, int, 5, int, 4); putTypedHashMapPair(&map, int, 4, int, 10); clearHashMap(&map); TEST_NULL(searchTypedHashMapKey(&map, int, 5, int)); TEST_NULL(searchTypedHashMapKey(&map, int, 4, int)); destroyHashMap(&map); } static void testClearEmpty() { HashMap map = createIntMap(); clearHashMap(&map); destroyHashMap(&map); } static void testOverflow(bool light) { int limit = light ? 10000 : 100000; HashMap map = getTestIntMap(); for(int i = 0; i < limit; i++) { putTypedHashMapPair(&map, int, i, int, i); } for(int i = 0; i < limit; i++) { TEST_NOT_NULL(searchTypedHashMapKey(&map, int, i, int)); } destroyHashMap(&map); } static void testToString() { HashMap map = getTestIntMap(); char buffer[128]; size_t n = toString(&map, buffer, sizeof(buffer), toStringInt, toStringInt); TEST_SIZE(29, n); TEST_STRING("[2 = 4, 1 = 3, 3 = 5, 0 = 20]", buffer); clearHashMap(&map); putTypedHashMapPair(&map, int, 1, int, 3); n = toString(&map, buffer, sizeof(buffer), toStringInt, toStringInt); TEST_SIZE(7, n); TEST_STRING("[1 = 3]", buffer); clearHashMap(&map); n = toString(&map, buffer, sizeof(buffer), toStringInt, toStringInt); TEST_SIZE(2, n); TEST_STRING("[]", buffer); destroyHashMap(&map); } static void testEntryForEach() { HashMap map = createIntMap(); putTypedHashMapPair(&map, int, 0, int, -1); putTypedHashMapPair(&map, int, 5, int, 4); putTypedHashMapPair(&map, int, 10, int, 3); putTypedHashMapPair(&map, int, 15, int, 2); int counter = 0; HashMapIterator i; initHashMapIterator(&i, &map); while(hasNextHashMapNode(&i)) { HashMapNode* n = nextHashMapNode(&i); counter += *(const int*)n->key + *(int*)n->value; } TEST_INT(38, counter); destroyHashMap(&map); } static void testInvalidPut() { HashMap map = createIntMap(); char buffer[128]; toString(&map, buffer, sizeof(buffer), toStringInt, toStringInt); TEST_STRING("[]", buffer); putTypedHashMapPair(&map, int, 0, int, 3); int* v = searchTypedHashMapKey(&map, int, 0, int); if(TEST_NOT_NULL(v)) { TEST_INT(3, *v); } toString(&map, buffer, sizeof(buffer), toStringInt, toStringInt); TEST_STRING("[0 = 3]", buffer); clearHashMap(&map); TEST_NULL(searchTypedHashMapKey(&map, int, 0, int)); destroyHashMap(&map); } static void testAddCollisions() { HashMap map = getTestIntMap(); for(int i = 0; i < 16; i++) { putTypedHashMapPair(&map, int, i * 64, int, i); } destroyHashMap(&map); } static void testRemove() { HashMap map = createIntMap(); putTypedHashMapPair(&map, int, 1, int, 3); putTypedHashMapPair(&map, int, 2, int, 4); putTypedHashMapPair(&map, int, 3, int, 5); TEST_TRUE(removeTypedHashMapKey(&map, size_t, 2)); TEST_FALSE(removeTypedHashMapKey(&map, size_t, 7)); int* a = searchTypedHashMapKey(&map, int, 1, int); int* b = searchTypedHashMapKey(&map, int, 2, int); int* c = searchTypedHashMapKey(&map, int, 3, int); TEST_NULL(b); if(TEST_NOT_NULL(a) && TEST_NOT_NULL(c)) { TEST_INT(3, *a); TEST_INT(5, *c); } destroyHashMap(&map); } static void testHash() { u32 buffer[] = {0xFFAA00BB, 0x00000000, 0x00FF00FF}; TEST_SIZE(0xFF550044, hashKey(buffer, sizeof(buffer))); const char* s = "wusi"; TEST_TRUE(hashString(&s, sizeof(&s)) != 0); TEST_SIZE(0, hashString(&s, 1)); } typedef struct { size_t a; size_t b; } A; static void testSearchStruct() { HashMap map; initHashMap(&map, sizeof(A), sizeof(int)); A a = {1, 2}; A b = {1, 3}; A c = {0, 0}; TEST_NULL(searchHashMapKey(&map, &a)); TEST_NULL(searchHashMapKey(&map, &b)); TEST_NULL(searchHashMapKey(&map, &c)); int v = 3; putHashMapPair(&map, &a, &v); int* ap = searchHashMapKey(&map, &a); if(TEST_NOT_NULL(ap)) { TEST_INT(3, *ap); } TEST_NULL(searchHashMapKey(&map, &b)); TEST_NULL(searchHashMapKey(&map, &c)); v = 4; putHashMapPair(&map, &c, &v); int* cp = searchHashMapKey(&map, &c); if(TEST_NOT_NULL(cp)) { TEST_INT(4, *cp); } destroyHashMap(&map); } static void testSearchSize() { HashMap map; initHashMap(&map, sizeof(size_t), sizeof(int)); TEST_NULL(searchTypedHashMapKey(&map, size_t, 0, int)); TEST_NULL(searchTypedHashMapKey(&map, size_t, 1, int)); TEST_NULL(searchTypedHashMapKey(&map, size_t, 2, int)); putTypedHashMapPair(&map, size_t, 1, int, 3); int* ap = searchTypedHashMapKey(&map, size_t, 1, int); if(TEST_NOT_NULL(ap)) { TEST_INT(3, *ap); } TEST_NULL(searchTypedHashMapKey(&map, size_t, 0, int)); TEST_NULL(searchTypedHashMapKey(&map, size_t, 2, int)); putTypedHashMapPair(&map, size_t, 0, int, 4); int* cp = searchTypedHashMapKey(&map, size_t, 0, int); if(TEST_NOT_NULL(cp)) { TEST_INT(4, *cp); } destroyHashMap(&map); } void testHashMap(bool light) { testAdd(); testMultipleAdd(); testSearch(); testSearchEmpty(); testAddReplace(); testClear(); testClearEmpty(); testOverflow(light); testToString(); testEntryForEach(); testInvalidPut(); testAddCollisions(); testRemove(); testHash(); testSearchStruct(); testSearchSize(); }