HashMapTests.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. toString(&map, buffer, sizeof(buffer), toStringInt, toStringInt);
  136. TEST_STRING("[]", buffer);
  137. putTypedHashMapPair(&map, int, 0, int, 3);
  138. int* v = searchTypedHashMapKey(&map, int, 0, int);
  139. if(TEST_NOT_NULL(v)) {
  140. TEST_INT(3, *v);
  141. }
  142. toString(&map, buffer, sizeof(buffer), toStringInt, toStringInt);
  143. TEST_STRING("[0 = 3]", buffer);
  144. clearHashMap(&map);
  145. TEST_NULL(searchTypedHashMapKey(&map, int, 0, int));
  146. destroyHashMap(&map);
  147. }
  148. static void testAddCollisions() {
  149. HashMap map = getTestIntMap();
  150. for(int i = 0; i < 16; i++) {
  151. putTypedHashMapPair(&map, int, i * 64, int, i);
  152. }
  153. destroyHashMap(&map);
  154. }
  155. static void testRemove() {
  156. HashMap map = createIntMap();
  157. putTypedHashMapPair(&map, int, 1, int, 3);
  158. putTypedHashMapPair(&map, int, 2, int, 4);
  159. putTypedHashMapPair(&map, int, 3, int, 5);
  160. TEST_TRUE(removeTypedHashMapKey(&map, size_t, 2));
  161. TEST_FALSE(removeTypedHashMapKey(&map, size_t, 7));
  162. int* a = searchTypedHashMapKey(&map, int, 1, int);
  163. int* b = searchTypedHashMapKey(&map, int, 2, int);
  164. int* c = searchTypedHashMapKey(&map, int, 3, int);
  165. TEST_NULL(b);
  166. if(TEST_NOT_NULL(a) && TEST_NOT_NULL(c)) {
  167. TEST_INT(3, *a);
  168. TEST_INT(5, *c);
  169. }
  170. destroyHashMap(&map);
  171. }
  172. static void testHash() {
  173. u32 buffer[] = {0xFFAA00BB, 0x00000000, 0x00FF00FF};
  174. TEST_SIZE(0xFF550044, hashKey(buffer, sizeof(buffer)));
  175. const char* s = "wusi";
  176. TEST_TRUE(hashString(&s, sizeof(&s)) != 0);
  177. TEST_SIZE(0, hashString(&s, 1));
  178. }
  179. typedef struct {
  180. size_t a;
  181. size_t b;
  182. } A;
  183. static void testSearchStruct() {
  184. HashMap map;
  185. initHashMap(&map, sizeof(A), sizeof(int));
  186. A a = {1, 2};
  187. A b = {1, 3};
  188. A c = {0, 0};
  189. TEST_NULL(searchHashMapKey(&map, &a));
  190. TEST_NULL(searchHashMapKey(&map, &b));
  191. TEST_NULL(searchHashMapKey(&map, &c));
  192. int v = 3;
  193. putHashMapPair(&map, &a, &v);
  194. int* ap = searchHashMapKey(&map, &a);
  195. if(TEST_NOT_NULL(ap)) {
  196. TEST_INT(3, *ap);
  197. }
  198. TEST_NULL(searchHashMapKey(&map, &b));
  199. TEST_NULL(searchHashMapKey(&map, &c));
  200. v = 4;
  201. putHashMapPair(&map, &c, &v);
  202. int* cp = searchHashMapKey(&map, &c);
  203. if(TEST_NOT_NULL(cp)) {
  204. TEST_INT(4, *cp);
  205. }
  206. destroyHashMap(&map);
  207. }
  208. static void testSearchSize() {
  209. HashMap map;
  210. initHashMap(&map, sizeof(size_t), sizeof(int));
  211. TEST_NULL(searchTypedHashMapKey(&map, size_t, 0, int));
  212. TEST_NULL(searchTypedHashMapKey(&map, size_t, 1, int));
  213. TEST_NULL(searchTypedHashMapKey(&map, size_t, 2, int));
  214. putTypedHashMapPair(&map, size_t, 1, int, 3);
  215. int* ap = searchTypedHashMapKey(&map, size_t, 1, int);
  216. if(TEST_NOT_NULL(ap)) {
  217. TEST_INT(3, *ap);
  218. }
  219. TEST_NULL(searchTypedHashMapKey(&map, size_t, 0, int));
  220. TEST_NULL(searchTypedHashMapKey(&map, size_t, 2, int));
  221. putTypedHashMapPair(&map, size_t, 0, int, 4);
  222. int* cp = searchTypedHashMapKey(&map, size_t, 0, int);
  223. if(TEST_NOT_NULL(cp)) {
  224. TEST_INT(4, *cp);
  225. }
  226. destroyHashMap(&map);
  227. }
  228. void testHashMap(bool light) {
  229. testAdd();
  230. testMultipleAdd();
  231. testSearch();
  232. testSearchEmpty();
  233. testAddReplace();
  234. testClear();
  235. testClearEmpty();
  236. testOverflow(light);
  237. testToString();
  238. testEntryForEach();
  239. testInvalidPut();
  240. testAddCollisions();
  241. testRemove();
  242. testHash();
  243. testSearchStruct();
  244. testSearchSize();
  245. }