HashMapTests.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. #include "../Tests.h"
  2. #include "core/HashMap.h"
  3. #include "core/Utility.h"
  4. #define INT_MAP CORE_HASH_MAP(sizeof(int), sizeof(int), coreHash, coreEqual)
  5. static CoreHashMap getTestIntMap() {
  6. CoreHashMap map = INT_MAP;
  7. coreHashMapPut(&map, int, 1, int, 3);
  8. coreHashMapPut(&map, int, 2, int, 4);
  9. coreHashMapPut(&map, int, 3, int, 5);
  10. coreHashMapPut(&map, int, 0, int, 20);
  11. return map;
  12. }
  13. static void checkIntMap(CoreHashMap* map) {
  14. int* a = coreHashMapSearch(map, int, 1, int);
  15. int* b = coreHashMapSearch(map, int, 2, int);
  16. const int* c = coreHashMapSearchC(map, int, 3, int);
  17. const int* d = coreHashMapSearchC(map, int, 0, int);
  18. if(CORE_TEST_NOT_NULL(a) && CORE_TEST_NOT_NULL(b) &&
  19. CORE_TEST_NOT_NULL(c) && CORE_TEST_NOT_NULL(d)) {
  20. CORE_TEST_INT(3, *a);
  21. CORE_TEST_INT(4, *b);
  22. CORE_TEST_INT(5, *c);
  23. CORE_TEST_INT(20, *d);
  24. }
  25. }
  26. static void testAdd() {
  27. CoreHashMap map = INT_MAP;
  28. coreHashMapPut(&map, int, 5, int, 4);
  29. int* value = coreHashMapSearch(&map, int, 5, int);
  30. if(CORE_TEST_NOT_NULL(value)) {
  31. CORE_TEST_INT(4, *value);
  32. }
  33. coreDestroyHashMap(&map);
  34. }
  35. static void testMultipleAdd() {
  36. CoreHashMap map = getTestIntMap();
  37. CORE_TEST_TRUE(coreHashMapContains(&map, int, 0));
  38. CORE_TEST_TRUE(coreHashMapContains(&map, int, 1));
  39. CORE_TEST_TRUE(coreHashMapContains(&map, int, 2));
  40. CORE_TEST_TRUE(coreHashMapContains(&map, int, 3));
  41. checkIntMap(&map);
  42. coreDestroyHashMap(&map);
  43. }
  44. static void testSearch() {
  45. CoreHashMap map = getTestIntMap();
  46. CORE_TEST_NULL(coreHashMapSearch(&map, int, 6, int));
  47. coreHashMapPut(&map, int, 5, int, 4);
  48. coreHashMapPut(&map, int, 10, int, 3);
  49. coreHashMapPut(&map, int, 15, int, 2);
  50. CORE_TEST_NULL(coreHashMapSearch(&map, int, 6, int));
  51. coreDestroyHashMap(&map);
  52. }
  53. static void testAddReplace() {
  54. CoreHashMap map = getTestIntMap();
  55. coreHashMapPut(&map, int, 5, int, 4);
  56. coreHashMapPut(&map, int, 5, int, 10);
  57. CORE_TEST_TRUE(coreHashMapContains(&map, int, 5));
  58. int* a = coreHashMapSearch(&map, int, 5, int);
  59. if(CORE_TEST_NOT_NULL(a)) {
  60. CORE_TEST_INT(10, *a);
  61. }
  62. coreDestroyHashMap(&map);
  63. }
  64. static void testClear() {
  65. CoreHashMap map = getTestIntMap();
  66. coreHashMapPut(&map, int, 5, int, 4);
  67. coreHashMapPut(&map, int, 4, int, 10);
  68. coreClearHashMap(&map);
  69. CORE_TEST_FALSE(coreHashMapContains(&map, int, 5));
  70. CORE_TEST_FALSE(coreHashMapContains(&map, int, 4));
  71. coreDestroyHashMap(&map);
  72. }
  73. static void testOverflow(bool light) {
  74. int limit = light ? 10000 : 100000;
  75. CoreHashMap map = getTestIntMap();
  76. for(int i = 0; i < limit; i++) {
  77. coreHashMapPut(&map, int, i, int, i);
  78. }
  79. for(int i = 0; i < limit; i++) {
  80. CORE_TEST_TRUE(coreHashMapContains(&map, int, i));
  81. }
  82. coreDestroyHashMap(&map);
  83. }
  84. static void testToString() {
  85. CoreHashMap map = getTestIntMap();
  86. char buffer[128];
  87. size_t n = coreToStringHashMap(&map, buffer, sizeof(buffer),
  88. coreToStringInt, coreToStringInt);
  89. CORE_TEST_SIZE(29, n);
  90. CORE_TEST_STRING("[2 = 4, 1 = 3, 3 = 5, 0 = 20]", buffer);
  91. coreClearHashMap(&map);
  92. coreHashMapPut(&map, int, 1, int, 3);
  93. n = coreToStringHashMap(&map, buffer, sizeof(buffer), coreToStringInt,
  94. coreToStringInt);
  95. CORE_TEST_SIZE(7, n);
  96. CORE_TEST_STRING("[1 = 3]", buffer);
  97. coreClearHashMap(&map);
  98. n = coreToStringHashMap(&map, buffer, sizeof(buffer), coreToStringInt,
  99. coreToStringInt);
  100. CORE_TEST_SIZE(2, n);
  101. CORE_TEST_STRING("[]", buffer);
  102. coreDestroyHashMap(&map);
  103. }
  104. static void testEntryForEach() {
  105. // T map;
  106. // map.add(5, 4).add(10, 3).add(15, 2);
  107. //
  108. // int counter = 0;
  109. // for(auto entry : map) {
  110. // counter += entry.getKey() + entry.value;
  111. // }
  112. // CORE_TEST_EQUAL(39, counter);
  113. //
  114. // const T& cmap = map;
  115. // counter = 0;
  116. // for(auto entry : cmap) {
  117. // counter += entry.getKey() + entry.value;
  118. // }
  119. // CORE_TEST_EQUAL(39, counter);
  120. }
  121. static void testKeyForEach() {
  122. // T map;
  123. // map.add(5, 4).add(10, 3).add(15, 2);
  124. //
  125. // int counter = 0;
  126. // for(const int& key : map.getKeys()) {
  127. // counter += key;
  128. // }
  129. // CORE_TEST_EQUAL(30, counter);
  130. //
  131. // const T& cmap = map;
  132. // counter = 0;
  133. // for(const int& key : cmap.getKeys()) {
  134. // counter += key;
  135. // }
  136. // CORE_TEST_EQUAL(30, counter);
  137. }
  138. static void testValueForEach() {
  139. // T map;
  140. // map.add(5, 4).add(10, 3).add(15, 2);
  141. //
  142. // int counter = 0;
  143. // for(int& value : map.getValues()) {
  144. // counter += value;
  145. // }
  146. // CORE_TEST_EQUAL(9, counter);
  147. //
  148. // const T& cmap = map;
  149. // counter = 0;
  150. // for(const int& value : cmap.getValues()) {
  151. // counter += value;
  152. // }
  153. // CORE_TEST_EQUAL(9, counter);
  154. }
  155. static void testInvalid() {
  156. // T map;
  157. // int* v;
  158. // CORE_TEST_TRUE(map.tryEmplace(v, INVALID, 2));
  159. // if(CORE_TEST_NOT_NULL(v)) {
  160. // CORE_TEST_EQUAL(2, *v);
  161. // }
  162. // CORE_TEST_FALSE(map.tryEmplace(v, INVALID, 6));
  163. // if(CORE_TEST_NOT_NULL(v)) {
  164. // CORE_TEST_EQUAL(2, *v);
  165. // }
  166. // CORE_TEST_EQUAL(3, map.put(INVALID, 3));
  167. // v = coreHashMapSearch(&map,int,INVALID);
  168. // if(CORE_TEST_NOT_NULL(v)) {
  169. // CORE_TEST_EQUAL(3, *v);
  170. // }
  171. // coreClearHashMap(&map);
  172. // CORE_TEST_NULL(coreHashMapSearch(&map,int,INVALID));
  173. }
  174. static void testInvalidPut() {
  175. // T map;
  176. // CORE_TEST_EQUAL(3, map.put(INVALID, 3));
  177. // int* v = coreHashMapSearch(&map,int,INVALID);
  178. // if(CORE_TEST_NOT_NULL(v)) {
  179. // CORE_TEST_EQUAL(3, *v);
  180. // }
  181. //
  182. // also check to string
  183. }
  184. static void testAddCollisions() {
  185. // T map;
  186. // for(int i = 0; i < 8; i++) {
  187. // map.add(i * 16, i);
  188. // }
  189. }
  190. static void testEmplace() {
  191. // Core::HashMap<int, HashMapTest> map;
  192. //
  193. // HashMapTest* ar = nullptr;
  194. // CORE_TEST_TRUE(map.tryEmplace(ar, 0, 3, 4));
  195. // CORE_TEST_TRUE(map.tryEmplace(ar, 3, 4, 5));
  196. // CORE_TEST_TRUE(map.tryEmplace(ar, 20, 5, 6));
  197. // CORE_TEST_FALSE(map.tryEmplace(ar, 3, 6, 7));
  198. // CORE_TEST_FALSE(map.tryEmplace(ar, 20, 7, 8));
  199. //
  200. // HashMapTest* a = coreHashMapSearch(&map,int,0);
  201. // HashMapTest* b = coreHashMapSearch(&map,int,3);
  202. // HashMapTest* c = coreHashMapSearch(&map,int,20);
  203. //
  204. // if(CORE_TEST_NOT_NULL(a) && CORE_TEST_NOT_NULL(b) &&
  205. // CORE_TEST_NOT_NULL(c)) {
  206. // CORE_TEST_EQUAL(HashMapTest(3, 4), *a);
  207. // CORE_TEST_EQUAL(HashMapTest(4, 5), *b);
  208. // CORE_TEST_EQUAL(HashMapTest(5, 6), *c);
  209. // }
  210. }
  211. static void testRemove() {
  212. // IntMap map;
  213. // map.add(1, 3).add(2, 4).add(3, 5);
  214. //
  215. // CORE_TEST_TRUE(map.remove(2));
  216. // CORE_TEST_FALSE(map.remove(7));
  217. //
  218. // int* a = coreHashMapSearch(&map,int,1);
  219. // int* b = coreHashMapSearch(&map,int,2);
  220. // int* c = coreHashMapSearch(&map,int,3);
  221. //
  222. // CORE_TEST_NULL(b);
  223. // if(CORE_TEST_NOT_NULL(a) && CORE_TEST_NOT_NULL(c)) {
  224. // CORE_TEST_EQUAL(3, *a);
  225. // CORE_TEST_EQUAL(5, *c);
  226. // }
  227. }
  228. void coreTestHashMap(bool light) {
  229. testAdd();
  230. testMultipleAdd();
  231. testSearch();
  232. testAddReplace();
  233. testClear();
  234. testOverflow(light);
  235. testToString();
  236. testEntryForEach();
  237. testKeyForEach();
  238. testValueForEach();
  239. testInvalid();
  240. testInvalidPut();
  241. testAddCollisions();
  242. testEmplace();
  243. testRemove();
  244. }