test_array.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*************************************************************************/
  2. /* test_array.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  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_ARRAY_H
  31. #define TEST_ARRAY_H
  32. #include "core/object/class_db.h"
  33. #include "core/object/script_language.h"
  34. #include "core/templates/hashfuncs.h"
  35. #include "core/templates/vector.h"
  36. #include "core/variant/array.h"
  37. #include "core/variant/container_type_validate.h"
  38. #include "core/variant/variant.h"
  39. #include "tests/test_macros.h"
  40. namespace TestArray {
  41. TEST_CASE("[Array] size(), clear(), and is_empty()") {
  42. Array arr;
  43. CHECK(arr.size() == 0);
  44. CHECK(arr.is_empty());
  45. arr.push_back(1);
  46. CHECK(arr.size() == 1);
  47. arr.clear();
  48. CHECK(arr.is_empty());
  49. CHECK(arr.size() == 0);
  50. }
  51. TEST_CASE("[Array] Assignment and comparison operators") {
  52. Array arr1;
  53. Array arr2;
  54. arr1.push_back(1);
  55. CHECK(arr1 != arr2);
  56. CHECK(arr1 > arr2);
  57. CHECK(arr1 >= arr2);
  58. arr2.push_back(2);
  59. CHECK(arr1 != arr2);
  60. CHECK(arr1 < arr2);
  61. CHECK(arr1 <= arr2);
  62. CHECK(arr2 > arr1);
  63. CHECK(arr2 >= arr1);
  64. Array arr3 = arr2;
  65. CHECK(arr3 == arr2);
  66. }
  67. TEST_CASE("[Array] append_array()") {
  68. Array arr1;
  69. Array arr2;
  70. arr1.push_back(1);
  71. arr1.append_array(arr2);
  72. CHECK(arr1.size() == 1);
  73. arr2.push_back(2);
  74. arr1.append_array(arr2);
  75. CHECK(arr1.size() == 2);
  76. CHECK(int(arr1[0]) == 1);
  77. CHECK(int(arr1[1]) == 2);
  78. }
  79. TEST_CASE("[Array] resize(), insert(), and erase()") {
  80. Array arr;
  81. arr.resize(2);
  82. CHECK(arr.size() == 2);
  83. arr.insert(0, 1);
  84. CHECK(int(arr[0]) == 1);
  85. arr.insert(0, 2);
  86. CHECK(int(arr[0]) == 2);
  87. arr.erase(2);
  88. CHECK(int(arr[0]) == 1);
  89. }
  90. TEST_CASE("[Array] front() and back()") {
  91. Array arr;
  92. arr.push_back(1);
  93. CHECK(int(arr.front()) == 1);
  94. CHECK(int(arr.back()) == 1);
  95. arr.push_back(3);
  96. CHECK(int(arr.front()) == 1);
  97. CHECK(int(arr.back()) == 3);
  98. }
  99. TEST_CASE("[Array] has() and count()") {
  100. Array arr;
  101. arr.push_back(1);
  102. arr.push_back(1);
  103. CHECK(arr.has(1));
  104. CHECK(!arr.has(2));
  105. CHECK(arr.count(1) == 2);
  106. CHECK(arr.count(2) == 0);
  107. }
  108. TEST_CASE("[Array] remove()") {
  109. Array arr;
  110. arr.push_back(1);
  111. arr.push_back(2);
  112. arr.remove(0);
  113. CHECK(arr.size() == 1);
  114. CHECK(int(arr[0]) == 2);
  115. arr.remove(0);
  116. CHECK(arr.size() == 0);
  117. // The array is now empty; try to use `remove()` again.
  118. // Normally, this prints an error message so we silence it.
  119. ERR_PRINT_OFF;
  120. arr.remove(0);
  121. ERR_PRINT_ON;
  122. CHECK(arr.size() == 0);
  123. }
  124. TEST_CASE("[Array] get()") {
  125. Array arr;
  126. arr.push_back(1);
  127. CHECK(int(arr.get(0)) == 1);
  128. }
  129. TEST_CASE("[Array] sort()") {
  130. Array arr;
  131. arr.push_back(3);
  132. arr.push_back(4);
  133. arr.push_back(2);
  134. arr.push_back(1);
  135. arr.sort();
  136. int val = 1;
  137. for (int i = 0; i < arr.size(); i++) {
  138. CHECK(int(arr[i]) == val);
  139. val++;
  140. }
  141. }
  142. TEST_CASE("[Array] push_front(), pop_front(), pop_back()") {
  143. Array arr;
  144. arr.push_front(1);
  145. arr.push_front(2);
  146. CHECK(int(arr[0]) == 2);
  147. arr.pop_front();
  148. CHECK(int(arr[0]) == 1);
  149. CHECK(arr.size() == 1);
  150. arr.push_front(2);
  151. arr.push_front(3);
  152. arr.pop_back();
  153. CHECK(int(arr[1]) == 2);
  154. CHECK(arr.size() == 2);
  155. }
  156. TEST_CASE("[Array] max() and min()") {
  157. Array arr;
  158. arr.push_back(3);
  159. arr.push_front(4);
  160. arr.push_back(5);
  161. arr.push_back(2);
  162. int max = int(arr.max());
  163. int min = int(arr.min());
  164. CHECK(max == 5);
  165. CHECK(min == 2);
  166. }
  167. } // namespace TestArray
  168. #endif // TEST_ARRAY_H