diff options
Diffstat (limited to 'timer/test/timer_test.c')
-rw-r--r-- | timer/test/timer_test.c | 95 |
1 files changed, 95 insertions, 0 deletions
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 @@ | |||
1 | #include "timer.h" | ||
2 | |||
3 | #include <stdio.h> | ||
4 | |||
5 | #include "test.h" | ||
6 | |||
7 | // Sleep puts the calling thread to sleep for a time >= than the given time. | ||
8 | TEST_CASE(sleep) { | ||
9 | const double sleep_time_sec = 0.1; | ||
10 | |||
11 | const time_point start = time_now(); | ||
12 | time_sleep(sec_to_time_delta(sleep_time_sec)); | ||
13 | const time_point end = time_now(); | ||
14 | |||
15 | TEST_TRUE(time_delta_to_sec(time_diff(start, end)) >= sleep_time_sec); | ||
16 | } | ||
17 | |||
18 | // The timer starts as soon as it is created. | ||
19 | TEST_CASE(start_on_new) { | ||
20 | const time_point test_start_time = time_now(); | ||
21 | Timer timer = timer_make(); | ||
22 | TEST_TRUE(time_delta_to_sec(time_diff(test_start_time, timer.start_time)) >= | ||
23 | 0.0); | ||
24 | } | ||
25 | |||
26 | // Tick updates the timer's last tick time. | ||
27 | TEST_CASE(tick_updates_last_tick_time) { | ||
28 | const double sleep_time_sec = 0.1; | ||
29 | |||
30 | Timer timer = timer_make(); | ||
31 | time_sleep(sec_to_time_delta(sleep_time_sec)); | ||
32 | timer_tick(&timer); | ||
33 | |||
34 | TEST_TRUE(time_delta_to_sec(time_diff(timer.start_time, timer.last_tick)) >= | ||
35 | sleep_time_sec); | ||
36 | } | ||
37 | |||
38 | // Tick updates the timer's delta time. | ||
39 | TEST_CASE(tick_updates_delta_time) { | ||
40 | const double sleep_time_sec = 0.1; | ||
41 | |||
42 | Timer timer = timer_make(); | ||
43 | time_sleep(sec_to_time_delta(sleep_time_sec)); | ||
44 | timer_tick(&timer); | ||
45 | |||
46 | TEST_TRUE(time_delta_to_sec(timer.delta_time) >= sleep_time_sec); | ||
47 | } | ||
48 | |||
49 | // Tick leaves the timer's start time unchanged. | ||
50 | TEST_CASE(tick_does_not_change_start_time) { | ||
51 | Timer timer = timer_make(); | ||
52 | const time_point start_time = timer.start_time; | ||
53 | time_sleep(sec_to_time_delta(0.1)); | ||
54 | timer_tick(&timer); | ||
55 | TEST_TRUE(time_delta_to_sec(time_diff(start_time, timer.start_time)) == 0.0); | ||
56 | } | ||
57 | |||
58 | // Start starts/restarts the timer and updates the timer's start time. | ||
59 | TEST_CASE(start_restarts_start_time) { | ||
60 | const double sleep_time_seconds = 0.1; | ||
61 | Timer timer = timer_make(); | ||
62 | const time_point start_time = timer.start_time; | ||
63 | time_sleep(sec_to_time_delta(sleep_time_seconds)); | ||
64 | timer_start(&timer); | ||
65 | TEST_TRUE(time_delta_to_sec(time_diff(start_time, timer.start_time)) >= | ||
66 | sleep_time_seconds); | ||
67 | } | ||
68 | |||
69 | // Count the number of hundred-seconds in a second. | ||
70 | TEST_CASE(count) { | ||
71 | Timer timer = timer_make(); | ||
72 | |||
73 | int hundred_seconds = 0; | ||
74 | const time_point start = timer.start_time; | ||
75 | { | ||
76 | while (time_delta_to_sec(timer.running_time) <= 1.0) { | ||
77 | hundred_seconds++; | ||
78 | time_sleep(sec_to_time_delta(0.1)); | ||
79 | timer_tick(&timer); | ||
80 | TEST_TRUE(time_delta_to_sec(timer.delta_time) >= 0.1); | ||
81 | } | ||
82 | } | ||
83 | const time_point end = time_now(); | ||
84 | |||
85 | const double time_elapsed = time_delta_to_sec(time_diff(start, end)); | ||
86 | |||
87 | TEST_EQUAL(hundred_seconds, 10); | ||
88 | TEST_TRUE(time_elapsed >= 1.0); | ||
89 | } | ||
90 | |||
91 | int main(int argc, const char** argv) { | ||
92 | (void)argc; // Unused. | ||
93 | printf("Usage: %s --unittest\n", argv[0]); | ||
94 | return 0; | ||
95 | } | ||