File: ClockTest.cpp

package info (click to toggle)
ola 0.10.8.nojsmin-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 16,656 kB
  • sloc: cpp: 132,274; python: 14,082; javascript: 6,774; sh: 4,616; ansic: 2,189; java: 518; xml: 253; makefile: 183
file content (325 lines) | stat: -rw-r--r-- 8,820 bytes parent folder | download | duplicates (3)
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/*
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 *
 * ClockTest.cpp
 * Unittest for String functions.
 * Copyright (C) 2005 Simon Newton
 */

#include <cppunit/extensions/HelperMacros.h>
#include <unistd.h>
#include <string>

#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#define VC_EXTRALEAN
#include <ola/win/CleanWindows.h>
#endif  // _WIN32

#include "ola/Clock.h"
#include "ola/testing/TestUtils.h"

class ClockTest: public CppUnit::TestFixture {
  CPPUNIT_TEST_SUITE(ClockTest);
  CPPUNIT_TEST(testTimeStamp);
  CPPUNIT_TEST(testTimeInterval);
  CPPUNIT_TEST(testTimeIntervalMultiplication);
  CPPUNIT_TEST(testClockMonotonic);
  CPPUNIT_TEST(testClockRealTime);
  CPPUNIT_TEST(testClockCurrentTime);
  CPPUNIT_TEST(testMockClockMonotonic);
  CPPUNIT_TEST(testMockClockRealTime);
  CPPUNIT_TEST(testMockClockCurrentTime);
  CPPUNIT_TEST_SUITE_END();

 public:
    void testTimeStamp();
    void testTimeInterval();
    void testTimeIntervalMultiplication();
    void testClockMonotonic();
    void testClockRealTime();
    void testClockCurrentTime();
    void testMockClockMonotonic();
    void testMockClockRealTime();
    void testMockClockCurrentTime();
};


CPPUNIT_TEST_SUITE_REGISTRATION(ClockTest);

using ola::Clock;
using ola::MockClock;
using ola::TimeStamp;
using ola::TimeInterval;
using std::string;

/*
 * Test the TimeStamp class
 */
void ClockTest::testTimeStamp() {
  Clock clock;
  TimeStamp timestamp, timestamp2;
  OLA_ASSERT_FALSE(timestamp.IsSet());
  OLA_ASSERT_FALSE(timestamp2.IsSet());

  // test assignment & copy constructor
  clock.CurrentMonotonicTime(&timestamp);
  OLA_ASSERT_TRUE(timestamp.IsSet());
  timestamp2 = timestamp;
  OLA_ASSERT_TRUE(timestamp2.IsSet());
  TimeStamp timestamp3(timestamp);
  OLA_ASSERT_TRUE(timestamp3.IsSet());
  OLA_ASSERT_EQ(timestamp, timestamp2);
  OLA_ASSERT_EQ(timestamp, timestamp3);

  // test timespec assignment
  TimeStamp timestamp4;
  struct timespec ts1;
  const time_t test_secs = 1604140280;
  const int test_nsecs = 42000;
  ts1.tv_sec = test_secs;
  ts1.tv_nsec = test_nsecs;
  timestamp4 = ts1;
  OLA_ASSERT_TRUE(timestamp4.IsSet());
  OLA_ASSERT_EQ(test_secs, timestamp4.Seconds());
  OLA_ASSERT_EQ(test_nsecs/1000, timestamp4.MicroSeconds());
  // test timeval assignment
  TimeStamp timestamp5;
  struct timeval tv1;
  tv1.tv_sec = test_secs;
  tv1.tv_usec = test_nsecs/1000;
  timestamp5 = tv1;
  OLA_ASSERT_TRUE(timestamp5.IsSet());
  OLA_ASSERT_EQ(test_secs, timestamp5.Seconds());
  OLA_ASSERT_EQ(test_nsecs/1000, timestamp5.MicroSeconds());
  // test timespec copy constructor
  TimeStamp timestamp6(ts1);
  OLA_ASSERT_TRUE(timestamp6.IsSet());
  OLA_ASSERT_EQ(test_secs, timestamp6.Seconds());
  OLA_ASSERT_EQ(test_nsecs/1000, timestamp6.MicroSeconds());
  // test timeval copy constructor
  TimeStamp timestamp7(tv1);
  OLA_ASSERT_TRUE(timestamp7.IsSet());
  OLA_ASSERT_EQ(test_secs, timestamp7.Seconds());
  OLA_ASSERT_EQ(test_nsecs/1000, timestamp7.MicroSeconds());


  // test equalities
  // Windows only seems to have ms resolution, to make the tests pass we need
  // to sleep here; XP only has 16ms resolution, so sleep a bit longer
  usleep(20000);
  clock.CurrentMonotonicTime(&timestamp3);
  OLA_ASSERT_NE(timestamp3, timestamp);
  OLA_ASSERT_GT(timestamp3, timestamp);
  OLA_ASSERT_LT(timestamp, timestamp3);

  // test intervals
  TimeInterval interval = timestamp3 - timestamp;

  // test subtraction / addition
  timestamp2 = timestamp + interval;
  OLA_ASSERT_EQ(timestamp2, timestamp3);
  timestamp2 -= interval;
  OLA_ASSERT_EQ(timestamp, timestamp2);

  // test toString and AsInt
  TimeInterval one_point_five_seconds(1500000);
  OLA_ASSERT_EQ(string("1.500000"), one_point_five_seconds.ToString());
  OLA_ASSERT_EQ(static_cast<int64_t>(1500000),
                       one_point_five_seconds.AsInt());
  OLA_ASSERT_EQ(static_cast<int64_t>(1500),
                       one_point_five_seconds.InMilliSeconds());
}


