Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/tests/core/math/test_vector4.cpp
23450 views
1
/**************************************************************************/
2
/* test_vector4.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_vector4)
34
35
#include "core/math/vector4.h"
36
37
namespace TestVector4 {
38
39
TEST_CASE("[Vector4] Constructor methods") {
40
constexpr Vector4 vector_empty = Vector4();
41
constexpr Vector4 vector_zero = Vector4(0.0, 0.0, 0.0, 0.0);
42
static_assert(
43
vector_empty == vector_zero,
44
"Vector4 Constructor with no inputs should return a zero Vector4.");
45
}
46
47
TEST_CASE("[Vector4] Axis methods") {
48
Vector4 vector = Vector4(1.2, 3.4, 5.6, -0.9);
49
CHECK_MESSAGE(
50
vector.max_axis_index() == Vector4::Axis::AXIS_Z,
51
"Vector4 max_axis_index should work as expected.");
52
CHECK_MESSAGE(
53
vector.min_axis_index() == Vector4::Axis::AXIS_W,
54
"Vector4 min_axis_index should work as expected.");
55
CHECK_MESSAGE(
56
vector[vector.max_axis_index()] == (real_t)5.6,
57
"Vector4 array operator should work as expected.");
58
CHECK_MESSAGE(
59
vector[vector.min_axis_index()] == (real_t)-0.9,
60
"Vector4 array operator should work as expected.");
61
62
vector[Vector4::Axis::AXIS_Y] = 3.7;
63
CHECK_MESSAGE(
64
vector[Vector4::Axis::AXIS_Y] == (real_t)3.7,
65
"Vector4 array operator setter should work as expected.");
66
}
67
68
TEST_CASE("[Vector4] Interpolation methods") {
69
constexpr Vector4 vector1 = Vector4(1, 2, 3, 4);
70
constexpr Vector4 vector2 = Vector4(4, 5, 6, 7);
71
CHECK_MESSAGE(
72
vector1.lerp(vector2, 0.5) == Vector4(2.5, 3.5, 4.5, 5.5),
73
"Vector4 lerp should work as expected.");
74
CHECK_MESSAGE(
75
vector1.lerp(vector2, 1.0 / 3.0).is_equal_approx(Vector4(2, 3, 4, 5)),
76
"Vector4 lerp should work as expected.");
77
CHECK_MESSAGE(
78
vector1.cubic_interpolate(vector2, Vector4(), Vector4(7, 7, 7, 7), 0.5) == Vector4(2.375, 3.5, 4.625, 5.75),
79
"Vector4 cubic_interpolate should work as expected.");
80
CHECK_MESSAGE(
81
vector1.cubic_interpolate(vector2, Vector4(), Vector4(7, 7, 7, 7), 1.0 / 3.0).is_equal_approx(Vector4(1.851851940155029297, 2.962963104248046875, 4.074074268341064453, 5.185185185185)),
82
"Vector4 cubic_interpolate should work as expected.");
83
}
84
85
TEST_CASE("[Vector4] Length methods") {
86
constexpr Vector4 vector1 = Vector4(10, 10, 10, 10);
87
constexpr Vector4 vector2 = Vector4(20, 30, 40, 50);
88
CHECK_MESSAGE(
89
vector1.length_squared() == 400,
90
"Vector4 length_squared should work as expected and return exact result.");
91
CHECK_MESSAGE(
92
vector1.length() == doctest::Approx(20),
93
"Vector4 length should work as expected.");
94
CHECK_MESSAGE(
95
vector2.length_squared() == 5400,
96
"Vector4 length_squared should work as expected and return exact result.");
97
CHECK_MESSAGE(
98
vector2.length() == doctest::Approx((real_t)73.484692283495),
99
"Vector4 length should work as expected.");
100
CHECK_MESSAGE(
101
vector1.distance_to(vector2) == doctest::Approx((real_t)54.772255750517),
102
"Vector4 distance_to should work as expected.");
103
CHECK_MESSAGE(
104
vector1.distance_squared_to(vector2) == doctest::Approx(3000),
105
"Vector4 distance_squared_to should work as expected.");
106
}
107
108
TEST_CASE("[Vector4] Limiting methods") {
109
constexpr Vector4 vector = Vector4(10, 10, 10, 10);
110
CHECK_MESSAGE(
111
Vector4(-5, 5, 15, -15).clamp(Vector4(), vector) == Vector4(0, 5, 10, 0),
112
"Vector4 clamp should work as expected.");
113
CHECK_MESSAGE(
114
vector.clamp(Vector4(0, 10, 15, 18), Vector4(5, 10, 20, 25)) == Vector4(5, 10, 15, 18),
115
"Vector4 clamp should work as expected.");
116
}
117
118
TEST_CASE("[Vector4] Normalization methods") {
119
CHECK_MESSAGE(
120
Vector4(1, 0, 0, 0).is_normalized() == true,
121
"Vector4 is_normalized should return true for a normalized vector.");
122
CHECK_MESSAGE(
123
Vector4(1, 1, 1, 1).is_normalized() == false,
124
"Vector4 is_normalized should return false for a non-normalized vector.");
125
CHECK_MESSAGE(
126
Vector4(1, 0, 0, 0).normalized() == Vector4(1, 0, 0, 0),
127
"Vector4 normalized should return the same vector for a normalized vector.");
128
CHECK_MESSAGE(
129
Vector4(1, 1, 0, 0).normalized().is_equal_approx(Vector4(Math::SQRT12, Math::SQRT12, 0, 0)),
130
"Vector4 normalized should work as expected.");
131
CHECK_MESSAGE(
132
Vector4(1, 1, 1, 1).normalized().is_equal_approx(Vector4(0.5, 0.5, 0.5, 0.5)),
133
"Vector4 normalized should work as expected.");
134
}
135
136
TEST_CASE("[Vector4] Operators") {
137
constexpr Vector4 decimal1 = Vector4(2.3, 4.9, 7.8, 3.2);
138
constexpr Vector4 decimal2 = Vector4(1.2, 3.4, 5.6, 1.7);
139
constexpr Vector4 power1 = Vector4(0.75, 1.5, 0.625, 0.125);
140
constexpr Vector4 power2 = Vector4(0.5, 0.125, 0.25, 0.75);
141
constexpr Vector4 int1 = Vector4(4, 5, 9, 2);
142
constexpr Vector4 int2 = Vector4(1, 2, 3, 1);
143
144
static_assert(
145
-decimal1 == Vector4(-2.3, -4.9, -7.8, -3.2),
146
"Vector4 change of sign should work as expected.");
147
CHECK_MESSAGE(
148
(decimal1 + decimal2).is_equal_approx(Vector4(3.5, 8.3, 13.4, 4.9)),
149
"Vector4 addition should behave as expected.");
150
static_assert(
151
(power1 + power2) == Vector4(1.25, 1.625, 0.875, 0.875),
152
"Vector4 addition with powers of two should give exact results.");
153
static_assert(
154
(int1 + int2) == Vector4(5, 7, 12, 3),
155
"Vector4 addition with integers should give exact results.");
156
157
CHECK_MESSAGE(
158
(decimal1 - decimal2).is_equal_approx(Vector4(1.1, 1.5, 2.2, 1.5)),
159
"Vector4 subtraction should behave as expected.");
160
static_assert(
161
(power1 - power2) == Vector4(0.25, 1.375, 0.375, -0.625),
162
"Vector4 subtraction with powers of two should give exact results.");
163
static_assert(
164
(int1 - int2) == Vector4(3, 3, 6, 1),
165
"Vector4 subtraction with integers should give exact results.");
166
167
CHECK_MESSAGE(
168
(decimal1 * decimal2).is_equal_approx(Vector4(2.76, 16.66, 43.68, 5.44)),
169
"Vector4 multiplication should behave as expected.");
170
static_assert(
171
(power1 * power2) == Vector4(0.375, 0.1875, 0.15625, 0.09375),
172
"Vector4 multiplication with powers of two should give exact results.");
173
static_assert(
174
(int1 * int2) == Vector4(4, 10, 27, 2),
175
"Vector4 multiplication with integers should give exact results.");
176
177
CHECK_MESSAGE(
178
(decimal1 / decimal2).is_equal_approx(Vector4(1.91666666666666666, 1.44117647058823529, 1.39285714285714286, 1.88235294118)),
179
"Vector4 division should behave as expected.");
180
static_assert(
181
(power1 / power2) == Vector4(1.5, 12.0, 2.5, 1.0 / 6.0),
182
"Vector4 division with powers of two should give exact results.");
183
static_assert(
184
(int1 / int2) == Vector4(4, 2.5, 3, 2),
185
"Vector4 division with integers should give exact results.");
186
187
CHECK_MESSAGE(
188
(decimal1 * 2).is_equal_approx(Vector4(4.6, 9.8, 15.6, 6.4)),
189
"Vector4 multiplication should behave as expected.");
190
static_assert(
191
(power1 * 2) == Vector4(1.5, 3, 1.25, 0.25),
192
"Vector4 multiplication with powers of two should give exact results.");
193
static_assert(
194
(int1 * 2) == Vector4(8, 10, 18, 4),
195
"Vector4 multiplication with integers should give exact results.");
196
197
CHECK_MESSAGE(
198
(decimal1 / 2).is_equal_approx(Vector4(1.15, 2.45, 3.9, 1.6)),
199
"Vector4 division should behave as expected.");
200
static_assert(
201
(power1 / 2) == Vector4(0.375, 0.75, 0.3125, 0.0625),
202
"Vector4 division with powers of two should give exact results.");
203
static_assert(
204
(int1 / 2) == Vector4(2, 2.5, 4.5, 1),
205
"Vector4 division with integers should give exact results.");
206
207
CHECK_MESSAGE(
208
((String)decimal1) == "(2.3, 4.9, 7.8, 3.2)",
209
"Vector4 cast to String should work as expected.");
210
CHECK_MESSAGE(
211
((String)decimal2) == "(1.2, 3.4, 5.6, 1.7)",
212
"Vector4 cast to String should work as expected.");
213
CHECK_MESSAGE(
214
((String)Vector4(9.7, 9.8, 9.9, -1.8)) == "(9.7, 9.8, 9.9, -1.8)",
215
"Vector4 cast to String should work as expected.");
216
#ifdef REAL_T_IS_DOUBLE
217
CHECK_MESSAGE(
218
((String)Vector4(Math::E, Math::SQRT2, Math::SQRT3, Math::SQRT3)) == "(2.71828182845905, 1.4142135623731, 1.73205080756888, 1.73205080756888)",
219
"Vector4 cast to String should print the correct amount of digits for real_t = double.");
220
#else
221
CHECK_MESSAGE(
222
((String)Vector4(Math::E, Math::SQRT2, Math::SQRT3, Math::SQRT3)) == "(2.718282, 1.414214, 1.732051, 1.732051)",
223
"Vector4 cast to String should print the correct amount of digits for real_t = float.");
224
#endif // REAL_T_IS_DOUBLE
225
}
226
227
TEST_CASE("[Vector4] Other methods") {
228
constexpr Vector4 vector = Vector4(1.2, 3.4, 5.6, 1.6);
229
CHECK_MESSAGE(
230
vector.direction_to(Vector4()).is_equal_approx(-vector.normalized()),
231
"Vector4 direction_to should work as expected.");
232
CHECK_MESSAGE(
233
Vector4(1, 1, 1, 1).direction_to(Vector4(2, 2, 2, 2)).is_equal_approx(Vector4(0.5, 0.5, 0.5, 0.5)),
234
"Vector4 direction_to should work as expected.");
235
CHECK_MESSAGE(
236
vector.inverse().is_equal_approx(Vector4(1 / 1.2, 1 / 3.4, 1 / 5.6, 1 / 1.6)),
237
"Vector4 inverse should work as expected.");
238
CHECK_MESSAGE(
239
vector.posmod(2).is_equal_approx(Vector4(1.2, 1.4, 1.6, 1.6)),
240
"Vector4 posmod should work as expected.");
241
CHECK_MESSAGE(
242
(-vector).posmod(2).is_equal_approx(Vector4(0.8, 0.6, 0.4, 0.4)),
243
"Vector4 posmod should work as expected.");
244
CHECK_MESSAGE(
245
vector.posmodv(Vector4(1, 2, 3, 4)).is_equal_approx(Vector4(0.2, 1.4, 2.6, 1.6)),
246
"Vector4 posmodv should work as expected.");
247
CHECK_MESSAGE(
248
(-vector).posmodv(Vector4(2, 3, 4, 5)).is_equal_approx(Vector4(0.8, 2.6, 2.4, 3.4)),
249
"Vector4 posmodv should work as expected.");
250
CHECK_MESSAGE(
251
vector.snapped(Vector4(1, 1, 1, 1)) == Vector4(1, 3, 6, 2),
252
"Vector4 snapped to integers should be the same as rounding.");
253
CHECK_MESSAGE(
254
vector.snapped(Vector4(0.25, 0.25, 0.25, 0.25)) == Vector4(1.25, 3.5, 5.5, 1.5),
255
"Vector4 snapped to 0.25 should give exact results.");
256
257
CHECK_MESSAGE(
258
Vector4(1.2, 2.5, 2.0, 1.6).is_equal_approx(vector.min(Vector4(3.0, 2.5, 2.0, 3.4))),
259
"Vector4 min should return expected value.");
260
261
CHECK_MESSAGE(
262
Vector4(5.3, 3.4, 5.6, 4.2).is_equal_approx(vector.max(Vector4(5.3, 2.0, 3.0, 4.2))),
263
"Vector4 max should return expected value.");
264
}
265
266
TEST_CASE("[Vector4] Rounding methods") {
267
constexpr Vector4 vector1 = Vector4(1.2, 3.4, 5.6, 1.6);
268
constexpr Vector4 vector2 = Vector4(1.2, -3.4, -5.6, -1.6);
269
CHECK_MESSAGE(
270
vector1.abs() == vector1,
271
"Vector4 abs should work as expected.");
272
CHECK_MESSAGE(
273
vector2.abs() == vector1,
274
"Vector4 abs should work as expected.");
275
CHECK_MESSAGE(
276
vector1.ceil() == Vector4(2, 4, 6, 2),
277
"Vector4 ceil should work as expected.");
278
CHECK_MESSAGE(
279
vector2.ceil() == Vector4(2, -3, -5, -1),
280
"Vector4 ceil should work as expected.");
281
282
CHECK_MESSAGE(
283
vector1.floor() == Vector4(1, 3, 5, 1),
284
"Vector4 floor should work as expected.");
285
CHECK_MESSAGE(
286
vector2.floor() == Vector4(1, -4, -6, -2),
287
"Vector4 floor should work as expected.");
288
289
CHECK_MESSAGE(
290
vector1.round() == Vector4(1, 3, 6, 2),
291
"Vector4 round should work as expected.");
292
CHECK_MESSAGE(
293
vector2.round() == Vector4(1, -3, -6, -2),
294
"Vector4 round should work as expected.");
295
296
CHECK_MESSAGE(
297
vector1.sign() == Vector4(1, 1, 1, 1),
298
"Vector4 sign should work as expected.");
299
CHECK_MESSAGE(
300
vector2.sign() == Vector4(1, -1, -1, -1),
301
"Vector4 sign should work as expected.");
302
}
303
304
TEST_CASE("[Vector4] Linear algebra methods") {
305
constexpr Vector4 vector_x = Vector4(1, 0, 0, 0);
306
constexpr Vector4 vector_y = Vector4(0, 1, 0, 0);
307
constexpr Vector4 vector1 = Vector4(1.7, 2.3, 1, 9.1);
308
constexpr Vector4 vector2 = Vector4(-8.2, -16, 3, 2.4);
309
310
CHECK_MESSAGE(
311
vector_x.dot(vector_y) == 0.0,
312
"Vector4 dot product of perpendicular vectors should be zero.");
313
CHECK_MESSAGE(
314
vector_x.dot(vector_x) == 1.0,
315
"Vector4 dot product of identical unit vectors should be one.");
316
CHECK_MESSAGE(
317
(vector_x * 10).dot(vector_x * 10) == 100.0,
318
"Vector4 dot product of same direction vectors should behave as expected.");
319
CHECK_MESSAGE(
320
(vector1 * 2).dot(vector2 * 4) == doctest::Approx((real_t)-25.9 * 8),
321
"Vector4 dot product should work as expected.");
322
}
323
324
TEST_CASE("[Vector4] Finite number checks") {
325
constexpr double infinite[] = { Math::NaN, Math::INF, -Math::INF };
326
327
CHECK_MESSAGE(
328
Vector4(0, 1, 2, 3).is_finite(),
329
"Vector4(0, 1, 2, 3) should be finite");
330
331
for (double x : infinite) {
332
CHECK_FALSE_MESSAGE(
333
Vector4(x, 1, 2, 3).is_finite(),
334
"Vector4 with one component infinite should not be finite.");
335
CHECK_FALSE_MESSAGE(
336
Vector4(0, x, 2, 3).is_finite(),
337
"Vector4 with one component infinite should not be finite.");
338
CHECK_FALSE_MESSAGE(
339
Vector4(0, 1, x, 3).is_finite(),
340
"Vector4 with one component infinite should not be finite.");
341
CHECK_FALSE_MESSAGE(
342
Vector4(0, 1, 2, x).is_finite(),
343
"Vector4 with one component infinite should not be finite.");
344
}
345
346
for (double x : infinite) {
347
for (double y : infinite) {
348
CHECK_FALSE_MESSAGE(
349
Vector4(x, y, 2, 3).is_finite(),
350
"Vector4 with two components infinite should not be finite.");
351
CHECK_FALSE_MESSAGE(
352
Vector4(x, 1, y, 3).is_finite(),
353
"Vector4 with two components infinite should not be finite.");
354
CHECK_FALSE_MESSAGE(
355
Vector4(x, 1, 2, y).is_finite(),
356
"Vector4 with two components infinite should not be finite.");
357
CHECK_FALSE_MESSAGE(
358
Vector4(0, x, y, 3).is_finite(),
359
"Vector4 with two components infinite should not be finite.");
360
CHECK_FALSE_MESSAGE(
361
Vector4(0, x, 2, y).is_finite(),
362
"Vector4 with two components infinite should not be finite.");
363
CHECK_FALSE_MESSAGE(
364
Vector4(0, 1, x, y).is_finite(),
365
"Vector4 with two components infinite should not be finite.");
366
}
367
}
368
369
for (double x : infinite) {
370
for (double y : infinite) {
371
for (double z : infinite) {
372
CHECK_FALSE_MESSAGE(
373
Vector4(0, x, y, z).is_finite(),
374
"Vector4 with three components infinite should not be finite.");
375
CHECK_FALSE_MESSAGE(
376
Vector4(x, 1, y, z).is_finite(),
377
"Vector4 with three components infinite should not be finite.");
378
CHECK_FALSE_MESSAGE(
379
Vector4(x, y, 2, z).is_finite(),
380
"Vector4 with three components infinite should not be finite.");
381
CHECK_FALSE_MESSAGE(
382
Vector4(x, y, z, 3).is_finite(),
383
"Vector4 with three components infinite should not be finite.");
384
}
385
}
386
}
387
388
for (double x : infinite) {
389
for (double y : infinite) {
390
for (double z : infinite) {
391
for (double w : infinite) {
392
CHECK_FALSE_MESSAGE(
393
Vector4(x, y, z, w).is_finite(),
394
"Vector4 with four components infinite should not be finite.");
395
}
396
}
397
}
398
}
399
}
400
401
} // namespace TestVector4
402
403