test_local_vector.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /**************************************************************************/
  2. /* test_local_vector.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_LOCAL_VECTOR_H
  31. #define TEST_LOCAL_VECTOR_H
  32. #include "core/templates/local_vector.h"
  33. #include "tests/test_macros.h"
  34. namespace TestLocalVector {
  35. TEST_CASE("[LocalVector] List Initialization.") {
  36. LocalVector<int> vector{ 0, 1, 2, 3, 4 };
  37. CHECK(vector.size() == 5);
  38. CHECK(vector[0] == 0);
  39. CHECK(vector[1] == 1);
  40. CHECK(vector[2] == 2);
  41. CHECK(vector[3] == 3);
  42. CHECK(vector[4] == 4);
  43. }
  44. TEST_CASE("[LocalVector] Push Back.") {
  45. LocalVector<int> vector;
  46. vector.push_back(0);
  47. vector.push_back(1);
  48. vector.push_back(2);
  49. vector.push_back(3);
  50. vector.push_back(4);
  51. CHECK(vector[0] == 0);
  52. CHECK(vector[1] == 1);
  53. CHECK(vector[2] == 2);
  54. CHECK(vector[3] == 3);
  55. CHECK(vector[4] == 4);
  56. }
  57. TEST_CASE("[LocalVector] Find, has.") {
  58. LocalVector<int> vector;
  59. vector.push_back(3);
  60. vector.push_back(1);
  61. vector.push_back(4);
  62. vector.push_back(0);
  63. vector.push_back(2);
  64. CHECK(vector[0] == 3);
  65. CHECK(vector[1] == 1);
  66. CHECK(vector[2] == 4);
  67. CHECK(vector[3] == 0);
  68. CHECK(vector[4] == 2);
  69. CHECK(vector.find(0) == 3);
  70. CHECK(vector.find(1) == 1);
  71. CHECK(vector.find(2) == 4);
  72. CHECK(vector.find(3) == 0);
  73. CHECK(vector.find(4) == 2);
  74. CHECK(vector.find(-1) == -1);
  75. CHECK(vector.find(5) == -1);
  76. CHECK(vector.has(0));
  77. CHECK(vector.has(1));
  78. CHECK(vector.has(2));
  79. CHECK(vector.has(3));
  80. CHECK(vector.has(4));
  81. CHECK(!vector.has(-1));
  82. CHECK(!vector.has(5));
  83. }
  84. TEST_CASE("[LocalVector] Remove.") {
  85. LocalVector<int> vector;
  86. vector.push_back(0);
  87. vector.push_back(1);
  88. vector.push_back(2);
  89. vector.push_back(3);
  90. vector.push_back(4);
  91. vector.remove_at(0);
  92. CHECK(vector[0] == 1);
  93. CHECK(vector[1] == 2);
  94. CHECK(vector[2] == 3);
  95. CHECK(vector[3] == 4);
  96. vector.remove_at(2);
  97. CHECK(vector[0] == 1);
  98. CHECK(vector[1] == 2);
  99. CHECK(vector[2] == 4);
  100. vector.remove_at(1);
  101. CHECK(vector[0] == 1);
  102. CHECK(vector[1] == 4);
  103. vector.remove_at(0);
  104. CHECK(vector[0] == 4);
  105. }
  106. TEST_CASE("[LocalVector] Remove Unordered.") {
  107. LocalVector<int> vector;
  108. vector.push_back(0);
  109. vector.push_back(1);
  110. vector.push_back(2);
  111. vector.push_back(3);
  112. vector.push_back(4);
  113. CHECK(vector.size() == 5);
  114. vector.remove_at_unordered(0);
  115. CHECK(vector.size() == 4);
  116. CHECK(vector.find(0) == -1);
  117. CHECK(vector.find(1) != -1);
  118. CHECK(vector.find(2) != -1);
  119. CHECK(vector.find(3) != -1);
  120. CHECK(vector.find(4) != -1);
  121. // Now the vector is no more ordered.
  122. vector.remove_at_unordered(vector.find(3));
  123. CHECK(vector.size() == 3);
  124. CHECK(vector.find(3) == -1);
  125. CHECK(vector.find(1) != -1);
  126. CHECK(vector.find(2) != -1);
  127. CHECK(vector.find(4) != -1);
  128. vector.remove_at_unordered(vector.find(2));
  129. CHECK(vector.size() == 2);
  130. CHECK(vector.find(2) == -1);
  131. CHECK(vector.find(1) != -1);
  132. CHECK(vector.find(4) != -1);
  133. vector.remove_at_unordered(vector.find(4));
  134. CHECK(vector.size() == 1);
  135. CHECK(vector.find(4) == -1);
  136. CHECK(vector.find(1) != -1);
  137. // Remove the last one.
  138. vector.remove_at_unordered(0);
  139. CHECK(vector.is_empty());
  140. CHECK(vector.size() == 0);
  141. }
  142. TEST_CASE("[LocalVector] Erase.") {
  143. LocalVector<int> vector;
  144. vector.push_back(1);
  145. vector.push_back(3);
  146. vector.push_back(0);
  147. vector.push_back(2);
  148. vector.push_back(4);
  149. CHECK(vector.find(2) == 3);
  150. vector.erase(2);
  151. CHECK(vector.find(2) == -1);
  152. CHECK(vector.size() == 4);
  153. }
  154. TEST_CASE("[LocalVector] Size / Resize / Reserve.") {
  155. LocalVector<int> vector;
  156. CHECK(vector.is_empty());
  157. CHECK(vector.size() == 0);
  158. CHECK(vector.get_capacity() == 0);
  159. vector.resize(10);
  160. CHECK(vector.size() == 10);
  161. CHECK(vector.get_capacity() >= 10);
  162. vector.resize(5);
  163. CHECK(vector.size() == 5);
  164. // Capacity is supposed to change only when the size increase.
  165. CHECK(vector.get_capacity() >= 10);
  166. vector.remove_at(0);
  167. vector.remove_at(0);
  168. vector.remove_at(0);
  169. CHECK(vector.size() == 2);
  170. // Capacity is supposed to change only when the size increase.
  171. CHECK(vector.get_capacity() >= 10);
  172. vector.reset();
  173. CHECK(vector.size() == 0);
  174. CHECK(vector.get_capacity() == 0);
  175. vector.reserve(3);
  176. CHECK(vector.is_empty());
  177. CHECK(vector.size() == 0);
  178. CHECK(vector.get_capacity() >= 3);
  179. vector.push_back(0);
  180. vector.push_back(0);
  181. vector.push_back(0);
  182. CHECK(vector.size() == 3);
  183. CHECK(vector.get_capacity() >= 3);
  184. vector.push_back(0);
  185. CHECK(vector.size() == 4);
  186. CHECK(vector.get_capacity() >= 4);
  187. }
  188. } // namespace TestLocalVector
  189. #endif // TEST_LOCAL_VECTOR_H