HashMapTests.c 7.8 KB

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