test_rect2i.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /**************************************************************************/
  2. /* test_rect2i.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/math/rect2.h"
  32. #include "core/math/rect2i.h"
  33. #include "thirdparty/doctest/doctest.h"
  34. namespace TestRect2i {
  35. TEST_CASE("[Rect2i] Constructor methods") {
  36. constexpr Rect2i recti = Rect2i(0, 100, 1280, 720);
  37. constexpr Rect2i recti_vector = Rect2i(Vector2i(0, 100), Vector2i(1280, 720));
  38. constexpr Rect2i recti_copy_recti = Rect2i(recti);
  39. const Rect2i recti_copy_rect = Rect2i(Rect2(0, 100, 1280, 720));
  40. static_assert(
  41. recti == recti_vector,
  42. "Rect2is created with the same dimensions but by different methods should be equal.");
  43. static_assert(
  44. recti == recti_copy_recti,
  45. "Rect2is created with the same dimensions but by different methods should be equal.");
  46. CHECK_MESSAGE(
  47. recti == recti_copy_rect,
  48. "Rect2is created with the same dimensions but by different methods should be equal.");
  49. }
  50. TEST_CASE("[Rect2i] String conversion") {
  51. // Note: This also depends on the Vector2 string representation.
  52. CHECK_MESSAGE(
  53. String(Rect2i(0, 100, 1280, 720)) == "[P: (0, 100), S: (1280, 720)]",
  54. "The string representation should match the expected value.");
  55. }
  56. TEST_CASE("[Rect2i] Basic getters") {
  57. constexpr Rect2i rect = Rect2i(0, 100, 1280, 720);
  58. CHECK_MESSAGE(
  59. rect.get_position() == Vector2i(0, 100),
  60. "get_position() should return the expected value.");
  61. CHECK_MESSAGE(
  62. rect.get_size() == Vector2i(1280, 720),
  63. "get_size() should return the expected value.");
  64. CHECK_MESSAGE(
  65. rect.get_end() == Vector2i(1280, 820),
  66. "get_end() should return the expected value.");
  67. CHECK_MESSAGE(
  68. rect.get_center() == Vector2i(640, 460),
  69. "get_center() should return the expected value.");
  70. CHECK_MESSAGE(
  71. Rect2i(0, 100, 1281, 721).get_center() == Vector2i(640, 460),
  72. "get_center() should return the expected value.");
  73. }
  74. TEST_CASE("[Rect2i] Basic setters") {
  75. Rect2i rect = Rect2i(0, 100, 1280, 720);
  76. rect.set_end(Vector2i(4000, 4000));
  77. CHECK_MESSAGE(
  78. rect == Rect2i(0, 100, 4000, 3900),
  79. "set_end() should result in the expected Rect2i.");
  80. rect = Rect2i(0, 100, 1280, 720);
  81. rect.set_position(Vector2i(4000, 4000));
  82. CHECK_MESSAGE(
  83. rect == Rect2i(4000, 4000, 1280, 720),
  84. "set_position() should result in the expected Rect2i.");
  85. rect = Rect2i(0, 100, 1280, 720);
  86. rect.set_size(Vector2i(4000, 4000));
  87. CHECK_MESSAGE(
  88. rect == Rect2i(0, 100, 4000, 4000),
  89. "set_size() should result in the expected Rect2i.");
  90. }
  91. TEST_CASE("[Rect2i] Area getters") {
  92. CHECK_MESSAGE(
  93. Rect2i(0, 100, 1280, 720).get_area() == 921'600,
  94. "get_area() should return the expected value.");
  95. CHECK_MESSAGE(
  96. Rect2i(0, 100, -1280, -720).get_area() == 921'600,
  97. "get_area() should return the expected value.");
  98. CHECK_MESSAGE(
  99. Rect2i(0, 100, 1280, -720).get_area() == -921'600,
  100. "get_area() should return the expected value.");
  101. CHECK_MESSAGE(
  102. Rect2i(0, 100, -1280, 720).get_area() == -921'600,
  103. "get_area() should return the expected value.");
  104. CHECK_MESSAGE(
  105. Rect2i(0, 100, 0, 720).get_area() == 0,
  106. "get_area() should return the expected value.");
  107. CHECK_MESSAGE(
  108. Rect2i(0, 100, 1280, 720).has_area(),
  109. "has_area() should return the expected value on Rect2i with an area.");
  110. CHECK_MESSAGE(
  111. !Rect2i(0, 100, 0, 500).has_area(),
  112. "has_area() should return the expected value on Rect2i with no area.");
  113. CHECK_MESSAGE(
  114. !Rect2i(0, 100, 500, 0).has_area(),
  115. "has_area() should return the expected value on Rect2i with no area.");
  116. CHECK_MESSAGE(
  117. !Rect2i(0, 100, 0, 0).has_area(),
  118. "has_area() should return the expected value on Rect2i with no area.");
  119. }
  120. TEST_CASE("[Rect2i] Absolute coordinates") {
  121. CHECK_MESSAGE(
  122. Rect2i(0, 100, 1280, 720).abs() == Rect2i(0, 100, 1280, 720),
  123. "abs() should return the expected Rect2i.");
  124. CHECK_MESSAGE(
  125. Rect2i(0, -100, 1280, 720).abs() == Rect2i(0, -100, 1280, 720),
  126. "abs() should return the expected Rect2i.");
  127. CHECK_MESSAGE(
  128. Rect2i(0, -100, -1280, -720).abs() == Rect2i(-1280, -820, 1280, 720),
  129. "abs() should return the expected Rect2i.");
  130. CHECK_MESSAGE(
  131. Rect2i(0, 100, -1280, 720).abs() == Rect2i(-1280, 100, 1280, 720),
  132. "abs() should return the expected Rect2i.");
  133. }
  134. TEST_CASE("[Rect2i] Intersection") {
  135. CHECK_MESSAGE(
  136. Rect2i(0, 100, 1280, 720).intersection(Rect2i(0, 300, 100, 100)) == Rect2i(0, 300, 100, 100),
  137. "intersection() with fully enclosed Rect2i should return the expected result.");
  138. // The resulting Rect2i is 100 pixels high because the first Rect2i is vertically offset by 100 pixels.
  139. CHECK_MESSAGE(
  140. Rect2i(0, 100, 1280, 720).intersection(Rect2i(1200, 700, 100, 100)) == Rect2i(1200, 700, 80, 100),
  141. "intersection() with partially enclosed Rect2i should return the expected result.");
  142. CHECK_MESSAGE(
  143. Rect2i(0, 100, 1280, 720).intersection(Rect2i(-4000, -4000, 100, 100)) == Rect2i(),
  144. "intersection() with non-enclosed Rect2i should return the expected result.");
  145. }
  146. TEST_CASE("[Rect2i] Enclosing") {
  147. CHECK_MESSAGE(
  148. Rect2i(0, 100, 1280, 720).encloses(Rect2i(0, 300, 100, 100)),
  149. "encloses() with fully contained Rect2i should return the expected result.");
  150. CHECK_MESSAGE(
  151. !Rect2i(0, 100, 1280, 720).encloses(Rect2i(1200, 700, 100, 100)),
  152. "encloses() with partially contained Rect2i should return the expected result.");
  153. CHECK_MESSAGE(
  154. !Rect2i(0, 100, 1280, 720).encloses(Rect2i(-4000, -4000, 100, 100)),
  155. "encloses() with non-contained Rect2i should return the expected result.");
  156. CHECK_MESSAGE(
  157. Rect2i(0, 100, 1280, 720).encloses(Rect2i(0, 100, 1280, 720)),
  158. "encloses() with identical Rect2i should return the expected result.");
  159. }
  160. TEST_CASE("[Rect2i] Expanding") {
  161. CHECK_MESSAGE(
  162. Rect2i(0, 100, 1280, 720).expand(Vector2i(500, 600)) == Rect2i(0, 100, 1280, 720),
  163. "expand() with contained Vector2i should return the expected result.");
  164. CHECK_MESSAGE(
  165. Rect2i(0, 100, 1280, 720).expand(Vector2i(0, 0)) == Rect2i(0, 0, 1280, 820),
  166. "expand() with non-contained Vector2i should return the expected result.");
  167. }
  168. TEST_CASE("[Rect2i] Growing") {
  169. CHECK_MESSAGE(
  170. Rect2i(0, 100, 1280, 720).grow(100) == Rect2i(-100, 0, 1480, 920),
  171. "grow() with positive value should return the expected Rect2i.");
  172. CHECK_MESSAGE(
  173. Rect2i(0, 100, 1280, 720).grow(-100) == Rect2i(100, 200, 1080, 520),
  174. "grow() with negative value should return the expected Rect2i.");
  175. CHECK_MESSAGE(
  176. Rect2i(0, 100, 1280, 720).grow(-4000) == Rect2i(4000, 4100, -6720, -7280),
  177. "grow() with large negative value should return the expected Rect2i.");
  178. CHECK_MESSAGE(
  179. Rect2i(0, 100, 1280, 720).grow_individual(100, 200, 300, 400) == Rect2i(-100, -100, 1680, 1320),
  180. "grow_individual() with positive values should return the expected Rect2i.");
  181. CHECK_MESSAGE(
  182. Rect2i(0, 100, 1280, 720).grow_individual(-100, 200, 300, -400) == Rect2i(100, -100, 1480, 520),
  183. "grow_individual() with positive and negative values should return the expected Rect2i.");
  184. CHECK_MESSAGE(
  185. Rect2i(0, 100, 1280, 720).grow_side(SIDE_TOP, 500) == Rect2i(0, -400, 1280, 1220),
  186. "grow_side() with positive value should return the expected Rect2i.");
  187. CHECK_MESSAGE(
  188. Rect2i(0, 100, 1280, 720).grow_side(SIDE_TOP, -500) == Rect2i(0, 600, 1280, 220),
  189. "grow_side() with negative value should return the expected Rect2i.");
  190. }
  191. TEST_CASE("[Rect2i] Has point") {
  192. Rect2i rect = Rect2i(0, 100, 1280, 720);
  193. CHECK_MESSAGE(
  194. rect.has_point(Vector2i(500, 600)),
  195. "has_point() with contained Vector2i should return the expected result.");
  196. CHECK_MESSAGE(
  197. !rect.has_point(Vector2i(0, 0)),
  198. "has_point() with non-contained Vector2i should return the expected result.");
  199. CHECK_MESSAGE(
  200. rect.has_point(rect.position),
  201. "has_point() with positive size should include `position`.");
  202. CHECK_MESSAGE(
  203. rect.has_point(rect.position + Vector2i(1, 1)),
  204. "has_point() with positive size should include `position + (1, 1)`.");
  205. CHECK_MESSAGE(
  206. !rect.has_point(rect.position + Vector2i(1, -1)),
  207. "has_point() with positive size should not include `position + (1, -1)`.");
  208. CHECK_MESSAGE(
  209. !rect.has_point(rect.position + rect.size),
  210. "has_point() with positive size should not include `position + size`.");
  211. CHECK_MESSAGE(
  212. !rect.has_point(rect.position + rect.size + Vector2i(1, 1)),
  213. "has_point() with positive size should not include `position + size + (1, 1)`.");
  214. CHECK_MESSAGE(
  215. rect.has_point(rect.position + rect.size + Vector2i(-1, -1)),
  216. "has_point() with positive size should include `position + size + (-1, -1)`.");
  217. CHECK_MESSAGE(
  218. !rect.has_point(rect.position + rect.size + Vector2i(-1, 1)),
  219. "has_point() with positive size should not include `position + size + (-1, 1)`.");
  220. CHECK_MESSAGE(
  221. rect.has_point(rect.position + Vector2i(0, 10)),
  222. "has_point() with point located on left edge should return true.");
  223. CHECK_MESSAGE(
  224. !rect.has_point(rect.position + Vector2i(rect.size.x, 10)),
  225. "has_point() with point located on right edge should return false.");
  226. CHECK_MESSAGE(
  227. rect.has_point(rect.position + Vector2i(10, 0)),
  228. "has_point() with point located on top edge should return true.");
  229. CHECK_MESSAGE(
  230. !rect.has_point(rect.position + Vector2i(10, rect.size.y)),
  231. "has_point() with point located on bottom edge should return false.");
  232. /*
  233. // FIXME: Disabled for now until GH-37617 is fixed one way or another.
  234. // More tests should then be written like for the positive size case.
  235. rect = Rect2i(0, 100, -1280, -720);
  236. CHECK_MESSAGE(
  237. rect.has_point(rect.position),
  238. "has_point() with negative size should include `position`.");
  239. CHECK_MESSAGE(
  240. !rect.has_point(rect.position + rect.size),
  241. "has_point() with negative size should not include `position + size`.");
  242. */
  243. rect = Rect2i(-4000, -200, 1280, 720);
  244. CHECK_MESSAGE(
  245. rect.has_point(rect.position + Vector2i(0, 10)),
  246. "has_point() with negative position and point located on left edge should return true.");
  247. CHECK_MESSAGE(
  248. !rect.has_point(rect.position + Vector2i(rect.size.x, 10)),
  249. "has_point() with negative position and point located on right edge should return false.");
  250. CHECK_MESSAGE(
  251. rect.has_point(rect.position + Vector2i(10, 0)),
  252. "has_point() with negative position and point located on top edge should return true.");
  253. CHECK_MESSAGE(
  254. !rect.has_point(rect.position + Vector2i(10, rect.size.y)),
  255. "has_point() with negative position and point located on bottom edge should return false.");
  256. }
  257. TEST_CASE("[Rect2i] Intersection") {
  258. CHECK_MESSAGE(
  259. Rect2i(0, 100, 1280, 720).intersects(Rect2i(0, 300, 100, 100)),
  260. "intersects() with fully enclosed Rect2i should return the expected result.");
  261. CHECK_MESSAGE(
  262. Rect2i(0, 100, 1280, 720).intersects(Rect2i(1200, 700, 100, 100)),
  263. "intersects() with partially enclosed Rect2i should return the expected result.");
  264. CHECK_MESSAGE(
  265. !Rect2i(0, 100, 1280, 720).intersects(Rect2i(-4000, -4000, 100, 100)),
  266. "intersects() with non-enclosed Rect2i should return the expected result.");
  267. CHECK_MESSAGE(
  268. !Rect2i(0, 0, 2, 2).intersects(Rect2i(2, 2, 2, 2)),
  269. "intersects() with adjacent Rect2i should return the expected result.");
  270. }
  271. TEST_CASE("[Rect2i] Merging") {
  272. CHECK_MESSAGE(
  273. Rect2i(0, 100, 1280, 720).merge(Rect2i(0, 300, 100, 100)) == Rect2i(0, 100, 1280, 720),
  274. "merge() with fully enclosed Rect2i should return the expected result.");
  275. CHECK_MESSAGE(
  276. Rect2i(0, 100, 1280, 720).merge(Rect2i(1200, 700, 100, 100)) == Rect2i(0, 100, 1300, 720),
  277. "merge() with partially enclosed Rect2i should return the expected result.");
  278. CHECK_MESSAGE(
  279. Rect2i(0, 100, 1280, 720).merge(Rect2i(-4000, -4000, 100, 100)) == Rect2i(-4000, -4000, 5280, 4820),
  280. "merge() with non-enclosed Rect2i should return the expected result.");
  281. }
  282. } // namespace TestRect2i