diff options
Diffstat (limited to 'simloop/test/simloop_test.c')
| -rw-r--r-- | simloop/test/simloop_test.c | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/simloop/test/simloop_test.c b/simloop/test/simloop_test.c new file mode 100644 index 0000000..3f2aa46 --- /dev/null +++ b/simloop/test/simloop_test.c | |||
| @@ -0,0 +1,60 @@ | |||
| 1 | #include <simloop.h> | ||
| 2 | |||
| 3 | #include <test.h> | ||
| 4 | #include <timer.h> | ||
| 5 | |||
| 6 | /// An initial render should always trigger on frame 0. | ||
| 7 | TEST_CASE(simloop_initial_render) { | ||
| 8 | constexpr int UPDATE_FPS = 10; | ||
| 9 | |||
| 10 | Timer timer = {}; | ||
| 11 | Simloop simloop = simloop_make(&(SimloopArgs){ | ||
| 12 | .update_fps = UPDATE_FPS, .max_render_fps = 0, .timer = &timer}); | ||
| 13 | SimloopOut simout; | ||
| 14 | |||
| 15 | simloop_update(&simloop, &simout); | ||
| 16 | TEST_TRUE(simout.should_render); | ||
| 17 | } | ||
| 18 | |||
| 19 | /// A simulation loop with no render frame cap. | ||
| 20 | TEST_CASE(simloop_test_no_render_frame_cap) { | ||
| 21 | constexpr int UPDATE_FPS = 10; | ||
| 22 | const time_delta STEP = sec_to_time_delta(1); | ||
| 23 | const time_delta SIM_TIME_SEC = sec_to_time_delta(30); | ||
| 24 | |||
| 25 | Timer timer = {}; | ||
| 26 | Simloop simloop = simloop_make(&(SimloopArgs){ | ||
| 27 | .update_fps = UPDATE_FPS, .max_render_fps = 0, .timer = &timer}); | ||
| 28 | SimloopOut simout; | ||
| 29 | |||
| 30 | for (time_delta t = STEP; t < SIM_TIME_SEC; t += STEP) { | ||
| 31 | timer_advance(&timer, t); | ||
| 32 | simloop_update(&simloop, &simout); | ||
| 33 | TEST_TRUE(simout.should_render); | ||
| 34 | TEST_EQUAL(simout.updates_pending, UPDATE_FPS); | ||
| 35 | } | ||
| 36 | } | ||
| 37 | |||
| 38 | /// A simulation loop with a render frame cap. | ||
| 39 | TEST_CASE(simloop_test_with_render_frame_cap) { | ||
| 40 | constexpr int UPDATE_FPS = 10; | ||
| 41 | constexpr int RENDER_FPS = 5; | ||
| 42 | const time_delta STEP = sec_to_time_delta(0.1); | ||
| 43 | const time_delta SIM_TIME_SEC = sec_to_time_delta(30); | ||
| 44 | const time_delta EXPECT_UPDATE = sec_to_time_delta(1.0 / (double)UPDATE_FPS); | ||
| 45 | const time_delta EXPECT_RENDER = sec_to_time_delta(1.0 / (double)RENDER_FPS); | ||
| 46 | |||
| 47 | Timer timer = {}; | ||
| 48 | Simloop simloop = simloop_make(&(SimloopArgs){ | ||
| 49 | .update_fps = UPDATE_FPS, .max_render_fps = 0, .timer = &timer}); | ||
| 50 | SimloopOut simout; | ||
| 51 | |||
| 52 | for (time_delta t = STEP; t < SIM_TIME_SEC; t += STEP) { | ||
| 53 | timer_advance(&timer, t); | ||
| 54 | simloop_update(&simloop, &simout); | ||
| 55 | TEST_TRUE(((STEP % EXPECT_RENDER) == 0) ? simout.should_render : true); | ||
| 56 | TEST_TRUE(((STEP % EXPECT_UPDATE) == 0) ? simout.updates_pending : true); | ||
| 57 | } | ||
| 58 | } | ||
| 59 | |||
| 60 | int main() { return 0; } | ||
