HashMapTests.cpp 11 KB

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