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