test_a_hash_map.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /**************************************************************************/
  2. /* test_a_hash_map.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifndef TEST_A_HASH_MAP_H
  31. #define TEST_A_HASH_MAP_H
  32. #include "core/templates/a_hash_map.h"
  33. #include "tests/test_macros.h"
  34. namespace TestAHashMap {
  35. TEST_CASE("[AHashMap] List initialization") {
  36. AHashMap<int, String> map{ { 0, "A" }, { 1, "B" }, { 2, "C" }, { 3, "D" }, { 4, "E" } };
  37. CHECK(map.size() == 5);
  38. CHECK(map[0] == "A");
  39. CHECK(map[1] == "B");
  40. CHECK(map[2] == "C");
  41. CHECK(map[3] == "D");
  42. CHECK(map[4] == "E");
  43. }
  44. TEST_CASE("[AHashMap] List initialization with existing elements") {
  45. AHashMap<int, String> map{ { 0, "A" }, { 0, "B" }, { 0, "C" }, { 0, "D" }, { 0, "E" } };
  46. CHECK(map.size() == 1);
  47. CHECK(map[0] == "E");
  48. }
  49. TEST_CASE("[AHashMap] Insert element") {
  50. AHashMap<int, int> map;
  51. AHashMap<int, int>::Iterator e = map.insert(42, 84);
  52. CHECK(e);
  53. CHECK(e->key == 42);
  54. CHECK(e->value == 84);
  55. CHECK(map[42] == 84);
  56. CHECK(map.has(42));
  57. CHECK(map.find(42));
  58. }
  59. TEST_CASE("[AHashMap] Overwrite element") {
  60. AHashMap<int, int> map;
  61. map.insert(42, 84);
  62. map.insert(42, 1234);
  63. CHECK(map[42] == 1234);
  64. }
  65. TEST_CASE("[AHashMap] Erase via element") {
  66. AHashMap<int, int> map;
  67. AHashMap<int, int>::Iterator e = map.insert(42, 84);
  68. map.remove(e);
  69. CHECK(!map.has(42));
  70. CHECK(!map.find(42));
  71. }
  72. TEST_CASE("[AHashMap] Erase via key") {
  73. AHashMap<int, int> map;
  74. map.insert(42, 84);
  75. map.erase(42);
  76. CHECK(!map.has(42));
  77. CHECK(!map.find(42));
  78. }
  79. TEST_CASE("[AHashMap] Size") {
  80. AHashMap<int, int> map;
  81. map.insert(42, 84);
  82. map.insert(123, 84);
  83. map.insert(123, 84);
  84. map.insert(0, 84);
  85. map.insert(123485, 84);
  86. CHECK(map.size() == 4);
  87. }
  88. TEST_CASE("[AHashMap] Iteration") {
  89. AHashMap<int, int> map;
  90. map.insert(42, 84);
  91. map.insert(123, 12385);
  92. map.insert(0, 12934);
  93. map.insert(123485, 1238888);
  94. map.insert(123, 111111);
  95. Vector<Pair<int, int>> expected;
  96. expected.push_back(Pair<int, int>(42, 84));
  97. expected.push_back(Pair<int, int>(123, 111111));
  98. expected.push_back(Pair<int, int>(0, 12934));
  99. expected.push_back(Pair<int, int>(123485, 1238888));
  100. int idx = 0;
  101. for (const KeyValue<int, int> &E : map) {
  102. CHECK(expected[idx] == Pair<int, int>(E.key, E.value));
  103. idx++;
  104. }
  105. idx--;
  106. for (AHashMap<int, int>::Iterator it = map.last(); it; --it) {
  107. CHECK(expected[idx] == Pair<int, int>(it->key, it->value));
  108. idx--;
  109. }
  110. }
  111. TEST_CASE("[AHashMap] Const iteration") {
  112. AHashMap<int, int> map;
  113. map.insert(42, 84);
  114. map.insert(123, 12385);
  115. map.insert(0, 12934);
  116. map.insert(123485, 1238888);
  117. map.insert(123, 111111);
  118. const AHashMap<int, int> const_map = map;
  119. Vector<Pair<int, int>> expected;
  120. expected.push_back(Pair<int, int>(42, 84));
  121. expected.push_back(Pair<int, int>(123, 111111));
  122. expected.push_back(Pair<int, int>(0, 12934));
  123. expected.push_back(Pair<int, int>(123485, 1238888));
  124. expected.push_back(Pair<int, int>(123, 111111));
  125. int idx = 0;
  126. for (const KeyValue<int, int> &E : const_map) {
  127. CHECK(expected[idx] == Pair<int, int>(E.key, E.value));
  128. idx++;
  129. }
  130. idx--;
  131. for (AHashMap<int, int>::ConstIterator it = const_map.last(); it; --it) {
  132. CHECK(expected[idx] == Pair<int, int>(it->key, it->value));
  133. idx--;
  134. }
  135. }
  136. TEST_CASE("[AHashMap] Replace key") {
  137. AHashMap<int, int> map;
  138. map.insert(42, 84);
  139. map.insert(0, 12934);
  140. CHECK(map.replace_key(0, 1));
  141. CHECK(map.has(1));
  142. CHECK(map[1] == 12934);
  143. }
  144. TEST_CASE("[AHashMap] Clear") {
  145. AHashMap<int, int> map;
  146. map.insert(42, 84);
  147. map.insert(123, 12385);
  148. map.insert(0, 12934);
  149. map.clear();
  150. CHECK(!map.has(42));
  151. CHECK(map.size() == 0);
  152. CHECK(map.is_empty());
  153. }
  154. TEST_CASE("[AHashMap] Get") {
  155. AHashMap<int, int> map;
  156. map.insert(42, 84);
  157. map.insert(123, 12385);
  158. map.insert(0, 12934);
  159. CHECK(map.get(123) == 12385);
  160. map.get(123) = 10;
  161. CHECK(map.get(123) == 10);
  162. CHECK(*map.getptr(0) == 12934);
  163. *map.getptr(0) = 1;
  164. CHECK(*map.getptr(0) == 1);
  165. CHECK(map.get(42) == 84);
  166. CHECK(map.getptr(-10) == nullptr);
  167. }
  168. TEST_CASE("[AHashMap] Insert, iterate and remove many elements") {
  169. const int elem_max = 1234;
  170. AHashMap<int, int> map;
  171. for (int i = 0; i < elem_max; i++) {
  172. map.insert(i, i);
  173. }
  174. //insert order should have been kept
  175. int idx = 0;
  176. for (auto &K : map) {
  177. CHECK(idx == K.key);
  178. CHECK(idx == K.value);
  179. CHECK(map.has(idx));
  180. idx++;
  181. }
  182. Vector<int> elems_still_valid;
  183. for (int i = 0; i < elem_max; i++) {
  184. if ((i % 5) == 0) {
  185. map.erase(i);
  186. } else {
  187. elems_still_valid.push_back(i);
  188. }
  189. }
  190. CHECK(elems_still_valid.size() == map.size());
  191. for (int i = 0; i < elems_still_valid.size(); i++) {
  192. CHECK(map.has(elems_still_valid[i]));
  193. }
  194. }
  195. TEST_CASE("[AHashMap] Insert, iterate and remove many strings") {
  196. const int elem_max = 432;
  197. AHashMap<String, String> map;
  198. // To not print WARNING: Excessive collision count (NN), is the right hash function being used?
  199. ERR_PRINT_OFF;
  200. for (int i = 0; i < elem_max; i++) {
  201. map.insert(itos(i), itos(i));
  202. }
  203. ERR_PRINT_ON;
  204. //insert order should have been kept
  205. int idx = 0;
  206. for (auto &K : map) {
  207. CHECK(itos(idx) == K.key);
  208. CHECK(itos(idx) == K.value);
  209. CHECK(map.has(itos(idx)));
  210. idx++;
  211. }
  212. Vector<String> elems_still_valid;
  213. for (int i = 0; i < elem_max; i++) {
  214. if ((i % 5) == 0) {
  215. map.erase(itos(i));
  216. } else {
  217. elems_still_valid.push_back(itos(i));
  218. }
  219. }
  220. CHECK(elems_still_valid.size() == map.size());
  221. for (int i = 0; i < elems_still_valid.size(); i++) {
  222. CHECK(map.has(elems_still_valid[i]));
  223. }
  224. elems_still_valid.clear();
  225. }
  226. TEST_CASE("[AHashMap] Copy constructor") {
  227. AHashMap<int, int> map0;
  228. const uint32_t count = 5;
  229. for (uint32_t i = 0; i < count; i++) {
  230. map0.insert(i, i);
  231. }
  232. AHashMap<int, int> map1(map0);
  233. CHECK(map0.size() == map1.size());
  234. CHECK(map0.get_capacity() == map1.get_capacity());
  235. CHECK(*map0.getptr(0) == *map1.getptr(0));
  236. }
  237. TEST_CASE("[AHashMap] Operator =") {
  238. AHashMap<int, int> map0;
  239. AHashMap<int, int> map1;
  240. const uint32_t count = 5;
  241. map1.insert(1234, 1234);
  242. for (uint32_t i = 0; i < count; i++) {
  243. map0.insert(i, i);
  244. }
  245. map1 = map0;
  246. CHECK(map0.size() == map1.size());
  247. CHECK(map0.get_capacity() == map1.get_capacity());
  248. CHECK(*map0.getptr(0) == *map1.getptr(0));
  249. }
  250. TEST_CASE("[AHashMap] Array methods") {
  251. AHashMap<int, int> map;
  252. for (int i = 0; i < 100; i++) {
  253. map.insert(100 - i, i);
  254. }
  255. for (int i = 0; i < 100; i++) {
  256. CHECK(map.get_by_index(i).value == i);
  257. }
  258. int index = map.get_index(1);
  259. CHECK(map.get_by_index(index).value == 99);
  260. CHECK(map.erase_by_index(index));
  261. CHECK(!map.erase_by_index(index));
  262. CHECK(map.get_index(1) == -1);
  263. }
  264. } // namespace TestAHashMap
  265. #endif // TEST_A_HASH_MAP_H