test_oa_hash_map.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /**************************************************************************/
  2. /* test_oa_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_OA_HASH_MAP_H
  31. #define TEST_OA_HASH_MAP_H
  32. #include "core/templates/oa_hash_map.h"
  33. #include "scene/resources/texture.h"
  34. #include "tests/test_macros.h"
  35. namespace TestOAHashMap {
  36. TEST_CASE("[OAHashMap] List initialization") {
  37. OAHashMap<int, String> map{ { 0, "A" }, { 1, "B" }, { 2, "C" }, { 3, "D" }, { 4, "E" } };
  38. CHECK(map.get_num_elements() == 5);
  39. String value;
  40. CHECK(map.lookup(0, value));
  41. CHECK(value == "A");
  42. CHECK(map.lookup(1, value));
  43. CHECK(value == "B");
  44. CHECK(map.lookup(2, value));
  45. CHECK(value == "C");
  46. CHECK(map.lookup(3, value));
  47. CHECK(value == "D");
  48. CHECK(map.lookup(4, value));
  49. CHECK(value == "E");
  50. }
  51. TEST_CASE("[OAHashMap] List initialization with existing elements") {
  52. OAHashMap<int, String> map{ { 0, "A" }, { 0, "B" }, { 0, "C" }, { 0, "D" }, { 0, "E" } };
  53. CHECK(map.get_num_elements() == 1);
  54. String value;
  55. CHECK(map.lookup(0, value));
  56. CHECK(value == "E");
  57. }
  58. TEST_CASE("[OAHashMap] Insert element") {
  59. OAHashMap<int, int> map;
  60. map.insert(42, 84);
  61. int data = 0;
  62. bool lookup_res = map.lookup(42, data);
  63. int value = *map.lookup_ptr(42);
  64. CHECK(lookup_res);
  65. CHECK(value == 84);
  66. CHECK(data == 84);
  67. }
  68. TEST_CASE("[OAHashMap] Set element") {
  69. OAHashMap<int, int> map;
  70. map.set(42, 84);
  71. int data = 0;
  72. bool lookup_res = map.lookup(42, data);
  73. int value = *map.lookup_ptr(42);
  74. CHECK(lookup_res);
  75. CHECK(value == 84);
  76. CHECK(data == 84);
  77. }
  78. TEST_CASE("[OAHashMap] Overwrite element") {
  79. OAHashMap<int, int> map;
  80. map.set(42, 84);
  81. map.set(42, 1234);
  82. int result = *map.lookup_ptr(42);
  83. CHECK(result == 1234);
  84. }
  85. TEST_CASE("[OAHashMap] Remove element") {
  86. OAHashMap<int, int> map;
  87. map.insert(42, 84);
  88. map.remove(42);
  89. CHECK(!map.has(42));
  90. }
  91. TEST_CASE("[OAHashMap] Get Num_Elements") {
  92. OAHashMap<int, int> map;
  93. map.set(42, 84);
  94. map.set(123, 84);
  95. map.set(123, 84);
  96. map.set(0, 84);
  97. map.set(123485, 84);
  98. CHECK(map.get_num_elements() == 4);
  99. }
  100. TEST_CASE("[OAHashMap] Iteration") {
  101. OAHashMap<int, int> map;
  102. map.insert(42, 84);
  103. map.insert(123, 12385);
  104. map.insert(0, 12934);
  105. map.insert(123485, 1238888);
  106. map.set(123, 111111);
  107. Vector<Pair<int, int>> expected;
  108. expected.push_back(Pair<int, int>(42, 84));
  109. expected.push_back(Pair<int, int>(123, 111111));
  110. expected.push_back(Pair<int, int>(0, 12934));
  111. expected.push_back(Pair<int, int>(123485, 1238888));
  112. for (OAHashMap<int, int>::Iterator it = map.iter(); it.valid; it = map.next_iter(it)) {
  113. int64_t result = expected.find(Pair<int, int>(*it.key, *it.value));
  114. CHECK(result >= 0);
  115. }
  116. }
  117. TEST_CASE("[OAHashMap] Insert, iterate, remove many strings") {
  118. uint64_t pre_mem = Memory::get_mem_usage();
  119. {
  120. const int elem_max = 40;
  121. OAHashMap<String, int> map;
  122. for (int i = 0; i < elem_max; i++) {
  123. map.insert(itos(i), i);
  124. }
  125. Vector<String> elems_still_valid;
  126. for (int i = 0; i < elem_max; i++) {
  127. if ((i % 5) == 0) {
  128. map.remove(itos(i));
  129. } else {
  130. elems_still_valid.push_back(itos(i));
  131. }
  132. }
  133. CHECK(elems_still_valid.size() == map.get_num_elements());
  134. for (int i = 0; i < elems_still_valid.size(); i++) {
  135. CHECK(map.has(elems_still_valid[i]));
  136. }
  137. }
  138. CHECK(Memory::get_mem_usage() == pre_mem);
  139. }
  140. TEST_CASE("[OAHashMap] Clear") {
  141. OAHashMap<int, int> map;
  142. map.insert(42, 84);
  143. map.insert(0, 1234);
  144. map.clear();
  145. CHECK(!map.has(42));
  146. CHECK(!map.has(0));
  147. CHECK(map.is_empty());
  148. }
  149. TEST_CASE("[OAHashMap] Copy constructor") {
  150. uint64_t pre_mem = Memory::get_mem_usage();
  151. {
  152. OAHashMap<int, int> map0;
  153. const uint32_t count = 5;
  154. for (uint32_t i = 0; i < count; i++) {
  155. map0.insert(i, i);
  156. }
  157. OAHashMap<int, int> map1(map0);
  158. CHECK(map0.get_num_elements() == map1.get_num_elements());
  159. CHECK(map0.get_capacity() == map1.get_capacity());
  160. CHECK(*map0.lookup_ptr(0) == *map1.lookup_ptr(0));
  161. }
  162. CHECK(Memory::get_mem_usage() == pre_mem);
  163. }
  164. TEST_CASE("[OAHashMap] Operator =") {
  165. uint64_t pre_mem = Memory::get_mem_usage();
  166. {
  167. OAHashMap<int, int> map0;
  168. OAHashMap<int, int> map1;
  169. const uint32_t count = 5;
  170. map1.insert(1234, 1234);
  171. for (uint32_t i = 0; i < count; i++) {
  172. map0.insert(i, i);
  173. }
  174. map1 = map0;
  175. CHECK(map0.get_num_elements() == map1.get_num_elements());
  176. CHECK(map0.get_capacity() == map1.get_capacity());
  177. CHECK(*map0.lookup_ptr(0) == *map1.lookup_ptr(0));
  178. }
  179. CHECK(Memory::get_mem_usage() == pre_mem);
  180. }
  181. TEST_CASE("[OAHashMap] Non-trivial types") {
  182. uint64_t pre_mem = Memory::get_mem_usage();
  183. {
  184. OAHashMap<String, Ref<Texture2D>> map1;
  185. const uint32_t count = 10;
  186. for (uint32_t i = 0; i < count; i++) {
  187. String string = "qwerty";
  188. string += itos(i);
  189. Ref<Texture2D> ref_texture_2d;
  190. map1.set(string, ref_texture_2d);
  191. Ref<Texture2D> map_vec = *map1.lookup_ptr(string);
  192. CHECK(map_vec == ref_texture_2d);
  193. }
  194. OAHashMap<String, Ref<Texture2D>> map1copy(map1);
  195. CHECK(map1copy.has(String("qwerty0")));
  196. map1copy = map1;
  197. CHECK(map1copy.has(String("qwerty2")));
  198. OAHashMap<int64_t, Vector4 *> map2;
  199. for (uint32_t i = 0; i < count; i++) {
  200. Vector4 *vec = memnew(Vector4);
  201. vec->x = 10;
  202. vec->y = 12;
  203. vec->z = 151;
  204. vec->w = -13;
  205. map2.set(i, vec);
  206. Vector4 *p = nullptr;
  207. map2.lookup(i, p);
  208. CHECK(*p == *vec);
  209. }
  210. OAHashMap<int64_t, Vector4 *> map3(map2);
  211. for (OAHashMap<int64_t, Vector4 *>::Iterator it = map2.iter(); it.valid; it = map2.next_iter(it)) {
  212. memdelete(*(it.value));
  213. }
  214. }
  215. CHECK(Memory::get_mem_usage() == pre_mem);
  216. }
  217. } // namespace TestOAHashMap
  218. #endif // TEST_OA_HASH_MAP_H