ProbingHashMapTests.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. #include "../../src/ErrorSimulator.hpp"
  2. #include "../Tests.hpp"
  3. #include "core/data/ProbingHashMap.hpp"
  4. template class 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::Error::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::Error::EXISTING_KEY, map.tryEmplace(ar, 3, 6, 7));
  113. CORE_TEST_EQUAL(Core::Error::EXISTING_KEY,
  114. map.tryEmplace(ar, 20, 7, 8));
  115. ProbingHashMapTestStruct* a = map.search(0);
  116. ProbingHashMapTestStruct* b = map.search(3);
  117. ProbingHashMapTestStruct* c = map.search(20);
  118. CORE_TEST_NOT_NULL(a);
  119. CORE_TEST_NOT_NULL(b);
  120. CORE_TEST_NOT_NULL(c);
  121. if(a != nullptr && b != nullptr && c != nullptr) {
  122. CORE_TEST_EQUAL(ProbingHashMapTestStruct(3, 4), *a);
  123. CORE_TEST_EQUAL(ProbingHashMapTestStruct(4, 5), *b);
  124. CORE_TEST_EQUAL(ProbingHashMapTestStruct(5, 6), *c);
  125. }
  126. }
  127. CORE_TEST_EQUAL(0, aInstances);
  128. }
  129. static void testToString1() {
  130. IntMap map;
  131. CORE_TEST_ERROR(map.add(1, 3));
  132. CORE_TEST_ERROR(map.add(2, 4));
  133. CORE_TEST_ERROR(map.add(3, 5));
  134. CORE_TEST_STRING("[2 = 4, 1 = 3, 3 = 5]", map);
  135. }
  136. static void testToString2() {
  137. IntMap map;
  138. CORE_TEST_ERROR(map.add(1, 3));
  139. CORE_TEST_STRING("[1 = 3]", map);
  140. }
  141. static void testToString3() {
  142. IntMap map;
  143. CORE_TEST_STRING("[]", map);
  144. }
  145. static void testCopy() {
  146. IntMap map;
  147. CORE_TEST_ERROR(map.add(1, 3));
  148. CORE_TEST_ERROR(map.add(2, 4));
  149. CORE_TEST_ERROR(map.add(3, 5));
  150. IntMap copy;
  151. CORE_TEST_ERROR(copy.copyFrom(map));
  152. int* a[6] = {map.search(1), map.search(2), map.search(3),
  153. copy.search(1), copy.search(2), copy.search(3)};
  154. for(int i = 0; i < 3; i++) {
  155. CORE_TEST_NOT_NULL(a[i]);
  156. CORE_TEST_NOT_NULL(a[i + 3]);
  157. if(a[i] != nullptr && a[i + 3] != nullptr) {
  158. CORE_TEST_EQUAL(*(a[i]), *(a[i + 3]));
  159. }
  160. }
  161. }
  162. static void testMove() {
  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(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 testMoveAssignment() {
  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. IntMap move;
  186. move = Core::move(map);
  187. int* a = move.search(1);
  188. int* b = move.search(2);
  189. int* c = move.search(3);
  190. CORE_TEST_NOT_NULL(a);
  191. CORE_TEST_NOT_NULL(b);
  192. CORE_TEST_NOT_NULL(c);
  193. if(a != nullptr && b != nullptr && c != nullptr) {
  194. CORE_TEST_EQUAL(3, *a);
  195. CORE_TEST_EQUAL(4, *b);
  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(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::ProbingHashMap<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. #ifdef ERROR_SIMULATOR
  270. IntMap map;
  271. int memFails = 0;
  272. for(int i = 0; i < 40; i++) {
  273. Core::Fail::leftAllocations = i;
  274. int* v = nullptr;
  275. Core::Error e = map.put(v, 1, 1);
  276. if(e == Core::Error::OUT_OF_MEMORY) {
  277. memFails++;
  278. }
  279. }
  280. int* found = map.search(1);
  281. if(CORE_TEST_NOT_NULL(found)) {
  282. CORE_TEST_EQUAL(1, *found);
  283. }
  284. Core::Fail::leftAllocations = -1;
  285. CORE_TEST_TRUE(memFails != 0);
  286. #endif
  287. }
  288. static void testInsertInvalid() {
  289. IntMap map;
  290. int* v;
  291. CORE_TEST_EQUAL(Core::Error::INVALID_ARGUMENT,
  292. map.tryEmplace(v, Core::emptyValue<int>(), 2));
  293. CORE_TEST_EQUAL(Core::Error::INVALID_ARGUMENT,
  294. map.put(v, Core::emptyValue<int>(), 2));
  295. }
  296. static void testAddCollisions() {
  297. IntMap map;
  298. for(int i = 0; i < 8; i++) {
  299. CORE_TEST_ERROR(map.add(i * 16, i));
  300. }
  301. }
  302. void Core::testProbingHashMap(bool light, bool outOfMemoryTest) {
  303. testAdd();
  304. testMultipleAdd();
  305. testSearch();
  306. testAddReplace();
  307. testClear();
  308. testOverflow(light);
  309. testEmplace();
  310. testToString1();
  311. testToString2();
  312. testToString3();
  313. testCopy();
  314. testMove();
  315. testMoveAssignment();
  316. testEntryForEach();
  317. testKeyForEach();
  318. testValueForEach();
  319. testTypes();
  320. if(outOfMemoryTest) {
  321. testOutOfMemory();
  322. }
  323. testInsertInvalid();
  324. testAddCollisions();
  325. }