Kumo
A hand-rolled game engine.
git clone https://git.sentomk.me/Kumo.git
fixed_step_test.cpp (5664B) · raw
1 #include "core/fixed_step.h" 2 3 #include <algorithm> 4 5 #include "check.h" 6 #include "core/clock.h" 7 #include "core/types.h" 8 9 using kumo::FixedStep; 10 using kumo::ManualClock; 11 using kumo::milliseconds; 12 using kumo::Nanoseconds; 13 using kumo::seconds; 14 15 namespace { 16 17 // A small deterministic random number generator, as in raster_test.cpp: the 18 // standard distributions give different numbers in different libraries. 19 struct Random { 20 uint64_t state; 21 uint32_t next() { 22 state = state * 6364136223846793005ull + 1442695040888963407ull; 23 return static_cast<uint32_t>(state >> 33); 24 } 25 // A value in [low, high]. 26 Nanoseconds between(Nanoseconds low, Nanoseconds high) { 27 return low + static_cast<Nanoseconds>( 28 next() % static_cast<uint64_t>(high - low + 1)); 29 } 30 }; 31 32 // One update of a made up simulation. Any state that depends on every step 33 // in order would do; this one cannot be right by accident. 34 uint64_t update(uint64_t state) { 35 return state * 6364136223846793005ull + 1442695040888963407ull; 36 } 37 38 // Runs a simulation for `total` of time, fed to the stepper in frames of 39 // `frame` (the last one is whatever is left), and returns its final state. 40 uint64_t simulate(Nanoseconds frame, Nanoseconds total) { 41 FixedStep stepper(kumo::step_from_hz(60), milliseconds(250)); 42 uint64_t state = 1; 43 Nanoseconds remaining = total; 44 while (remaining > 0) { 45 const Nanoseconds this_frame = std::min(frame, remaining); 46 remaining -= this_frame; 47 for (int i = stepper.advance(this_frame); i > 0; --i) state = update(state); 48 } 49 return state; 50 } 51 52 } // namespace 53 54 TEST(a_frame_of_exactly_one_step_runs_one_step) { 55 FixedStep stepper(milliseconds(10), milliseconds(100)); 56 CHECK_EQ(stepper.advance(milliseconds(10)), 1); 57 CHECK_EQ(stepper.left_over(), 0); 58 } 59 60 TEST(a_longer_frame_runs_several_steps_and_keeps_the_rest) { 61 FixedStep stepper(milliseconds(10), milliseconds(100)); 62 CHECK_EQ(stepper.advance(milliseconds(25)), 2); 63 CHECK_EQ(stepper.left_over(), milliseconds(5)); 64 CHECK_EQ(stepper.advance(milliseconds(5)), 1); // the 5 ms kept plus these 65 CHECK_EQ(stepper.left_over(), 0); 66 } 67 68 TEST(short_frames_run_nothing_until_they_add_up) { 69 FixedStep stepper(milliseconds(10), milliseconds(100)); 70 CHECK_EQ(stepper.advance(milliseconds(4)), 0); 71 CHECK_EQ(stepper.advance(milliseconds(4)), 0); 72 CHECK_EQ(stepper.advance(milliseconds(4)), 1); 73 CHECK_EQ(stepper.left_over(), milliseconds(2)); 74 } 75 76 TEST(alpha_is_how_far_into_the_next_step_the_time_is) { 77 FixedStep stepper(milliseconds(10), milliseconds(100)); 78 CHECK(stepper.alpha() == 0.0); 79 stepper.advance(milliseconds(15)); 80 CHECK(stepper.alpha() == 0.5); 81 stepper.advance(milliseconds(5)); 82 CHECK(stepper.alpha() == 0.0); 83 } 84 85 TEST(a_negative_frame_counts_as_no_time) { 86 FixedStep stepper(milliseconds(10), milliseconds(100)); 87 CHECK_EQ(stepper.advance(milliseconds(-5)), 0); 88 CHECK_EQ(stepper.left_over(), 0); 89 CHECK_EQ(stepper.advance(milliseconds(10)), 1); 90 } 91 92 TEST(a_very_long_frame_counts_as_the_longest_that_is_believed) { 93 FixedStep stepper(milliseconds(10), milliseconds(100)); 94 CHECK_EQ(stepper.advance(seconds(10)), 10); // 100 ms of steps, not 10 s 95 CHECK_EQ(stepper.left_over(), 0); 96 } 97 98 TEST(a_stall_does_not_start_an_avalanche) { 99 FixedStep stepper(milliseconds(10), milliseconds(100)); 100 stepper.advance(seconds(5)); // the process was stopped for a while 101 // The next frames are ordinary: one step each, not a long catching up. 102 CHECK_EQ(stepper.advance(milliseconds(10)), 1); 103 CHECK_EQ(stepper.advance(milliseconds(10)), 1); 104 } 105 106 TEST(the_totals_count_every_step_that_was_run) { 107 FixedStep stepper(milliseconds(10), milliseconds(100)); 108 stepper.advance(milliseconds(25)); 109 stepper.advance(milliseconds(50)); 110 CHECK_EQ(stepper.total_steps(), 7u); 111 CHECK_EQ(stepper.simulated_time(), milliseconds(70)); 112 } 113 114 TEST(the_same_total_time_gives_the_same_steps_however_it_is_split) { 115 const Nanoseconds total = seconds(10); 116 const Nanoseconds step = kumo::step_from_hz(60); 117 const uint64_t expected = static_cast<uint64_t>(total / step); 118 const Nanoseconds expected_left_over = total - total / step * step; 119 120 Random random = {2024}; 121 int wrong = 0; 122 for (int round = 0; round < 200; ++round) { 123 FixedStep stepper(step, milliseconds(250)); 124 Nanoseconds remaining = total; 125 while (remaining > 0) { 126 // Frames of 1 microsecond to 200 ms, none longer than the maximum. 127 const Nanoseconds frame = std::min( 128 random.between(kumo::microseconds(1), milliseconds(200)), remaining); 129 stepper.advance(frame); 130 remaining -= frame; 131 } 132 if (stepper.total_steps() != expected) ++wrong; 133 if (stepper.left_over() != expected_left_over) ++wrong; 134 } 135 CHECK_EQ(wrong, 0); 136 } 137 138 TEST(a_simulation_comes_out_the_same_at_any_frame_rate) { 139 const Nanoseconds total = seconds(5); 140 const uint64_t at_60 = simulate(kumo::step_from_hz(60), total); 141 CHECK(at_60 != 1); // it did run 142 CHECK(simulate(kumo::step_from_hz(24), total) == at_60); 143 CHECK(simulate(kumo::step_from_hz(30), total) == at_60); 144 CHECK(simulate(kumo::step_from_hz(144), total) == at_60); 145 CHECK(simulate(kumo::step_from_hz(240), total) == at_60); 146 CHECK(simulate(milliseconds(7), total) == at_60); 147 CHECK(simulate(kumo::microseconds(137), total) == at_60); 148 } 149 150 TEST(a_manual_clock_drives_the_loop_exactly) { 151 ManualClock clock; 152 kumo::FrameTimer timer(clock); 153 FixedStep stepper(milliseconds(10), milliseconds(100)); 154 int steps = 0; 155 for (int frame = 0; frame < 100; ++frame) { 156 clock.advance(milliseconds(7)); // 700 ms in all 157 steps += stepper.advance(timer.tick()); 158 } 159 CHECK_EQ(steps, 70); 160 CHECK_EQ(stepper.left_over(), 0); 161 }
