test_hash_set.h 5.7 KB

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