HashMapTests.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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_ERROR(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_ERROR(map.add(5, 4));
  17. CORE_TEST_ERROR(map.add(10, 3));
  18. CORE_TEST_ERROR(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_ERROR(map.add(5, 4));
  41. CORE_TEST_ERROR(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_ERROR(map.add(5, 4));
  52. CORE_TEST_ERROR(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_ERROR(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<typename String>
  80. check_return Core::Error toString(String& s) const {
  81. CORE_RETURN_ERROR(s.append("A("));
  82. CORE_RETURN_ERROR(s.append(a));
  83. CORE_RETURN_ERROR(s.append(", "));
  84. CORE_RETURN_ERROR(s.append(b));
  85. CORE_RETURN_ERROR(s.append(")"));
  86. return Core::Error::NONE;
  87. }
  88. };
  89. static void testEmplace() {
  90. Core::HashMap<int, A> map;
  91. A* ar = nullptr;
  92. CORE_TEST_ERROR(map.tryEmplace(ar, 0, 3, 4));
  93. CORE_TEST_ERROR(map.tryEmplace(ar, 3, 4, 5));
  94. CORE_TEST_ERROR(map.tryEmplace(ar, 20, 5, 6));
  95. CORE_TEST_EQUAL(Core::Error::EXISTING_KEY, map.tryEmplace(ar, 3, 6, 7));
  96. CORE_TEST_EQUAL(Core::Error::EXISTING_KEY, map.tryEmplace(ar, 20, 7, 8));
  97. A* a = map.search(0);
  98. A* b = map.search(3);
  99. A* c = map.search(20);
  100. CORE_TEST_NOT_NULL(a);
  101. CORE_TEST_NOT_NULL(b);
  102. CORE_TEST_NOT_NULL(c);
  103. if(a != nullptr && b != nullptr && c != nullptr) {
  104. CORE_TEST_EQUAL(A(3, 4), *a);
  105. CORE_TEST_EQUAL(A(4, 5), *b);
  106. CORE_TEST_EQUAL(A(5, 6), *c);
  107. }
  108. }
  109. static void testToString1() {
  110. IntMap map;
  111. CORE_TEST_ERROR(map.add(1, 3));
  112. CORE_TEST_ERROR(map.add(2, 4));
  113. CORE_TEST_ERROR(map.add(3, 5));
  114. CORE_TEST_STRING("[1 = 3, 2 = 4, 3 = 5]", map);
  115. }
  116. static void testToString2() {
  117. IntMap map;
  118. CORE_TEST_ERROR(map.add(1, 3));
  119. CORE_TEST_STRING("[1 = 3]", map);
  120. }
  121. static void testToString3() {
  122. IntMap map;
  123. CORE_TEST_STRING("[]", map);
  124. }
  125. static void testCopy() {
  126. IntMap map;
  127. CORE_TEST_ERROR(map.add(1, 3));
  128. CORE_TEST_ERROR(map.add(2, 4));
  129. CORE_TEST_ERROR(map.add(3, 5));
  130. IntMap copy;
  131. CORE_TEST_ERROR(copy.copyFrom(map));
  132. int* a[6] = {map.search(1), map.search(2), map.search(3),
  133. copy.search(1), copy.search(2), copy.search(3)};
  134. for(int i = 0; i < 3; i++) {
  135. CORE_TEST_NOT_NULL(a[i]);
  136. CORE_TEST_NOT_NULL(a[i + 3]);
  137. if(a[i] != nullptr && a[i + 3] != nullptr) {
  138. CORE_TEST_EQUAL(*(a[i]), *(a[i + 3]));
  139. }
  140. }
  141. }
  142. static void testMove() {
  143. IntMap map;
  144. CORE_TEST_ERROR(map.add(1, 3));
  145. CORE_TEST_ERROR(map.add(2, 4));
  146. CORE_TEST_ERROR(map.add(3, 5));
  147. IntMap move(Core::move(map));
  148. int* a = move.search(1);
  149. int* b = move.search(2);
  150. int* c = move.search(3);
  151. CORE_TEST_NOT_NULL(a);
  152. CORE_TEST_NOT_NULL(b);
  153. CORE_TEST_NOT_NULL(c);
  154. if(a != nullptr && b != nullptr && c != nullptr) {
  155. CORE_TEST_EQUAL(3, *a);
  156. CORE_TEST_EQUAL(4, *b);
  157. CORE_TEST_EQUAL(5, *c);
  158. }
  159. }
  160. static void testMoveAssignment() {
  161. IntMap map;
  162. CORE_TEST_ERROR(map.add(1, 3));
  163. CORE_TEST_ERROR(map.add(2, 4));
  164. CORE_TEST_ERROR(map.add(3, 5));
  165. IntMap move;
  166. move = Core::move(map);
  167. int* a = move.search(1);
  168. int* b = move.search(2);
  169. int* c = move.search(3);
  170. CORE_TEST_NOT_NULL(a);
  171. CORE_TEST_NOT_NULL(b);
  172. CORE_TEST_NOT_NULL(c);
  173. if(a != nullptr && b != nullptr && c != nullptr) {
  174. CORE_TEST_EQUAL(3, *a);
  175. CORE_TEST_EQUAL(4, *b);
  176. CORE_TEST_EQUAL(5, *c);
  177. }
  178. }
  179. static void testRemove() {
  180. IntMap map;
  181. CORE_TEST_ERROR(map.add(1, 3));
  182. CORE_TEST_ERROR(map.add(2, 4));
  183. CORE_TEST_ERROR(map.add(3, 5));
  184. CORE_TEST_ERROR(map.remove(2));
  185. CORE_TEST_EQUAL(Core::Error::NOT_FOUND, map.remove(7));
  186. int* a = map.search(1);
  187. int* b = map.search(2);
  188. int* c = map.search(3);
  189. CORE_TEST_NOT_NULL(a);
  190. CORE_TEST_NULL(b);
  191. CORE_TEST_NOT_NULL(c);
  192. if(a != nullptr && c != nullptr) {
  193. CORE_TEST_EQUAL(3, *a);
  194. CORE_TEST_EQUAL(5, *c);
  195. }
  196. }
  197. static void testEntryForEach() {
  198. IntMap map;
  199. CORE_TEST_ERROR(map.add(5, 4));
  200. CORE_TEST_ERROR(map.add(10, 3));
  201. CORE_TEST_ERROR(map.add(15, 2));
  202. int counter = 0;
  203. for(auto& entry : map) {
  204. counter += entry.getKey() + entry.value;
  205. }
  206. CORE_TEST_EQUAL(39, counter);
  207. const IntMap& cmap = map;
  208. counter = 0;
  209. for(const auto& entry : cmap) {
  210. counter += entry.getKey() + entry.value;
  211. }
  212. CORE_TEST_EQUAL(39, counter);
  213. }
  214. static void testKeyForEach() {
  215. IntMap map;
  216. CORE_TEST_ERROR(map.add(5, 4));
  217. CORE_TEST_ERROR(map.add(10, 3));
  218. CORE_TEST_ERROR(map.add(15, 2));
  219. int counter = 0;
  220. for(const int& key : map.getKeys()) {
  221. counter += key;
  222. }
  223. CORE_TEST_EQUAL(30, counter);
  224. const IntMap& cmap = map;
  225. counter = 0;
  226. for(const int& key : cmap.getKeys()) {
  227. counter += key;
  228. }
  229. CORE_TEST_EQUAL(30, counter);
  230. }
  231. static void testValueForEach() {
  232. IntMap map;
  233. CORE_TEST_ERROR(map.add(5, 4));
  234. CORE_TEST_ERROR(map.add(10, 3));
  235. CORE_TEST_ERROR(map.add(15, 2));
  236. int counter = 0;
  237. for(int& value : map.getValues()) {
  238. counter += value;
  239. }
  240. CORE_TEST_EQUAL(9, counter);
  241. const IntMap& cmap = map;
  242. counter = 0;
  243. for(const int& value : cmap.getValues()) {
  244. counter += value;
  245. }
  246. CORE_TEST_EQUAL(9, counter);
  247. }
  248. template<typename T>
  249. static void testType() {
  250. Core::HashMap<T, int> m;
  251. CORE_TEST_ERROR(m.add(T(), 3));
  252. }
  253. static void testTypes() {
  254. testType<char>();
  255. testType<signed char>();
  256. testType<signed short>();
  257. testType<signed int>();
  258. testType<signed long>();
  259. testType<signed long long>();
  260. testType<unsigned char>();
  261. testType<unsigned short>();
  262. testType<unsigned int>();
  263. testType<unsigned long>();
  264. testType<unsigned long long>();
  265. }
  266. void Core::HashMapTests::test() {
  267. testAdd();
  268. testMultipleAdd();
  269. testSearch();
  270. testAddReplace();
  271. testClear();
  272. testOverflow();
  273. testEmplace();
  274. testToString1();
  275. testToString2();
  276. testToString3();
  277. testCopy();
  278. testMove();
  279. testMoveAssignment();
  280. testRemove();
  281. testEntryForEach();
  282. testKeyForEach();
  283. testValueForEach();
  284. testTypes();
  285. }