test_rect2.h 15 KB

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