]> git.localhorst.tv Git - blank.git/blob - tst/app/TimerTest.cpp
unit test for interval timer
[blank.git] / tst / app / TimerTest.cpp
1 #include "TimerTest.hpp"
2
3 #include "app/IntervalTimer.hpp"
4
5 CPPUNIT_TEST_SUITE_REGISTRATION(blank::test::TimerTest);
6
7
8 namespace blank {
9 namespace test {
10
11 void TimerTest::setUp() {
12 }
13
14 void TimerTest::tearDown() {
15 }
16
17
18 void TimerTest::testIntervalTimer() {
19         IntervalTimer timer(50);
20         CPPUNIT_ASSERT_MESSAGE(
21                 "fresh timer is running",
22                 !timer.Running()
23         );
24         CPPUNIT_ASSERT_MESSAGE(
25                 "fresh timer hit",
26                 !timer.Hit()
27         );
28         CPPUNIT_ASSERT_EQUAL_MESSAGE(
29                 "fresh timer with non-zero elapsed time",
30                 0, timer.Elapsed()
31         );
32         CPPUNIT_ASSERT_EQUAL_MESSAGE(
33                 "fresh timer at non-zero iteration",
34                 0, timer.Iteration()
35         );
36
37         timer.Start();
38         CPPUNIT_ASSERT_MESSAGE(
39                 "startet timer is not running",
40                 timer.Running()
41         );
42         CPPUNIT_ASSERT_MESSAGE(
43                 "started timer hit without update",
44                 !timer.Hit()
45         );
46         CPPUNIT_ASSERT_EQUAL_MESSAGE(
47                 "started, but not updated timer with non-zero elapsed time",
48                 0, timer.Elapsed()
49         );
50         CPPUNIT_ASSERT_EQUAL_MESSAGE(
51                 "started, but not updated timer at non-zero iteration",
52                 0, timer.Iteration()
53         );
54
55         timer.Update(25);
56         CPPUNIT_ASSERT_MESSAGE(
57                 "updated timer is not running",
58                 timer.Running()
59         );
60         CPPUNIT_ASSERT_MESSAGE(
61                 "timer hit after update, but before it should",
62                 !timer.Hit()
63         );
64         CPPUNIT_ASSERT_EQUAL_MESSAGE(
65                 "wrong elapsed time on updated timer",
66                 25, timer.Elapsed()
67         );
68         CPPUNIT_ASSERT_EQUAL_MESSAGE(
69                 "wrong iteration on updated timer",
70                 0, timer.Iteration()
71         );
72
73         timer.Update(25);
74         CPPUNIT_ASSERT_MESSAGE(
75                 "timer not hit after updating to its exact interval time",
76                 timer.Hit()
77         );
78         CPPUNIT_ASSERT_EQUAL_MESSAGE(
79                 "wrong elapsed time on updated timer",
80                 50, timer.Elapsed()
81         );
82         CPPUNIT_ASSERT_EQUAL_MESSAGE(
83                 "wrong iteration on updated timer at exact interval time",
84                 1, timer.Iteration()
85         );
86
87         timer.Update(49);
88         CPPUNIT_ASSERT_MESSAGE(
89                 "timer hit after updating from exact interval time to just before the next",
90                 !timer.Hit()
91         );
92         CPPUNIT_ASSERT_EQUAL_MESSAGE(
93                 "wrong elapsed time on updated timer",
94                 99, timer.Elapsed()
95         );
96         CPPUNIT_ASSERT_EQUAL_MESSAGE(
97                 "wrong iteration after updating timer from exact interval time to just before the next",
98                 1, timer.Iteration()
99         );
100
101         timer.Update(2);
102         CPPUNIT_ASSERT_MESSAGE(
103                 "timer not hit after updating across interval time boundary",
104                 timer.Hit()
105         );
106         CPPUNIT_ASSERT_EQUAL_MESSAGE(
107                 "wrong elapsed time on updated timer",
108                 101, timer.Elapsed()
109         );
110         CPPUNIT_ASSERT_EQUAL_MESSAGE(
111                 "wrong iteration after updating across interval time boundary",
112                 2, timer.Iteration()
113         );
114
115         timer.Stop();
116         CPPUNIT_ASSERT_MESSAGE(
117                 "stopped timer is running",
118                 !timer.Running()
119         );
120         CPPUNIT_ASSERT_MESSAGE(
121                 "stopped timer hit",
122                 !timer.Hit()
123         );
124         CPPUNIT_ASSERT_EQUAL_MESSAGE(
125                 "stopped timer has non-zero elapsed time",
126                 0, timer.Elapsed()
127         );
128         CPPUNIT_ASSERT_EQUAL_MESSAGE(
129                 "stopped timer at non-zero iteration",
130                 0, timer.Iteration()
131         );
132 }
133
134 }
135 }