#include "core/fixed.h" #include "check.h" #include "core/types.h" using kumo::Fixed; using kumo::lerp; TEST(fixed_whole_numbers_keep_their_value) { CHECK_EQ(Fixed::from_int(0).raw, 0); CHECK_EQ(Fixed::from_int(1).raw, 65536); CHECK_EQ(Fixed::from_int(-3).raw, -3 * 65536); CHECK_EQ(Fixed::from_int(32767).floor(), 32767); CHECK_EQ(Fixed::from_int(-32768).floor(), -32768); } TEST(fixed_ratio_is_exact_where_the_value_can_be) { CHECK_EQ(Fixed::ratio(1, 2).raw, 32768); CHECK_EQ(Fixed::ratio(3, 2).raw, 98304); CHECK_EQ(Fixed::ratio(-5, 4).raw, -81920); CHECK_EQ(Fixed::ratio(7, 1), Fixed::from_int(7)); } TEST(fixed_ratio_rounds_toward_zero) { CHECK_EQ(Fixed::ratio(1, 3).raw, 21845); CHECK_EQ(Fixed::ratio(-1, 3).raw, -21845); // Three thirds fall short of one by a little, and never go over. CHECK_EQ((Fixed::ratio(1, 3) + Fixed::ratio(1, 3) + Fixed::ratio(1, 3)).raw, 65535); } TEST(fixed_floor_goes_toward_minus_infinity) { CHECK_EQ(Fixed::from_raw(0).floor(), 0); CHECK_EQ(Fixed::from_raw(65535).floor(), 0); CHECK_EQ(Fixed::from_raw(65536).floor(), 1); CHECK_EQ(Fixed::from_raw(-1).floor(), -1); CHECK_EQ(Fixed::from_raw(-65536).floor(), -1); CHECK_EQ(Fixed::from_raw(-65537).floor(), -2); } TEST(fixed_round_takes_halves_up) { CHECK_EQ(Fixed::from_raw(32767).round(), 0); CHECK_EQ(Fixed::from_raw(32768).round(), 1); CHECK_EQ(Fixed::from_raw(-32768).round(), 0); CHECK_EQ(Fixed::from_raw(-32769).round(), -1); CHECK_EQ(Fixed::from_int(-4).round(), -4); } TEST(fixed_addition_and_subtraction_are_exact) { const Fixed a = Fixed::ratio(5, 4); const Fixed b = Fixed::ratio(-7, 8); CHECK_EQ((a + b).raw, 24576); CHECK_EQ((a - b).raw, 139264); CHECK_EQ((a + b) - b, a); CHECK_EQ((-a).raw, -a.raw); Fixed c = a; c += b; c -= b; CHECK_EQ(c, a); } TEST(fixed_multiplication) { CHECK_EQ((Fixed::ratio(3, 2) * Fixed::ratio(5, 2)).raw, 245760); // 3.75 CHECK_EQ((Fixed::ratio(-3, 2) * Fixed::ratio(5, 2)).raw, -245760); CHECK_EQ((Fixed::ratio(-3, 2) * Fixed::ratio(-5, 2)).raw, 245760); CHECK_EQ(Fixed::from_int(123) * Fixed::from_int(-45), Fixed::from_int(-5535)); // By one is the identity, for any value. CHECK_EQ(Fixed::from_raw(12345) * Fixed::from_int(1), Fixed::from_raw(12345)); } TEST(fixed_multiplication_rounds_toward_minus_infinity) { // The smallest step times the smallest step is far below the smallest step. CHECK_EQ((Fixed::from_raw(1) * Fixed::from_raw(1)).raw, 0); CHECK_EQ((Fixed::from_raw(-1) * Fixed::from_raw(1)).raw, -1); } TEST(fixed_multiplication_does_not_lose_bits_to_the_intermediate) { // The raw values of 180 and 180 multiply to about 1.4e14, which does not fit // in 32 bits before the shift: the product is taken in 64 bits. CHECK_EQ(Fixed::from_int(180) * Fixed::from_int(180), Fixed::from_int(32400)); CHECK_EQ(Fixed::from_int(-180) * Fixed::from_int(180), Fixed::from_int(-32400)); } TEST(fixed_comparisons_follow_the_value) { CHECK(Fixed::from_int(-1) < Fixed::from_int(0)); CHECK(Fixed::from_raw(1) > Fixed::from_int(0)); CHECK(Fixed::from_int(2) >= Fixed::from_int(2)); CHECK(Fixed::from_int(2) <= Fixed::from_int(2)); CHECK(Fixed::from_int(2) != Fixed::from_int(3)); CHECK(Fixed{} == Fixed::from_int(0)); } TEST(fixed_lerp_is_exact_at_both_ends) { const Fixed a = Fixed::ratio(-17, 3); const Fixed b = Fixed::ratio(101, 7); CHECK_EQ(lerp(a, b, Fixed{}), a); CHECK_EQ(lerp(a, b, Fixed::from_int(1)), b); } TEST(fixed_lerp_in_between) { CHECK_EQ(lerp(Fixed::from_int(10), Fixed::from_int(20), Fixed::ratio(1, 2)), Fixed::from_int(15)); CHECK_EQ(lerp(Fixed::from_int(20), Fixed::from_int(10), Fixed::ratio(1, 4)), Fixed::ratio(35, 2)); // The same value twice stays where it is, whatever t is. CHECK_EQ(lerp(Fixed::from_int(7), Fixed::from_int(7), Fixed::ratio(2, 3)), Fixed::from_int(7)); } TEST(fixed_is_constexpr) { constexpr Fixed value = Fixed::ratio(3, 2) * Fixed::from_int(2); static_assert(value == Fixed::from_int(3)); CHECK_EQ(value.floor(), 3); }