Debian Codegraph @

tests/core/math/test_rect2i.h

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