From 9dceaee478545ed8df89722f5c90bb171de100e8 Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Sat, 11 Apr 2026 17:43:31 -0700 Subject: Add modulo checks --- simloop/test/simloop_test.c | 35 ++++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 13 deletions(-) (limited to 'simloop') diff --git a/simloop/test/simloop_test.c b/simloop/test/simloop_test.c index 41f9ac2..e8efd94 100644 --- a/simloop/test/simloop_test.c +++ b/simloop/test/simloop_test.c @@ -65,10 +65,14 @@ TEST_CASE(simloop_initial_render_not_retriggered) { /// 1. Updates based on the desired update frame rate. /// 2. Renders at every loop (provided there are updates). TEST_CASE(simloop_no_render_frame_cap) { - constexpr int UPDATE_FPS = 10; - const time_delta EXPECT_UPDATE = sec_to_time_delta(1.0 / (double)UPDATE_FPS); - const time_delta STEP = sec_to_time_delta(1); - const time_delta SIM_TIME_SEC = sec_to_time_delta(30); + constexpr int UPDATE_FPS = 10; // 100ms delta + const time_delta UPDATE_DDT = sec_to_time_delta(1.0 / (double)UPDATE_FPS); + const time_delta STEP = sec_to_time_delta(1); + const time_delta SIM_TIME_SEC = sec_to_time_delta(30); + + // We need simulation time to be an exact multiple of the desired deltas for + // the modulo comparisons below. + TEST_TRUE((STEP % UPDATE_DDT) == 0); Timer timer = {}; Simloop simloop = @@ -78,7 +82,7 @@ TEST_CASE(simloop_no_render_frame_cap) { for (time_delta t = 0; t < SIM_TIME_SEC; t += STEP) { timer_advance(&timer, t); simloop_update(&simloop, &simout); - const bool expect_update = (t > 0) && ((t % EXPECT_UPDATE) == 0); + const bool expect_update = (t > 0) && ((t % UPDATE_DDT) == 0); // A render is still expected at time 0. TEST_EQUAL(simout.should_render, (t == 0) || expect_update); TEST_EQUAL(simout.should_update, expect_update); @@ -89,12 +93,17 @@ TEST_CASE(simloop_no_render_frame_cap) { /// 1. Updates based on the desired update frame rate. /// 2. Renders based on the desired render frame rate. TEST_CASE(simloop_with_render_frame_cap) { - constexpr int UPDATE_FPS = 10; - constexpr int RENDER_FPS = 5; - const time_delta EXPECT_UPDATE = sec_to_time_delta(1.0 / (double)UPDATE_FPS); - const time_delta EXPECT_RENDER = sec_to_time_delta(1.0 / (double)RENDER_FPS); - const time_delta STEP = sec_to_time_delta(0.1); - const time_delta SIM_TIME_SEC = sec_to_time_delta(30); + constexpr int UPDATE_FPS = 10; // 100ms delta + constexpr int RENDER_FPS = 5; // 200ms delta + const time_delta UPDATE_DDT = sec_to_time_delta(1.0 / (double)UPDATE_FPS); + const time_delta RENDER_DDT = sec_to_time_delta(1.0 / (double)RENDER_FPS); + const time_delta STEP = sec_to_time_delta(0.1); // 100ms + const time_delta SIM_TIME_SEC = sec_to_time_delta(30); + + // We need simulation time to be an exact multiple of the desired deltas for + // the modulo comparisons below. + TEST_TRUE((UPDATE_DDT % STEP) == 0); + TEST_TRUE((RENDER_DDT % STEP) == 0); Timer timer = {}; Simloop simloop = simloop_make(&(SimloopArgs){ @@ -105,8 +114,8 @@ TEST_CASE(simloop_with_render_frame_cap) { timer_advance(&timer, t); simloop_update(&simloop, &simout); // A render is still expected at time 0. - TEST_EQUAL(simout.should_render, (t % EXPECT_RENDER) == 0); - TEST_EQUAL(simout.should_update, (t > 0) && ((t % EXPECT_UPDATE) == 0)); + TEST_EQUAL(simout.should_render, (t % RENDER_DDT) == 0); + TEST_EQUAL(simout.should_update, (t > 0) && ((t % UPDATE_DDT) == 0)); } } -- cgit v1.2.3