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