HashMapTests.cpp 9.8 KB

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