ProbingHashMapTests.cpp 9.3 KB

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