HashMapTests.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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_) noexcept : 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 = default;
  94. bool operator==(const ProbingTest& other) const = default;
  95. size_t toString(Core::StringBase& s) const {
  96. return s.addFormat("A({}, {})", a, b);
  97. }
  98. };
  99. static void testEmplaceProbing() {
  100. {
  101. Core::HashMap<int, ProbingTest> map;
  102. ProbingTest* ar = nullptr;
  103. Core::testTrue(map.tryEmplace(ar, 0, 3, 4));
  104. Core::testTrue(map.tryEmplace(ar, 3, 4, 5));
  105. Core::testTrue(map.tryEmplace(ar, 20, 5, 6));
  106. Core::testFalse(map.tryEmplace(ar, 3, 6, 7));
  107. Core::testFalse(map.tryEmplace(ar, 20, 7, 8));
  108. ProbingTest* a = map.search(0);
  109. ProbingTest* b = map.search(3);
  110. ProbingTest* c = map.search(20);
  111. if(Core::testNotNull(a) && Core::testNotNull(b) &&
  112. Core::testNotNull(c)) {
  113. Core::test(ProbingTest(3, 4), *a);
  114. Core::test(ProbingTest(4, 5), *b);
  115. Core::test(ProbingTest(5, 6), *c);
  116. }
  117. }
  118. Core::test(0, aInstances);
  119. }
  120. static void testToString() {
  121. Core::testString("[0 = 20, 2 = 4, 1 = 3, 3 = 5]", getTestIntMap());
  122. Core::testString("[1 = 3]", IntMap().add(1, 3));
  123. Core::testString("[]", IntMap());
  124. }
  125. static void testCopy() {
  126. IntMap map = getTestIntMap();
  127. IntMap copy = map;
  128. IntMap copyA;
  129. copyA = map;
  130. checkIntMap(map);
  131. checkIntMap(copy);
  132. checkIntMap(copyA);
  133. map.add(1, 20).add(2, 30).add(3, 40);
  134. checkIntMap(copy);
  135. checkIntMap(copyA);
  136. }
  137. static void testMove() {
  138. IntMap map = getTestIntMap();
  139. IntMap move(Core::move(map));
  140. checkIntMap(move);
  141. }
  142. static void testMoveAssignment() {
  143. IntMap map = getTestIntMap();
  144. IntMap move;
  145. move = Core::move(map);
  146. checkIntMap(move);
  147. }
  148. static void testEntryForEach() {
  149. IntMap map;
  150. map.add(0, 1).add(5, 4).add(10, 3).add(15, 2);
  151. int counter = 0;
  152. for(auto entry : map) {
  153. counter += entry.getKey() + entry.value;
  154. }
  155. Core::test(40, counter);
  156. const IntMap& cmap = map;
  157. counter = 0;
  158. for(auto entry : cmap) {
  159. counter += entry.getKey() + entry.value;
  160. }
  161. Core::test(40, counter);
  162. }
  163. static void testKeyForEach() {
  164. IntMap map;
  165. map.add(5, 4).add(10, 3).add(15, 2);
  166. int counter = 0;
  167. for(const int& key : map.getKeys()) {
  168. counter += key;
  169. }
  170. Core::test(30, counter);
  171. const IntMap& cmap = map;
  172. counter = 0;
  173. for(const int& key : cmap.getKeys()) {
  174. counter += key;
  175. }
  176. Core::test(30, counter);
  177. }
  178. static void testValueForEach() {
  179. IntMap map;
  180. map.add(5, 4).add(10, 3).add(15, 2);
  181. int counter = 0;
  182. for(int& value : map.getValues()) {
  183. counter += value;
  184. }
  185. Core::test(9, counter);
  186. const IntMap& cmap = map;
  187. counter = 0;
  188. for(const int& value : cmap.getValues()) {
  189. counter += value;
  190. }
  191. Core::test(9, counter);
  192. }
  193. template<typename T>
  194. static void testType() {
  195. Core::HashMap<T, int> m;
  196. m.add(1, 3);
  197. }
  198. static void testTypes() {
  199. testType<char>();
  200. testType<signed char>();
  201. testType<signed short>();
  202. testType<signed int>();
  203. testType<signed long>();
  204. testType<signed long long>();
  205. testType<unsigned char>();
  206. testType<unsigned short>();
  207. testType<unsigned int>();
  208. testType<unsigned long>();
  209. testType<unsigned long long>();
  210. }
  211. static void testInvalid() {
  212. IntMap map;
  213. int* v = nullptr;
  214. Core::testTrue(map.tryEmplace(v, 0, 2));
  215. if(Core::testNotNull(v)) {
  216. Core::test(2, *v);
  217. }
  218. Core::testFalse(map.tryEmplace(v, 0, 6));
  219. if(Core::testNotNull(v)) {
  220. Core::test(2, *v);
  221. }
  222. Core::test(3, map.put(0, 3));
  223. v = map.search(0);
  224. if(Core::testNotNull(v)) {
  225. Core::test(3, *v);
  226. }
  227. map.clear();
  228. Core::testNull(map.search(0));
  229. }
  230. static void testInvalidPut() {
  231. IntMap map;
  232. Core::testString("[]", map);
  233. Core::test(3, map.put(0, 3));
  234. Core::testString("[0 = 3]", map);
  235. int* v = map.search(0);
  236. if(Core::testNotNull(v)) {
  237. Core::test(3, *v);
  238. }
  239. map.clear();
  240. Core::testFalse(map.contains(0));
  241. Core::testString("[]", map);
  242. }
  243. static void testAddCollisions() {
  244. IntMap map;
  245. for(int i = 0; i < 16; i++) {
  246. map.add(i * 64, i);
  247. }
  248. }
  249. static void testRemove() {
  250. IntMap map;
  251. map.add(1, 3).add(2, 4).add(3, 5);
  252. Core::testTrue(map.remove(2));
  253. Core::testFalse(map.remove(7));
  254. int* a = map.search(1);
  255. int* b = map.search(2);
  256. int* c = map.search(3);
  257. Core::testNull(b);
  258. if(Core::testNotNull(a) && Core::testNotNull(c)) {
  259. Core::test(3, *a);
  260. Core::test(5, *c);
  261. }
  262. }
  263. static void testRemoveLong() {
  264. IntMap map;
  265. Core::Random r(5);
  266. constexpr size_t LIMIT = 75;
  267. Core::Array<i32, LIMIT> a;
  268. for(int i = 0; i < 10'000; i++) {
  269. i32 r1 = r.nextI32(0, LIMIT);
  270. if(r.nextBool()) {
  271. i32 r2 = r.nextI32(0, LIMIT);
  272. map.add(r1, 2);
  273. map.add(r2, 2);
  274. a[static_cast<size_t>(r1)] = 1;
  275. a[static_cast<size_t>(r2)] = 1;
  276. } else {
  277. map.remove(r1);
  278. a[static_cast<size_t>(r1)] = 0;
  279. }
  280. Core::Array<i32, LIMIT> copy = a;
  281. for(int key : map.getKeys()) {
  282. Core::testTrue(copy[static_cast<size_t>(key)]);
  283. copy[static_cast<size_t>(key)] = 0;
  284. }
  285. for(int key : copy) {
  286. Core::test(0, key);
  287. }
  288. }
  289. }
  290. void testHashMap(bool light) {
  291. testAdd();
  292. testMultipleAdd();
  293. testSearch();
  294. testAddReplace();
  295. testClear();
  296. testOverflow(light);
  297. testToString();
  298. testCopy();
  299. testMove();
  300. testMoveAssignment();
  301. testEntryForEach();
  302. testKeyForEach();
  303. testValueForEach();
  304. testTypes();
  305. testInvalid();
  306. testInvalidPut();
  307. testAddCollisions();
  308. testRemove();
  309. testRemoveLong();
  310. testEmplaceProbing();
  311. }