HashMapTests.cpp 7.4 KB

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