test_oa_hash_map.h 6.8 KB

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