unix_timer_tests.cc
Go to the documentation of this file.
1 #include "gtest/gtest.h"
2 #include "serial/impl/unix.h"
3 
4 #include <unistd.h>
5 #include <stdlib.h>
6 
8 
9 namespace {
10 
15 TEST(timer_tests, short_intervals) {
16  for (int trial = 0; trial < 100; trial++)
17  {
18  uint32_t ms = rand() % 20;
19  MillisecondTimer mt(ms);
20  usleep(1000 * ms);
21  int32_t r = mt.remaining();
22 
23  // 1ms slush, for the cost of calling usleep.
24  EXPECT_NEAR(r+1, 0, 1);
25  }
26 }
27 
28 TEST(timer_tests, overlapping_long_intervals) {
29  MillisecondTimer* timers[10];
30 
31  // Experimentally determined. Corresponds to the extra time taken by the loops,
32  // the big usleep, and the test infrastructure itself.
33  const int slush_factor = 14;
34 
35  // Set up the timers to each time one second, 1ms apart.
36  for (int t = 0; t < 10; t++)
37  {
38  timers[t] = new MillisecondTimer(1000);
39  usleep(1000);
40  }
41 
42  // Check in on them after 500ms.
43  usleep(500000);
44  for (int t = 0; t < 10; t++)
45  {
46  EXPECT_NEAR(timers[t]->remaining(), 500 - slush_factor + t, 5);
47  }
48 
49  // Check in on them again after another 500ms and free them.
50  usleep(500000);
51  for (int t = 0; t < 10; t++)
52  {
53  EXPECT_NEAR(timers[t]->remaining(), -slush_factor + t, 5);
54  delete timers[t];
55  }
56 }
57 
58 } // namespace
59 
60 int main(int argc, char **argv) {
61  ::testing::InitGoogleTest(&argc, argv);
62  return RUN_ALL_TESTS();
63 }
int main(int argc, char **argv)


serial
Author(s): William Woodall , John Harrison
autogenerated on Thu Jan 9 2020 07:18:58