diff options
Diffstat (limited to 'simloop/test/simloop_test.c')
| -rw-r--r-- | simloop/test/simloop_test.c | 72 |
1 files changed, 52 insertions, 20 deletions
diff --git a/simloop/test/simloop_test.c b/simloop/test/simloop_test.c index 3f2aa46..c79ee32 100644 --- a/simloop/test/simloop_test.c +++ b/simloop/test/simloop_test.c | |||
| @@ -3,57 +3,89 @@ | |||
| 3 | #include <test.h> | 3 | #include <test.h> |
| 4 | #include <timer.h> | 4 | #include <timer.h> |
| 5 | 5 | ||
| 6 | /// An initial render should always trigger on frame 0. | 6 | /// At time/frame 0: |
| 7 | /// 1. An initial render is always triggered. | ||
| 8 | /// 2. No update is triggered (not enough time passed). | ||
| 7 | TEST_CASE(simloop_initial_render) { | 9 | TEST_CASE(simloop_initial_render) { |
| 8 | constexpr int UPDATE_FPS = 10; | 10 | Timer timer = {}; |
| 11 | Simloop simloop = simloop_make( | ||
| 12 | &(SimloopArgs){.update_fps = 10, .max_render_fps = 0, .timer = &timer}); | ||
| 13 | SimloopOut simout; | ||
| 9 | 14 | ||
| 10 | Timer timer = {}; | 15 | simloop_update(&simloop, &simout); |
| 11 | Simloop simloop = simloop_make(&(SimloopArgs){ | 16 | |
| 12 | .update_fps = UPDATE_FPS, .max_render_fps = 0, .timer = &timer}); | 17 | TEST_TRUE(simout.should_render); |
| 18 | TEST_TRUE(!simout.should_update); | ||
| 19 | TEST_EQUAL(simout.frame, 0); | ||
| 20 | } | ||
| 21 | |||
| 22 | /// The initial render is not re-triggered if there is a render frame rate cap | ||
| 23 | /// and time does not advance. | ||
| 24 | TEST_CASE(simloop_initial_render_not_retriggered) { | ||
| 25 | Timer timer = {}; | ||
| 26 | Simloop simloop = simloop_make( | ||
| 27 | &(SimloopArgs){.update_fps = 10, .max_render_fps = 10, .timer = &timer}); | ||
| 13 | SimloopOut simout; | 28 | SimloopOut simout; |
| 14 | 29 | ||
| 15 | simloop_update(&simloop, &simout); | 30 | simloop_update(&simloop, &simout); |
| 31 | |||
| 16 | TEST_TRUE(simout.should_render); | 32 | TEST_TRUE(simout.should_render); |
| 33 | TEST_TRUE(!simout.should_update); | ||
| 34 | TEST_EQUAL(simout.frame, 0); | ||
| 35 | |||
| 36 | for (int i = 0; i < 10; i++) { | ||
| 37 | // Note that time does not advance. | ||
| 38 | simloop_update(&simloop, &simout); | ||
| 39 | TEST_TRUE(!simout.should_render); | ||
| 40 | TEST_TRUE(!simout.should_update); | ||
| 41 | TEST_EQUAL(simout.frame, 0); | ||
| 42 | } | ||
| 17 | } | 43 | } |
| 18 | 44 | ||
| 19 | /// A simulation loop with no render frame cap. | 45 | /// A simulation loop with no render frame cap: |
| 20 | TEST_CASE(simloop_test_no_render_frame_cap) { | 46 | /// 1. Updates based on the desired update frame rate. |
| 21 | constexpr int UPDATE_FPS = 10; | 47 | /// 2. Renders at every loop. |
| 22 | const time_delta STEP = sec_to_time_delta(1); | 48 | TEST_CASE(simloop_no_render_frame_cap) { |
| 23 | const time_delta SIM_TIME_SEC = sec_to_time_delta(30); | 49 | constexpr int UPDATE_FPS = 10; |
| 50 | const time_delta EXPECT_UPDATE = sec_to_time_delta(1.0 / (double)UPDATE_FPS); | ||
| 51 | const time_delta STEP = sec_to_time_delta(1); | ||
| 52 | const time_delta SIM_TIME_SEC = sec_to_time_delta(30); | ||
| 24 | 53 | ||
| 25 | Timer timer = {}; | 54 | Timer timer = {}; |
| 26 | Simloop simloop = simloop_make(&(SimloopArgs){ | 55 | Simloop simloop = simloop_make(&(SimloopArgs){ |
| 27 | .update_fps = UPDATE_FPS, .max_render_fps = 0, .timer = &timer}); | 56 | .update_fps = UPDATE_FPS, .max_render_fps = 0, .timer = &timer}); |
| 28 | SimloopOut simout; | 57 | SimloopOut simout; |
| 29 | 58 | ||
| 30 | for (time_delta t = STEP; t < SIM_TIME_SEC; t += STEP) { | 59 | for (time_delta t = 0; t < SIM_TIME_SEC; t += STEP) { |
| 31 | timer_advance(&timer, t); | 60 | timer_advance(&timer, t); |
| 32 | simloop_update(&simloop, &simout); | 61 | simloop_update(&simloop, &simout); |
| 33 | TEST_TRUE(simout.should_render); | 62 | TEST_TRUE(simout.should_render); |
| 34 | TEST_EQUAL(simout.updates_pending, UPDATE_FPS); | 63 | TEST_EQUAL((t > 0) && ((t % EXPECT_UPDATE) == 0), simout.should_update); |
| 35 | } | 64 | } |
| 36 | } | 65 | } |
| 37 | 66 | ||
| 38 | /// A simulation loop with a render frame cap. | 67 | /// A simulation loop with a render frame cap: |
| 39 | TEST_CASE(simloop_test_with_render_frame_cap) { | 68 | /// 1. Updates based on the desired update frame rate. |
| 69 | /// 2. Renders based on the desired render frame rate. | ||
| 70 | TEST_CASE(simloop_with_render_frame_cap) { | ||
| 40 | constexpr int UPDATE_FPS = 10; | 71 | constexpr int UPDATE_FPS = 10; |
| 41 | constexpr int RENDER_FPS = 5; | 72 | 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); | 73 | 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); | 74 | const time_delta EXPECT_RENDER = sec_to_time_delta(1.0 / (double)RENDER_FPS); |
| 75 | const time_delta STEP = sec_to_time_delta(0.1); | ||
| 76 | const time_delta SIM_TIME_SEC = sec_to_time_delta(30); | ||
| 46 | 77 | ||
| 47 | Timer timer = {}; | 78 | Timer timer = {}; |
| 48 | Simloop simloop = simloop_make(&(SimloopArgs){ | 79 | Simloop simloop = simloop_make(&(SimloopArgs){ |
| 49 | .update_fps = UPDATE_FPS, .max_render_fps = 0, .timer = &timer}); | 80 | .update_fps = UPDATE_FPS, .max_render_fps = RENDER_FPS, .timer = &timer}); |
| 50 | SimloopOut simout; | 81 | SimloopOut simout; |
| 51 | 82 | ||
| 52 | for (time_delta t = STEP; t < SIM_TIME_SEC; t += STEP) { | 83 | for (time_delta t = 0; t < SIM_TIME_SEC; t += STEP) { |
| 53 | timer_advance(&timer, t); | 84 | timer_advance(&timer, t); |
| 54 | simloop_update(&simloop, &simout); | 85 | simloop_update(&simloop, &simout); |
| 55 | TEST_TRUE(((STEP % EXPECT_RENDER) == 0) ? simout.should_render : true); | 86 | // Also expecting initial render at t=0. |
| 56 | TEST_TRUE(((STEP % EXPECT_UPDATE) == 0) ? simout.updates_pending : true); | 87 | TEST_EQUAL((t % EXPECT_RENDER) == 0, simout.should_render); |
| 88 | TEST_EQUAL((t > 0) && ((t % EXPECT_UPDATE) == 0), simout.should_update); | ||
| 57 | } | 89 | } |
| 58 | } | 90 | } |
| 59 | 91 | ||
