]> git.localhorst.tv Git - gong.git/blob - tst/app/TimerTest.cpp
code, assets, and other stuff stolen from blank
[gong.git] / tst / app / TimerTest.cpp
1 #include "TimerTest.hpp"
2
3 #include "app/IntervalTimer.hpp"
4
5 #include <limits>
6
7 CPPUNIT_TEST_SUITE_REGISTRATION(gong::app::test::TimerTest);
8
9
10 namespace gong {
11 namespace app {
12 namespace test {
13
14 void TimerTest::setUp() {
15 }
16
17 void TimerTest::tearDown() {
18 }
19
20
21 void TimerTest::testCoarseTimer() {
22         CoarseTimer timer(50);
23         CPPUNIT_ASSERT_MESSAGE(
24                 "fresh coarse timer is running",
25                 !timer.Running()
26         );
27         CPPUNIT_ASSERT_MESSAGE(
28                 "fresh coarse timer hit",
29                 !timer.Hit()
30         );
31         CPPUNIT_ASSERT_EQUAL_MESSAGE(
32                 "fresh coarse timer with non-zero elapsed time",
33                 0, timer.Elapsed()
34         );
35         CPPUNIT_ASSERT_EQUAL_MESSAGE(
36                 "fresh coarse timer at non-zero iteration",
37                 0, timer.Iteration()
38         );
39
40         timer.Start();
41         CPPUNIT_ASSERT_MESSAGE(
42                 "startet coarse timer is not running",
43                 timer.Running()
44         );
45         CPPUNIT_ASSERT_MESSAGE(
46                 "started coarse timer hit without update",
47                 !timer.Hit()
48         );
49         CPPUNIT_ASSERT_EQUAL_MESSAGE(
50                 "started, but not updated coarse timer with non-zero elapsed time",
51                 0, timer.Elapsed()
52         );
53         CPPUNIT_ASSERT_EQUAL_MESSAGE(
54                 "started, but not updated coarse timer at non-zero iteration",
55                 0, timer.Iteration()
56         );
57
58         timer.Update(25);
59         CPPUNIT_ASSERT_MESSAGE(
60                 "updated coarse timer is not running",
61                 timer.Running()
62         );
63         CPPUNIT_ASSERT_MESSAGE(
64                 "coarse timer hit after update, but before it should",
65                 !timer.Hit()
66         );
67         CPPUNIT_ASSERT_EQUAL_MESSAGE(
68                 "wrong elapsed time on updated coarse timer",
69                 25, timer.Elapsed()
70         );
71         CPPUNIT_ASSERT_EQUAL_MESSAGE(
72                 "wrong iteration on updated coarse timer",
73                 0, timer.Iteration()
74         );
75
76         timer.Update(25);
77         CPPUNIT_ASSERT_MESSAGE(
78                 "coarse timer not hit after updating to its exact interval time",
79                 timer.Hit()
80         );
81         CPPUNIT_ASSERT_EQUAL_MESSAGE(
82                 "wrong elapsed time on updated coarse timer",
83                 50, timer.Elapsed()
84         );
85         CPPUNIT_ASSERT_EQUAL_MESSAGE(
86                 "wrong iteration on updated coarse timer at exact interval time",
87                 1, timer.Iteration()
88         );
89
90         timer.Update(49);
91         CPPUNIT_ASSERT_MESSAGE(
92                 "coarse timer hit after updating from exact interval time to just before the next",
93                 !timer.Hit()
94         );
95         CPPUNIT_ASSERT_EQUAL_MESSAGE(
96                 "wrong elapsed time on updated coarse timer",
97                 99, timer.Elapsed()
98         );
99         CPPUNIT_ASSERT_EQUAL_MESSAGE(
100                 "wrong iteration after updating coarse timer from exact interval time to just before the next",
101                 1, timer.Iteration()
102         );
103
104         timer.Update(2);
105         CPPUNIT_ASSERT_MESSAGE(
106                 "coarse timer not hit after updating across interval time boundary",
107                 timer.Hit()
108         );
109         CPPUNIT_ASSERT_EQUAL_MESSAGE(
110                 "wrong elapsed time on updated coarse timer",
111                 101, timer.Elapsed()
112         );
113         CPPUNIT_ASSERT_EQUAL_MESSAGE(
114                 "wrong iteration after updating across interval time boundary",
115                 2, timer.Iteration()
116         );
117
118         timer.Stop();
119         CPPUNIT_ASSERT_MESSAGE(
120                 "stopped coarse timer is running",
121                 !timer.Running()
122         );
123         CPPUNIT_ASSERT_MESSAGE(
124                 "stopped coarse timer hit",
125                 !timer.Hit()
126         );
127         CPPUNIT_ASSERT_EQUAL_MESSAGE(
128                 "stopped coarse timer has non-zero elapsed time",
129                 0, timer.Elapsed()
130         );
131         CPPUNIT_ASSERT_EQUAL_MESSAGE(
132                 "stopped coarse timer at non-zero iteration",
133                 0, timer.Iteration()
134         );
135 }
136
137 void TimerTest::testFineTimer() {
138         FineTimer timer(0.5f);
139         CPPUNIT_ASSERT_MESSAGE(
140                 "fresh fine timer is running",
141                 !timer.Running()
142         );
143         CPPUNIT_ASSERT_MESSAGE(
144                 "fresh fine timer hit",
145                 !timer.Hit()
146         );
147         CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
148                 "fresh fine timer with non-zero elapsed time",
149                 0.0f, timer.Elapsed(), std::numeric_limits<float>::epsilon()
150         );
151         CPPUNIT_ASSERT_EQUAL_MESSAGE(
152                 "fresh fine timer at non-zero iteration",
153                 0, timer.Iteration()
154         );
155
156         timer.Start();
157         CPPUNIT_ASSERT_MESSAGE(
158                 "startet fine timer is not running",
159                 timer.Running()
160         );
161         CPPUNIT_ASSERT_MESSAGE(
162                 "started fine timer hit without update",
163                 !timer.Hit()
164         );
165         CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
166                 "started, but not updated fine timer with non-zero elapsed time",
167                 0.0f, timer.Elapsed(), std::numeric_limits<float>::epsilon()
168         );
169         CPPUNIT_ASSERT_EQUAL_MESSAGE(
170                 "started, but not updated fine timer at non-zero iteration",
171                 0, timer.Iteration()
172         );
173
174         timer.Update(0.25f);
175         CPPUNIT_ASSERT_MESSAGE(
176                 "updated fine timer is not running",
177                 timer.Running()
178         );
179         CPPUNIT_ASSERT_MESSAGE(
180                 "fine timer hit after update, but before it should",
181                 !timer.Hit()
182         );
183         CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
184                 "wrong elapsed time on updated fine timer",
185                 0.25f, timer.Elapsed(), std::numeric_limits<float>::epsilon()
186         );
187         CPPUNIT_ASSERT_EQUAL_MESSAGE(
188                 "wrong iteration on updated fine timer",
189                 0, timer.Iteration()
190         );
191
192         timer.Update(0.25f);
193         CPPUNIT_ASSERT_MESSAGE(
194                 "fine timer not hit after updating to its exact interval time",
195                 timer.Hit()
196         );
197         CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
198                 "wrong elapsed time on updated fine timer",
199                 0.5f, timer.Elapsed(), std::numeric_limits<float>::epsilon()
200         );
201         CPPUNIT_ASSERT_EQUAL_MESSAGE(
202                 "wrong iteration on updated fine timer at exact interval time",
203                 1, timer.Iteration()
204         );
205
206         timer.Update(0.49f);
207         CPPUNIT_ASSERT_MESSAGE(
208                 "fine timer hit after updating from exact interval time to just before the next",
209                 !timer.Hit()
210         );
211         CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
212                 "wrong elapsed time on updated fine timer",
213                 0.99f, timer.Elapsed(), std::numeric_limits<float>::epsilon()
214         );
215         CPPUNIT_ASSERT_EQUAL_MESSAGE(
216                 "wrong iteration after updating fine timer from exact interval time to just before the next",
217                 1, timer.Iteration()
218         );
219
220         timer.Update(0.02f);
221         CPPUNIT_ASSERT_MESSAGE(
222                 "fine timer not hit after updating across interval time boundary",
223                 timer.Hit()
224         );
225         CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
226                 "wrong elapsed time on updated fine timer",
227                 1.01f, timer.Elapsed(), std::numeric_limits<float>::epsilon()
228         );
229         CPPUNIT_ASSERT_EQUAL_MESSAGE(
230                 "wrong iteration after updating across interval time boundary",
231                 2, timer.Iteration()
232         );
233
234         timer.Stop();
235         CPPUNIT_ASSERT_MESSAGE(
236                 "stopped fine timer is running",
237                 !timer.Running()
238         );
239         CPPUNIT_ASSERT_MESSAGE(
240                 "stopped fine timer hit",
241                 !timer.Hit()
242         );
243         CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
244                 "stopped fine timer has non-zero elapsed time",
245                 0.0f, timer.Elapsed(), std::numeric_limits<float>::epsilon()
246         );
247         CPPUNIT_ASSERT_EQUAL_MESSAGE(
248                 "stopped fine timer at non-zero iteration",
249                 0, timer.Iteration()
250         );
251 }
252
253 }
254 }
255 }