test_rect2.h 20 KB

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