ProbingHashMapTests.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. #include "../../src/ErrorSimulator.hpp"
  2. #include "../Tests.hpp"
  3. #include "core/data/ProbingHashMap.hpp"
  4. #include "core/utils/Error.hpp"
  5. template struct Core::ProbingHashMap<int, int>;
  6. using IntMap = Core::ProbingHashMap<int, int>;
  7. static void testAdd() {
  8. IntMap map;
  9. CORE_TEST_ERROR(map.add(5, 4));
  10. int* value = map.search(5);
  11. CORE_TEST_NOT_NULL(value);
  12. if(value != nullptr) {
  13. CORE_TEST_EQUAL(4, *value);
  14. }
  15. }
  16. static void testMultipleAdd() {
  17. IntMap map;
  18. CORE_TEST_ERROR(map.add(5, 4));
  19. CORE_TEST_ERROR(map.add(10, 3));
  20. CORE_TEST_ERROR(map.add(15, 2));
  21. CORE_TEST_TRUE(map.contains(5));
  22. CORE_TEST_TRUE(map.contains(10));
  23. CORE_TEST_TRUE(map.contains(15));
  24. int* a = map.search(5);
  25. int* b = map.search(10);
  26. int* c = map.search(15);
  27. CORE_TEST_NOT_NULL(a);
  28. CORE_TEST_NOT_NULL(b);
  29. CORE_TEST_NOT_NULL(c);
  30. if(a != nullptr && b != nullptr && c != nullptr) {
  31. CORE_TEST_EQUAL(4, *a);
  32. CORE_TEST_EQUAL(3, *b);
  33. CORE_TEST_EQUAL(2, *c);
  34. }
  35. }
  36. static void testSearch() {
  37. IntMap map;
  38. CORE_TEST_NULL(map.search(6));
  39. CORE_TEST_ERROR(map.add(5, 4));
  40. CORE_TEST_ERROR(map.add(10, 3));
  41. CORE_TEST_ERROR(map.add(15, 2));
  42. CORE_TEST_NULL(map.search(6));
  43. }
  44. static void testAddReplace() {
  45. IntMap map;
  46. CORE_TEST_ERROR(map.add(5, 4));
  47. CORE_TEST_ERROR(map.add(5, 10));
  48. CORE_TEST_TRUE(map.contains(5));
  49. int* a = map.search(5);
  50. CORE_TEST_NOT_NULL(a);
  51. if(a != nullptr) {
  52. CORE_TEST_EQUAL(10, *a);
  53. }
  54. }
  55. static void testClear() {
  56. IntMap map;
  57. CORE_TEST_ERROR(map.add(5, 4));
  58. CORE_TEST_ERROR(map.add(4, 10));
  59. map.clear();
  60. CORE_TEST_FALSE(map.contains(5));
  61. CORE_TEST_FALSE(map.contains(4));
  62. }
  63. static void testOverflow(bool light) {
  64. int limit = light ? 10000 : 100000;
  65. IntMap map;
  66. for(int i = 0; i < limit; i++) {
  67. CORE_TEST_ERROR(map.add(i, i));
  68. }
  69. for(int i = 0; i < limit; i++) {
  70. CORE_TEST_TRUE(map.contains(i));
  71. }
  72. }
  73. static int aInstances = 0;
  74. struct ProbingHashMapTestStruct final {
  75. int a;
  76. int b;
  77. ProbingHashMapTestStruct(int a_, int b_) : a(a_), b(b_) {
  78. aInstances++;
  79. }
  80. ProbingHashMapTestStruct(const ProbingHashMapTestStruct& o)
  81. : a(o.a), b(o.b) {
  82. aInstances++;
  83. }
  84. ProbingHashMapTestStruct(ProbingHashMapTestStruct&& o) : a(o.a), b(o.b) {
  85. aInstances++;
  86. }
  87. ~ProbingHashMapTestStruct() {
  88. aInstances--;
  89. }
  90. ProbingHashMapTestStruct&
  91. operator=(const ProbingHashMapTestStruct& o) = default;
  92. ProbingHashMapTestStruct& operator=(ProbingHashMapTestStruct&& o) = default;
  93. bool operator==(const ProbingHashMapTestStruct& other) const {
  94. return a == other.a && b == other.b;
  95. }
  96. template<typename String>
  97. check_return Core::Error toString(String& s) const {
  98. CORE_RETURN_ERROR(s.append("A("));
  99. CORE_RETURN_ERROR(s.append(a));
  100. CORE_RETURN_ERROR(s.append(", "));
  101. CORE_RETURN_ERROR(s.append(b));
  102. CORE_RETURN_ERROR(s.append(")"));
  103. return Core::ErrorCode::NONE;
  104. }
  105. };
  106. static void testEmplace() {
  107. {
  108. Core::ProbingHashMap<int, ProbingHashMapTestStruct> map;
  109. ProbingHashMapTestStruct* ar = nullptr;
  110. CORE_TEST_ERROR(map.tryEmplace(ar, 0, 3, 4));
  111. CORE_TEST_ERROR(map.tryEmplace(ar, 3, 4, 5));
  112. CORE_TEST_ERROR(map.tryEmplace(ar, 20, 5, 6));
  113. CORE_TEST_EQUAL(Core::ErrorCode::EXISTING_KEY,
  114. map.tryEmplace(ar, 3, 6, 7));
  115. CORE_TEST_EQUAL(Core::ErrorCode::EXISTING_KEY,
  116. map.tryEmplace(ar, 20, 7, 8));
  117. ProbingHashMapTestStruct* a = map.search(0);
  118. ProbingHashMapTestStruct* b = map.search(3);
  119. ProbingHashMapTestStruct* c = map.search(20);
  120. CORE_TEST_NOT_NULL(a);
  121. CORE_TEST_NOT_NULL(b);
  122. CORE_TEST_NOT_NULL(c);
  123. if(a != nullptr && b != nullptr && c != nullptr) {
  124. CORE_TEST_EQUAL(ProbingHashMapTestStruct(3, 4), *a);
  125. CORE_TEST_EQUAL(ProbingHashMapTestStruct(4, 5), *b);
  126. CORE_TEST_EQUAL(ProbingHashMapTestStruct(5, 6), *c);
  127. }
  128. }
  129. CORE_TEST_EQUAL(0, aInstances);
  130. }
  131. static void testToString1() {
  132. IntMap map;
  133. CORE_TEST_ERROR(map.add(1, 3));
  134. CORE_TEST_ERROR(map.add(2, 4));
  135. CORE_TEST_ERROR(map.add(3, 5));
  136. CORE_TEST_STRING("[2 = 4, 1 = 3, 3 = 5]", map);
  137. }
  138. static void testToString2() {
  139. IntMap map;
  140. CORE_TEST_ERROR(map.add(1, 3));
  141. CORE_TEST_STRING("[1 = 3]", map);
  142. }
  143. static void testToString3() {
  144. IntMap map;
  145. CORE_TEST_STRING("[]", map);
  146. }
  147. static void testCopy() {
  148. IntMap map;
  149. CORE_TEST_ERROR(map.add(1, 3));
  150. CORE_TEST_ERROR(map.add(2, 4));
  151. CORE_TEST_ERROR(map.add(3, 5));
  152. IntMap copy;
  153. CORE_TEST_ERROR(copy.copyFrom(map));
  154. int* a[6] = {map.search(1), map.search(2), map.search(3),
  155. copy.search(1), copy.search(2), copy.search(3)};
  156. for(int i = 0; i < 3; i++) {
  157. CORE_TEST_NOT_NULL(a[i]);
  158. CORE_TEST_NOT_NULL(a[i + 3]);
  159. if(a[i] != nullptr && a[i + 3] != nullptr) {
  160. CORE_TEST_EQUAL(*(a[i]), *(a[i + 3]));
  161. }
  162. }
  163. }
  164. static void testMove() {
  165. IntMap map;
  166. CORE_TEST_ERROR(map.add(1, 3));
  167. CORE_TEST_ERROR(map.add(2, 4));
  168. CORE_TEST_ERROR(map.add(3, 5));
  169. IntMap move(Core::move(map));
  170. int* a = move.search(1);
  171. int* b = move.search(2);
  172. int* c = move.search(3);
  173. CORE_TEST_NOT_NULL(a);
  174. CORE_TEST_NOT_NULL(b);
  175. CORE_TEST_NOT_NULL(c);
  176. if(a != nullptr && b != nullptr && c != nullptr) {
  177. CORE_TEST_EQUAL(3, *a);
  178. CORE_TEST_EQUAL(4, *b);
  179. CORE_TEST_EQUAL(5, *c);
  180. }
  181. }
  182. static void testMoveAssignment() {
  183. IntMap map;
  184. CORE_TEST_ERROR(map.add(1, 3));
  185. CORE_TEST_ERROR(map.add(2, 4));
  186. CORE_TEST_ERROR(map.add(3, 5));
  187. IntMap move;
  188. move = Core::move(map);
  189. int* a = move.search(1);
  190. int* b = move.search(2);
  191. int* c = move.search(3);
  192. CORE_TEST_NOT_NULL(a);
  193. CORE_TEST_NOT_NULL(b);
  194. CORE_TEST_NOT_NULL(c);
  195. if(a != nullptr && b != nullptr && c != nullptr) {
  196. CORE_TEST_EQUAL(3, *a);
  197. CORE_TEST_EQUAL(4, *b);
  198. CORE_TEST_EQUAL(5, *c);
  199. }
  200. }
  201. static void testEntryForEach() {
  202. IntMap map;
  203. CORE_TEST_ERROR(map.add(5, 4));
  204. CORE_TEST_ERROR(map.add(10, 3));
  205. CORE_TEST_ERROR(map.add(15, 2));
  206. int counter = 0;
  207. for(auto entry : map) {
  208. counter += entry.getKey() + entry.value;
  209. }
  210. CORE_TEST_EQUAL(39, counter);
  211. const IntMap& cmap = map;
  212. counter = 0;
  213. for(auto entry : cmap) {
  214. counter += entry.getKey() + entry.value;
  215. }
  216. CORE_TEST_EQUAL(39, counter);
  217. }
  218. static void testKeyForEach() {
  219. IntMap map;
  220. CORE_TEST_ERROR(map.add(5, 4));
  221. CORE_TEST_ERROR(map.add(10, 3));
  222. CORE_TEST_ERROR(map.add(15, 2));
  223. int counter = 0;
  224. for(const int& key : map.getKeys()) {
  225. counter += key;
  226. }
  227. CORE_TEST_EQUAL(30, counter);
  228. const IntMap& cmap = map;
  229. counter = 0;
  230. for(const int& key : cmap.getKeys()) {
  231. counter += key;
  232. }
  233. CORE_TEST_EQUAL(30, counter);
  234. }
  235. static void testValueForEach() {
  236. IntMap map;
  237. CORE_TEST_ERROR(map.add(5, 4));
  238. CORE_TEST_ERROR(map.add(10, 3));
  239. CORE_TEST_ERROR(map.add(15, 2));
  240. int counter = 0;
  241. for(int& value : map.getValues()) {
  242. counter += value;
  243. }
  244. CORE_TEST_EQUAL(9, counter);
  245. const IntMap& cmap = map;
  246. counter = 0;
  247. for(const int& value : cmap.getValues()) {
  248. counter += value;
  249. }
  250. CORE_TEST_EQUAL(9, counter);
  251. }
  252. template<typename T>
  253. static void testType() {
  254. Core::ProbingHashMap<T, int> m;
  255. CORE_TEST_ERROR(m.add(T(), 3));
  256. }
  257. static void testTypes() {
  258. testType<char>();
  259. testType<signed char>();
  260. testType<signed short>();
  261. testType<signed int>();
  262. testType<signed long>();
  263. testType<signed long long>();
  264. testType<unsigned char>();
  265. testType<unsigned short>();
  266. testType<unsigned int>();
  267. testType<unsigned long>();
  268. testType<unsigned long long>();
  269. }
  270. static void testOutOfMemory() {
  271. #ifdef ERROR_SIMULATOR
  272. IntMap map;
  273. int memFails = 0;
  274. for(int i = 0; i < 40; i++) {
  275. Core::Fail::leftAllocations = i;
  276. int* v = nullptr;
  277. Core::Error e = map.put(v, 1, 1);
  278. if(e == Core::ErrorCode::OUT_OF_MEMORY) {
  279. memFails++;
  280. }
  281. }
  282. int* found = map.search(1);
  283. if(CORE_TEST_NOT_NULL(found)) {
  284. CORE_TEST_EQUAL(1, *found);
  285. }
  286. Core::Fail::leftAllocations = -1;
  287. CORE_TEST_TRUE(memFails != 0);
  288. #endif
  289. }
  290. static void testInsertInvalid() {
  291. IntMap map;
  292. int* v;
  293. CORE_TEST_EQUAL(Core::ErrorCode::INVALID_ARGUMENT,
  294. map.tryEmplace(v, Core::emptyValue<int>(), 2));
  295. CORE_TEST_EQUAL(Core::ErrorCode::INVALID_ARGUMENT,
  296. map.put(v, Core::emptyValue<int>(), 2));
  297. }
  298. static void testAddCollisions() {
  299. IntMap map;
  300. for(int i = 0; i < 8; i++) {
  301. CORE_TEST_ERROR(map.add(i * 16, i));
  302. }
  303. }
  304. static void testLargeRehash() {
  305. IntMap map;
  306. CORE_TEST_EQUAL(Core::ErrorCode::CAPACITY_REACHED, map.rehash(1llu << 50));
  307. }
  308. void Core::testProbingHashMap(bool light, bool outOfMemoryTest) {
  309. testAdd();
  310. testMultipleAdd();
  311. testSearch();
  312. testAddReplace();
  313. testClear();
  314. testOverflow(light);
  315. testEmplace();
  316. testToString1();
  317. testToString2();
  318. testToString3();
  319. testCopy();
  320. testMove();
  321. testMoveAssignment();
  322. testEntryForEach();
  323. testKeyForEach();
  324. testValueForEach();
  325. testTypes();
  326. if(outOfMemoryTest) {
  327. testOutOfMemory();
  328. }
  329. testInsertInvalid();
  330. testAddCollisions();
  331. testLargeRehash();
  332. }