ProbingHashMapTests.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. #include "tests/ProbingHashMapTests.hpp"
  2. #include <stdio.h>
  3. #include "data/ProbingHashMap.hpp"
  4. #include "test/Test.hpp"
  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. }
  39. static void testAddReplace() {
  40. IntMap map;
  41. CORE_TEST_ERROR(map.add(5, 4));
  42. CORE_TEST_ERROR(map.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. CORE_TEST_ERROR(map.add(5, 4));
  53. CORE_TEST_ERROR(map.add(4, 10));
  54. map.clear();
  55. CORE_TEST_FALSE(map.contains(5));
  56. CORE_TEST_FALSE(map.contains(4));
  57. }
  58. static void testOverflow() {
  59. IntMap map;
  60. for(int i = 0; i < 100000; i++) {
  61. CORE_TEST_ERROR(map.add(i, i));
  62. }
  63. for(int i = 0; i < 100000; 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::Error::NONE;
  98. }
  99. };
  100. static void testEmplace() {
  101. {
  102. Core::ProbingHashMap<int, ProbingHashMapTestStruct> map;
  103. ProbingHashMapTestStruct* ar = nullptr;
  104. CORE_TEST_ERROR(map.tryEmplace(ar, 0, 3, 4));
  105. CORE_TEST_ERROR(map.tryEmplace(ar, 3, 4, 5));
  106. CORE_TEST_ERROR(map.tryEmplace(ar, 20, 5, 6));
  107. CORE_TEST_EQUAL(Core::Error::EXISTING_KEY, map.tryEmplace(ar, 3, 6, 7));
  108. CORE_TEST_EQUAL(Core::Error::EXISTING_KEY,
  109. map.tryEmplace(ar, 20, 7, 8));
  110. ProbingHashMapTestStruct* a = map.search(0);
  111. ProbingHashMapTestStruct* b = map.search(3);
  112. ProbingHashMapTestStruct* c = map.search(20);
  113. CORE_TEST_NOT_NULL(a);
  114. CORE_TEST_NOT_NULL(b);
  115. CORE_TEST_NOT_NULL(c);
  116. if(a != nullptr && b != nullptr && c != nullptr) {
  117. CORE_TEST_EQUAL(ProbingHashMapTestStruct(3, 4), *a);
  118. CORE_TEST_EQUAL(ProbingHashMapTestStruct(4, 5), *b);
  119. CORE_TEST_EQUAL(ProbingHashMapTestStruct(5, 6), *c);
  120. }
  121. }
  122. CORE_TEST_EQUAL(0, aInstances);
  123. }
  124. static void testToString1() {
  125. IntMap map;
  126. CORE_TEST_ERROR(map.add(1, 3));
  127. CORE_TEST_ERROR(map.add(2, 4));
  128. CORE_TEST_ERROR(map.add(3, 5));
  129. CORE_TEST_STRING("[2 = 4, 1 = 3, 3 = 5]", map);
  130. }
  131. static void testToString2() {
  132. IntMap map;
  133. CORE_TEST_ERROR(map.add(1, 3));
  134. CORE_TEST_STRING("[1 = 3]", map);
  135. }
  136. static void testToString3() {
  137. IntMap map;
  138. CORE_TEST_STRING("[]", map);
  139. }
  140. static void testCopy() {
  141. IntMap map;
  142. CORE_TEST_ERROR(map.add(1, 3));
  143. CORE_TEST_ERROR(map.add(2, 4));
  144. CORE_TEST_ERROR(map.add(3, 5));
  145. IntMap copy;
  146. CORE_TEST_ERROR(copy.copyFrom(map));
  147. int* a[6] = {map.search(1), map.search(2), map.search(3),
  148. copy.search(1), copy.search(2), copy.search(3)};
  149. for(int i = 0; i < 3; i++) {
  150. CORE_TEST_NOT_NULL(a[i]);
  151. CORE_TEST_NOT_NULL(a[i + 3]);
  152. if(a[i] != nullptr && a[i + 3] != nullptr) {
  153. CORE_TEST_EQUAL(*(a[i]), *(a[i + 3]));
  154. }
  155. }
  156. }
  157. static void testMove() {
  158. IntMap map;
  159. CORE_TEST_ERROR(map.add(1, 3));
  160. CORE_TEST_ERROR(map.add(2, 4));
  161. CORE_TEST_ERROR(map.add(3, 5));
  162. IntMap move(Core::move(map));
  163. int* a = move.search(1);
  164. int* b = move.search(2);
  165. int* c = move.search(3);
  166. CORE_TEST_NOT_NULL(a);
  167. CORE_TEST_NOT_NULL(b);
  168. CORE_TEST_NOT_NULL(c);
  169. if(a != nullptr && b != nullptr && c != nullptr) {
  170. CORE_TEST_EQUAL(3, *a);
  171. CORE_TEST_EQUAL(4, *b);
  172. CORE_TEST_EQUAL(5, *c);
  173. }
  174. }
  175. static void testMoveAssignment() {
  176. IntMap map;
  177. CORE_TEST_ERROR(map.add(1, 3));
  178. CORE_TEST_ERROR(map.add(2, 4));
  179. CORE_TEST_ERROR(map.add(3, 5));
  180. IntMap move;
  181. move = Core::move(map);
  182. int* a = move.search(1);
  183. int* b = move.search(2);
  184. int* c = move.search(3);
  185. CORE_TEST_NOT_NULL(a);
  186. CORE_TEST_NOT_NULL(b);
  187. CORE_TEST_NOT_NULL(c);
  188. if(a != nullptr && b != nullptr && c != nullptr) {
  189. CORE_TEST_EQUAL(3, *a);
  190. CORE_TEST_EQUAL(4, *b);
  191. CORE_TEST_EQUAL(5, *c);
  192. }
  193. }
  194. static void testEntryForEach() {
  195. IntMap map;
  196. CORE_TEST_ERROR(map.add(5, 4));
  197. CORE_TEST_ERROR(map.add(10, 3));
  198. CORE_TEST_ERROR(map.add(15, 2));
  199. int counter = 0;
  200. for(auto entry : map) {
  201. counter += entry.getKey() + entry.value;
  202. }
  203. CORE_TEST_EQUAL(39, counter);
  204. const IntMap& cmap = map;
  205. counter = 0;
  206. for(auto entry : cmap) {
  207. counter += entry.getKey() + entry.value;
  208. }
  209. CORE_TEST_EQUAL(39, counter);
  210. }
  211. static void testKeyForEach() {
  212. IntMap map;
  213. CORE_TEST_ERROR(map.add(5, 4));
  214. CORE_TEST_ERROR(map.add(10, 3));
  215. CORE_TEST_ERROR(map.add(15, 2));
  216. int counter = 0;
  217. for(const int& key : map.getKeys()) {
  218. counter += key;
  219. }
  220. CORE_TEST_EQUAL(30, counter);
  221. const IntMap& cmap = map;
  222. counter = 0;
  223. for(const int& key : cmap.getKeys()) {
  224. counter += key;
  225. }
  226. CORE_TEST_EQUAL(30, counter);
  227. }
  228. static void testValueForEach() {
  229. IntMap map;
  230. CORE_TEST_ERROR(map.add(5, 4));
  231. CORE_TEST_ERROR(map.add(10, 3));
  232. CORE_TEST_ERROR(map.add(15, 2));
  233. int counter = 0;
  234. for(int& value : map.getValues()) {
  235. counter += value;
  236. }
  237. CORE_TEST_EQUAL(9, counter);
  238. const IntMap& cmap = map;
  239. counter = 0;
  240. for(const int& value : cmap.getValues()) {
  241. counter += value;
  242. }
  243. CORE_TEST_EQUAL(9, counter);
  244. }
  245. template<typename T>
  246. static void testType() {
  247. Core::ProbingHashMap<T, int> m;
  248. CORE_TEST_ERROR(m.add(T(), 3));
  249. }
  250. static void testTypes() {
  251. testType<char>();
  252. testType<signed char>();
  253. testType<signed short>();
  254. testType<signed int>();
  255. testType<signed long>();
  256. testType<signed long long>();
  257. testType<unsigned char>();
  258. testType<unsigned short>();
  259. testType<unsigned int>();
  260. testType<unsigned long>();
  261. testType<unsigned long long>();
  262. }
  263. void Core::ProbingHashMapTests::test() {
  264. testAdd();
  265. testMultipleAdd();
  266. testSearch();
  267. testAddReplace();
  268. testClear();
  269. testOverflow();
  270. testEmplace();
  271. testToString1();
  272. testToString2();
  273. testToString3();
  274. testCopy();
  275. testMove();
  276. testMoveAssignment();
  277. testEntryForEach();
  278. testKeyForEach();
  279. testValueForEach();
  280. testTypes();
  281. }