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