HashMapTests.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. #include "tests/HashMapTests.h"
  2. #include "data/HashMap.h"
  3. #include "test/Test.h"
  4. using IntMap = Core::HashMap<int, int>;
  5. static void testAdd() {
  6. IntMap map;
  7. CORE_TEST_NOT_NULL(map.add(5, 4));
  8. int* value = map.search(5);
  9. CORE_TEST_NOT_NULL(value);
  10. if(value != nullptr) {
  11. CORE_TEST_EQUAL(4, *value);
  12. }
  13. }
  14. static void testMultipleAdd() {
  15. IntMap map;
  16. CORE_TEST_NOT_NULL(map.add(5, 4));
  17. CORE_TEST_NOT_NULL(map.add(10, 3));
  18. CORE_TEST_NOT_NULL(map.add(15, 2));
  19. CORE_TEST_TRUE(map.contains(5));
  20. CORE_TEST_TRUE(map.contains(10));
  21. CORE_TEST_TRUE(map.contains(15));
  22. int* a = map.search(5);
  23. int* b = map.search(10);
  24. int* c = map.search(15);
  25. CORE_TEST_NOT_NULL(a);
  26. CORE_TEST_NOT_NULL(b);
  27. CORE_TEST_NOT_NULL(c);
  28. if(a != nullptr && b != nullptr && c != nullptr) {
  29. CORE_TEST_EQUAL(4, *a);
  30. CORE_TEST_EQUAL(3, *b);
  31. CORE_TEST_EQUAL(2, *c);
  32. }
  33. }
  34. static void testSearch() {
  35. IntMap map;
  36. CORE_TEST_NULL(map.search(6));
  37. }
  38. static void testAddReplace() {
  39. IntMap map;
  40. CORE_TEST_NOT_NULL(map.add(5, 4));
  41. CORE_TEST_NOT_NULL(map.add(5, 10));
  42. CORE_TEST_TRUE(map.contains(5));
  43. int* a = map.search(5);
  44. CORE_TEST_NOT_NULL(a);
  45. if(a != nullptr) {
  46. CORE_TEST_EQUAL(10, *a);
  47. }
  48. }
  49. static void testClear() {
  50. IntMap map;
  51. CORE_TEST_NOT_NULL(map.add(5, 4));
  52. CORE_TEST_NOT_NULL(map.add(4, 10));
  53. map.clear();
  54. CORE_TEST_FALSE(map.contains(5));
  55. CORE_TEST_FALSE(map.contains(4));
  56. }
  57. static void testOverflow() {
  58. IntMap map;
  59. for(int i = 0; i < 100000; i++) {
  60. CORE_TEST_NOT_NULL(map.add(i, i));
  61. }
  62. for(int i = 0; i < 100000; i++) {
  63. CORE_TEST_TRUE(map.contains(i));
  64. }
  65. }
  66. struct A final {
  67. int a;
  68. int b;
  69. A(int a_, int b_) : a(a_), b(b_) {
  70. }
  71. // none of these should be needed for the hashmap
  72. A(const A&) = delete;
  73. A(A&&) = delete;
  74. A& operator=(const A&) = delete;
  75. A& operator=(A&&) = delete;
  76. bool operator==(const A& other) const {
  77. return a == other.a && b == other.b;
  78. }
  79. template<int N>
  80. check_return bool toString(Core::ArrayString<N>& s) const {
  81. return s.append("A(") || s.append(a) || s.append(", ") || s.append(b) ||
  82. s.append(")");
  83. }
  84. };
  85. static void testEmplace() {
  86. Core::HashMap<int, A> map;
  87. CORE_TEST_NOT_NULL(map.tryEmplace(0, 3, 4));
  88. CORE_TEST_NOT_NULL(map.tryEmplace(3, 4, 5));
  89. CORE_TEST_NOT_NULL(map.tryEmplace(20, 5, 6));
  90. CORE_TEST_NULL(map.tryEmplace(3, 6, 7));
  91. CORE_TEST_NULL(map.tryEmplace(20, 7, 8));
  92. A* a = map.search(0);
  93. A* b = map.search(3);
  94. A* c = map.search(20);
  95. CORE_TEST_NOT_NULL(a);
  96. CORE_TEST_NOT_NULL(b);
  97. CORE_TEST_NOT_NULL(c);
  98. if(a != nullptr && b != nullptr && c != nullptr) {
  99. CORE_TEST_EQUAL(A(3, 4), *a);
  100. CORE_TEST_EQUAL(A(4, 5), *b);
  101. CORE_TEST_EQUAL(A(5, 6), *c);
  102. }
  103. }
  104. static void testToString1() {
  105. IntMap map;
  106. CORE_TEST_NOT_NULL(map.add(1, 3));
  107. CORE_TEST_NOT_NULL(map.add(2, 4));
  108. CORE_TEST_NOT_NULL(map.add(3, 5));
  109. CORE_TEST_STRING("[1 = 3, 2 = 4, 3 = 5]", map);
  110. }
  111. static void testToString2() {
  112. IntMap map;
  113. CORE_TEST_NOT_NULL(map.add(1, 3));
  114. CORE_TEST_STRING("[1 = 3]", map);
  115. }
  116. static void testToString3() {
  117. IntMap map;
  118. CORE_TEST_STRING("[]", map);
  119. }
  120. static void testCopy() {
  121. IntMap map;
  122. CORE_TEST_NOT_NULL(map.add(1, 3));
  123. CORE_TEST_NOT_NULL(map.add(2, 4));
  124. CORE_TEST_NOT_NULL(map.add(3, 5));
  125. IntMap copy;
  126. CORE_TEST_FALSE(copy.copyFrom(map));
  127. int* a[6] = {map.search(1), map.search(2), map.search(3),
  128. copy.search(1), copy.search(2), copy.search(3)};
  129. for(int i = 0; i < 3; i++) {
  130. CORE_TEST_NOT_NULL(a[i]);
  131. CORE_TEST_NOT_NULL(a[i + 3]);
  132. if(a[i] != nullptr && a[i + 3] != nullptr) {
  133. CORE_TEST_EQUAL(*(a[i]), *(a[i + 3]));
  134. }
  135. }
  136. }
  137. static void testMove() {
  138. IntMap map;
  139. CORE_TEST_NOT_NULL(map.add(1, 3));
  140. CORE_TEST_NOT_NULL(map.add(2, 4));
  141. CORE_TEST_NOT_NULL(map.add(3, 5));
  142. IntMap move(Core::move(map));
  143. int* a = move.search(1);
  144. int* b = move.search(2);
  145. int* c = move.search(3);
  146. CORE_TEST_NOT_NULL(a);
  147. CORE_TEST_NOT_NULL(b);
  148. CORE_TEST_NOT_NULL(c);
  149. if(a != nullptr && b != nullptr && c != nullptr) {
  150. CORE_TEST_EQUAL(3, *a);
  151. CORE_TEST_EQUAL(4, *b);
  152. CORE_TEST_EQUAL(5, *c);
  153. }
  154. }
  155. static void testMoveAssignment() {
  156. IntMap map;
  157. CORE_TEST_NOT_NULL(map.add(1, 3));
  158. CORE_TEST_NOT_NULL(map.add(2, 4));
  159. CORE_TEST_NOT_NULL(map.add(3, 5));
  160. IntMap move;
  161. move = Core::move(map);
  162. int* a = move.search(1);
  163. int* b = move.search(2);
  164. int* c = move.search(3);
  165. CORE_TEST_NOT_NULL(a);
  166. CORE_TEST_NOT_NULL(b);
  167. CORE_TEST_NOT_NULL(c);
  168. if(a != nullptr && b != nullptr && c != nullptr) {
  169. CORE_TEST_EQUAL(3, *a);
  170. CORE_TEST_EQUAL(4, *b);
  171. CORE_TEST_EQUAL(5, *c);
  172. }
  173. }
  174. static void testRemove() {
  175. IntMap map;
  176. CORE_TEST_NOT_NULL(map.add(1, 3));
  177. CORE_TEST_NOT_NULL(map.add(2, 4));
  178. CORE_TEST_NOT_NULL(map.add(3, 5));
  179. bool remove1 = map.remove(2);
  180. bool remove2 = map.remove(7);
  181. int* a = map.search(1);
  182. int* b = map.search(2);
  183. int* c = map.search(3);
  184. CORE_TEST_NOT_NULL(a);
  185. CORE_TEST_NULL(b);
  186. CORE_TEST_NOT_NULL(c);
  187. CORE_TEST_FALSE(remove1);
  188. CORE_TEST_TRUE(remove2);
  189. if(a != nullptr && c != nullptr) {
  190. CORE_TEST_EQUAL(3, *a);
  191. CORE_TEST_EQUAL(5, *c);
  192. }
  193. }
  194. static void testEntryForEach() {
  195. IntMap map;
  196. CORE_TEST_NOT_NULL(map.add(5, 4));
  197. CORE_TEST_NOT_NULL(map.add(10, 3));
  198. CORE_TEST_NOT_NULL(map.add(15, 2));
  199. int counter = 0;
  200. for(auto& entry : map.entries()) {
  201. counter += entry.getKey() + entry.value;
  202. }
  203. CORE_TEST_EQUAL(39, counter);
  204. const IntMap& cmap = map;
  205. counter = 0;
  206. for(const auto& entry : cmap.entries()) {
  207. counter += entry.getKey() + entry.value;
  208. }
  209. CORE_TEST_EQUAL(39, counter);
  210. }
  211. static void testKeyForEach() {
  212. IntMap map;
  213. CORE_TEST_NOT_NULL(map.add(5, 4));
  214. CORE_TEST_NOT_NULL(map.add(10, 3));
  215. CORE_TEST_NOT_NULL(map.add(15, 2));
  216. int counter = 0;
  217. for(const int& key : map.keys()) {
  218. counter += key;
  219. }
  220. CORE_TEST_EQUAL(30, counter);
  221. const IntMap& cmap = map;
  222. counter = 0;
  223. for(const int& key : cmap.keys()) {
  224. counter += key;
  225. }
  226. CORE_TEST_EQUAL(30, counter);
  227. }
  228. static void testValueForEach() {
  229. IntMap map;
  230. CORE_TEST_NOT_NULL(map.add(5, 4));
  231. CORE_TEST_NOT_NULL(map.add(10, 3));
  232. CORE_TEST_NOT_NULL(map.add(15, 2));
  233. int counter = 0;
  234. for(int& value : map.values()) {
  235. counter += value;
  236. }
  237. CORE_TEST_EQUAL(9, counter);
  238. const IntMap& cmap = map;
  239. counter = 0;
  240. for(const int& value : cmap.values()) {
  241. counter += value;
  242. }
  243. CORE_TEST_EQUAL(9, counter);
  244. }
  245. template<typename T>
  246. static void testType() {
  247. Core::HashMap<T, int> m;
  248. CORE_TEST_NOT_NULL(m.add(T(), 3));
  249. }
  250. static void testTypes() {
  251. testType<char>();
  252. testType<signed char>();
  253. testType<signed short>();
  254. testType<signed int>();
  255. testType<signed long>();
  256. testType<signed long long>();
  257. testType<unsigned char>();
  258. testType<unsigned short>();
  259. testType<unsigned int>();
  260. testType<unsigned long>();
  261. testType<unsigned long long>();
  262. }
  263. void Core::HashMapTests::test() {
  264. testAdd();
  265. testMultipleAdd();
  266. testSearch();
  267. testAddReplace();
  268. testClear();
  269. testOverflow();
  270. testEmplace();
  271. testToString1();
  272. testToString2();
  273. testToString3();
  274. testCopy();
  275. testMove();
  276. testMoveAssignment();
  277. testRemove();
  278. testEntryForEach();
  279. testKeyForEach();
  280. testValueForEach();
  281. testTypes();
  282. }