HashMapTests.cpp 9.3 KB

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