Kumo

A hand-rolled game engine.

git clone https://git.sentomk.me/Kumo.git

fixed_test.cpp (4090B) · raw


      1 #include "core/fixed.h"
      2 
      3 #include "check.h"
      4 #include "core/types.h"
      5 
      6 using kumo::Fixed;
      7 using kumo::lerp;
      8 
      9 TEST(fixed_whole_numbers_keep_their_value) {
     10   CHECK_EQ(Fixed::from_int(0).raw, 0);
     11   CHECK_EQ(Fixed::from_int(1).raw, 65536);
     12   CHECK_EQ(Fixed::from_int(-3).raw, -3 * 65536);
     13   CHECK_EQ(Fixed::from_int(32767).floor(), 32767);
     14   CHECK_EQ(Fixed::from_int(-32768).floor(), -32768);
     15 }
     16 
     17 TEST(fixed_ratio_is_exact_where_the_value_can_be) {
     18   CHECK_EQ(Fixed::ratio(1, 2).raw, 32768);
     19   CHECK_EQ(Fixed::ratio(3, 2).raw, 98304);
     20   CHECK_EQ(Fixed::ratio(-5, 4).raw, -81920);
     21   CHECK_EQ(Fixed::ratio(7, 1), Fixed::from_int(7));
     22 }
     23 
     24 TEST(fixed_ratio_rounds_toward_zero) {
     25   CHECK_EQ(Fixed::ratio(1, 3).raw, 21845);
     26   CHECK_EQ(Fixed::ratio(-1, 3).raw, -21845);
     27   // Three thirds fall short of one by a little, and never go over.
     28   CHECK_EQ((Fixed::ratio(1, 3) + Fixed::ratio(1, 3) + Fixed::ratio(1, 3)).raw,
     29            65535);
     30 }
     31 
     32 TEST(fixed_floor_goes_toward_minus_infinity) {
     33   CHECK_EQ(Fixed::from_raw(0).floor(), 0);
     34   CHECK_EQ(Fixed::from_raw(65535).floor(), 0);
     35   CHECK_EQ(Fixed::from_raw(65536).floor(), 1);
     36   CHECK_EQ(Fixed::from_raw(-1).floor(), -1);
     37   CHECK_EQ(Fixed::from_raw(-65536).floor(), -1);
     38   CHECK_EQ(Fixed::from_raw(-65537).floor(), -2);
     39 }
     40 
     41 TEST(fixed_round_takes_halves_up) {
     42   CHECK_EQ(Fixed::from_raw(32767).round(), 0);
     43   CHECK_EQ(Fixed::from_raw(32768).round(), 1);
     44   CHECK_EQ(Fixed::from_raw(-32768).round(), 0);
     45   CHECK_EQ(Fixed::from_raw(-32769).round(), -1);
     46   CHECK_EQ(Fixed::from_int(-4).round(), -4);
     47 }
     48 
     49 TEST(fixed_addition_and_subtraction_are_exact) {
     50   const Fixed a = Fixed::ratio(5, 4);
     51   const Fixed b = Fixed::ratio(-7, 8);
     52   CHECK_EQ((a + b).raw, 24576);
     53   CHECK_EQ((a - b).raw, 139264);
     54   CHECK_EQ((a + b) - b, a);
     55   CHECK_EQ((-a).raw, -a.raw);
     56   Fixed c = a;
     57   c += b;
     58   c -= b;
     59   CHECK_EQ(c, a);
     60 }
     61 
     62 TEST(fixed_multiplication) {
     63   CHECK_EQ((Fixed::ratio(3, 2) * Fixed::ratio(5, 2)).raw, 245760);  // 3.75
     64   CHECK_EQ((Fixed::ratio(-3, 2) * Fixed::ratio(5, 2)).raw, -245760);
     65   CHECK_EQ((Fixed::ratio(-3, 2) * Fixed::ratio(-5, 2)).raw, 245760);
     66   CHECK_EQ(Fixed::from_int(123) * Fixed::from_int(-45), Fixed::from_int(-5535));
     67   // By one is the identity, for any value.
     68   CHECK_EQ(Fixed::from_raw(12345) * Fixed::from_int(1), Fixed::from_raw(12345));
     69 }
     70 
     71 TEST(fixed_multiplication_rounds_toward_minus_infinity) {
     72   // The smallest step times the smallest step is far below the smallest step.
     73   CHECK_EQ((Fixed::from_raw(1) * Fixed::from_raw(1)).raw, 0);
     74   CHECK_EQ((Fixed::from_raw(-1) * Fixed::from_raw(1)).raw, -1);
     75 }
     76 
     77 TEST(fixed_multiplication_does_not_lose_bits_to_the_intermediate) {
     78   // The raw values of 180 and 180 multiply to about 1.4e14, which does not fit
     79   // in 32 bits before the shift: the product is taken in 64 bits.
     80   CHECK_EQ(Fixed::from_int(180) * Fixed::from_int(180), Fixed::from_int(32400));
     81   CHECK_EQ(Fixed::from_int(-180) * Fixed::from_int(180),
     82            Fixed::from_int(-32400));
     83 }
     84 
     85 TEST(fixed_comparisons_follow_the_value) {
     86   CHECK(Fixed::from_int(-1) < Fixed::from_int(0));
     87   CHECK(Fixed::from_raw(1) > Fixed::from_int(0));
     88   CHECK(Fixed::from_int(2) >= Fixed::from_int(2));
     89   CHECK(Fixed::from_int(2) <= Fixed::from_int(2));
     90   CHECK(Fixed::from_int(2) != Fixed::from_int(3));
     91   CHECK(Fixed{} == Fixed::from_int(0));
     92 }
     93 
     94 TEST(fixed_lerp_is_exact_at_both_ends) {
     95   const Fixed a = Fixed::ratio(-17, 3);
     96   const Fixed b = Fixed::ratio(101, 7);
     97   CHECK_EQ(lerp(a, b, Fixed{}), a);
     98   CHECK_EQ(lerp(a, b, Fixed::from_int(1)), b);
     99 }
    100 
    101 TEST(fixed_lerp_in_between) {
    102   CHECK_EQ(lerp(Fixed::from_int(10), Fixed::from_int(20), Fixed::ratio(1, 2)),
    103            Fixed::from_int(15));
    104   CHECK_EQ(lerp(Fixed::from_int(20), Fixed::from_int(10), Fixed::ratio(1, 4)),
    105            Fixed::ratio(35, 2));
    106   // The same value twice stays where it is, whatever t is.
    107   CHECK_EQ(lerp(Fixed::from_int(7), Fixed::from_int(7), Fixed::ratio(2, 3)),
    108            Fixed::from_int(7));
    109 }
    110 
    111 TEST(fixed_is_constexpr) {
    112   constexpr Fixed value = Fixed::ratio(3, 2) * Fixed::from_int(2);
    113   static_assert(value == Fixed::from_int(3));
    114   CHECK_EQ(value.floor(), 3);
    115 }