HashMapTests.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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. 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::Error::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::Error::EXISTING_KEY, map.tryEmplace(ar, 3, 6, 7));
  96. CORE_TEST_EQUAL(Core::Error::EXISTING_KEY, map.tryEmplace(ar, 20, 7, 8));
  97. HashMapTestStruct* a = map.search(0);
  98. HashMapTestStruct* b = map.search(3);
  99. HashMapTestStruct* 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(HashMapTestStruct(3, 4), *a);
  105. CORE_TEST_EQUAL(HashMapTestStruct(4, 5), *b);
  106. CORE_TEST_EQUAL(HashMapTestStruct(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. static void testOutOfMemory() {
  267. #ifdef ERROR_SIMULATOR
  268. IntMap map;
  269. int memFails = 0;
  270. for(int i = 0; i < 40; i++) {
  271. Core::Fail::leftAllocations = 2;
  272. int* v = nullptr;
  273. Core::Error e = map.put(v, 1, 1);
  274. if(e == Core::Error::OUT_OF_MEMORY) {
  275. memFails++;
  276. }
  277. }
  278. int* found = map.search(1);
  279. if(CORE_TEST_NOT_NULL(found)) {
  280. CORE_TEST_EQUAL(1, *found);
  281. }
  282. Core::Fail::leftAllocations = -1;
  283. CORE_TEST_TRUE(memFails != 0);
  284. #endif
  285. }
  286. void Core::testHashMap(bool light, bool outOfMemoryTest) {
  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. if(outOfMemoryTest) {
  306. testOutOfMemory();
  307. }
  308. }