HashMapTests.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. #include "../Tests.hpp"
  2. #include "core/data/HashMap.hpp"
  3. #include "core/data/ProbingHashMap.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. constexpr int INVALID = Core::emptyValue<int>();
  9. template<typename T>
  10. static T getTestIntMap() {
  11. T map;
  12. map.add(1, 3).add(2, 4).add(3, 5).add(INVALID, 20);
  13. return map;
  14. }
  15. template<typename T>
  16. static void checkIntMap(T& map) {
  17. int* a = map.search(1);
  18. int* b = map.search(2);
  19. int* c = map.search(3);
  20. int* d = map.search(INVALID);
  21. if(CORE_TEST_NOT_NULL(a) && CORE_TEST_NOT_NULL(b) &&
  22. CORE_TEST_NOT_NULL(c) && CORE_TEST_NOT_NULL(d)) {
  23. CORE_TEST_EQUAL(3, *a);
  24. CORE_TEST_EQUAL(4, *b);
  25. CORE_TEST_EQUAL(5, *c);
  26. CORE_TEST_EQUAL(20, *d);
  27. }
  28. }
  29. template<typename T>
  30. static void testAdd() {
  31. T map;
  32. map.add(5, 4);
  33. int* value = map.search(5);
  34. CORE_TEST_NOT_NULL(value);
  35. if(value != nullptr) {
  36. CORE_TEST_EQUAL(4, *value);
  37. }
  38. }
  39. template<typename T>
  40. static void testMultipleAdd() {
  41. T map = getTestIntMap<T>();
  42. CORE_TEST_TRUE(map.contains(1));
  43. CORE_TEST_TRUE(map.contains(2));
  44. CORE_TEST_TRUE(map.contains(3));
  45. checkIntMap(map);
  46. }
  47. template<typename T>
  48. static void testSearch() {
  49. T map;
  50. CORE_TEST_NULL(map.search(6));
  51. map.add(5, 4).add(10, 3).add(15, 2);
  52. CORE_TEST_NULL(map.search(6));
  53. }
  54. template<typename T>
  55. static void testAddReplace() {
  56. T map;
  57. map.add(5, 4).add(5, 10);
  58. CORE_TEST_TRUE(map.contains(5));
  59. int* a = map.search(5);
  60. if(CORE_TEST_NOT_NULL(a)) {
  61. CORE_TEST_EQUAL(10, *a);
  62. }
  63. }
  64. template<typename T>
  65. static void testClear() {
  66. T map;
  67. map.add(5, 4).add(4, 10);
  68. map.clear();
  69. CORE_TEST_FALSE(map.contains(5));
  70. CORE_TEST_FALSE(map.contains(4));
  71. }
  72. template<typename T>
  73. static void testOverflow(bool light) {
  74. int limit = light ? 10000 : 100000;
  75. T map;
  76. map.add(INVALID, 42);
  77. for(int i = 0; i < limit; i++) {
  78. map.add(i, i);
  79. }
  80. for(int i = 0; i < limit; i++) {
  81. CORE_TEST_TRUE(map.contains(i));
  82. }
  83. CORE_TEST_TRUE(map.contains(INVALID));
  84. }
  85. static int aInstances = 0;
  86. struct HashMapTest {
  87. int a;
  88. int b;
  89. HashMapTest(int a_, int b_) : a(a_), b(b_) {
  90. }
  91. // none of these should be needed for the hashmap
  92. HashMapTest(const HashMapTest&) = delete;
  93. HashMapTest(HashMapTest&&) = delete;
  94. HashMapTest& operator=(const HashMapTest&) = delete;
  95. HashMapTest& operator=(HashMapTest&&) = delete;
  96. bool operator==(const HashMapTest& other) const {
  97. return a == other.a && b == other.b;
  98. }
  99. template<typename String>
  100. check_return Core::Error toString(String& s) const {
  101. CORE_RETURN_ERROR(s.append("A("));
  102. CORE_RETURN_ERROR(s.append(a));
  103. CORE_RETURN_ERROR(s.append(", "));
  104. CORE_RETURN_ERROR(s.append(b));
  105. CORE_RETURN_ERROR(s.append(")"));
  106. return Core::ErrorCode::NONE;
  107. }
  108. };
  109. struct ProbingTest final : public HashMapTest {
  110. ProbingTest(int a_, int b_) : HashMapTest(a_, b_) {
  111. aInstances++;
  112. }
  113. ProbingTest(const ProbingTest& o) : HashMapTest(o.a, o.b) {
  114. aInstances++;
  115. }
  116. ProbingTest(ProbingTest&& o) : HashMapTest(o.a, o.b) {
  117. aInstances++;
  118. }
  119. ~ProbingTest() {
  120. aInstances--;
  121. }
  122. ProbingTest& operator=(ProbingTest o) {
  123. a = o.a;
  124. b = o.b;
  125. return *this;
  126. }
  127. };
  128. template<typename T>
  129. static void testEmplace() {
  130. {
  131. Core::ProbingHashMap<int, ProbingTest> map;
  132. ProbingTest* ar = nullptr;
  133. CORE_TEST_TRUE(map.tryEmplace(ar, 0, 3, 4));
  134. CORE_TEST_TRUE(map.tryEmplace(ar, 3, 4, 5));
  135. CORE_TEST_TRUE(map.tryEmplace(ar, 20, 5, 6));
  136. CORE_TEST_FALSE(map.tryEmplace(ar, 3, 6, 7));
  137. CORE_TEST_FALSE(map.tryEmplace(ar, 20, 7, 8));
  138. ProbingTest* a = map.search(0);
  139. ProbingTest* b = map.search(3);
  140. ProbingTest* c = map.search(20);
  141. if(CORE_TEST_NOT_NULL(a) && CORE_TEST_NOT_NULL(b) &&
  142. CORE_TEST_NOT_NULL(c)) {
  143. CORE_TEST_EQUAL(ProbingTest(3, 4), *a);
  144. CORE_TEST_EQUAL(ProbingTest(4, 5), *b);
  145. CORE_TEST_EQUAL(ProbingTest(5, 6), *c);
  146. }
  147. }
  148. CORE_TEST_EQUAL(0, aInstances);
  149. }
  150. template<typename T>
  151. static void testToString() {
  152. if constexpr(Core::IsSame<T, IntMap>) {
  153. CORE_TEST_STRING("[1 = 3, 2 = 4, 3 = 5, 2147483647 = 20]",
  154. getTestIntMap<T>());
  155. } else {
  156. CORE_TEST_STRING("[2 = 4, 1 = 3, 3 = 5, 2147483647 = 20]",
  157. getTestIntMap<T>());
  158. }
  159. CORE_TEST_STRING("[1 = 3]", T().add(1, 3));
  160. CORE_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(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. CORE_TEST_EQUAL(39, counter);
  197. const T& cmap = map;
  198. counter = 0;
  199. for(auto entry : cmap) {
  200. counter += entry.getKey() + entry.value;
  201. }
  202. CORE_TEST_EQUAL(39, 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. CORE_TEST_EQUAL(30, counter);
  213. const T& cmap = map;
  214. counter = 0;
  215. for(const int& key : cmap.getKeys()) {
  216. counter += key;
  217. }
  218. CORE_TEST_EQUAL(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. CORE_TEST_EQUAL(9, counter);
  229. const T& cmap = map;
  230. counter = 0;
  231. for(const int& value : cmap.getValues()) {
  232. counter += value;
  233. }
  234. CORE_TEST_EQUAL(9, counter);
  235. }
  236. template<typename T>
  237. static void testType() {
  238. Core::ProbingHashMap<T, int> m;
  239. m.add(T(), 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. CORE_TEST_TRUE(map.tryEmplace(v, INVALID, 2));
  260. if(CORE_TEST_NOT_NULL(v)) {
  261. CORE_TEST_EQUAL(2, *v);
  262. }
  263. CORE_TEST_FALSE(map.tryEmplace(v, INVALID, 6));
  264. if(CORE_TEST_NOT_NULL(v)) {
  265. CORE_TEST_EQUAL(2, *v);
  266. }
  267. CORE_TEST_EQUAL(3, map.put(INVALID, 3));
  268. v = map.search(INVALID);
  269. if(CORE_TEST_NOT_NULL(v)) {
  270. CORE_TEST_EQUAL(3, *v);
  271. }
  272. map.clear();
  273. CORE_TEST_NULL(map.search(INVALID));
  274. }
  275. template<typename T>
  276. static void testInvalidPut() {
  277. T map;
  278. CORE_TEST_EQUAL(3, map.put(INVALID, 3));
  279. int* v = map.search(INVALID);
  280. if(CORE_TEST_NOT_NULL(v)) {
  281. CORE_TEST_EQUAL(3, *v);
  282. }
  283. }
  284. template<typename T>
  285. static void testAddCollisions() {
  286. T map;
  287. for(int i = 0; i < 8; i++) {
  288. map.add(i * 16, i);
  289. }
  290. }
  291. template<typename T>
  292. static void testMap(bool light) {
  293. testAdd<T>();
  294. testMultipleAdd<T>();
  295. testSearch<T>();
  296. testAddReplace<T>();
  297. testClear<T>();
  298. testOverflow<T>(light);
  299. testEmplace<T>();
  300. testToString<T>();
  301. testCopy<T>();
  302. testMove<T>();
  303. testMoveAssignment<T>();
  304. testEntryForEach<T>();
  305. testKeyForEach<T>();
  306. testValueForEach<T>();
  307. testTypes<T>();
  308. testInvalid<T>();
  309. testInvalidPut<T>();
  310. testAddCollisions<T>();
  311. }
  312. static void testEmplace() {
  313. Core::HashMap<int, HashMapTest> map;
  314. HashMapTest* ar = nullptr;
  315. CORE_TEST_TRUE(map.tryEmplace(ar, 0, 3, 4));
  316. CORE_TEST_TRUE(map.tryEmplace(ar, 3, 4, 5));
  317. CORE_TEST_TRUE(map.tryEmplace(ar, 20, 5, 6));
  318. CORE_TEST_FALSE(map.tryEmplace(ar, 3, 6, 7));
  319. CORE_TEST_FALSE(map.tryEmplace(ar, 20, 7, 8));
  320. HashMapTest* a = map.search(0);
  321. HashMapTest* b = map.search(3);
  322. HashMapTest* c = map.search(20);
  323. if(CORE_TEST_NOT_NULL(a) && CORE_TEST_NOT_NULL(b) &&
  324. CORE_TEST_NOT_NULL(c)) {
  325. CORE_TEST_EQUAL(HashMapTest(3, 4), *a);
  326. CORE_TEST_EQUAL(HashMapTest(4, 5), *b);
  327. CORE_TEST_EQUAL(HashMapTest(5, 6), *c);
  328. }
  329. }
  330. static void testRemove() {
  331. IntMap map;
  332. map.add(1, 3).add(2, 4).add(3, 5);
  333. CORE_TEST_TRUE(map.remove(2));
  334. CORE_TEST_FALSE(map.remove(7));
  335. int* a = map.search(1);
  336. int* b = map.search(2);
  337. int* c = map.search(3);
  338. CORE_TEST_NULL(b);
  339. if(CORE_TEST_NOT_NULL(a) && CORE_TEST_NOT_NULL(c)) {
  340. CORE_TEST_EQUAL(3, *a);
  341. CORE_TEST_EQUAL(5, *c);
  342. }
  343. }
  344. void Core::testHashMap(bool light) {
  345. testMap<ProbingIntMap>(light);
  346. testMap<IntMap>(light);
  347. testEmplace();
  348. testRemove();
  349. }