test_rect2i.h 14 KB

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