ProbingHashMapTests.cpp 8.0 KB

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