HashMapTests.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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. void toString(String& s) const {
  101. s.append("A(").append(a).append(", ").append(b).append(")");
  102. }
  103. };
  104. struct ProbingTest final : public HashMapTest {
  105. ProbingTest(int a_, int b_) : HashMapTest(a_, b_) {
  106. aInstances++;
  107. }
  108. ProbingTest(const ProbingTest& o) : HashMapTest(o.a, o.b) {
  109. aInstances++;
  110. }
  111. ProbingTest(ProbingTest&& o) : HashMapTest(o.a, o.b) {
  112. aInstances++;
  113. }
  114. ~ProbingTest() {
  115. aInstances--;
  116. }
  117. ProbingTest& operator=(ProbingTest o) {
  118. a = o.a;
  119. b = o.b;
  120. return *this;
  121. }
  122. };
  123. template<typename T>
  124. static void testEmplace() {
  125. {
  126. Core::ProbingHashMap<int, ProbingTest> map;
  127. ProbingTest* ar = nullptr;
  128. CORE_TEST_TRUE(map.tryEmplace(ar, 0, 3, 4));
  129. CORE_TEST_TRUE(map.tryEmplace(ar, 3, 4, 5));
  130. CORE_TEST_TRUE(map.tryEmplace(ar, 20, 5, 6));
  131. CORE_TEST_FALSE(map.tryEmplace(ar, 3, 6, 7));
  132. CORE_TEST_FALSE(map.tryEmplace(ar, 20, 7, 8));
  133. ProbingTest* a = map.search(0);
  134. ProbingTest* b = map.search(3);
  135. ProbingTest* c = map.search(20);
  136. if(CORE_TEST_NOT_NULL(a) && CORE_TEST_NOT_NULL(b) &&
  137. CORE_TEST_NOT_NULL(c)) {
  138. CORE_TEST_EQUAL(ProbingTest(3, 4), *a);
  139. CORE_TEST_EQUAL(ProbingTest(4, 5), *b);
  140. CORE_TEST_EQUAL(ProbingTest(5, 6), *c);
  141. }
  142. }
  143. CORE_TEST_EQUAL(0, aInstances);
  144. }
  145. template<typename T>
  146. static void testToString() {
  147. if constexpr(Core::IsSame<T, IntMap>) {
  148. CORE_TEST_STRING("[1 = 3, 2 = 4, 3 = 5, 2147483647 = 20]",
  149. getTestIntMap<T>());
  150. } else {
  151. CORE_TEST_STRING("[2 = 4, 1 = 3, 3 = 5, 2147483647 = 20]",
  152. getTestIntMap<T>());
  153. }
  154. CORE_TEST_STRING("[1 = 3]", T().add(1, 3));
  155. CORE_TEST_STRING("[]", T());
  156. }
  157. template<typename T>
  158. static void testCopy() {
  159. T map = getTestIntMap<T>();
  160. T copy = map;
  161. T copyA;
  162. copyA = map;
  163. checkIntMap(map);
  164. checkIntMap(copy);
  165. checkIntMap(copyA);
  166. map.add(1, 20).add(2, 30).add(3, 40);
  167. checkIntMap(copy);
  168. checkIntMap(copyA);
  169. }
  170. template<typename T>
  171. static void testMove() {
  172. T map = getTestIntMap<T>();
  173. T move(Core::move(map));
  174. checkIntMap(move);
  175. }
  176. template<typename T>
  177. static void testMoveAssignment() {
  178. T map = getTestIntMap<T>();
  179. T move;
  180. move = Core::move(map);
  181. checkIntMap(move);
  182. }
  183. template<typename T>
  184. static void testEntryForEach() {
  185. T map;
  186. map.add(5, 4).add(10, 3).add(15, 2);
  187. int counter = 0;
  188. for(auto entry : map) {
  189. counter += entry.getKey() + entry.value;
  190. }
  191. CORE_TEST_EQUAL(39, counter);
  192. const T& cmap = map;
  193. counter = 0;
  194. for(auto entry : cmap) {
  195. counter += entry.getKey() + entry.value;
  196. }
  197. CORE_TEST_EQUAL(39, counter);
  198. }
  199. template<typename T>
  200. static void testKeyForEach() {
  201. T map;
  202. map.add(5, 4).add(10, 3).add(15, 2);
  203. int counter = 0;
  204. for(const int& key : map.getKeys()) {
  205. counter += key;
  206. }
  207. CORE_TEST_EQUAL(30, counter);
  208. const T& cmap = map;
  209. counter = 0;
  210. for(const int& key : cmap.getKeys()) {
  211. counter += key;
  212. }
  213. CORE_TEST_EQUAL(30, counter);
  214. }
  215. template<typename T>
  216. static void testValueForEach() {
  217. T map;
  218. map.add(5, 4).add(10, 3).add(15, 2);
  219. int counter = 0;
  220. for(int& value : map.getValues()) {
  221. counter += value;
  222. }
  223. CORE_TEST_EQUAL(9, counter);
  224. const T& cmap = map;
  225. counter = 0;
  226. for(const int& value : cmap.getValues()) {
  227. counter += value;
  228. }
  229. CORE_TEST_EQUAL(9, counter);
  230. }
  231. template<typename T>
  232. static void testType() {
  233. Core::ProbingHashMap<T, int> m;
  234. m.add(T(), 3);
  235. }
  236. template<typename T>
  237. static void testTypes() {
  238. testType<char>();
  239. testType<signed char>();
  240. testType<signed short>();
  241. testType<signed int>();
  242. testType<signed long>();
  243. testType<signed long long>();
  244. testType<unsigned char>();
  245. testType<unsigned short>();
  246. testType<unsigned int>();
  247. testType<unsigned long>();
  248. testType<unsigned long long>();
  249. }
  250. template<typename T>
  251. static void testInvalid() {
  252. T map;
  253. int* v;
  254. CORE_TEST_TRUE(map.tryEmplace(v, INVALID, 2));
  255. if(CORE_TEST_NOT_NULL(v)) {
  256. CORE_TEST_EQUAL(2, *v);
  257. }
  258. CORE_TEST_FALSE(map.tryEmplace(v, INVALID, 6));
  259. if(CORE_TEST_NOT_NULL(v)) {
  260. CORE_TEST_EQUAL(2, *v);
  261. }
  262. CORE_TEST_EQUAL(3, map.put(INVALID, 3));
  263. v = map.search(INVALID);
  264. if(CORE_TEST_NOT_NULL(v)) {
  265. CORE_TEST_EQUAL(3, *v);
  266. }
  267. map.clear();
  268. CORE_TEST_NULL(map.search(INVALID));
  269. }
  270. template<typename T>
  271. static void testInvalidPut() {
  272. T map;
  273. CORE_TEST_EQUAL(3, map.put(INVALID, 3));
  274. int* v = map.search(INVALID);
  275. if(CORE_TEST_NOT_NULL(v)) {
  276. CORE_TEST_EQUAL(3, *v);
  277. }
  278. }
  279. template<typename T>
  280. static void testAddCollisions() {
  281. T map;
  282. for(int i = 0; i < 8; i++) {
  283. map.add(i * 16, i);
  284. }
  285. }
  286. template<typename T>
  287. static void testMap(bool light) {
  288. testAdd<T>();
  289. testMultipleAdd<T>();
  290. testSearch<T>();
  291. testAddReplace<T>();
  292. testClear<T>();
  293. testOverflow<T>(light);
  294. testEmplace<T>();
  295. testToString<T>();
  296. testCopy<T>();
  297. testMove<T>();
  298. testMoveAssignment<T>();
  299. testEntryForEach<T>();
  300. testKeyForEach<T>();
  301. testValueForEach<T>();
  302. testTypes<T>();
  303. testInvalid<T>();
  304. testInvalidPut<T>();
  305. testAddCollisions<T>();
  306. }
  307. static void testEmplace() {
  308. Core::HashMap<int, HashMapTest> map;
  309. HashMapTest* ar = nullptr;
  310. CORE_TEST_TRUE(map.tryEmplace(ar, 0, 3, 4));
  311. CORE_TEST_TRUE(map.tryEmplace(ar, 3, 4, 5));
  312. CORE_TEST_TRUE(map.tryEmplace(ar, 20, 5, 6));
  313. CORE_TEST_FALSE(map.tryEmplace(ar, 3, 6, 7));
  314. CORE_TEST_FALSE(map.tryEmplace(ar, 20, 7, 8));
  315. HashMapTest* a = map.search(0);
  316. HashMapTest* b = map.search(3);
  317. HashMapTest* c = map.search(20);
  318. if(CORE_TEST_NOT_NULL(a) && CORE_TEST_NOT_NULL(b) &&
  319. CORE_TEST_NOT_NULL(c)) {
  320. CORE_TEST_EQUAL(HashMapTest(3, 4), *a);
  321. CORE_TEST_EQUAL(HashMapTest(4, 5), *b);
  322. CORE_TEST_EQUAL(HashMapTest(5, 6), *c);
  323. }
  324. }
  325. static void testRemove() {
  326. IntMap map;
  327. map.add(1, 3).add(2, 4).add(3, 5);
  328. CORE_TEST_TRUE(map.remove(2));
  329. CORE_TEST_FALSE(map.remove(7));
  330. int* a = map.search(1);
  331. int* b = map.search(2);
  332. int* c = map.search(3);
  333. CORE_TEST_NULL(b);
  334. if(CORE_TEST_NOT_NULL(a) && CORE_TEST_NOT_NULL(c)) {
  335. CORE_TEST_EQUAL(3, *a);
  336. CORE_TEST_EQUAL(5, *c);
  337. }
  338. }
  339. void Core::testHashMap(bool light) {
  340. testMap<ProbingIntMap>(light);
  341. testMap<IntMap>(light);
  342. testEmplace();
  343. testRemove();
  344. }