LibMultiSense
LibMultiSense Documentation
TestTimestamp.cc
Go to the documentation of this file.
1 
37 #include "MultiSense/details/utility/TimeStamp.hh"
38 
39 #include <gtest/gtest.h>
40 
41 using namespace crl::multisense::details::utility;
42 
43 TEST(Construction, default_)
44 {
45  const TimeStamp a;
46 
47  EXPECT_EQ(a.getSeconds(), 0);
48  EXPECT_EQ(a.getMicroSeconds(), 0);
49 }
50 
51 TEST(Construction, second_microsecond)
52 {
53  for (int32_t sec = -1000000; sec <= 1000000; sec += 100000)
54  {
55  for (int32_t usec = -1000000; usec <= 1100000; usec += 10000)
56  {
57  const TimeStamp time(sec, usec);
58 
59  EXPECT_EQ(time.getSeconds() * 1000000 + time.getMicroSeconds(),
60  sec * 1000000 + usec);
61 
62  if (usec >= 0 && usec < 1000000)
63  {
64  EXPECT_EQ(time.getSeconds(), sec);
65  EXPECT_EQ(time.getMicroSeconds(), usec);
66  }
67  }
68  }
69 }
70 
71 TEST(Construction, timeval)
72 {
73  for (int32_t sec = -1000000; sec <= 1000000; sec += 100000)
74  {
75  for (int32_t usec = -1000000; usec <= 1100000; usec += 10000)
76  {
77  struct timeval tv;
78  tv.tv_sec = sec;
79  tv.tv_usec = usec;
80 
81  const TimeStamp time(tv);
82 
83  EXPECT_EQ(time.getSeconds() * 1000000 + time.getMicroSeconds(),
84  sec * 1000000 + usec);
85 
86  if (usec >= 0 && usec < 1000000)
87  {
88  EXPECT_EQ(time.getSeconds(), sec);
89  EXPECT_EQ(time.getMicroSeconds(), usec);
90  }
91  }
92  }
93 }
94 
95 TEST(Construction, nanoseconds)
96 {
97  // numbers are large enough to check for rollover of int32_t when convering seconds to nanoseconds
98  for (int64_t sec = -1000000; sec <= 1000000; sec += 100000)
99  {
100  for (int64_t usec = -1000000; usec <= 1100000; usec += 10000)
101  {
102  const int64_t nsec = sec * 1000000000 + usec * 1000;
103 
104  const TimeStamp time(nsec);
105 
106  EXPECT_EQ(static_cast<int64_t>(time.getSeconds()) * 1000000 + time.getMicroSeconds(),
107  sec * 1000000 + usec);
108 
109  if (usec >= 0 && usec < 1000000)
110  {
111  EXPECT_EQ(time.getSeconds(), sec);
112  EXPECT_EQ(time.getMicroSeconds(), usec);
113  }
114  }
115  }
116 }
117 
118 // set() functions have been implicitly tested via constructors
119 
120 TEST(GetNanoSeconds, getNanoSeconds)
121 {
122  // handle values larger than a uint32_t (2,147,483,647) to check rollover
123  // increment by 0.1 seconds
124  //
125  for (int64_t nsec = -100000000000; nsec <= 100000000000; nsec += 100000000)
126  {
127  const TimeStamp time(nsec);
128  EXPECT_EQ(time.getNanoSeconds(), nsec);
129  }
130 }
131 
132 TEST(Operators, add)
133 {
134  // handle nsec values larger than a uint32_t (2,147,483,647) to check rollover
135  // increment by 0.1 seconds
136  const int64_t max_nsec = 10000000000;
137  const int64_t step_nsec = 100000000;
138  for (int64_t a_nsec = -max_nsec; a_nsec <= max_nsec; a_nsec += step_nsec)
139  {
140  for (int64_t b_nsec = -max_nsec; b_nsec <= max_nsec; b_nsec += step_nsec)
141  {
142  const TimeStamp a(a_nsec);
143  const TimeStamp b(b_nsec);
144 
145  EXPECT_EQ((a + b).getNanoSeconds(), a_nsec + b_nsec);
146  }
147  }
148 }
149 
150 TEST(Operators, subtract)
151 {
152  // handle nsec values larger than a uint32_t (2,147,483,647) to check rollover
153  // increment by 0.1 seconds
154  const int64_t max_nsec = 10000000000;
155  const int64_t step_nsec = 100000000;
156  for (int64_t a_nsec = -max_nsec; a_nsec <= max_nsec; a_nsec += step_nsec)
157  {
158  for (int64_t b_nsec = -max_nsec; b_nsec <= max_nsec; b_nsec += step_nsec)
159  {
160  const TimeStamp a(a_nsec);
161  const TimeStamp b(b_nsec);
162 
163  EXPECT_EQ((a - b).getNanoSeconds(), a_nsec - b_nsec);
164  }
165  }
166 }
TEST
TEST(Construction, default_)
Definition: TestTimestamp.cc:43