test_rect2.h 15 KB

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