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