HashMapTests.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. #include "../../src/ErrorSimulator.hpp"
  2. #include "../Tests.hpp"
  3. #include "core/data/HashMap.hpp"
  4. template class Core::HashMap<int, int>;
  5. using IntMap = Core::HashMap<int, int>;
  6. static void testAdd() {
  7. IntMap map;
  8. CORE_TEST_ERROR(map.add(5, 4));
  9. int* value = map.search(5);
  10. CORE_TEST_NOT_NULL(value);
  11. if(value != nullptr) {
  12. CORE_TEST_EQUAL(4, *value);
  13. }
  14. }
  15. static void testMultipleAdd() {
  16. IntMap map;
  17. CORE_TEST_ERROR(map.add(5, 4));
  18. CORE_TEST_ERROR(map.add(10, 3));
  19. CORE_TEST_ERROR(map.add(15, 2));
  20. CORE_TEST_TRUE(map.contains(5));
  21. CORE_TEST_TRUE(map.contains(10));
  22. CORE_TEST_TRUE(map.contains(15));
  23. int* a = map.search(5);
  24. int* b = map.search(10);
  25. int* c = map.search(15);
  26. CORE_TEST_NOT_NULL(a);
  27. CORE_TEST_NOT_NULL(b);
  28. CORE_TEST_NOT_NULL(c);
  29. if(a != nullptr && b != nullptr && c != nullptr) {
  30. CORE_TEST_EQUAL(4, *a);
  31. CORE_TEST_EQUAL(3, *b);
  32. CORE_TEST_EQUAL(2, *c);
  33. }
  34. }
  35. static void testSearch() {
  36. IntMap map;
  37. CORE_TEST_NULL(map.search(6));
  38. }
  39. static void testAddReplace() {
  40. IntMap map;
  41. CORE_TEST_ERROR(map.add(5, 4));
  42. CORE_TEST_ERROR(map.add(5, 10));
  43. CORE_TEST_TRUE(map.contains(5));
  44. int* a = map.search(5);
  45. CORE_TEST_NOT_NULL(a);
  46. if(a != nullptr) {
  47. CORE_TEST_EQUAL(10, *a);
  48. }
  49. }
  50. static void testClear() {
  51. IntMap map;
  52. CORE_TEST_ERROR(map.add(5, 4));
  53. CORE_TEST_ERROR(map.add(4, 10));
  54. map.clear();
  55. CORE_TEST_FALSE(map.contains(5));
  56. CORE_TEST_FALSE(map.contains(4));
  57. }
  58. static void testOverflow(bool light) {
  59. IntMap map;
  60. int limit = light ? 10000 : 100000;
  61. for(int i = 0; i < limit; i++) {
  62. CORE_TEST_ERROR(map.add(i, i));
  63. }
  64. for(int i = 0; i < limit; i++) {
  65. CORE_TEST_TRUE(map.contains(i));
  66. }
  67. }
  68. struct HashMapTestStruct final {
  69. int a;
  70. int b;
  71. HashMapTestStruct(int a_, int b_) : a(a_), b(b_) {
  72. }
  73. // none of these should be needed for the hashmap
  74. HashMapTestStruct(const HashMapTestStruct&) = delete;
  75. HashMapTestStruct(HashMapTestStruct&&) = delete;
  76. HashMapTestStruct& operator=(const HashMapTestStruct&) = delete;
  77. HashMapTestStruct& operator=(HashMapTestStruct&&) = delete;
  78. bool operator==(const HashMapTestStruct& other) const {
  79. return a == other.a && b == other.b;
  80. }
  81. template<typename String>
  82. check_return Core::Error toString(String& s) const {
  83. CORE_RETURN_ERROR(s.append("A("));
  84. CORE_RETURN_ERROR(s.append(a));
  85. CORE_RETURN_ERROR(s.append(", "));
  86. CORE_RETURN_ERROR(s.append(b));
  87. CORE_RETURN_ERROR(s.append(")"));
  88. return Core::Error::NONE;
  89. }
  90. };
  91. static void testEmplace() {
  92. Core::HashMap<int, HashMapTestStruct> map;
  93. HashMapTestStruct* ar = nullptr;
  94. CORE_TEST_ERROR(map.tryEmplace(ar, 0, 3, 4));
  95. CORE_TEST_ERROR(map.tryEmplace(ar, 3, 4, 5));
  96. CORE_TEST_ERROR(map.tryEmplace(ar, 20, 5, 6));
  97. CORE_TEST_EQUAL(Core::Error::EXISTING_KEY, map.tryEmplace(ar, 3, 6, 7));
  98. CORE_TEST_EQUAL(Core::Error::EXISTING_KEY, map.tryEmplace(ar, 20, 7, 8));
  99. HashMapTestStruct* a = map.search(0);
  100. HashMapTestStruct* b = map.search(3);
  101. HashMapTestStruct* 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(HashMapTestStruct(3, 4), *a);
  107. CORE_TEST_EQUAL(HashMapTestStruct(4, 5), *b);
  108. CORE_TEST_EQUAL(HashMapTestStruct(5, 6), *c);
  109. }
  110. }
  111. static void testToString1() {
  112. IntMap map;
  113. CORE_TEST_ERROR(map.add(1, 3));
  114. CORE_TEST_ERROR(map.add(2, 4));
  115. CORE_TEST_ERROR(map.add(3, 5));
  116. CORE_TEST_STRING("[1 = 3, 2 = 4, 3 = 5]", map);
  117. }
  118. static void testToString2() {
  119. IntMap map;
  120. CORE_TEST_ERROR(map.add(1, 3));
  121. CORE_TEST_STRING("[1 = 3]", map);
  122. }
  123. static void testToString3() {
  124. IntMap map;
  125. CORE_TEST_STRING("[]", map);
  126. }
  127. static void testCopy() {
  128. IntMap map;
  129. CORE_TEST_ERROR(map.add(1, 3));
  130. CORE_TEST_ERROR(map.add(2, 4));
  131. CORE_TEST_ERROR(map.add(3, 5));
  132. IntMap copy;
  133. CORE_TEST_ERROR(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_ERROR(map.add(1, 3));
  147. CORE_TEST_ERROR(map.add(2, 4));
  148. CORE_TEST_ERROR(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_ERROR(map.add(1, 3));
  165. CORE_TEST_ERROR(map.add(2, 4));
  166. CORE_TEST_ERROR(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_ERROR(map.add(1, 3));
  184. CORE_TEST_ERROR(map.add(2, 4));
  185. CORE_TEST_ERROR(map.add(3, 5));
  186. CORE_TEST_ERROR(map.remove(2));
  187. CORE_TEST_EQUAL(Core::Error::NOT_FOUND, 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. if(a != nullptr && c != nullptr) {
  195. CORE_TEST_EQUAL(3, *a);
  196. CORE_TEST_EQUAL(5, *c);
  197. }
  198. }
  199. static void testEntryForEach() {
  200. IntMap map;
  201. CORE_TEST_ERROR(map.add(5, 4));
  202. CORE_TEST_ERROR(map.add(10, 3));
  203. CORE_TEST_ERROR(map.add(15, 2));
  204. int counter = 0;
  205. for(auto& entry : map) {
  206. counter += entry.getKey() + entry.value;
  207. }
  208. CORE_TEST_EQUAL(39, counter);
  209. const IntMap& cmap = map;
  210. counter = 0;
  211. for(const auto& entry : cmap) {
  212. counter += entry.getKey() + entry.value;
  213. }
  214. CORE_TEST_EQUAL(39, counter);
  215. }
  216. static void testKeyForEach() {
  217. IntMap map;
  218. CORE_TEST_ERROR(map.add(5, 4));
  219. CORE_TEST_ERROR(map.add(10, 3));
  220. CORE_TEST_ERROR(map.add(15, 2));
  221. int counter = 0;
  222. for(const int& key : map.getKeys()) {
  223. counter += key;
  224. }
  225. CORE_TEST_EQUAL(30, counter);
  226. const IntMap& cmap = map;
  227. counter = 0;
  228. for(const int& key : cmap.getKeys()) {
  229. counter += key;
  230. }
  231. CORE_TEST_EQUAL(30, counter);
  232. }
  233. static void testValueForEach() {
  234. IntMap map;
  235. CORE_TEST_ERROR(map.add(5, 4));
  236. CORE_TEST_ERROR(map.add(10, 3));
  237. CORE_TEST_ERROR(map.add(15, 2));
  238. int counter = 0;
  239. for(int& value : map.getValues()) {
  240. counter += value;
  241. }
  242. CORE_TEST_EQUAL(9, counter);
  243. const IntMap& cmap = map;
  244. counter = 0;
  245. for(const int& value : cmap.getValues()) {
  246. counter += value;
  247. }
  248. CORE_TEST_EQUAL(9, counter);
  249. }
  250. template<typename T>
  251. static void testType() {
  252. Core::HashMap<T, int> m;
  253. CORE_TEST_ERROR(m.add(T(), 3));
  254. }
  255. static void testTypes() {
  256. testType<char>();
  257. testType<signed char>();
  258. testType<signed short>();
  259. testType<signed int>();
  260. testType<signed long>();
  261. testType<signed long long>();
  262. testType<unsigned char>();
  263. testType<unsigned short>();
  264. testType<unsigned int>();
  265. testType<unsigned long>();
  266. testType<unsigned long long>();
  267. }
  268. static void testOutOfMemory() {
  269. IntMap map;
  270. int memFails = 0;
  271. for(int i = 0; i < 40; i++) {
  272. Core::Fail::leftAllocations = 2;
  273. int* v = nullptr;
  274. Core::Error e = map.put(v, 1, 1);
  275. if(e == Core::Error::OUT_OF_MEMORY) {
  276. memFails++;
  277. }
  278. }
  279. int* found = map.search(1);
  280. if(CORE_TEST_NOT_NULL(found)) {
  281. CORE_TEST_EQUAL(1, *found);
  282. }
  283. Core::Fail::leftAllocations = -1;
  284. CORE_TEST_TRUE(memFails > 0);
  285. }
  286. void Core::testHashMap(bool light) {
  287. testAdd();
  288. testMultipleAdd();
  289. testSearch();
  290. testAddReplace();
  291. testClear();
  292. testOverflow(light);
  293. testEmplace();
  294. testToString1();
  295. testToString2();
  296. testToString3();
  297. testCopy();
  298. testMove();
  299. testMoveAssignment();
  300. testRemove();
  301. testEntryForEach();
  302. testKeyForEach();
  303. testValueForEach();
  304. testTypes();
  305. testOutOfMemory();
  306. }