|
@@ -0,0 +1,266 @@
|
|
|
+#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 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 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() {
|
|
|
+ // T map;
|
|
|
+ // map.add(5, 4).add(10, 3).add(15, 2);
|
|
|
+ //
|
|
|
+ // int counter = 0;
|
|
|
+ // for(auto entry : map) {
|
|
|
+ // counter += entry.getKey() + entry.value;
|
|
|
+ // }
|
|
|
+ // CORE_TEST_EQUAL(39, counter);
|
|
|
+ //
|
|
|
+ // const T& cmap = map;
|
|
|
+ // counter = 0;
|
|
|
+ // for(auto entry : cmap) {
|
|
|
+ // counter += entry.getKey() + entry.value;
|
|
|
+ // }
|
|
|
+ // CORE_TEST_EQUAL(39, counter);
|
|
|
+}
|
|
|
+
|
|
|
+static void testKeyForEach() {
|
|
|
+ // T map;
|
|
|
+ // map.add(5, 4).add(10, 3).add(15, 2);
|
|
|
+ //
|
|
|
+ // int counter = 0;
|
|
|
+ // for(const int& key : map.getKeys()) {
|
|
|
+ // counter += key;
|
|
|
+ // }
|
|
|
+ // CORE_TEST_EQUAL(30, counter);
|
|
|
+ //
|
|
|
+ // const T& cmap = map;
|
|
|
+ // counter = 0;
|
|
|
+ // for(const int& key : cmap.getKeys()) {
|
|
|
+ // counter += key;
|
|
|
+ // }
|
|
|
+ // CORE_TEST_EQUAL(30, counter);
|
|
|
+}
|
|
|
+
|
|
|
+static void testValueForEach() {
|
|
|
+ // T map;
|
|
|
+ // map.add(5, 4).add(10, 3).add(15, 2);
|
|
|
+ //
|
|
|
+ // int counter = 0;
|
|
|
+ // for(int& value : map.getValues()) {
|
|
|
+ // counter += value;
|
|
|
+ // }
|
|
|
+ // CORE_TEST_EQUAL(9, counter);
|
|
|
+ //
|
|
|
+ // const T& cmap = map;
|
|
|
+ // counter = 0;
|
|
|
+ // for(const int& value : cmap.getValues()) {
|
|
|
+ // counter += value;
|
|
|
+ // }
|
|
|
+ // CORE_TEST_EQUAL(9, counter);
|
|
|
+}
|
|
|
+
|
|
|
+static void testInvalid() {
|
|
|
+ // T map;
|
|
|
+ // int* v;
|
|
|
+ // CORE_TEST_TRUE(map.tryEmplace(v, INVALID, 2));
|
|
|
+ // if(CORE_TEST_NOT_NULL(v)) {
|
|
|
+ // CORE_TEST_EQUAL(2, *v);
|
|
|
+ // }
|
|
|
+ // CORE_TEST_FALSE(map.tryEmplace(v, INVALID, 6));
|
|
|
+ // if(CORE_TEST_NOT_NULL(v)) {
|
|
|
+ // CORE_TEST_EQUAL(2, *v);
|
|
|
+ // }
|
|
|
+ // CORE_TEST_EQUAL(3, map.put(INVALID, 3));
|
|
|
+ // v = coreHashMapSearch(&map,int,INVALID);
|
|
|
+ // if(CORE_TEST_NOT_NULL(v)) {
|
|
|
+ // CORE_TEST_EQUAL(3, *v);
|
|
|
+ // }
|
|
|
+ // coreClearHashMap(&map);
|
|
|
+ // CORE_TEST_NULL(coreHashMapSearch(&map,int,INVALID));
|
|
|
+}
|
|
|
+
|
|
|
+static void testInvalidPut() {
|
|
|
+ // T map;
|
|
|
+ // CORE_TEST_EQUAL(3, map.put(INVALID, 3));
|
|
|
+ // int* v = coreHashMapSearch(&map,int,INVALID);
|
|
|
+ // if(CORE_TEST_NOT_NULL(v)) {
|
|
|
+ // CORE_TEST_EQUAL(3, *v);
|
|
|
+ // }
|
|
|
+ //
|
|
|
+ // also check to string
|
|
|
+}
|
|
|
+
|
|
|
+static void testAddCollisions() {
|
|
|
+ // T map;
|
|
|
+ // for(int i = 0; i < 8; i++) {
|
|
|
+ // map.add(i * 16, i);
|
|
|
+ // }
|
|
|
+}
|
|
|
+
|
|
|
+static void testEmplace() {
|
|
|
+ // Core::HashMap<int, HashMapTest> map;
|
|
|
+ //
|
|
|
+ // HashMapTest* ar = nullptr;
|
|
|
+ // CORE_TEST_TRUE(map.tryEmplace(ar, 0, 3, 4));
|
|
|
+ // CORE_TEST_TRUE(map.tryEmplace(ar, 3, 4, 5));
|
|
|
+ // CORE_TEST_TRUE(map.tryEmplace(ar, 20, 5, 6));
|
|
|
+ // CORE_TEST_FALSE(map.tryEmplace(ar, 3, 6, 7));
|
|
|
+ // CORE_TEST_FALSE(map.tryEmplace(ar, 20, 7, 8));
|
|
|
+ //
|
|
|
+ // HashMapTest* a = coreHashMapSearch(&map,int,0);
|
|
|
+ // HashMapTest* b = coreHashMapSearch(&map,int,3);
|
|
|
+ // HashMapTest* c = coreHashMapSearch(&map,int,20);
|
|
|
+ //
|
|
|
+ // if(CORE_TEST_NOT_NULL(a) && CORE_TEST_NOT_NULL(b) &&
|
|
|
+ // CORE_TEST_NOT_NULL(c)) {
|
|
|
+ // CORE_TEST_EQUAL(HashMapTest(3, 4), *a);
|
|
|
+ // CORE_TEST_EQUAL(HashMapTest(4, 5), *b);
|
|
|
+ // CORE_TEST_EQUAL(HashMapTest(5, 6), *c);
|
|
|
+ // }
|
|
|
+}
|
|
|
+
|
|
|
+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);
|
|
|
+ // }
|
|
|
+}
|
|
|
+
|
|
|
+void coreTestHashMap(bool light) {
|
|
|
+ testAdd();
|
|
|
+ testMultipleAdd();
|
|
|
+ testSearch();
|
|
|
+ testAddReplace();
|
|
|
+ testClear();
|
|
|
+ testOverflow(light);
|
|
|
+ testToString();
|
|
|
+ testEntryForEach();
|
|
|
+ testKeyForEach();
|
|
|
+ testValueForEach();
|
|
|
+ testInvalid();
|
|
|
+ testInvalidPut();
|
|
|
+ testAddCollisions();
|
|
|
+ testEmplace();
|
|
|
+ testRemove();
|
|
|
+}
|