HashMapTests.c 7.8 KB

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