Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/tests/core/math/test_vector2.cpp
23450 views
1
/**************************************************************************/
2
/* test_vector2.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_vector2)
34
35
#include "core/math/vector2.h"
36
#include "core/math/vector2i.h"
37
38
namespace TestVector2 {
39
40
TEST_CASE("[Vector2] Constructor methods") {
41
constexpr Vector2 vector_empty = Vector2();
42
constexpr Vector2 vector_zero = Vector2(0.0, 0.0);
43
static_assert(
44
vector_empty == vector_zero,
45
"Vector2 Constructor with no inputs should return a zero Vector2.");
46
}
47
48
TEST_CASE("[Vector2] Angle methods") {
49
constexpr Vector2 vector_x = Vector2(1, 0);
50
constexpr Vector2 vector_y = Vector2(0, 1);
51
CHECK_MESSAGE(
52
vector_x.angle_to(vector_y) == doctest::Approx((real_t)Math::TAU / 4),
53
"Vector2 angle_to should work as expected.");
54
CHECK_MESSAGE(
55
vector_y.angle_to(vector_x) == doctest::Approx((real_t)-Math::TAU / 4),
56
"Vector2 angle_to should work as expected.");
57
CHECK_MESSAGE(
58
vector_x.angle_to_point(vector_y) == doctest::Approx((real_t)Math::TAU * 3 / 8),
59
"Vector2 angle_to_point should work as expected.");
60
CHECK_MESSAGE(
61
vector_y.angle_to_point(vector_x) == doctest::Approx((real_t)-Math::TAU / 8),
62
"Vector2 angle_to_point should work as expected.");
63
}
64
65
TEST_CASE("[Vector2] Axis methods") {
66
Vector2 vector = Vector2(1.2, 3.4);
67
CHECK_MESSAGE(
68
vector.max_axis_index() == Vector2::Axis::AXIS_Y,
69
"Vector2 max_axis_index should work as expected.");
70
CHECK_MESSAGE(
71
vector.min_axis_index() == Vector2::Axis::AXIS_X,
72
"Vector2 min_axis_index should work as expected.");
73
CHECK_MESSAGE(
74
vector[vector.min_axis_index()] == (real_t)1.2,
75
"Vector2 array operator should work as expected.");
76
vector[Vector2::Axis::AXIS_Y] = 3.7;
77
CHECK_MESSAGE(
78
vector[Vector2::Axis::AXIS_Y] == (real_t)3.7,
79
"Vector2 array operator setter should work as expected.");
80
}
81
82
TEST_CASE("[Vector2] Interpolation methods") {
83
constexpr Vector2 vector1 = Vector2(1, 2);
84
constexpr Vector2 vector2 = Vector2(4, 5);
85
CHECK_MESSAGE(
86
vector1.lerp(vector2, 0.5) == Vector2(2.5, 3.5),
87
"Vector2 lerp should work as expected.");
88
CHECK_MESSAGE(
89
vector1.lerp(vector2, 1.0 / 3.0).is_equal_approx(Vector2(2, 3)),
90
"Vector2 lerp should work as expected.");
91
CHECK_MESSAGE(
92
vector1.normalized().slerp(vector2.normalized(), 0.5).is_equal_approx(Vector2(0.538953602313995361, 0.84233558177947998)),
93
"Vector2 slerp should work as expected.");
94
CHECK_MESSAGE(
95
vector1.normalized().slerp(vector2.normalized(), 1.0 / 3.0).is_equal_approx(Vector2(0.508990883827209473, 0.860771894454956055)),
96
"Vector2 slerp should work as expected.");
97
CHECK_MESSAGE(
98
Vector2(5, 0).slerp(Vector2(0, 5), 0.5).is_equal_approx(Vector2(5, 5) * Math::SQRT12),
99
"Vector2 slerp with non-normalized values should work as expected.");
100
CHECK_MESSAGE(
101
Vector2(1, 1).slerp(Vector2(2, 2), 0.5).is_equal_approx(Vector2(1.5, 1.5)),
102
"Vector2 slerp with colinear inputs should behave as expected.");
103
CHECK_MESSAGE(
104
Vector2().slerp(Vector2(), 0.5) == Vector2(),
105
"Vector2 slerp with both inputs as zero vectors should return a zero vector.");
106
CHECK_MESSAGE(
107
Vector2().slerp(Vector2(1, 1), 0.5) == Vector2(0.5, 0.5),
108
"Vector2 slerp with one input as zero should behave like a regular lerp.");
109
CHECK_MESSAGE(
110
Vector2(1, 1).slerp(Vector2(), 0.5) == Vector2(0.5, 0.5),
111
"Vector2 slerp with one input as zero should behave like a regular lerp.");
112
CHECK_MESSAGE(
113
Vector2(4, 6).slerp(Vector2(8, 10), 0.5).is_equal_approx(Vector2(5.9076470794008017626, 8.07918879020090480697)),
114
"Vector2 slerp should work as expected.");
115
CHECK_MESSAGE(
116
vector1.slerp(vector2, 0.5).length() == doctest::Approx((real_t)4.31959610746631919),
117
"Vector2 slerp with different length input should return a vector with an interpolated length.");
118
CHECK_MESSAGE(
119
vector1.angle_to(vector1.slerp(vector2, 0.5)) * 2 == doctest::Approx(vector1.angle_to(vector2)),
120
"Vector2 slerp with different length input should return a vector with an interpolated angle.");
121
CHECK_MESSAGE(
122
vector1.cubic_interpolate(vector2, Vector2(), Vector2(7, 7), 0.5) == Vector2(2.375, 3.5),
123
"Vector2 cubic_interpolate should work as expected.");
124
CHECK_MESSAGE(
125
vector1.cubic_interpolate(vector2, Vector2(), Vector2(7, 7), 1.0 / 3.0).is_equal_approx(Vector2(1.851851940155029297, 2.962963104248046875)),
126
"Vector2 cubic_interpolate should work as expected.");
127
CHECK_MESSAGE(
128
Vector2(1, 0).move_toward(Vector2(10, 0), 3) == Vector2(4, 0),
129
"Vector2 move_toward should work as expected.");
130
}
131
132
TEST_CASE("[Vector2] Length methods") {
133
constexpr Vector2 vector1 = Vector2(10, 10);
134
constexpr Vector2 vector2 = Vector2(20, 30);
135
CHECK_MESSAGE(
136
vector1.length_squared() == 200,
137
"Vector2 length_squared should work as expected and return exact result.");
138
CHECK_MESSAGE(
139
vector1.length() == doctest::Approx(10 * (real_t)Math::SQRT2),
140
"Vector2 length should work as expected.");
141
CHECK_MESSAGE(
142
vector2.length_squared() == 1300,
143
"Vector2 length_squared should work as expected and return exact result.");
144
CHECK_MESSAGE(
145
vector2.length() == doctest::Approx((real_t)36.05551275463989293119),
146
"Vector2 length should work as expected.");
147
CHECK_MESSAGE(
148
vector1.distance_squared_to(vector2) == 500,
149
"Vector2 distance_squared_to should work as expected and return exact result.");
150
CHECK_MESSAGE(
151
vector1.distance_to(vector2) == doctest::Approx((real_t)22.36067977499789696409),
152
"Vector2 distance_to should work as expected.");
153
}
154
155
TEST_CASE("[Vector2] Limiting methods") {
156
constexpr Vector2 vector = Vector2(10, 10);
157
CHECK_MESSAGE(
158
vector.limit_length().is_equal_approx(Vector2(Math::SQRT12, Math::SQRT12)),
159
"Vector2 limit_length should work as expected.");
160
CHECK_MESSAGE(
161
vector.limit_length(5).is_equal_approx(5 * Vector2(Math::SQRT12, Math::SQRT12)),
162
"Vector2 limit_length should work as expected.");
163
164
CHECK_MESSAGE(
165
Vector2(-5, 15).clamp(Vector2(), vector).is_equal_approx(Vector2(0, 10)),
166
"Vector2 clamp should work as expected.");
167
CHECK_MESSAGE(
168
vector.clamp(Vector2(0, 15), Vector2(5, 20)).is_equal_approx(Vector2(5, 15)),
169
"Vector2 clamp should work as expected.");
170
}
171
172
TEST_CASE("[Vector2] Normalization methods") {
173
CHECK_MESSAGE(
174
Vector2(1, 0).is_normalized() == true,
175
"Vector2 is_normalized should return true for a normalized vector.");
176
CHECK_MESSAGE(
177
Vector2(1, 1).is_normalized() == false,
178
"Vector2 is_normalized should return false for a non-normalized vector.");
179
CHECK_MESSAGE(
180
Vector2(1, 0).normalized() == Vector2(1, 0),
181
"Vector2 normalized should return the same vector for a normalized vector.");
182
CHECK_MESSAGE(
183
Vector2(1, 1).normalized().is_equal_approx(Vector2(Math::SQRT12, Math::SQRT12)),
184
"Vector2 normalized should work as expected.");
185
186
Vector2 vector = Vector2(3.2, -5.4);
187
vector.normalize();
188
CHECK_MESSAGE(
189
vector == Vector2(3.2, -5.4).normalized(),
190
"Vector2 normalize should convert same way as Vector2 normalized.");
191
CHECK_MESSAGE(
192
vector.is_equal_approx(Vector2(0.509802390301732898898, -0.860291533634174266891)),
193
"Vector2 normalize should work as expected.");
194
}
195
196
TEST_CASE("[Vector2] Operators") {
197
constexpr Vector2 decimal1 = Vector2(2.3, 4.9);
198
constexpr Vector2 decimal2 = Vector2(1.2, 3.4);
199
constexpr Vector2 power1 = Vector2(0.75, 1.5);
200
constexpr Vector2 power2 = Vector2(0.5, 0.125);
201
constexpr Vector2 int1 = Vector2(4, 5);
202
constexpr Vector2 int2 = Vector2(1, 2);
203
204
CHECK_MESSAGE(
205
(decimal1 + decimal2).is_equal_approx(Vector2(3.5, 8.3)),
206
"Vector2 addition should behave as expected.");
207
static_assert(
208
(power1 + power2) == Vector2(1.25, 1.625),
209
"Vector2 addition with powers of two should give exact results.");
210
static_assert(
211
(int1 + int2) == Vector2(5, 7),
212
"Vector2 addition with integers should give exact results.");
213
214
CHECK_MESSAGE(
215
(decimal1 - decimal2).is_equal_approx(Vector2(1.1, 1.5)),
216
"Vector2 subtraction should behave as expected.");
217
static_assert(
218
(power1 - power2) == Vector2(0.25, 1.375),
219
"Vector2 subtraction with powers of two should give exact results.");
220
static_assert(
221
(int1 - int2) == Vector2(3, 3),
222
"Vector2 subtraction with integers should give exact results.");
223
224
CHECK_MESSAGE(
225
(decimal1 * decimal2).is_equal_approx(Vector2(2.76, 16.66)),
226
"Vector2 multiplication should behave as expected.");
227
static_assert(
228
(power1 * power2) == Vector2(0.375, 0.1875),
229
"Vector2 multiplication with powers of two should give exact results.");
230
static_assert(
231
(int1 * int2) == Vector2(4, 10),
232
"Vector2 multiplication with integers should give exact results.");
233
234
CHECK_MESSAGE(
235
(decimal1 / decimal2).is_equal_approx(Vector2(1.91666666666666666, 1.44117647058823529)),
236
"Vector2 division should behave as expected.");
237
static_assert(
238
(power1 / power2) == Vector2(1.5, 12.0),
239
"Vector2 division with powers of two should give exact results.");
240
static_assert(
241
(int1 / int2) == Vector2(4, 2.5),
242
"Vector2 division with integers should give exact results.");
243
244
CHECK_MESSAGE(
245
(decimal1 * 2).is_equal_approx(Vector2(4.6, 9.8)),
246
"Vector2 multiplication should behave as expected.");
247
static_assert(
248
(power1 * 2) == Vector2(1.5, 3),
249
"Vector2 multiplication with powers of two should give exact results.");
250
static_assert(
251
(int1 * 2) == Vector2(8, 10),
252
"Vector2 multiplication with integers should give exact results.");
253
254
CHECK_MESSAGE(
255
(decimal1 / 2).is_equal_approx(Vector2(1.15, 2.45)),
256
"Vector2 division should behave as expected.");
257
static_assert(
258
(power1 / 2) == Vector2(0.375, 0.75),
259
"Vector2 division with powers of two should give exact results.");
260
static_assert(
261
(int1 / 2) == Vector2(2, 2.5),
262
"Vector2 division with integers should give exact results.");
263
264
CHECK_MESSAGE(
265
((Vector2i)decimal1) == Vector2i(2, 4),
266
"Vector2 cast to Vector2i should work as expected.");
267
CHECK_MESSAGE(
268
((Vector2i)decimal2) == Vector2i(1, 3),
269
"Vector2 cast to Vector2i should work as expected.");
270
CHECK_MESSAGE(
271
Vector2(Vector2i(1, 2)) == Vector2(1, 2),
272
"Vector2 constructed from Vector2i should work as expected.");
273
274
CHECK_MESSAGE(
275
((String)decimal1) == "(2.3, 4.9)",
276
"Vector2 cast to String should work as expected.");
277
CHECK_MESSAGE(
278
((String)decimal2) == "(1.2, 3.4)",
279
"Vector2 cast to String should work as expected.");
280
CHECK_MESSAGE(
281
((String)Vector2(9.8, 9.9)) == "(9.8, 9.9)",
282
"Vector2 cast to String should work as expected.");
283
#ifdef REAL_T_IS_DOUBLE
284
CHECK_MESSAGE(
285
((String)Vector2(Math::PI, Math::TAU)) == "(3.14159265358979, 6.28318530717959)",
286
"Vector2 cast to String should print the correct amount of digits for real_t = double.");
287
#else
288
CHECK_MESSAGE(
289
((String)Vector2(Math::PI, Math::TAU)) == "(3.141593, 6.283185)",
290
"Vector2 cast to String should print the correct amount of digits for real_t = float.");
291
#endif // REAL_T_IS_DOUBLE
292
}
293
294
TEST_CASE("[Vector2] Other methods") {
295
constexpr Vector2 vector = Vector2(1.2, 3.4);
296
CHECK_MESSAGE(
297
vector.aspect() == doctest::Approx((real_t)1.2 / (real_t)3.4),
298
"Vector2 aspect should work as expected.");
299
300
CHECK_MESSAGE(
301
vector.direction_to(Vector2()).is_equal_approx(-vector.normalized()),
302
"Vector2 direction_to should work as expected.");
303
CHECK_MESSAGE(
304
Vector2(1, 1).direction_to(Vector2(2, 2)).is_equal_approx(Vector2(Math::SQRT12, Math::SQRT12)),
305
"Vector2 direction_to should work as expected.");
306
307
CHECK_MESSAGE(
308
vector.posmod(2).is_equal_approx(Vector2(1.2, 1.4)),
309
"Vector2 posmod should work as expected.");
310
CHECK_MESSAGE(
311
(-vector).posmod(2).is_equal_approx(Vector2(0.8, 0.6)),
312
"Vector2 posmod should work as expected.");
313
CHECK_MESSAGE(
314
vector.posmodv(Vector2(1, 2)).is_equal_approx(Vector2(0.2, 1.4)),
315
"Vector2 posmodv should work as expected.");
316
CHECK_MESSAGE(
317
(-vector).posmodv(Vector2(2, 3)).is_equal_approx(Vector2(0.8, 2.6)),
318
"Vector2 posmodv should work as expected.");
319
320
CHECK_MESSAGE(
321
vector.rotated(Math::TAU).is_equal_approx(Vector2(1.2, 3.4)),
322
"Vector2 rotated should work as expected.");
323
CHECK_MESSAGE(
324
vector.rotated(Math::TAU / 4).is_equal_approx(Vector2(-3.4, 1.2)),
325
"Vector2 rotated should work as expected.");
326
CHECK_MESSAGE(
327
vector.rotated(Math::TAU / 3).is_equal_approx(Vector2(-3.544486372867091398996, -0.660769515458673623883)),
328
"Vector2 rotated should work as expected.");
329
CHECK_MESSAGE(
330
vector.rotated(Math::TAU / 2).is_equal_approx(vector.rotated(Math::TAU / -2)),
331
"Vector2 rotated should work as expected.");
332
333
CHECK_MESSAGE(
334
vector.snapped(Vector2(1, 1)) == Vector2(1, 3),
335
"Vector2 snapped to integers should be the same as rounding.");
336
CHECK_MESSAGE(
337
Vector2(3.4, 5.6).snapped(Vector2(1, 1)) == Vector2(3, 6),
338
"Vector2 snapped to integers should be the same as rounding.");
339
CHECK_MESSAGE(
340
vector.snapped(Vector2(0.25, 0.25)) == Vector2(1.25, 3.5),
341
"Vector2 snapped to 0.25 should give exact results.");
342
343
CHECK_MESSAGE(
344
Vector2(1.2, 2.5).is_equal_approx(vector.min(Vector2(3.0, 2.5))),
345
"Vector2 min should return expected value.");
346
347
CHECK_MESSAGE(
348
Vector2(5.3, 3.4).is_equal_approx(vector.max(Vector2(5.3, 2.0))),
349
"Vector2 max should return expected value.");
350
}
351
352
TEST_CASE("[Vector2] Plane methods") {
353
constexpr Vector2 vector = Vector2(1.2, 3.4);
354
constexpr Vector2 vector_y = Vector2(0, 1);
355
constexpr Vector2 vector_normal = Vector2(0.95879811270838721622267, 0.2840883296913739899919);
356
constexpr real_t p_d = 99.1;
357
CHECK_MESSAGE(
358
vector.bounce(vector_y) == Vector2(1.2, -3.4),
359
"Vector2 bounce on a plane with normal of the Y axis should.");
360
CHECK_MESSAGE(
361
vector.bounce(vector_normal).is_equal_approx(Vector2(-2.85851197982345523329, 2.197477931904161412358)),
362
"Vector2 bounce with normal should return expected value.");
363
CHECK_MESSAGE(
364
vector.reflect(vector_y) == Vector2(-1.2, 3.4),
365
"Vector2 reflect on a plane with normal of the Y axis should.");
366
CHECK_MESSAGE(
367
vector.reflect(vector_normal).is_equal_approx(Vector2(2.85851197982345523329, -2.197477931904161412358)),
368
"Vector2 reflect with normal should return expected value.");
369
CHECK_MESSAGE(
370
vector.project(vector_y) == Vector2(0, 3.4),
371
"Vector2 projected on the Y axis should only give the Y component.");
372
CHECK_MESSAGE(
373
vector.project(vector_normal).is_equal_approx(Vector2(2.0292559899117276166, 0.60126103404791929382)),
374
"Vector2 projected on a normal should return expected value.");
375
CHECK_MESSAGE(
376
vector_normal.plane_project(p_d, vector).is_equal_approx(Vector2(94.187635516479631, 30.951892004882851)),
377
"Vector2 plane_project should return expected value.");
378
CHECK_MESSAGE(
379
vector.slide(vector_y) == Vector2(1.2, 0),
380
"Vector2 slide on a plane with normal of the Y axis should set the Y to zero.");
381
CHECK_MESSAGE(
382
vector.slide(vector_normal).is_equal_approx(Vector2(-0.8292559899117276166456, 2.798738965952080706179)),
383
"Vector2 slide with normal should return expected value.");
384
// There's probably a better way to test these ones?
385
#ifdef MATH_CHECKS
386
constexpr Vector2 vector_non_normal = Vector2(5.4, 1.6);
387
ERR_PRINT_OFF;
388
CHECK_MESSAGE(
389
vector.bounce(vector_non_normal).is_equal_approx(Vector2()),
390
"Vector2 bounce should return empty Vector2 with non-normalized input.");
391
CHECK_MESSAGE(
392
vector.reflect(vector_non_normal).is_equal_approx(Vector2()),
393
"Vector2 reflect should return empty Vector2 with non-normalized input.");
394
CHECK_MESSAGE(
395
vector.slide(vector_non_normal).is_equal_approx(Vector2()),
396
"Vector2 slide should return empty Vector2 with non-normalized input.");
397
ERR_PRINT_ON;
398
#endif // MATH_CHECKS
399
}
400
401
TEST_CASE("[Vector2] Rounding methods") {
402
constexpr Vector2 vector1 = Vector2(1.2, 5.6);
403
constexpr Vector2 vector2 = Vector2(1.2, -5.6);
404
CHECK_MESSAGE(
405
vector1.abs() == vector1,
406
"Vector2 abs should work as expected.");
407
CHECK_MESSAGE(
408
vector2.abs() == vector1,
409
"Vector2 abs should work as expected.");
410
411
CHECK_MESSAGE(
412
vector1.ceil() == Vector2(2, 6),
413
"Vector2 ceil should work as expected.");
414
CHECK_MESSAGE(
415
vector2.ceil() == Vector2(2, -5),
416
"Vector2 ceil should work as expected.");
417
418
CHECK_MESSAGE(
419
vector1.floor() == Vector2(1, 5),
420
"Vector2 floor should work as expected.");
421
CHECK_MESSAGE(
422
vector2.floor() == Vector2(1, -6),
423
"Vector2 floor should work as expected.");
424
425
CHECK_MESSAGE(
426
vector1.round() == Vector2(1, 6),
427
"Vector2 round should work as expected.");
428
CHECK_MESSAGE(
429
vector2.round() == Vector2(1, -6),
430
"Vector2 round should work as expected.");
431
432
CHECK_MESSAGE(
433
vector1.sign() == Vector2(1, 1),
434
"Vector2 sign should work as expected.");
435
CHECK_MESSAGE(
436
vector2.sign() == Vector2(1, -1),
437
"Vector2 sign should work as expected.");
438
}
439
440
TEST_CASE("[Vector2] Linear algebra methods") {
441
constexpr Vector2 vector_x = Vector2(1, 0);
442
constexpr Vector2 vector_y = Vector2(0, 1);
443
constexpr Vector2 a = Vector2(3.5, 8.5);
444
constexpr Vector2 b = Vector2(5.2, 4.6);
445
CHECK_MESSAGE(
446
vector_x.cross(vector_y) == 1,
447
"Vector2 cross product of X and Y should give 1.");
448
CHECK_MESSAGE(
449
vector_y.cross(vector_x) == -1,
450
"Vector2 cross product of Y and X should give negative 1.");
451
CHECK_MESSAGE(
452
a.cross(b) == doctest::Approx((real_t)-28.1),
453
"Vector2 cross should return expected value.");
454
CHECK_MESSAGE(
455
Vector2(-a.x, a.y).cross(Vector2(b.x, -b.y)) == doctest::Approx((real_t)-28.1),
456
"Vector2 cross should return expected value.");
457
458
CHECK_MESSAGE(
459
vector_x.dot(vector_y) == 0.0,
460
"Vector2 dot product of perpendicular vectors should be zero.");
461
CHECK_MESSAGE(
462
vector_x.dot(vector_x) == 1.0,
463
"Vector2 dot product of identical unit vectors should be one.");
464
CHECK_MESSAGE(
465
(vector_x * 10).dot(vector_x * 10) == 100.0,
466
"Vector2 dot product of same direction vectors should behave as expected.");
467
CHECK_MESSAGE(
468
a.dot(b) == doctest::Approx((real_t)57.3),
469
"Vector2 dot should return expected value.");
470
CHECK_MESSAGE(
471
Vector2(-a.x, a.y).dot(Vector2(b.x, -b.y)) == doctest::Approx((real_t)-57.3),
472
"Vector2 dot should return expected value.");
473
}
474
475
TEST_CASE("[Vector2] Finite number checks") {
476
constexpr double infinite[] = { Math::NaN, Math::INF, -Math::INF };
477
478
CHECK_MESSAGE(
479
Vector2(0, 1).is_finite(),
480
"Vector2(0, 1) should be finite");
481
482
for (double x : infinite) {
483
CHECK_FALSE_MESSAGE(
484
Vector2(x, 1).is_finite(),
485
"Vector2 with one component infinite should not be finite.");
486
CHECK_FALSE_MESSAGE(
487
Vector2(0, x).is_finite(),
488
"Vector2 with one component infinite should not be finite.");
489
}
490
491
for (double x : infinite) {
492
for (double y : infinite) {
493
CHECK_FALSE_MESSAGE(
494
Vector2(x, y).is_finite(),
495
"Vector2 with two components infinite should not be finite.");
496
}
497
}
498
}
499
500
} // namespace TestVector2
501
502