#include "core/vec2.h" #include "check.h" #include "core/fixed.h" using kumo::Fixed; using kumo::lerp; using kumo::Vec2; TEST(vec2_default_and_constructors) { const Vec2 zero{}; CHECK_EQ(zero, Vec2::zero()); CHECK_EQ(zero.x, Fixed::from_int(0)); CHECK_EQ(zero.y, Fixed::from_int(0)); const Vec2 point = Vec2::from_ints(3, -4); CHECK_EQ(point.x, Fixed::from_int(3)); CHECK_EQ(point.y, Fixed::from_int(-4)); } TEST(vec2_arithmetic_and_negation) { const Vec2 a = Vec2::from_ints(2, -3); const Vec2 b = {Fixed::ratio(1, 2), Fixed::from_int(5)}; CHECK_EQ(-a, Vec2::from_ints(-2, 3)); CHECK_EQ(a + b, (Vec2{Fixed::ratio(5, 2), Fixed::from_int(2)})); CHECK_EQ(a - b, (Vec2{Fixed::ratio(3, 2), Fixed::from_int(-8)})); Vec2 acc = a; acc += b; CHECK_EQ(acc, a + b); acc -= b; CHECK_EQ(acc, a); } TEST(vec2_scalar_multiplication) { const Vec2 v = {Fixed::ratio(3, 2), Fixed::from_int(-4)}; const Fixed half = Fixed::ratio(1, 2); const Vec2 expected = {Fixed::ratio(3, 4), Fixed::from_int(-2)}; CHECK_EQ(v * half, expected); CHECK_EQ(half * v, expected); Vec2 scaled = v; scaled *= half; CHECK_EQ(scaled, expected); } TEST(vec2_lerp_interpolates_both_components) { const Vec2 a = Vec2::from_ints(10, -20); const Vec2 b = Vec2::from_ints(30, 20); CHECK_EQ(lerp(a, b, Fixed::from_int(0)), a); CHECK_EQ(lerp(a, b, Fixed::from_int(1)), b); CHECK_EQ(lerp(a, b, Fixed::ratio(1, 2)), Vec2::from_ints(20, 0)); CHECK_EQ(lerp(a, b, Fixed::ratio(1, 4)), Vec2::from_ints(15, -10)); } TEST(vec2_is_constexpr) { constexpr Vec2 a = Vec2::from_ints(1, 2); constexpr Vec2 b = Vec2::from_ints(5, 6); constexpr Vec2 mid = lerp(a, b, Fixed::ratio(1, 2)); static_assert(mid == Vec2::from_ints(3, 4)); CHECK_EQ(mid, Vec2::from_ints(3, 4)); }