ProbingHashMapTests.cpp 8.1 KB

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