HashMapTests.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. module Tests;
  2. import Core.HashMap;
  3. import Core.Random;
  4. import Core.Test;
  5. import Core.Types;
  6. import Core.ToString;
  7. import Core.Meta;
  8. import Core.Array;
  9. template struct Core::HashMap<int, int>;
  10. using IntMap = Core::HashMap<int, int>;
  11. static IntMap getTestIntMap() {
  12. IntMap map;
  13. map.add(1, 3).add(2, 4).add(3, 5).add(0, 20);
  14. return map;
  15. }
  16. static void checkIntMap(IntMap& map) {
  17. int* a = map.search(1);
  18. int* b = map.search(2);
  19. int* c = map.search(3);
  20. int* d = map.search(0);
  21. if(Core::testNotNull(a) && Core::testNotNull(b) && Core::testNotNull(c) &&
  22. Core::testNotNull(d)) {
  23. Core::test(3, *a);
  24. Core::test(4, *b);
  25. Core::test(5, *c);
  26. Core::test(20, *d);
  27. }
  28. }
  29. static void testAdd() {
  30. IntMap map;
  31. map.add(5, 4);
  32. int* value = map.search(5);
  33. if(Core::testNotNull(value)) {
  34. Core::test(4, *value);
  35. }
  36. }
  37. static void testMultipleAdd() {
  38. IntMap map = getTestIntMap();
  39. Core::testTrue(map.contains(0));
  40. Core::testTrue(map.contains(1));
  41. Core::testTrue(map.contains(2));
  42. Core::testTrue(map.contains(3));
  43. checkIntMap(map);
  44. }
  45. static void testSearch() {
  46. IntMap map;
  47. Core::testNull(map.search(6));
  48. map.add(5, 4).add(10, 3).add(15, 2);
  49. Core::testNull(map.search(6));
  50. }
  51. static void testAddReplace() {
  52. IntMap map;
  53. map.add(5, 4).add(5, 10);
  54. Core::testTrue(map.contains(5));
  55. int* a = map.search(5);
  56. if(Core::testNotNull(a)) {
  57. Core::test(10, *a);
  58. }
  59. }
  60. static void testClear() {
  61. IntMap map;
  62. map.clear();
  63. map.add(5, 4).add(4, 10);
  64. map.clear();
  65. Core::testFalse(map.contains(5));
  66. Core::testFalse(map.contains(4));
  67. }
  68. static void testOverflow(bool light) {
  69. int limit = light ? 10'000 : 100'000;
  70. IntMap map;
  71. for(int i = 0; i < limit; i++) {
  72. map.add(i, i);
  73. }
  74. for(int i = 0; i < limit; i++) {
  75. Core::testTrue(map.contains(i));
  76. }
  77. }
  78. static int aInstances = 0;
  79. struct ProbingTest final {
  80. int a;
  81. int b;
  82. ProbingTest(int a_, int b_) : a(a_), b(b_) {
  83. aInstances++;
  84. }
  85. ProbingTest(const ProbingTest& o) = delete;
  86. ProbingTest(ProbingTest&& o) noexcept : a(o.a), b(o.b) {
  87. aInstances++;
  88. }
  89. ~ProbingTest() {
  90. aInstances--;
  91. }
  92. ProbingTest& operator=(const ProbingTest& o) = delete;
  93. ProbingTest& operator=(ProbingTest&& o) noexcept {
  94. a = o.a;
  95. b = o.b;
  96. return *this;
  97. }
  98. bool operator==(const ProbingTest& other) const {
  99. return a == other.a && b == other.b;
  100. }
  101. size_t toString(char* s, size_t n) const {
  102. size_t total = 0;
  103. Core::addString("A(", s, n, total);
  104. Core::addString(a, s, n, total);
  105. Core::addString(", ", s, n, total);
  106. Core::addString(b, s, n, total);
  107. Core::addString(")", s, n, total);
  108. return total;
  109. }
  110. };
  111. static void testEmplaceProbing() {
  112. {
  113. Core::HashMap<int, ProbingTest> map;
  114. ProbingTest* ar = nullptr;
  115. Core::testTrue(map.tryEmplace(ar, 0, 3, 4));
  116. Core::testTrue(map.tryEmplace(ar, 3, 4, 5));
  117. Core::testTrue(map.tryEmplace(ar, 20, 5, 6));
  118. Core::testFalse(map.tryEmplace(ar, 3, 6, 7));
  119. Core::testFalse(map.tryEmplace(ar, 20, 7, 8));
  120. ProbingTest* a = map.search(0);
  121. ProbingTest* b = map.search(3);
  122. ProbingTest* c = map.search(20);
  123. if(Core::testNotNull(a) && Core::testNotNull(b) &&
  124. Core::testNotNull(c)) {
  125. Core::test(ProbingTest(3, 4), *a);
  126. Core::test(ProbingTest(4, 5), *b);
  127. Core::test(ProbingTest(5, 6), *c);
  128. }
  129. }
  130. Core::test(0, aInstances);
  131. }
  132. static void testToString() {
  133. Core::testString("[0 = 20, 2 = 4, 1 = 3, 3 = 5]", getTestIntMap());
  134. Core::testString("[1 = 3]", IntMap().add(1, 3));
  135. Core::testString("[]", IntMap());
  136. }
  137. static void testCopy() {
  138. IntMap map = getTestIntMap();
  139. IntMap copy = map;
  140. IntMap copyA;
  141. copyA = map;
  142. checkIntMap(map);
  143. checkIntMap(copy);
  144. checkIntMap(copyA);
  145. map.add(1, 20).add(2, 30).add(3, 40);
  146. checkIntMap(copy);
  147. checkIntMap(copyA);
  148. }
  149. static void testMove() {
  150. IntMap map = getTestIntMap();
  151. IntMap move(Core::move(map));
  152. checkIntMap(move);
  153. }
  154. static void testMoveAssignment() {
  155. IntMap map = getTestIntMap();
  156. IntMap move;
  157. move = Core::move(map);
  158. checkIntMap(move);
  159. }
  160. static void testEntryForEach() {
  161. IntMap map;
  162. map.add(0, 1).add(5, 4).add(10, 3).add(15, 2);
  163. int counter = 0;
  164. for(auto entry : map) {
  165. counter += entry.getKey() + entry.value;
  166. }
  167. Core::test(40, counter);
  168. const IntMap& cmap = map;
  169. counter = 0;
  170. for(auto entry : cmap) {
  171. counter += entry.getKey() + entry.value;
  172. }
  173. Core::test(40, counter);
  174. }
  175. static void testKeyForEach() {
  176. IntMap map;
  177. map.add(5, 4).add(10, 3).add(15, 2);
  178. int counter = 0;
  179. for(const int& key : map.getKeys()) {
  180. counter += key;
  181. }
  182. Core::test(30, counter);
  183. const IntMap& cmap = map;
  184. counter = 0;
  185. for(const int& key : cmap.getKeys()) {
  186. counter += key;
  187. }
  188. Core::test(30, counter);
  189. }
  190. static void testValueForEach() {
  191. IntMap map;
  192. map.add(5, 4).add(10, 3).add(15, 2);
  193. int counter = 0;
  194. for(int& value : map.getValues()) {
  195. counter += value;
  196. }
  197. Core::test(9, counter);
  198. const IntMap& cmap = map;
  199. counter = 0;
  200. for(const int& value : cmap.getValues()) {
  201. counter += value;
  202. }
  203. Core::test(9, counter);
  204. }
  205. template<typename T>
  206. static void testType() {
  207. Core::HashMap<T, int> m;
  208. m.add(1, 3);
  209. }
  210. static void testTypes() {
  211. testType<char>();
  212. testType<signed char>();
  213. testType<signed short>();
  214. testType<signed int>();
  215. testType<signed long>();
  216. testType<signed long long>();
  217. testType<unsigned char>();
  218. testType<unsigned short>();
  219. testType<unsigned int>();
  220. testType<unsigned long>();
  221. testType<unsigned long long>();
  222. }
  223. static void testInvalid() {
  224. IntMap map;
  225. int* v = nullptr;
  226. Core::testTrue(map.tryEmplace(v, 0, 2));
  227. if(Core::testNotNull(v)) {
  228. Core::test(2, *v);
  229. }
  230. Core::testFalse(map.tryEmplace(v, 0, 6));
  231. if(Core::testNotNull(v)) {
  232. Core::test(2, *v);
  233. }
  234. Core::test(3, map.put(0, 3));
  235. v = map.search(0);
  236. if(Core::testNotNull(v)) {
  237. Core::test(3, *v);
  238. }
  239. map.clear();
  240. Core::testNull(map.search(0));
  241. }
  242. static void testInvalidPut() {
  243. IntMap map;
  244. Core::testString("[]", map);
  245. Core::test(3, map.put(0, 3));
  246. Core::testString("[0 = 3]", map);
  247. int* v = map.search(0);
  248. if(Core::testNotNull(v)) {
  249. Core::test(3, *v);
  250. }
  251. map.clear();
  252. Core::testFalse(map.contains(0));
  253. Core::testString("[]", map);
  254. }
  255. static void testAddCollisions() {
  256. IntMap map;
  257. for(int i = 0; i < 16; i++) {
  258. map.add(i * 64, i);
  259. }
  260. }
  261. static void testRemove() {
  262. IntMap map;
  263. map.add(1, 3).add(2, 4).add(3, 5);
  264. Core::testTrue(map.remove(2));
  265. Core::testFalse(map.remove(7));
  266. int* a = map.search(1);
  267. int* b = map.search(2);
  268. int* c = map.search(3);
  269. Core::testNull(b);
  270. if(Core::testNotNull(a) && Core::testNotNull(c)) {
  271. Core::test(3, *a);
  272. Core::test(5, *c);
  273. }
  274. }
  275. static void testRemoveLong() {
  276. IntMap map;
  277. Core::Random r(5);
  278. constexpr size_t LIMIT = 75;
  279. Core::Array<i32, LIMIT> a;
  280. for(int i = 0; i < 10'000; i++) {
  281. i32 r1 = r.nextI32(0, LIMIT);
  282. if(r.nextBool()) {
  283. i32 r2 = r.nextI32(0, LIMIT);
  284. map.add(r1, 2);
  285. map.add(r2, 2);
  286. a[static_cast<size_t>(r1)] = 1;
  287. a[static_cast<size_t>(r2)] = 1;
  288. } else {
  289. map.remove(r1);
  290. a[static_cast<size_t>(r1)] = 0;
  291. }
  292. Core::Array<i32, LIMIT> copy = a;
  293. for(int key : map.getKeys()) {
  294. Core::testTrue(copy[static_cast<size_t>(key)]);
  295. copy[static_cast<size_t>(key)] = 0;
  296. }
  297. for(int key : copy) {
  298. Core::test(0, key);
  299. }
  300. }
  301. }
  302. void testHashMap(bool light) {
  303. testAdd();
  304. testMultipleAdd();
  305. testSearch();
  306. testAddReplace();
  307. testClear();
  308. testOverflow(light);
  309. testToString();
  310. testCopy();
  311. testMove();
  312. testMoveAssignment();
  313. testEntryForEach();
  314. testKeyForEach();
  315. testValueForEach();
  316. testTypes();
  317. testInvalid();
  318. testInvalidPut();
  319. testAddCollisions();
  320. testRemove();
  321. testRemoveLong();
  322. testEmplaceProbing();
  323. }