ProbingHashMapTests.cpp 7.7 KB

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