/*
 * test time intervals
 */
void ClockTest::testTimeInterval() {
  // test intervals
  TimeInterval interval(500000);  // 0.5s
  TimeInterval interval2 = interval;
  TimeInterval interval3(interval);
  OLA_ASSERT_EQ(interval, interval2);
  OLA_ASSERT_EQ(interval, interval3);

  TimeInterval interval4(1, 500000);  // 1.5s
  OLA_ASSERT_NE(interval, interval4);
  OLA_ASSERT_LT(interval, interval4);
  TimeInterval interval5(1, 600000);  // 1.6s
  OLA_ASSERT_NE(interval4, interval5);
  OLA_ASSERT_LT(interval4, interval5);
}


/*
 * Test multiplication of TimeIntervals.
 */
void ClockTest::testTimeIntervalMultiplication() {
  TimeInterval half_second(500000);  // 0.5s
  TimeInterval zero_seconds = half_second * 0;
  OLA_ASSERT_EQ((int64_t) 0, zero_seconds.InMilliSeconds());

  TimeInterval another_half_second = half_second * 1;
  OLA_ASSERT_EQ((int64_t) 500, another_half_second.InMilliSeconds());

  TimeInterval two_seconds = half_second * 4;
  OLA_ASSERT_EQ((int64_t) 2000, two_seconds.InMilliSeconds());

  TimeInterval twenty_seconds = half_second * 40;
  OLA_ASSERT_EQ((int64_t) 20000, twenty_seconds.InMilliSeconds());
}

/**
 * @brief Test the monotonic clock
 */
void ClockTest::testClockMonotonic() {
  Clock clock;
  TimeStamp first;
  clock.CurrentMonotonicTime(&first);
#ifdef _WIN32
  Sleep(1000);
#else
  sleep(1);
#endif  // _WIN32

  TimeStamp second;
  clock.CurrentMonotonicTime(&second);
  OLA_ASSERT_LT(first, second);
}

/**
 * @brief Test the real time clock
 */
void ClockTest::testClockRealTime() {
  Clock clock;
  TimeStamp first;
  clock.CurrentRealTime(&first);
#ifdef _WIN32
  Sleep(1000);
#else
  sleep(1);
#endif  // _WIN32

  TimeStamp second;
  clock.CurrentRealTime(&second);
  OLA_ASSERT_LT(first, second);
}

/**
 * @brief Test the CurrentTime wrapper method
 */
void ClockTest::testClockCurrentTime() {
  Clock clock;
  TimeStamp first;
  clock.CurrentTime(&first);
#ifdef _WIN32
  Sleep(1000);
#else
  sleep(1);
#endif  // _WIN32

  TimeStamp second;
  clock.CurrentTime(&second);
  OLA_ASSERT_LT(first, second);
}

/**
 * @brief Test the mock monotonic clock
 */
void ClockTest::testMockClockMonotonic() {
  MockClock clock;

  TimeStamp first;
  clock.CurrentMonotonicTime(&first);

  TimeInterval one_second(1, 0);
  clock.AdvanceTime(one_second);

  TimeStamp second;
  clock.CurrentMonotonicTime(&second);
  OLA_ASSERT_LT(first, second);
  OLA_ASSERT_TRUE_MSG(
      one_second <= (second - first),
      "This test should not have failed. Was the time changed?");

  TimeInterval ten_point_five_seconds(10, 500000);
  clock.AdvanceTime(10, 500000);

  TimeStamp third;
  clock.CurrentMonotonicTime(&third);
  OLA_ASSERT_LT(second, third);
  OLA_ASSERT_TRUE_MSG(
      ten_point_five_seconds <= (third - second),
      "This test should not have failed. Was the time changed?");
}

/**
 * @brief Test the mock real time clock
 * 
 */
void ClockTest::testMockClockRealTime() {
  MockClock clock;

  TimeStamp first;
  clock.CurrentRealTime(&first);

  TimeInterval one_second(1, 0);
  clock.AdvanceTime(one_second);

  TimeStamp second;
  clock.CurrentRealTime(&second);
  OLA_ASSERT_LT(first, second);
  OLA_ASSERT_TRUE_MSG(
      one_second <= (second - first),
      "This test should not have failed. Was the time changed?");

  TimeInterval ten_point_five_seconds(10, 500000);
  clock.AdvanceTime(10, 500000);

  TimeStamp third;
  clock.CurrentRealTime(&third);
  OLA_ASSERT_LT(second, third);
  OLA_ASSERT_TRUE_MSG(
      ten_point_five_seconds <= (third - second),
      "This test should not have failed. Was the time changed?");
}

/**
 * @brief Test the mock CurrentTime wrapper method
 */
void ClockTest::testMockClockCurrentTime() {
  MockClock clock;

  TimeStamp first;
  clock.CurrentTime(&first);

  TimeInterval one_second(1, 0);
  clock.AdvanceTime(one_second);

  TimeStamp second;
  clock.CurrentTime(&second);
  OLA_ASSERT_LT(first, second);
  OLA_ASSERT_TRUE(one_second <= (second - first));

  TimeInterval ten_point_five_seconds(10, 500000);
  clock.AdvanceTime(10, 500000);

  TimeStamp third;
  clock.CurrentTime(&third);
  OLA_ASSERT_LT(second, third);
  OLA_ASSERT_TRUE(ten_point_five_seconds <= (third - second));
}