test_hash_set.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /**************************************************************************/
  2. /* test_hash_set.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_HASH_SET_H
  31. #define TEST_HASH_SET_H
  32. #include "core/templates/hash_set.h"
  33. #include "tests/test_macros.h"
  34. namespace TestHashSet {
  35. TEST_CASE("[HashSet] List initialization") {
  36. HashSet<int> set{ 0, 1, 2, 3, 4 };
  37. CHECK(set.size() == 5);
  38. CHECK(set.has(0));
  39. CHECK(set.has(1));
  40. CHECK(set.has(2));
  41. CHECK(set.has(3));
  42. CHECK(set.has(4));
  43. }
  44. TEST_CASE("[HashSet] List initialization with existing elements") {
  45. HashSet<int> set{ 0, 0, 0, 0, 0 };
  46. CHECK(set.size() == 1);
  47. CHECK(set.has(0));
  48. }
  49. TEST_CASE("[HashSet] Insert element") {
  50. HashSet<int> set;
  51. HashSet<int>::Iterator e = set.insert(42);
  52. CHECK(e);
  53. CHECK(*e == 42);
  54. CHECK(set.has(42));
  55. CHECK(set.find(42));
  56. set.reset();
  57. }
  58. TEST_CASE("[HashSet] Insert existing element") {
  59. HashSet<int> set;
  60. set.insert(42);
  61. set.insert(42);
  62. CHECK(set.has(42));
  63. CHECK(set.size() == 1);
  64. }
  65. TEST_CASE("[HashSet] Insert, iterate and remove many elements") {
  66. const int elem_max = 12343;
  67. HashSet<int> set;
  68. for (int i = 0; i < elem_max; i++) {
  69. set.insert(i);
  70. }
  71. //insert order should have been kept
  72. int idx = 0;
  73. for (const int &K : set) {
  74. CHECK(idx == K);
  75. CHECK(set.has(idx));
  76. idx++;
  77. }
  78. Vector<int> elems_still_valid;
  79. for (int i = 0; i < elem_max; i++) {
  80. if ((i % 5) == 0) {
  81. set.erase(i);
  82. } else {
  83. elems_still_valid.push_back(i);
  84. }
  85. }
  86. CHECK(elems_still_valid.size() == set.size());
  87. for (int i = 0; i < elems_still_valid.size(); i++) {
  88. CHECK(set.has(elems_still_valid[i]));
  89. }
  90. }
  91. TEST_CASE("[HashSet] Insert, iterate and remove many strings") {
  92. // This tests a key that uses allocation, to see if any leaks occur
  93. uint64_t pre_mem = Memory::get_mem_usage();
  94. const int elem_max = 4018;
  95. HashSet<String> set;
  96. for (int i = 0; i < elem_max; i++) {
  97. set.insert(itos(i));
  98. }
  99. //insert order should have been kept
  100. int idx = 0;
  101. for (const String &K : set) {
  102. CHECK(itos(idx) == K);
  103. CHECK(set.has(itos(idx)));
  104. idx++;
  105. }
  106. Vector<String> elems_still_valid;
  107. for (int i = 0; i < elem_max; i++) {
  108. if ((i % 5) == 0) {
  109. set.erase(itos(i));
  110. } else {
  111. elems_still_valid.push_back(itos(i));
  112. }
  113. }
  114. CHECK(elems_still_valid.size() == set.size());
  115. for (int i = 0; i < elems_still_valid.size(); i++) {
  116. CHECK(set.has(elems_still_valid[i]));
  117. }
  118. elems_still_valid.clear();
  119. set.reset();
  120. CHECK(Memory::get_mem_usage() == pre_mem);
  121. }
  122. TEST_CASE("[HashSet] Erase via element") {
  123. HashSet<int> set;
  124. HashSet<int>::Iterator e = set.insert(42);
  125. set.remove(e);
  126. CHECK(!set.has(42));
  127. CHECK(!set.find(42));
  128. }
  129. TEST_CASE("[HashSet] Erase via key") {
  130. HashSet<int> set;
  131. set.insert(42);
  132. set.insert(49);
  133. set.erase(42);
  134. CHECK(!set.has(42));
  135. CHECK(!set.find(42));
  136. }
  137. TEST_CASE("[HashSet] Insert and erase half elements") {
  138. HashSet<int> set;
  139. set.insert(1);
  140. set.insert(2);
  141. set.insert(3);
  142. set.insert(4);
  143. set.erase(1);
  144. set.erase(3);
  145. CHECK(set.size() == 2);
  146. CHECK(set.has(2));
  147. CHECK(set.has(4));
  148. }
  149. TEST_CASE("[HashSet] Size") {
  150. HashSet<int> set;
  151. set.insert(42);
  152. set.insert(123);
  153. set.insert(123);
  154. set.insert(0);
  155. set.insert(123485);
  156. CHECK(set.size() == 4);
  157. }
  158. TEST_CASE("[HashSet] Iteration") {
  159. HashSet<int> set;
  160. set.insert(42);
  161. set.insert(123);
  162. set.insert(0);
  163. set.insert(123485);
  164. Vector<int> expected;
  165. expected.push_back(42);
  166. expected.push_back(123);
  167. expected.push_back(0);
  168. expected.push_back(123485);
  169. int idx = 0;
  170. for (const int &E : set) {
  171. CHECK(expected[idx] == E);
  172. ++idx;
  173. }
  174. }
  175. TEST_CASE("[HashSet] Copy") {
  176. HashSet<int> set;
  177. set.insert(42);
  178. set.insert(123);
  179. set.insert(0);
  180. set.insert(123485);
  181. Vector<int> expected;
  182. expected.push_back(42);
  183. expected.push_back(123);
  184. expected.push_back(0);
  185. expected.push_back(123485);
  186. HashSet<int> copy_assign = set;
  187. int idx = 0;
  188. for (const int &E : copy_assign) {
  189. CHECK(expected[idx] == E);
  190. ++idx;
  191. }
  192. HashSet<int> copy_construct(set);
  193. idx = 0;
  194. for (const int &E : copy_construct) {
  195. CHECK(expected[idx] == E);
  196. ++idx;
  197. }
  198. }
  199. } // namespace TestHashSet
  200. #endif // TEST_HASH_SET_H