From cc96d69ed11c60a782cd8b993d4bdf2ce8c99560 Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Sat, 4 Dec 2021 18:31:18 -0800 Subject: Add timer library. --- timer/test/timer_test.c | 95 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 timer/test/timer_test.c (limited to 'timer/test/timer_test.c') diff --git a/timer/test/timer_test.c b/timer/test/timer_test.c new file mode 100644 index 0000000..a220ead --- /dev/null +++ b/timer/test/timer_test.c @@ -0,0 +1,95 @@ +#include "timer.h" + +#include + +#include "test.h" + +// Sleep puts the calling thread to sleep for a time >= than the given time. +TEST_CASE(sleep) { + const double sleep_time_sec = 0.1; + + const time_point start = time_now(); + time_sleep(sec_to_time_delta(sleep_time_sec)); + const time_point end = time_now(); + + TEST_TRUE(time_delta_to_sec(time_diff(start, end)) >= sleep_time_sec); +} + +// The timer starts as soon as it is created. +TEST_CASE(start_on_new) { + const time_point test_start_time = time_now(); + Timer timer = timer_make(); + TEST_TRUE(time_delta_to_sec(time_diff(test_start_time, timer.start_time)) >= + 0.0); +} + +// Tick updates the timer's last tick time. +TEST_CASE(tick_updates_last_tick_time) { + const double sleep_time_sec = 0.1; + + Timer timer = timer_make(); + time_sleep(sec_to_time_delta(sleep_time_sec)); + timer_tick(&timer); + + TEST_TRUE(time_delta_to_sec(time_diff(timer.start_time, timer.last_tick)) >= + sleep_time_sec); +} + +// Tick updates the timer's delta time. +TEST_CASE(tick_updates_delta_time) { + const double sleep_time_sec = 0.1; + + Timer timer = timer_make(); + time_sleep(sec_to_time_delta(sleep_time_sec)); + timer_tick(&timer); + + TEST_TRUE(time_delta_to_sec(timer.delta_time) >= sleep_time_sec); +} + +// Tick leaves the timer's start time unchanged. +TEST_CASE(tick_does_not_change_start_time) { + Timer timer = timer_make(); + const time_point start_time = timer.start_time; + time_sleep(sec_to_time_delta(0.1)); + timer_tick(&timer); + TEST_TRUE(time_delta_to_sec(time_diff(start_time, timer.start_time)) == 0.0); +} + +// Start starts/restarts the timer and updates the timer's start time. +TEST_CASE(start_restarts_start_time) { + const double sleep_time_seconds = 0.1; + Timer timer = timer_make(); + const time_point start_time = timer.start_time; + time_sleep(sec_to_time_delta(sleep_time_seconds)); + timer_start(&timer); + TEST_TRUE(time_delta_to_sec(time_diff(start_time, timer.start_time)) >= + sleep_time_seconds); +} + +// Count the number of hundred-seconds in a second. +TEST_CASE(count) { + Timer timer = timer_make(); + + int hundred_seconds = 0; + const time_point start = timer.start_time; + { + while (time_delta_to_sec(timer.running_time) <= 1.0) { + hundred_seconds++; + time_sleep(sec_to_time_delta(0.1)); + timer_tick(&timer); + TEST_TRUE(time_delta_to_sec(timer.delta_time) >= 0.1); + } + } + const time_point end = time_now(); + + const double time_elapsed = time_delta_to_sec(time_diff(start, end)); + + TEST_EQUAL(hundred_seconds, 10); + TEST_TRUE(time_elapsed >= 1.0); +} + +int main(int argc, const char** argv) { + (void)argc; // Unused. + printf("Usage: %s --unittest\n", argv[0]); + return 0; +} -- cgit v1.2.3