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