test_hash_set.h 6.5 KB

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