test_rect2.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. /*************************************************************************/
  2. /* test_rect2.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_RECT2_H
  31. #define TEST_RECT2_H
  32. #include "core/math/rect2.h"
  33. #include "thirdparty/doctest/doctest.h"
  34. namespace TestRect2 {
  35. // We also test Rect2i here, for consistency with the source code where Rect2
  36. // and Rect2i are defined in the same file.
  37. // Rect2
  38. TEST_CASE("[Rect2] Constructor methods") {
  39. const Rect2 rect = Rect2(0, 100, 1280, 720);
  40. const Rect2 rect_vector = Rect2(Vector2(0, 100), Vector2(1280, 720));
  41. const Rect2 rect_copy_rect = Rect2(rect);
  42. const Rect2 rect_copy_recti = Rect2(Rect2i(0, 100, 1280, 720));
  43. CHECK_MESSAGE(
  44. rect == rect_vector,
  45. "Rect2s created with the same dimensions but by different methods should be equal.");
  46. CHECK_MESSAGE(
  47. rect == rect_copy_rect,
  48. "Rect2s created with the same dimensions but by different methods should be equal.");
  49. CHECK_MESSAGE(
  50. rect == rect_copy_recti,
  51. "Rect2s created with the same dimensions but by different methods should be equal.");
  52. }
  53. TEST_CASE("[Rect2] String conversion") {
  54. // Note: This also depends on the Vector2 string representation.
  55. CHECK_MESSAGE(
  56. String(Rect2(0, 100, 1280, 720)) == "[P: (0, 100), S: (1280, 720)]",
  57. "The string representation should match the expected value.");
  58. }
  59. TEST_CASE("[Rect2] Basic getters") {
  60. const Rect2 rect = Rect2(0, 100, 1280, 720);
  61. CHECK_MESSAGE(
  62. rect.get_position().is_equal_approx(Vector2(0, 100)),
  63. "get_position() should return the expected value.");
  64. CHECK_MESSAGE(
  65. rect.get_size().is_equal_approx(Vector2(1280, 720)),
  66. "get_size() should return the expected value.");
  67. CHECK_MESSAGE(
  68. rect.get_end().is_equal_approx(Vector2(1280, 820)),
  69. "get_end() should return the expected value.");
  70. CHECK_MESSAGE(
  71. rect.get_center().is_equal_approx(Vector2(640, 460)),
  72. "get_center() should return the expected value.");
  73. CHECK_MESSAGE(
  74. Rect2(0, 100, 1281, 721).get_center().is_equal_approx(Vector2(640.5, 460.5)),
  75. "get_center() should return the expected value.");
  76. }
  77. TEST_CASE("[Rect2] Basic setters") {
  78. Rect2 rect = Rect2(0, 100, 1280, 720);
  79. rect.set_end(Vector2(4000, 4000));
  80. CHECK_MESSAGE(
  81. rect.is_equal_approx(Rect2(0, 100, 4000, 3900)),
  82. "set_end() should result in the expected Rect2.");
  83. rect = Rect2(0, 100, 1280, 720);
  84. rect.set_position(Vector2(4000, 4000));
  85. CHECK_MESSAGE(
  86. rect.is_equal_approx(Rect2(4000, 4000, 1280, 720)),
  87. "set_position() should result in the expected Rect2.");
  88. rect = Rect2(0, 100, 1280, 720);
  89. rect.set_size(Vector2(4000, 4000));
  90. CHECK_MESSAGE(
  91. rect.is_equal_approx(Rect2(0, 100, 4000, 4000)),
  92. "set_size() should result in the expected Rect2.");
  93. }
  94. TEST_CASE("[Rect2] Area getters") {
  95. CHECK_MESSAGE(
  96. Math::is_equal_approx(Rect2(0, 100, 1280, 720).get_area(), 921'600),
  97. "get_area() should return the expected value.");
  98. CHECK_MESSAGE(
  99. Math::is_equal_approx(Rect2(0, 100, -1280, -720).get_area(), 921'600),
  100. "get_area() should return the expected value.");
  101. CHECK_MESSAGE(
  102. Math::is_equal_approx(Rect2(0, 100, 1280, -720).get_area(), -921'600),
  103. "get_area() should return the expected value.");
  104. CHECK_MESSAGE(
  105. Math::is_equal_approx(Rect2(0, 100, -1280, 720).get_area(), -921'600),
  106. "get_area() should return the expected value.");
  107. CHECK_MESSAGE(
  108. Math::is_zero_approx(Rect2(0, 100, 0, 720).get_area()),
  109. "get_area() should return the expected value.");
  110. CHECK_MESSAGE(
  111. !Rect2(0, 100, 1280, 720).has_no_area(),
  112. "has_no_area() should return the expected value on Rect2 with an area.");
  113. CHECK_MESSAGE(
  114. Rect2(0, 100, 0, 500).has_no_area(),
  115. "has_no_area() should return the expected value on Rect2 with no area.");
  116. CHECK_MESSAGE(
  117. Rect2(0, 100, 500, 0).has_no_area(),
  118. "has_no_area() should return the expected value on Rect2 with no area.");
  119. CHECK_MESSAGE(
  120. Rect2(0, 100, 0, 0).has_no_area(),
  121. "has_no_area() should return the expected value on Rect2 with no area.");
  122. }
  123. TEST_CASE("[Rect2] Absolute coordinates") {
  124. CHECK_MESSAGE(
  125. Rect2(0, 100, 1280, 720).abs().is_equal_approx(Rect2(0, 100, 1280, 720)),
  126. "abs() should return the expected Rect2.");
  127. CHECK_MESSAGE(
  128. Rect2(0, -100, 1280, 720).abs().is_equal_approx(Rect2(0, -100, 1280, 720)),
  129. "abs() should return the expected Rect2.");
  130. CHECK_MESSAGE(
  131. Rect2(0, -100, -1280, -720).abs().is_equal_approx(Rect2(-1280, -820, 1280, 720)),
  132. "abs() should return the expected Rect2.");
  133. CHECK_MESSAGE(
  134. Rect2(0, 100, -1280, 720).abs().is_equal_approx(Rect2(-1280, 100, 1280, 720)),
  135. "abs() should return the expected Rect2.");
  136. }
  137. TEST_CASE("[Rect2] Intersection") {
  138. CHECK_MESSAGE(
  139. Rect2(0, 100, 1280, 720).intersection(Rect2(0, 300, 100, 100)).is_equal_approx(Rect2(0, 300, 100, 100)),
  140. "intersection() with fully enclosed Rect2 should return the expected result.");
  141. // The resulting Rect2 is 100 pixels high because the first Rect2 is vertically offset by 100 pixels.
  142. CHECK_MESSAGE(
  143. Rect2(0, 100, 1280, 720).intersection(Rect2(1200, 700, 100, 100)).is_equal_approx(Rect2(1200, 700, 80, 100)),
  144. "intersection() with partially enclosed Rect2 should return the expected result.");
  145. CHECK_MESSAGE(
  146. Rect2(0, 100, 1280, 720).intersection(Rect2(-4000, -4000, 100, 100)).is_equal_approx(Rect2()),
  147. "intersection() with non-enclosed Rect2 should return the expected result.");
  148. }
  149. TEST_CASE("[Rect2] Enclosing") {
  150. CHECK_MESSAGE(
  151. Rect2(0, 100, 1280, 720).encloses(Rect2(0, 300, 100, 100)),
  152. "encloses() with fully contained Rect2 should return the expected result.");
  153. CHECK_MESSAGE(
  154. !Rect2(0, 100, 1280, 720).encloses(Rect2(1200, 700, 100, 100)),
  155. "encloses() with partially contained Rect2 should return the expected result.");
  156. CHECK_MESSAGE(
  157. !Rect2(0, 100, 1280, 720).encloses(Rect2(-4000, -4000, 100, 100)),
  158. "encloses() with non-contained Rect2 should return the expected result.");
  159. }
  160. TEST_CASE("[Rect2] Expanding") {
  161. CHECK_MESSAGE(
  162. Rect2(0, 100, 1280, 720).expand(Vector2(500, 600)).is_equal_approx(Rect2(0, 100, 1280, 720)),
  163. "expand() with contained Vector2 should return the expected result.");
  164. CHECK_MESSAGE(
  165. Rect2(0, 100, 1280, 720).expand(Vector2(0, 0)).is_equal_approx(Rect2(0, 0, 1280, 820)),
  166. "expand() with non-contained Vector2 should return the expected result.");
  167. }
  168. TEST_CASE("[Rect2] Growing") {
  169. CHECK_MESSAGE(
  170. Rect2(0, 100, 1280, 720).grow(100).is_equal_approx(Rect2(-100, 0, 1480, 920)),
  171. "grow() with positive value should return the expected Rect2.");
  172. CHECK_MESSAGE(
  173. Rect2(0, 100, 1280, 720).grow(-100).is_equal_approx(Rect2(100, 200, 1080, 520)),
  174. "grow() with negative value should return the expected Rect2.");
  175. CHECK_MESSAGE(
  176. Rect2(0, 100, 1280, 720).grow(-4000).is_equal_approx(Rect2(4000, 4100, -6720, -7280)),
  177. "grow() with large negative value should return the expected Rect2.");
  178. CHECK_MESSAGE(
  179. Rect2(0, 100, 1280, 720).grow_individual(100, 200, 300, 400).is_equal_approx(Rect2(-100, -100, 1680, 1320)),
  180. "grow_individual() with positive values should return the expected Rect2.");
  181. CHECK_MESSAGE(
  182. Rect2(0, 100, 1280, 720).grow_individual(-100, 200, 300, -400).is_equal_approx(Rect2(100, -100, 1480, 520)),
  183. "grow_individual() with positive and negative values should return the expected Rect2.");
  184. CHECK_MESSAGE(
  185. Rect2(0, 100, 1280, 720).grow_side(SIDE_TOP, 500).is_equal_approx(Rect2(0, -400, 1280, 1220)),
  186. "grow_side() with positive value should return the expected Rect2.");
  187. CHECK_MESSAGE(
  188. Rect2(0, 100, 1280, 720).grow_side(SIDE_TOP, -500).is_equal_approx(Rect2(0, 600, 1280, 220)),
  189. "grow_side() with negative value should return the expected Rect2.");
  190. }
  191. TEST_CASE("[Rect2] Has point") {
  192. CHECK_MESSAGE(
  193. Rect2(0, 100, 1280, 720).has_point(Vector2(500, 600)),
  194. "has_point() with contained Vector2 should return the expected result.");
  195. CHECK_MESSAGE(
  196. !Rect2(0, 100, 1280, 720).has_point(Vector2(0, 0)),
  197. "has_point() with non-contained Vector2 should return the expected result.");
  198. CHECK_MESSAGE(
  199. Rect2(0, 100, 1280, 720).has_point(Vector2(0, 110)),
  200. "has_point() with positive Vector2 on left edge should return the expected result.");
  201. CHECK_MESSAGE(
  202. !Rect2(0, 100, 1280, 720).has_point(Vector2(1280, 110)),
  203. "has_point() with positive Vector2 on right edge should return the expected result.");
  204. CHECK_MESSAGE(
  205. Rect2(-4000, 100, 1280, 720).has_point(Vector2(-4000, 110)),
  206. "has_point() with negative Vector2 on left edge should return the expected result.");
  207. CHECK_MESSAGE(
  208. !Rect2(-4000, 100, 1280, 720).has_point(Vector2(-2720, 110)),
  209. "has_point() with negative Vector2 on right edge should return the expected result.");
  210. }
  211. TEST_CASE("[Rect2] Intersection") {
  212. CHECK_MESSAGE(
  213. Rect2(0, 100, 1280, 720).intersects(Rect2(0, 300, 100, 100)),
  214. "intersects() with fully enclosed Rect2 should return the expected result.");
  215. CHECK_MESSAGE(
  216. Rect2(0, 100, 1280, 720).intersects(Rect2(1200, 700, 100, 100)),
  217. "intersects() with partially enclosed Rect2 should return the expected result.");
  218. CHECK_MESSAGE(
  219. !Rect2(0, 100, 1280, 720).intersects(Rect2(-4000, -4000, 100, 100)),
  220. "intersects() with non-enclosed Rect2 should return the expected result.");
  221. }
  222. TEST_CASE("[Rect2] Merging") {
  223. CHECK_MESSAGE(
  224. Rect2(0, 100, 1280, 720).merge(Rect2(0, 300, 100, 100)).is_equal_approx(Rect2(0, 100, 1280, 720)),
  225. "merge() with fully enclosed Rect2 should return the expected result.");
  226. CHECK_MESSAGE(
  227. Rect2(0, 100, 1280, 720).merge(Rect2(1200, 700, 100, 100)).is_equal_approx(Rect2(0, 100, 1300, 720)),
  228. "merge() with partially enclosed Rect2 should return the expected result.");
  229. CHECK_MESSAGE(
  230. Rect2(0, 100, 1280, 720).merge(Rect2(-4000, -4000, 100, 100)).is_equal_approx(Rect2(-4000, -4000, 5280, 4820)),
  231. "merge() with non-enclosed Rect2 should return the expected result.");
  232. }
  233. // Rect2i
  234. TEST_CASE("[Rect2i] Constructor methods") {
  235. Rect2i recti = Rect2i(0, 100, 1280, 720);
  236. Rect2i recti_vector = Rect2i(Vector2i(0, 100), Vector2i(1280, 720));
  237. Rect2i recti_copy_recti = Rect2i(recti);
  238. Rect2i recti_copy_rect = Rect2i(Rect2(0, 100, 1280, 720));
  239. CHECK_MESSAGE(
  240. recti == recti_vector,
  241. "Rect2is created with the same dimensions but by different methods should be equal.");
  242. CHECK_MESSAGE(
  243. recti == recti_copy_recti,
  244. "Rect2is created with the same dimensions but by different methods should be equal.");
  245. CHECK_MESSAGE(
  246. recti == recti_copy_rect,
  247. "Rect2is created with the same dimensions but by different methods should be equal.");
  248. }
  249. TEST_CASE("[Rect2i] String conversion") {
  250. // Note: This also depends on the Vector2 string representation.
  251. CHECK_MESSAGE(
  252. String(Rect2i(0, 100, 1280, 720)) == "[P: (0, 100), S: (1280, 720)]",
  253. "The string representation should match the expected value.");
  254. }
  255. TEST_CASE("[Rect2i] Basic getters") {
  256. const Rect2i rect = Rect2i(0, 100, 1280, 720);
  257. CHECK_MESSAGE(
  258. rect.get_position() == Vector2i(0, 100),
  259. "get_position() should return the expected value.");
  260. CHECK_MESSAGE(
  261. rect.get_size() == Vector2i(1280, 720),
  262. "get_size() should return the expected value.");
  263. CHECK_MESSAGE(
  264. rect.get_end() == Vector2i(1280, 820),
  265. "get_end() should return the expected value.");
  266. CHECK_MESSAGE(
  267. rect.get_center() == Vector2i(640, 460),
  268. "get_center() should return the expected value.");
  269. CHECK_MESSAGE(
  270. Rect2i(0, 100, 1281, 721).get_center() == Vector2i(640, 460),
  271. "get_center() should return the expected value.");
  272. }
  273. TEST_CASE("[Rect2i] Basic setters") {
  274. Rect2i rect = Rect2i(0, 100, 1280, 720);
  275. rect.set_end(Vector2i(4000, 4000));
  276. CHECK_MESSAGE(
  277. rect == Rect2i(0, 100, 4000, 3900),
  278. "set_end() should result in the expected Rect2i.");
  279. rect = Rect2i(0, 100, 1280, 720);
  280. rect.set_position(Vector2i(4000, 4000));
  281. CHECK_MESSAGE(
  282. rect == Rect2i(4000, 4000, 1280, 720),
  283. "set_position() should result in the expected Rect2i.");
  284. rect = Rect2i(0, 100, 1280, 720);
  285. rect.set_size(Vector2i(4000, 4000));
  286. CHECK_MESSAGE(
  287. rect == Rect2i(0, 100, 4000, 4000),
  288. "set_size() should result in the expected Rect2i.");
  289. }
  290. TEST_CASE("[Rect2i] Area getters") {
  291. CHECK_MESSAGE(
  292. Rect2i(0, 100, 1280, 720).get_area() == 921'600,
  293. "get_area() should return the expected value.");
  294. CHECK_MESSAGE(
  295. Rect2i(0, 100, -1280, -720).get_area() == 921'600,
  296. "get_area() should return the expected value.");
  297. CHECK_MESSAGE(
  298. Rect2i(0, 100, 1280, -720).get_area() == -921'600,
  299. "get_area() should return the expected value.");
  300. CHECK_MESSAGE(
  301. Rect2i(0, 100, -1280, 720).get_area() == -921'600,
  302. "get_area() should return the expected value.");
  303. CHECK_MESSAGE(
  304. Rect2i(0, 100, 0, 720).get_area() == 0,
  305. "get_area() should return the expected value.");
  306. CHECK_MESSAGE(
  307. !Rect2i(0, 100, 1280, 720).has_no_area(),
  308. "has_no_area() should return the expected value on Rect2i with an area.");
  309. CHECK_MESSAGE(
  310. Rect2i(0, 100, 0, 500).has_no_area(),
  311. "has_no_area() should return the expected value on Rect2i with no area.");
  312. CHECK_MESSAGE(
  313. Rect2i(0, 100, 500, 0).has_no_area(),
  314. "has_no_area() should return the expected value on Rect2i with no area.");
  315. CHECK_MESSAGE(
  316. Rect2i(0, 100, 0, 0).has_no_area(),
  317. "has_no_area() should return the expected value on Rect2i with no area.");
  318. }
  319. TEST_CASE("[Rect2i] Absolute coordinates") {
  320. CHECK_MESSAGE(
  321. Rect2i(0, 100, 1280, 720).abs() == Rect2i(0, 100, 1280, 720),
  322. "abs() should return the expected Rect2i.");
  323. CHECK_MESSAGE(
  324. Rect2i(0, -100, 1280, 720).abs() == Rect2i(0, -100, 1280, 720),
  325. "abs() should return the expected Rect2i.");
  326. CHECK_MESSAGE(
  327. Rect2i(0, -100, -1280, -720).abs() == Rect2i(-1280, -820, 1280, 720),
  328. "abs() should return the expected Rect2i.");
  329. CHECK_MESSAGE(
  330. Rect2i(0, 100, -1280, 720).abs() == Rect2i(-1280, 100, 1280, 720),
  331. "abs() should return the expected Rect2i.");
  332. }
  333. TEST_CASE("[Rect2i] Intersection") {
  334. CHECK_MESSAGE(
  335. Rect2i(0, 100, 1280, 720).intersection(Rect2i(0, 300, 100, 100)) == Rect2i(0, 300, 100, 100),
  336. "intersection() with fully enclosed Rect2i should return the expected result.");
  337. // The resulting Rect2i is 100 pixels high because the first Rect2i is vertically offset by 100 pixels.
  338. CHECK_MESSAGE(
  339. Rect2i(0, 100, 1280, 720).intersection(Rect2i(1200, 700, 100, 100)) == Rect2i(1200, 700, 80, 100),
  340. "intersection() with partially enclosed Rect2i should return the expected result.");
  341. CHECK_MESSAGE(
  342. Rect2i(0, 100, 1280, 720).intersection(Rect2i(-4000, -4000, 100, 100)) == Rect2i(),
  343. "intersection() with non-enclosed Rect2i should return the expected result.");
  344. }
  345. TEST_CASE("[Rect2i] Enclosing") {
  346. CHECK_MESSAGE(
  347. Rect2i(0, 100, 1280, 720).encloses(Rect2i(0, 300, 100, 100)),
  348. "encloses() with fully contained Rect2i should return the expected result.");
  349. CHECK_MESSAGE(
  350. !Rect2i(0, 100, 1280, 720).encloses(Rect2i(1200, 700, 100, 100)),
  351. "encloses() with partially contained Rect2i should return the expected result.");
  352. CHECK_MESSAGE(
  353. !Rect2i(0, 100, 1280, 720).encloses(Rect2i(-4000, -4000, 100, 100)),
  354. "encloses() with non-contained Rect2i should return the expected result.");
  355. }
  356. TEST_CASE("[Rect2i] Expanding") {
  357. CHECK_MESSAGE(
  358. Rect2i(0, 100, 1280, 720).expand(Vector2i(500, 600)) == Rect2i(0, 100, 1280, 720),
  359. "expand() with contained Vector2i should return the expected result.");
  360. CHECK_MESSAGE(
  361. Rect2i(0, 100, 1280, 720).expand(Vector2i(0, 0)) == Rect2i(0, 0, 1280, 820),
  362. "expand() with non-contained Vector2i should return the expected result.");
  363. }
  364. TEST_CASE("[Rect2i] Growing") {
  365. CHECK_MESSAGE(
  366. Rect2i(0, 100, 1280, 720).grow(100) == Rect2i(-100, 0, 1480, 920),
  367. "grow() with positive value should return the expected Rect2i.");
  368. CHECK_MESSAGE(
  369. Rect2i(0, 100, 1280, 720).grow(-100) == Rect2i(100, 200, 1080, 520),
  370. "grow() with negative value should return the expected Rect2i.");
  371. CHECK_MESSAGE(
  372. Rect2i(0, 100, 1280, 720).grow(-4000) == Rect2i(4000, 4100, -6720, -7280),
  373. "grow() with large negative value should return the expected Rect2i.");
  374. CHECK_MESSAGE(
  375. Rect2i(0, 100, 1280, 720).grow_individual(100, 200, 300, 400) == Rect2i(-100, -100, 1680, 1320),
  376. "grow_individual() with positive values should return the expected Rect2i.");
  377. CHECK_MESSAGE(
  378. Rect2i(0, 100, 1280, 720).grow_individual(-100, 200, 300, -400) == Rect2i(100, -100, 1480, 520),
  379. "grow_individual() with positive and negative values should return the expected Rect2i.");
  380. CHECK_MESSAGE(
  381. Rect2i(0, 100, 1280, 720).grow_side(SIDE_TOP, 500) == Rect2i(0, -400, 1280, 1220),
  382. "grow_side() with positive value should return the expected Rect2i.");
  383. CHECK_MESSAGE(
  384. Rect2i(0, 100, 1280, 720).grow_side(SIDE_TOP, -500) == Rect2i(0, 600, 1280, 220),
  385. "grow_side() with negative value should return the expected Rect2i.");
  386. }
  387. TEST_CASE("[Rect2i] Has point") {
  388. CHECK_MESSAGE(
  389. Rect2i(0, 100, 1280, 720).has_point(Vector2i(500, 600)),
  390. "has_point() with contained Vector2i should return the expected result.");
  391. CHECK_MESSAGE(
  392. !Rect2i(0, 100, 1280, 720).has_point(Vector2i(0, 0)),
  393. "has_point() with non-contained Vector2i should return the expected result.");
  394. CHECK_MESSAGE(
  395. Rect2i(0, 100, 1280, 720).has_point(Vector2(0, 110)),
  396. "has_point() with positive Vector2 on left edge should return the expected result.");
  397. CHECK_MESSAGE(
  398. !Rect2i(0, 100, 1280, 720).has_point(Vector2(1280, 110)),
  399. "has_point() with positive Vector2 on right edge should return the expected result.");
  400. CHECK_MESSAGE(
  401. Rect2i(-4000, 100, 1280, 720).has_point(Vector2(-4000, 110)),
  402. "has_point() with negative Vector2 on left edge should return the expected result.");
  403. CHECK_MESSAGE(
  404. !Rect2i(-4000, 100, 1280, 720).has_point(Vector2(-2720, 110)),
  405. "has_point() with negative Vector2 on right edge should return the expected result.");
  406. }
  407. TEST_CASE("[Rect2i] Intersection") {
  408. CHECK_MESSAGE(
  409. Rect2i(0, 100, 1280, 720).intersects(Rect2i(0, 300, 100, 100)),
  410. "intersects() with fully enclosed Rect2i should return the expected result.");
  411. CHECK_MESSAGE(
  412. Rect2i(0, 100, 1280, 720).intersects(Rect2i(1200, 700, 100, 100)),
  413. "intersects() with partially enclosed Rect2i should return the expected result.");
  414. CHECK_MESSAGE(
  415. !Rect2i(0, 100, 1280, 720).intersects(Rect2i(-4000, -4000, 100, 100)),
  416. "intersects() with non-enclosed Rect2i should return the expected result.");
  417. }
  418. TEST_CASE("[Rect2i] Merging") {
  419. CHECK_MESSAGE(
  420. Rect2i(0, 100, 1280, 720).merge(Rect2i(0, 300, 100, 100)) == Rect2i(0, 100, 1280, 720),
  421. "merge() with fully enclosed Rect2i should return the expected result.");
  422. CHECK_MESSAGE(
  423. Rect2i(0, 100, 1280, 720).merge(Rect2i(1200, 700, 100, 100)) == Rect2i(0, 100, 1300, 720),
  424. "merge() with partially enclosed Rect2i should return the expected result.");
  425. CHECK_MESSAGE(
  426. Rect2i(0, 100, 1280, 720).merge(Rect2i(-4000, -4000, 100, 100)) == Rect2i(-4000, -4000, 5280, 4820),
  427. "merge() with non-enclosed Rect2i should return the expected result.");
  428. }
  429. } // namespace TestRect2
  430. #endif // TEST_RECT2_H