HashMapTests.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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. CORE_TEST_ERROR(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. 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. if(CORE_TEST_NOT_NULL(a)) {
  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(bool light) {
  57. IntMap map;
  58. int limit = light ? 10000 : 100000;
  59. for(int i = 0; i < limit; i++) {
  60. CORE_TEST_ERROR(map.add(i, i));
  61. }
  62. for(int i = 0; i < limit; i++) {
  63. CORE_TEST_TRUE(map.contains(i));
  64. }
  65. }
  66. struct HashMapTestStruct final {
  67. int a;
  68. int b;
  69. HashMapTestStruct(int a_, int b_) : a(a_), b(b_) {
  70. }
  71. // none of these should be needed for the hashmap
  72. HashMapTestStruct(const HashMapTestStruct&) = delete;
  73. HashMapTestStruct(HashMapTestStruct&&) = delete;
  74. HashMapTestStruct& operator=(const HashMapTestStruct&) = delete;
  75. HashMapTestStruct& operator=(HashMapTestStruct&&) = delete;
  76. bool operator==(const HashMapTestStruct& 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::ErrorCode::NONE;
  87. }
  88. };
  89. static void testEmplace() {
  90. Core::HashMap<int, HashMapTestStruct> map;
  91. HashMapTestStruct* 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::ErrorCode::EXISTING_KEY, map.tryEmplace(ar, 3, 6, 7));
  96. CORE_TEST_EQUAL(Core::ErrorCode::EXISTING_KEY,
  97. map.tryEmplace(ar, 20, 7, 8));
  98. HashMapTestStruct* a = map.search(0);
  99. HashMapTestStruct* b = map.search(3);
  100. HashMapTestStruct* c = map.search(20);
  101. CORE_TEST_NOT_NULL(a);
  102. CORE_TEST_NOT_NULL(b);
  103. CORE_TEST_NOT_NULL(c);
  104. if(a != nullptr && b != nullptr && c != nullptr) {
  105. CORE_TEST_EQUAL(HashMapTestStruct(3, 4), *a);
  106. CORE_TEST_EQUAL(HashMapTestStruct(4, 5), *b);
  107. CORE_TEST_EQUAL(HashMapTestStruct(5, 6), *c);
  108. }
  109. }
  110. static void testToString1() {
  111. IntMap map;
  112. CORE_TEST_ERROR(map.add(1, 3));
  113. CORE_TEST_ERROR(map.add(2, 4));
  114. CORE_TEST_ERROR(map.add(3, 5));
  115. CORE_TEST_STRING("[1 = 3, 2 = 4, 3 = 5]", map);
  116. }
  117. static void testToString2() {
  118. IntMap map;
  119. CORE_TEST_ERROR(map.add(1, 3));
  120. CORE_TEST_STRING("[1 = 3]", map);
  121. }
  122. static void testToString3() {
  123. IntMap map;
  124. CORE_TEST_STRING("[]", map);
  125. }
  126. static void testCopy() {
  127. IntMap map;
  128. CORE_TEST_ERROR(map.add(1, 3));
  129. CORE_TEST_ERROR(map.add(2, 4));
  130. CORE_TEST_ERROR(map.add(3, 5));
  131. IntMap copy;
  132. CORE_TEST_ERROR(copy.copyFrom(map));
  133. int* a[6] = {map.search(1), map.search(2), map.search(3),
  134. copy.search(1), copy.search(2), copy.search(3)};
  135. for(int i = 0; i < 3; i++) {
  136. CORE_TEST_NOT_NULL(a[i]);
  137. CORE_TEST_NOT_NULL(a[i + 3]);
  138. if(a[i] != nullptr && a[i + 3] != nullptr) {
  139. CORE_TEST_EQUAL(*(a[i]), *(a[i + 3]));
  140. }
  141. }
  142. }
  143. static void testMove() {
  144. IntMap map;
  145. CORE_TEST_ERROR(map.add(1, 3));
  146. CORE_TEST_ERROR(map.add(2, 4));
  147. CORE_TEST_ERROR(map.add(3, 5));
  148. IntMap move(Core::move(map));
  149. int* a = move.search(1);
  150. int* b = move.search(2);
  151. int* c = move.search(3);
  152. CORE_TEST_NOT_NULL(a);
  153. CORE_TEST_NOT_NULL(b);
  154. CORE_TEST_NOT_NULL(c);
  155. if(a != nullptr && b != nullptr && c != nullptr) {
  156. CORE_TEST_EQUAL(3, *a);
  157. CORE_TEST_EQUAL(4, *b);
  158. CORE_TEST_EQUAL(5, *c);
  159. }
  160. }
  161. static void testMoveAssignment() {
  162. IntMap map;
  163. CORE_TEST_ERROR(map.add(1, 3));
  164. CORE_TEST_ERROR(map.add(2, 4));
  165. CORE_TEST_ERROR(map.add(3, 5));
  166. IntMap move;
  167. move = Core::move(map);
  168. int* a = move.search(1);
  169. int* b = move.search(2);
  170. int* c = move.search(3);
  171. CORE_TEST_NOT_NULL(a);
  172. CORE_TEST_NOT_NULL(b);
  173. CORE_TEST_NOT_NULL(c);
  174. if(a != nullptr && b != nullptr && c != nullptr) {
  175. CORE_TEST_EQUAL(3, *a);
  176. CORE_TEST_EQUAL(4, *b);
  177. CORE_TEST_EQUAL(5, *c);
  178. }
  179. }
  180. static void testRemove() {
  181. IntMap map;
  182. CORE_TEST_ERROR(map.add(1, 3));
  183. CORE_TEST_ERROR(map.add(2, 4));
  184. CORE_TEST_ERROR(map.add(3, 5));
  185. CORE_TEST_ERROR(map.remove(2));
  186. CORE_TEST_EQUAL(Core::ErrorCode::NOT_FOUND, map.remove(7));
  187. int* a = map.search(1);
  188. int* b = map.search(2);
  189. int* c = map.search(3);
  190. CORE_TEST_NOT_NULL(a);
  191. CORE_TEST_NULL(b);
  192. CORE_TEST_NOT_NULL(c);
  193. if(a != nullptr && c != nullptr) {
  194. CORE_TEST_EQUAL(3, *a);
  195. CORE_TEST_EQUAL(5, *c);
  196. }
  197. }
  198. static void testEntryForEach() {
  199. IntMap map;
  200. CORE_TEST_ERROR(map.add(5, 4));
  201. CORE_TEST_ERROR(map.add(10, 3));
  202. CORE_TEST_ERROR(map.add(15, 2));
  203. int counter = 0;
  204. for(auto& entry : map) {
  205. counter += entry.getKey() + entry.value;
  206. }
  207. CORE_TEST_EQUAL(39, counter);
  208. const IntMap& cmap = map;
  209. counter = 0;
  210. for(const auto& entry : cmap) {
  211. counter += entry.getKey() + entry.value;
  212. }
  213. CORE_TEST_EQUAL(39, counter);
  214. }
  215. static void testKeyForEach() {
  216. IntMap map;
  217. CORE_TEST_ERROR(map.add(5, 4));
  218. CORE_TEST_ERROR(map.add(10, 3));
  219. CORE_TEST_ERROR(map.add(15, 2));
  220. int counter = 0;
  221. for(const int& key : map.getKeys()) {
  222. counter += key;
  223. }
  224. CORE_TEST_EQUAL(30, counter);
  225. const IntMap& cmap = map;
  226. counter = 0;
  227. for(const int& key : cmap.getKeys()) {
  228. counter += key;
  229. }
  230. CORE_TEST_EQUAL(30, counter);
  231. }
  232. static void testValueForEach() {
  233. IntMap map;
  234. CORE_TEST_ERROR(map.add(5, 4));
  235. CORE_TEST_ERROR(map.add(10, 3));
  236. CORE_TEST_ERROR(map.add(15, 2));
  237. int counter = 0;
  238. for(int& value : map.getValues()) {
  239. counter += value;
  240. }
  241. CORE_TEST_EQUAL(9, counter);
  242. const IntMap& cmap = map;
  243. counter = 0;
  244. for(const int& value : cmap.getValues()) {
  245. counter += value;
  246. }
  247. CORE_TEST_EQUAL(9, counter);
  248. }
  249. template<typename T>
  250. static void testType() {
  251. Core::HashMap<T, int> m;
  252. CORE_TEST_ERROR(m.add(T(), 3));
  253. }
  254. static void testTypes() {
  255. testType<char>();
  256. testType<signed char>();
  257. testType<signed short>();
  258. testType<signed int>();
  259. testType<signed long>();
  260. testType<signed long long>();
  261. testType<unsigned char>();
  262. testType<unsigned short>();
  263. testType<unsigned int>();
  264. testType<unsigned long>();
  265. testType<unsigned long long>();
  266. }
  267. static void testOutOfMemory() {
  268. #ifdef ERROR_SIMULATOR
  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::ErrorCode::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. #endif
  286. }
  287. void Core::testHashMap(bool light, bool outOfMemoryTest) {
  288. testAdd();
  289. testMultipleAdd();
  290. testSearch();
  291. testAddReplace();
  292. testClear();
  293. testOverflow(light);
  294. testEmplace();
  295. testToString1();
  296. testToString2();
  297. testToString3();
  298. testCopy();
  299. testMove();
  300. testMoveAssignment();
  301. testRemove();
  302. testEntryForEach();
  303. testKeyForEach();
  304. testValueForEach();
  305. testTypes();
  306. if(outOfMemoryTest) {
  307. testOutOfMemory();
  308. }
  309. }