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