123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224 |
- #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() {
- // IntMap map;
- // map.add(1, 3).add(2, 4).add(3, 5);
- //
- // CORE_TEST_TRUE(map.remove(2));
- // CORE_TEST_FALSE(map.remove(7));
- //
- // int* a = coreHashMapSearch(&map,int,1);
- // int* b = coreHashMapSearch(&map,int,2);
- // int* c = coreHashMapSearch(&map,int,3);
- //
- // CORE_TEST_NULL(b);
- // if(CORE_TEST_NOT_NULL(a) && CORE_TEST_NOT_NULL(c)) {
- // CORE_TEST_EQUAL(3, *a);
- // CORE_TEST_EQUAL(5, *c);
- // }
- }
- 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));
- }
- void coreTestHashMap(bool light) {
- testAdd();
- testMultipleAdd();
- testSearch();
- testSearchEmpty();
- testAddReplace();
- testClear();
- testClearEmpty();
- testOverflow(light);
- testToString();
- testEntryForEach();
- testInvalidPut();
- testAddCollisions();
- testRemove();
- testHash();
- }
|