HashMapTests.cpp 8.0 KB

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