Kumo
A hand-rolled game engine.
git clone https://git.sentomk.me/Kumo.git
vec2_test.cpp (1793B) · raw
1 #include "core/vec2.h" 2 3 #include "check.h" 4 #include "core/fixed.h" 5 6 using kumo::Fixed; 7 using kumo::lerp; 8 using kumo::Vec2; 9 10 TEST(vec2_default_and_constructors) { 11 const Vec2 zero{}; 12 CHECK_EQ(zero, Vec2::zero()); 13 CHECK_EQ(zero.x, Fixed::from_int(0)); 14 CHECK_EQ(zero.y, Fixed::from_int(0)); 15 16 const Vec2 point = Vec2::from_ints(3, -4); 17 CHECK_EQ(point.x, Fixed::from_int(3)); 18 CHECK_EQ(point.y, Fixed::from_int(-4)); 19 } 20 21 TEST(vec2_arithmetic_and_negation) { 22 const Vec2 a = Vec2::from_ints(2, -3); 23 const Vec2 b = {Fixed::ratio(1, 2), Fixed::from_int(5)}; 24 25 CHECK_EQ(-a, Vec2::from_ints(-2, 3)); 26 CHECK_EQ(a + b, (Vec2{Fixed::ratio(5, 2), Fixed::from_int(2)})); 27 CHECK_EQ(a - b, (Vec2{Fixed::ratio(3, 2), Fixed::from_int(-8)})); 28 29 Vec2 acc = a; 30 acc += b; 31 CHECK_EQ(acc, a + b); 32 acc -= b; 33 CHECK_EQ(acc, a); 34 } 35 36 TEST(vec2_scalar_multiplication) { 37 const Vec2 v = {Fixed::ratio(3, 2), Fixed::from_int(-4)}; 38 const Fixed half = Fixed::ratio(1, 2); 39 const Vec2 expected = {Fixed::ratio(3, 4), Fixed::from_int(-2)}; 40 41 CHECK_EQ(v * half, expected); 42 CHECK_EQ(half * v, expected); 43 44 Vec2 scaled = v; 45 scaled *= half; 46 CHECK_EQ(scaled, expected); 47 } 48 49 TEST(vec2_lerp_interpolates_both_components) { 50 const Vec2 a = Vec2::from_ints(10, -20); 51 const Vec2 b = Vec2::from_ints(30, 20); 52 53 CHECK_EQ(lerp(a, b, Fixed::from_int(0)), a); 54 CHECK_EQ(lerp(a, b, Fixed::from_int(1)), b); 55 CHECK_EQ(lerp(a, b, Fixed::ratio(1, 2)), Vec2::from_ints(20, 0)); 56 CHECK_EQ(lerp(a, b, Fixed::ratio(1, 4)), Vec2::from_ints(15, -10)); 57 } 58 59 TEST(vec2_is_constexpr) { 60 constexpr Vec2 a = Vec2::from_ints(1, 2); 61 constexpr Vec2 b = Vec2::from_ints(5, 6); 62 constexpr Vec2 mid = lerp(a, b, Fixed::ratio(1, 2)); 63 static_assert(mid == Vec2::from_ints(3, 4)); 64 CHECK_EQ(mid, Vec2::from_ints(3, 4)); 65 }
