test_local_vector.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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.") {
  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. }
  77. TEST_CASE("[LocalVector] Remove.") {
  78. LocalVector<int> vector;
  79. vector.push_back(0);
  80. vector.push_back(1);
  81. vector.push_back(2);
  82. vector.push_back(3);
  83. vector.push_back(4);
  84. vector.remove_at(0);
  85. CHECK(vector[0] == 1);
  86. CHECK(vector[1] == 2);
  87. CHECK(vector[2] == 3);
  88. CHECK(vector[3] == 4);
  89. vector.remove_at(2);
  90. CHECK(vector[0] == 1);
  91. CHECK(vector[1] == 2);
  92. CHECK(vector[2] == 4);
  93. vector.remove_at(1);
  94. CHECK(vector[0] == 1);
  95. CHECK(vector[1] == 4);
  96. vector.remove_at(0);
  97. CHECK(vector[0] == 4);
  98. }
  99. TEST_CASE("[LocalVector] Remove Unordered.") {
  100. LocalVector<int> vector;
  101. vector.push_back(0);
  102. vector.push_back(1);
  103. vector.push_back(2);
  104. vector.push_back(3);
  105. vector.push_back(4);
  106. CHECK(vector.size() == 5);
  107. vector.remove_at_unordered(0);
  108. CHECK(vector.size() == 4);
  109. CHECK(vector.find(0) == -1);
  110. CHECK(vector.find(1) != -1);
  111. CHECK(vector.find(2) != -1);
  112. CHECK(vector.find(3) != -1);
  113. CHECK(vector.find(4) != -1);
  114. // Now the vector is no more ordered.
  115. vector.remove_at_unordered(vector.find(3));
  116. CHECK(vector.size() == 3);
  117. CHECK(vector.find(3) == -1);
  118. CHECK(vector.find(1) != -1);
  119. CHECK(vector.find(2) != -1);
  120. CHECK(vector.find(4) != -1);
  121. vector.remove_at_unordered(vector.find(2));
  122. CHECK(vector.size() == 2);
  123. CHECK(vector.find(2) == -1);
  124. CHECK(vector.find(1) != -1);
  125. CHECK(vector.find(4) != -1);
  126. vector.remove_at_unordered(vector.find(4));
  127. CHECK(vector.size() == 1);
  128. CHECK(vector.find(4) == -1);
  129. CHECK(vector.find(1) != -1);
  130. // Remove the last one.
  131. vector.remove_at_unordered(0);
  132. CHECK(vector.is_empty());
  133. CHECK(vector.size() == 0);
  134. }
  135. TEST_CASE("[LocalVector] Erase.") {
  136. LocalVector<int> vector;
  137. vector.push_back(1);
  138. vector.push_back(3);
  139. vector.push_back(0);
  140. vector.push_back(2);
  141. vector.push_back(4);
  142. CHECK(vector.find(2) == 3);
  143. vector.erase(2);
  144. CHECK(vector.find(2) == -1);
  145. CHECK(vector.size() == 4);
  146. }
  147. TEST_CASE("[LocalVector] Size / Resize / Reserve.") {
  148. LocalVector<int> vector;
  149. CHECK(vector.is_empty());
  150. CHECK(vector.size() == 0);
  151. CHECK(vector.get_capacity() == 0);
  152. vector.resize(10);
  153. CHECK(vector.size() == 10);
  154. CHECK(vector.get_capacity() >= 10);
  155. vector.resize(5);
  156. CHECK(vector.size() == 5);
  157. // Capacity is supposed to change only when the size increase.
  158. CHECK(vector.get_capacity() >= 10);
  159. vector.remove_at(0);
  160. vector.remove_at(0);
  161. vector.remove_at(0);
  162. CHECK(vector.size() == 2);
  163. // Capacity is supposed to change only when the size increase.
  164. CHECK(vector.get_capacity() >= 10);
  165. vector.reset();
  166. CHECK(vector.size() == 0);
  167. CHECK(vector.get_capacity() == 0);
  168. vector.reserve(3);
  169. CHECK(vector.is_empty());
  170. CHECK(vector.size() == 0);
  171. CHECK(vector.get_capacity() >= 3);
  172. vector.push_back(0);
  173. vector.push_back(0);
  174. vector.push_back(0);
  175. CHECK(vector.size() == 3);
  176. CHECK(vector.get_capacity() >= 3);
  177. vector.push_back(0);
  178. CHECK(vector.size() == 4);
  179. CHECK(vector.get_capacity() >= 4);
  180. }
  181. } // namespace TestLocalVector
  182. #endif // TEST_LOCAL_VECTOR_H