HashMapTests.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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 testSearchEmpty() {
  54. CoreHashMap map = INT_MAP;
  55. CORE_TEST_NULL(coreHashMapSearch(&map, int, 6, int));
  56. coreDestroyHashMap(&map);
  57. }
  58. static void testAddReplace() {
  59. CoreHashMap map = getTestIntMap();
  60. coreHashMapPut(&map, int, 5, int, 4);
  61. coreHashMapPut(&map, int, 5, int, 10);
  62. CORE_TEST_TRUE(coreHashMapContains(&map, int, 5));
  63. int* a = coreHashMapSearch(&map, int, 5, int);
  64. if(CORE_TEST_NOT_NULL(a)) {
  65. CORE_TEST_INT(10, *a);
  66. }
  67. coreDestroyHashMap(&map);
  68. }
  69. static void testClear() {
  70. CoreHashMap map = getTestIntMap();
  71. coreHashMapPut(&map, int, 5, int, 4);
  72. coreHashMapPut(&map, int, 4, int, 10);
  73. coreClearHashMap(&map);
  74. CORE_TEST_FALSE(coreHashMapContains(&map, int, 5));
  75. CORE_TEST_FALSE(coreHashMapContains(&map, int, 4));
  76. coreDestroyHashMap(&map);
  77. }
  78. static void testClearEmpty() {
  79. CoreHashMap map = INT_MAP;
  80. coreClearHashMap(&map);
  81. coreDestroyHashMap(&map);
  82. }
  83. static void testOverflow(bool light) {
  84. int limit = light ? 10000 : 100000;
  85. CoreHashMap map = getTestIntMap();
  86. for(int i = 0; i < limit; i++) {
  87. coreHashMapPut(&map, int, i, int, i);
  88. }
  89. for(int i = 0; i < limit; i++) {
  90. CORE_TEST_TRUE(coreHashMapContains(&map, int, i));
  91. }
  92. coreDestroyHashMap(&map);
  93. }
  94. static void testToString() {
  95. CoreHashMap map = getTestIntMap();
  96. char buffer[128];
  97. size_t n = coreToStringHashMap(&map, buffer, sizeof(buffer),
  98. coreToStringInt, coreToStringInt);
  99. CORE_TEST_SIZE(29, n);
  100. CORE_TEST_STRING("[2 = 4, 1 = 3, 3 = 5, 0 = 20]", buffer);
  101. coreClearHashMap(&map);
  102. coreHashMapPut(&map, int, 1, int, 3);
  103. n = coreToStringHashMap(&map, buffer, sizeof(buffer), coreToStringInt,
  104. coreToStringInt);
  105. CORE_TEST_SIZE(7, n);
  106. CORE_TEST_STRING("[1 = 3]", buffer);
  107. coreClearHashMap(&map);
  108. n = coreToStringHashMap(&map, buffer, sizeof(buffer), coreToStringInt,
  109. coreToStringInt);
  110. CORE_TEST_SIZE(2, n);
  111. CORE_TEST_STRING("[]", buffer);
  112. coreDestroyHashMap(&map);
  113. }
  114. static void testEntryForEach() {
  115. CoreHashMap map = INT_MAP;
  116. coreHashMapPut(&map, int, 0, int, -1);
  117. coreHashMapPut(&map, int, 5, int, 4);
  118. coreHashMapPut(&map, int, 10, int, 3);
  119. coreHashMapPut(&map, int, 15, int, 2);
  120. int counter = 0;
  121. CoreHashMapIterator i = CORE_HASH_MAP_ITERATOR(&map);
  122. while(true) {
  123. CoreHashMapNode* n = coreHashMapNext(&i);
  124. if(n == nullptr) {
  125. break;
  126. }
  127. counter += coreHashMapKey(n, int) + coreHashMapValue(n, int);
  128. }
  129. CORE_TEST_INT(38, counter);
  130. coreDestroyHashMap(&map);
  131. }
  132. static void testInvalidPut() {
  133. CoreHashMap map = INT_MAP;
  134. char buffer[128];
  135. coreToStringHashMap(&map, buffer, sizeof(buffer), coreToStringInt,
  136. coreToStringInt);
  137. CORE_TEST_STRING("[]", buffer);
  138. coreHashMapPut(&map, int, 0, int, 3);
  139. int* v = coreHashMapSearch(&map, int, 0, int);
  140. if(CORE_TEST_NOT_NULL(v)) {
  141. CORE_TEST_INT(3, *v);
  142. }
  143. coreToStringHashMap(&map, buffer, sizeof(buffer), coreToStringInt,
  144. coreToStringInt);
  145. CORE_TEST_STRING("[0 = 3]", buffer);
  146. coreClearHashMap(&map);
  147. CORE_TEST_NULL(coreHashMapSearch(&map, int, 0, int));
  148. coreDestroyHashMap(&map);
  149. }
  150. static void testAddCollisions() {
  151. CoreHashMap map = getTestIntMap();
  152. for(int i = 0; i < 16; i++) {
  153. coreHashMapPut(&map, int, i * 64, int, i);
  154. }
  155. coreDestroyHashMap(&map);
  156. }
  157. static void testRemove() {
  158. CoreHashMap map = INT_MAP;
  159. coreHashMapPut(&map, int, 1, int, 3);
  160. coreHashMapPut(&map, int, 2, int, 4);
  161. coreHashMapPut(&map, int, 3, int, 5);
  162. CORE_TEST_TRUE(coreHashMapRemove(&map, size_t, 2));
  163. CORE_TEST_FALSE(coreHashMapRemove(&map, size_t, 7));
  164. int* a = coreHashMapSearch(&map, int, 1, int);
  165. int* b = coreHashMapSearch(&map, int, 2, int);
  166. int* c = coreHashMapSearch(&map, int, 3, int);
  167. CORE_TEST_NULL(b);
  168. if(CORE_TEST_NOT_NULL(a) && CORE_TEST_NOT_NULL(c)) {
  169. CORE_TEST_INT(3, *a);
  170. CORE_TEST_INT(5, *c);
  171. }
  172. coreDestroyHashMap(&map);
  173. }
  174. static void testHash() {
  175. u32 buffer[] = {0xFFAA00BB, 0x00000000, 0x00FF00FF};
  176. CORE_TEST_SIZE(0xFF550044, coreHash(buffer, sizeof(buffer)));
  177. const char* s = "wusi";
  178. CORE_TEST_TRUE(coreHashString(&s, sizeof(&s)) != 0);
  179. CORE_TEST_SIZE(0, coreHashString(&s, 1));
  180. }
  181. typedef struct {
  182. size_t a;
  183. size_t b;
  184. } A;
  185. static void testSearchStruct() {
  186. CoreHashMap map =
  187. CORE_HASH_MAP(sizeof(A), sizeof(int), coreHash, coreEqual);
  188. A a = {1, 2};
  189. A b = {1, 3};
  190. A c = {0, 0};
  191. CORE_TEST_NULL(coreHashMapSearchPointer(&map, &a));
  192. CORE_TEST_NULL(coreHashMapSearchPointer(&map, &b));
  193. CORE_TEST_NULL(coreHashMapSearchPointer(&map, &c));
  194. int v = 3;
  195. coreHashMapPutPointer(&map, &a, &v);
  196. int* ap = coreHashMapSearchPointer(&map, &a);
  197. if(CORE_TEST_NOT_NULL(ap)) {
  198. CORE_TEST_INT(3, *ap);
  199. }
  200. CORE_TEST_NULL(coreHashMapSearchPointer(&map, &b));
  201. CORE_TEST_NULL(coreHashMapSearchPointer(&map, &c));
  202. v = 4;
  203. coreHashMapPutPointer(&map, &c, &v);
  204. int* cp = coreHashMapSearchPointer(&map, &c);
  205. if(CORE_TEST_NOT_NULL(cp)) {
  206. CORE_TEST_INT(4, *cp);
  207. }
  208. coreDestroyHashMap(&map);
  209. }
  210. static void testSearchSize() {
  211. CoreHashMap map =
  212. CORE_HASH_MAP(sizeof(size_t), sizeof(int), coreHash, coreEqual);
  213. CORE_TEST_NULL(coreHashMapSearch(&map, size_t, 0, int));
  214. CORE_TEST_NULL(coreHashMapSearch(&map, size_t, 1, int));
  215. CORE_TEST_NULL(coreHashMapSearch(&map, size_t, 2, int));
  216. coreHashMapPut(&map, size_t, 1, int, 3);
  217. int* ap = coreHashMapSearch(&map, size_t, 1, int);
  218. if(CORE_TEST_NOT_NULL(ap)) {
  219. CORE_TEST_INT(3, *ap);
  220. }
  221. CORE_TEST_NULL(coreHashMapSearch(&map, size_t, 0, int));
  222. CORE_TEST_NULL(coreHashMapSearch(&map, size_t, 2, int));
  223. coreHashMapPut(&map, size_t, 0, int, 4);
  224. int* cp = coreHashMapSearch(&map, size_t, 0, int);
  225. if(CORE_TEST_NOT_NULL(cp)) {
  226. CORE_TEST_INT(4, *cp);
  227. }
  228. coreDestroyHashMap(&map);
  229. }
  230. void coreTestHashMap(bool light) {
  231. testAdd();
  232. testMultipleAdd();
  233. testSearch();
  234. testSearchEmpty();
  235. testAddReplace();
  236. testClear();
  237. testClearEmpty();
  238. testOverflow(light);
  239. testToString();
  240. testEntryForEach();
  241. testInvalidPut();
  242. testAddCollisions();
  243. testRemove();
  244. testHash();
  245. testSearchStruct();
  246. testSearchSize();
  247. }