aboutsummaryrefslogtreecommitdiff
path: root/simloop/test/simloop_test.c
diff options
context:
space:
mode:
Diffstat (limited to 'simloop/test/simloop_test.c')
-rw-r--r--simloop/test/simloop_test.c38
1 files changed, 33 insertions, 5 deletions
diff --git a/simloop/test/simloop_test.c b/simloop/test/simloop_test.c
index 61e7dff..bcf9d57 100644
--- a/simloop/test/simloop_test.c
+++ b/simloop/test/simloop_test.c
@@ -222,8 +222,14 @@ TEST_CASE(simloop_determinism) {
222} 222}
223 223
224/// The simulation loop attempts to catch up with the clock in the event of a 224/// The simulation loop attempts to catch up with the clock in the event of a
225/// time spike. This is possible only if the simulation loops with a frequency 225/// time spike.
226/// higher than the requested update frequency given by the update delta time. 226///
227/// Catch-up is possible only if the simulation loops with a frequency higher
228/// than the requested update frequency given by the update delta time.
229///
230/// Catch-up is performed only for sufficiently small time spikes. For large
231/// time spikes, the simulation clock is warped. This test is for the small
232/// time spike case.
227static void simloop_catch_up( 233static void simloop_catch_up(
228 struct test_case_metadata* metadata, int update_ddt_ms, int loop_step_ms, 234 struct test_case_metadata* metadata, int update_ddt_ms, int loop_step_ms,
229 bool expect_catchup) { 235 bool expect_catchup) {
@@ -241,9 +247,10 @@ static void simloop_catch_up(
241 int frames = 0; 247 int frames = 0;
242 248
243 // Simulate a time spike. 249 // Simulate a time spike.
244 // Advance time to t=10s. That is a lag of 10,000ms / 100ms = 100 frames. 250 // Advance time to t=1s. That is a lag of 1,000ms / 100ms = 10 frames.
245 // The simulation now has 20s to catch up. 251 // 10 frames is the maximum allowed catch-up.
246 simloop_time_t dt = time_delta_from_sec(10); 252 // The simulation now has 29s to catch up.
253 simloop_time_t dt = time_delta_from_sec(1);
247 for (simloop_time_t t = dt; t <= SIM_DURATION_SEC;) { 254 for (simloop_time_t t = dt; t <= SIM_DURATION_SEC;) {
248 simloop_update(&simloop, dt, &simout); 255 simloop_update(&simloop, dt, &simout);
249 256
@@ -275,4 +282,25 @@ TEST_CASE(simloop_catch_up_failure) {
275 simloop_catch_up(metadata, UPDATE_DDT_MS, LOOP_DDT_MS, false); 282 simloop_catch_up(metadata, UPDATE_DDT_MS, LOOP_DDT_MS, false);
276} 283}
277 284
285/// This tests the large time spike case, where the simulation clock is warped
286/// to the wall clock.
287TEST_CASE(simloop_warp) {
288 const int UPDATE_FPS = 50;
289 const simloop_time_t UPDATE_DDT =
290 time_delta_from_sec(1.0 / (double)UPDATE_FPS);
291
292 Simloop simloop = simloop_make(&(SimloopArgs){.update_fps = UPDATE_FPS});
293 SimloopOut simout;
294
295 // The maximum allowed catch-up is 10 frames. Simulate a time spike larger
296 // than that.
297 const simloop_time_t TIME_SPIKE = UPDATE_DDT * 20;
298 simloop_update(&simloop, TIME_SPIKE, &simout);
299 TEST_TRUE(simout.should_update); // Warp should still request update.
300
301 // Now "advance" by 0.
302 simloop_update(&simloop, 0, &simout);
303 TEST_TRUE(!simout.should_update); // No more updates after warp.
304}
305
278int main() { return 0; } 306int main() { return 0; }