HashMapTests.c 8.3 KB

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