HashMapTests.cpp 8.0 KB

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