aboutsummaryrefslogtreecommitdiff
path: root/timer/test/timer_test.c
blob: a220ead59804dce89168f7b698ebf757ca9bf3a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include "timer.h"

#include <stdio.h>

#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;
}