123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288 |
- #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();
- }
|