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