ProbingHashMapTests.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. #include "../../src/ErrorSimulator.hpp"
  2. #include "../Tests.hpp"
  3. #include "core/data/ProbingHashMap.hpp"
  4. #include "core/utils/Error.hpp"
  5. #include "core/utils/HashCode.hpp"
  6. template struct Core::ProbingHashMap<int, int>;
  7. using IntMap = Core::ProbingHashMap<int, int>;
  8. constexpr int INVALID = Core::emptyValue<int>();
  9. static IntMap getTestIntMap() {
  10. IntMap map;
  11. map.add(1, 3).add(2, 4).add(3, 5).add(INVALID, 20);
  12. return map;
  13. }
  14. static void checkIntMap(IntMap& map) {
  15. int* a = map.search(1);
  16. int* b = map.search(2);
  17. int* c = map.search(3);
  18. int* d = map.search(INVALID);
  19. if(CORE_TEST_NOT_NULL(a) && CORE_TEST_NOT_NULL(b) &&
  20. CORE_TEST_NOT_NULL(c) && CORE_TEST_NOT_NULL(d)) {
  21. CORE_TEST_EQUAL(3, *a);
  22. CORE_TEST_EQUAL(4, *b);
  23. CORE_TEST_EQUAL(5, *c);
  24. CORE_TEST_EQUAL(20, *d);
  25. }
  26. }
  27. static void testAdd() {
  28. IntMap map;
  29. map.add(5, 4);
  30. int* value = map.search(5);
  31. CORE_TEST_NOT_NULL(value);
  32. if(value != nullptr) {
  33. CORE_TEST_EQUAL(4, *value);
  34. }
  35. }
  36. static void testMultipleAdd() {
  37. IntMap map = getTestIntMap();
  38. CORE_TEST_TRUE(map.contains(1));
  39. CORE_TEST_TRUE(map.contains(2));
  40. CORE_TEST_TRUE(map.contains(3));
  41. checkIntMap(map);
  42. }
  43. static void testSearch() {
  44. IntMap map;
  45. CORE_TEST_NULL(map.search(6));
  46. map.add(5, 4).add(10, 3).add(15, 2);
  47. CORE_TEST_NULL(map.search(6));
  48. }
  49. static void testAddReplace() {
  50. IntMap map;
  51. map.add(5, 4).add(5, 10);
  52. CORE_TEST_TRUE(map.contains(5));
  53. int* a = map.search(5);
  54. if(CORE_TEST_NOT_NULL(a)) {
  55. CORE_TEST_EQUAL(10, *a);
  56. }
  57. }
  58. static void testClear() {
  59. IntMap map;
  60. map.add(5, 4).add(4, 10);
  61. map.clear();
  62. CORE_TEST_FALSE(map.contains(5));
  63. CORE_TEST_FALSE(map.contains(4));
  64. }
  65. static void testOverflow(bool light) {
  66. int limit = light ? 10000 : 100000;
  67. IntMap map;
  68. map.add(INVALID, 42);
  69. for(int i = 0; i < limit; i++) {
  70. map.add(i, i);
  71. }
  72. for(int i = 0; i < limit; i++) {
  73. CORE_TEST_TRUE(map.contains(i));
  74. }
  75. CORE_TEST_TRUE(map.contains(INVALID));
  76. }
  77. static int aInstances = 0;
  78. struct ProbingTest final {
  79. int a;
  80. int b;
  81. ProbingTest(int a_, int b_) : a(a_), b(b_) {
  82. aInstances++;
  83. }
  84. ProbingTest(const ProbingTest& o) : a(o.a), b(o.b) {
  85. aInstances++;
  86. }
  87. ProbingTest(ProbingTest&& o) : a(o.a), b(o.b) {
  88. aInstances++;
  89. }
  90. ~ProbingTest() {
  91. aInstances--;
  92. }
  93. ProbingTest& operator=(const ProbingTest& o) = default;
  94. ProbingTest& operator=(ProbingTest&& o) = default;
  95. bool operator==(const ProbingTest& other) const {
  96. return a == other.a && b == other.b;
  97. }
  98. template<typename String>
  99. check_return Core::Error toString(String& s) const {
  100. CORE_RETURN_ERROR(s.append("A("));
  101. CORE_RETURN_ERROR(s.append(a));
  102. CORE_RETURN_ERROR(s.append(", "));
  103. CORE_RETURN_ERROR(s.append(b));
  104. CORE_RETURN_ERROR(s.append(")"));
  105. return Core::ErrorCode::NONE;
  106. }
  107. };
  108. static void testEmplace() {
  109. {
  110. Core::ProbingHashMap<int, ProbingTest> map;
  111. ProbingTest* ar = nullptr;
  112. CORE_TEST_TRUE(map.tryEmplace(ar, 0, 3, 4));
  113. CORE_TEST_TRUE(map.tryEmplace(ar, 3, 4, 5));
  114. CORE_TEST_TRUE(map.tryEmplace(ar, 20, 5, 6));
  115. CORE_TEST_FALSE(map.tryEmplace(ar, 3, 6, 7));
  116. CORE_TEST_FALSE(map.tryEmplace(ar, 20, 7, 8));
  117. ProbingTest* a = map.search(0);
  118. ProbingTest* b = map.search(3);
  119. ProbingTest* c = map.search(20);
  120. if(CORE_TEST_NOT_NULL(a) && CORE_TEST_NOT_NULL(b) &&
  121. CORE_TEST_NOT_NULL(c)) {
  122. CORE_TEST_EQUAL(ProbingTest(3, 4), *a);
  123. CORE_TEST_EQUAL(ProbingTest(4, 5), *b);
  124. CORE_TEST_EQUAL(ProbingTest(5, 6), *c);
  125. }
  126. }
  127. CORE_TEST_EQUAL(0, aInstances);
  128. }
  129. static void testToString() {
  130. CORE_TEST_STRING("[2 = 4, 1 = 3, 3 = 5, 2147483647 = 20]", getTestIntMap());
  131. CORE_TEST_STRING("[1 = 3]", IntMap().add(1, 3));
  132. CORE_TEST_STRING("[]", IntMap());
  133. }
  134. static void testCopy() {
  135. IntMap map = getTestIntMap();
  136. IntMap copy = map;
  137. IntMap copyA;
  138. copyA = map;
  139. checkIntMap(map);
  140. checkIntMap(copy);
  141. checkIntMap(copyA);
  142. }
  143. static void testMove() {
  144. IntMap map = getTestIntMap();
  145. IntMap move(Core::move(map));
  146. checkIntMap(move);
  147. }
  148. static void testMoveAssignment() {
  149. IntMap map = getTestIntMap();
  150. IntMap move;
  151. move = Core::move(map);
  152. checkIntMap(move);
  153. }
  154. static void testEntryForEach() {
  155. IntMap map;
  156. map.add(5, 4).add(10, 3).add(15, 2);
  157. int counter = 0;
  158. for(auto entry : map) {
  159. counter += entry.getKey() + entry.value;
  160. }
  161. CORE_TEST_EQUAL(39, counter);
  162. const IntMap& cmap = map;
  163. counter = 0;
  164. for(auto entry : cmap) {
  165. counter += entry.getKey() + entry.value;
  166. }
  167. CORE_TEST_EQUAL(39, counter);
  168. }
  169. static void testKeyForEach() {
  170. IntMap map;
  171. map.add(5, 4).add(10, 3).add(15, 2);
  172. int counter = 0;
  173. for(const int& key : map.getKeys()) {
  174. counter += key;
  175. }
  176. CORE_TEST_EQUAL(30, counter);
  177. const IntMap& cmap = map;
  178. counter = 0;
  179. for(const int& key : cmap.getKeys()) {
  180. counter += key;
  181. }
  182. CORE_TEST_EQUAL(30, counter);
  183. }
  184. static void testValueForEach() {
  185. IntMap map;
  186. map.add(5, 4).add(10, 3).add(15, 2);
  187. int counter = 0;
  188. for(int& value : map.getValues()) {
  189. counter += value;
  190. }
  191. CORE_TEST_EQUAL(9, counter);
  192. const IntMap& cmap = map;
  193. counter = 0;
  194. for(const int& value : cmap.getValues()) {
  195. counter += value;
  196. }
  197. CORE_TEST_EQUAL(9, counter);
  198. }
  199. template<typename T>
  200. static void testType() {
  201. Core::ProbingHashMap<T, int> m;
  202. m.add(T(), 3);
  203. }
  204. static void testTypes() {
  205. testType<char>();
  206. testType<signed char>();
  207. testType<signed short>();
  208. testType<signed int>();
  209. testType<signed long>();
  210. testType<signed long long>();
  211. testType<unsigned char>();
  212. testType<unsigned short>();
  213. testType<unsigned int>();
  214. testType<unsigned long>();
  215. testType<unsigned long long>();
  216. }
  217. static void testInvalid() {
  218. IntMap map;
  219. int* v;
  220. CORE_TEST_TRUE(map.tryEmplace(v, INVALID, 2));
  221. if(CORE_TEST_NOT_NULL(v)) {
  222. CORE_TEST_EQUAL(2, *v);
  223. }
  224. CORE_TEST_FALSE(map.tryEmplace(v, INVALID, 6));
  225. if(CORE_TEST_NOT_NULL(v)) {
  226. CORE_TEST_EQUAL(2, *v);
  227. }
  228. CORE_TEST_EQUAL(3, map.put(INVALID, 3));
  229. v = map.search(INVALID);
  230. if(CORE_TEST_NOT_NULL(v)) {
  231. CORE_TEST_EQUAL(3, *v);
  232. }
  233. map.clear();
  234. CORE_TEST_NULL(map.search(INVALID));
  235. }
  236. static void testInvalidPut() {
  237. IntMap map;
  238. CORE_TEST_EQUAL(3, map.put(INVALID, 3));
  239. int* v = map.search(INVALID);
  240. if(CORE_TEST_NOT_NULL(v)) {
  241. CORE_TEST_EQUAL(3, *v);
  242. }
  243. }
  244. static void testAddCollisions() {
  245. IntMap map;
  246. for(int i = 0; i < 8; i++) {
  247. map.add(i * 16, i);
  248. }
  249. }
  250. void Core::testProbingHashMap(bool light) {
  251. testAdd();
  252. testMultipleAdd();
  253. testSearch();
  254. testAddReplace();
  255. testClear();
  256. testOverflow(light);
  257. testEmplace();
  258. testToString();
  259. testCopy();
  260. testMove();
  261. testMoveAssignment();
  262. testEntryForEach();
  263. testKeyForEach();
  264. testValueForEach();
  265. testTypes();
  266. testInvalid();
  267. testInvalidPut();
  268. testAddCollisions();
  269. }