HashMapTests.cpp 7.9 KB

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