HashMapTests.cpp 9.2 KB

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