ProbingHashMapTests.cpp 8.0 KB

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