37 #include "MultiSense/details/utility/TimeStamp.hh"
39 #include <gtest/gtest.h>
41 using namespace crl::multisense::details::utility;
43 TEST(Construction, default_)
47 EXPECT_EQ(a.getSeconds(), 0);
48 EXPECT_EQ(a.getMicroSeconds(), 0);
51 TEST(Construction, second_microsecond)
53 for (int32_t sec = -1000000; sec <= 1000000; sec += 100000)
55 for (int32_t usec = -1000000; usec <= 1100000; usec += 10000)
57 const TimeStamp time(sec, usec);
59 EXPECT_EQ(time.getSeconds() * 1000000 + time.getMicroSeconds(),
60 sec * 1000000 + usec);
62 if (usec >= 0 && usec < 1000000)
64 EXPECT_EQ(time.getSeconds(), sec);
65 EXPECT_EQ(time.getMicroSeconds(), usec);
71 TEST(Construction, timeval)
73 for (int32_t sec = -1000000; sec <= 1000000; sec += 100000)
75 for (int32_t usec = -1000000; usec <= 1100000; usec += 10000)
81 const TimeStamp time(tv);
83 EXPECT_EQ(time.getSeconds() * 1000000 + time.getMicroSeconds(),
84 sec * 1000000 + usec);
86 if (usec >= 0 && usec < 1000000)
88 EXPECT_EQ(time.getSeconds(), sec);
89 EXPECT_EQ(time.getMicroSeconds(), usec);
95 TEST(Construction, nanoseconds)
98 for (int64_t sec = -1000000; sec <= 1000000; sec += 100000)
100 for (int64_t usec = -1000000; usec <= 1100000; usec += 10000)
102 const int64_t nsec = sec * 1000000000 + usec * 1000;
104 const TimeStamp time(nsec);
106 EXPECT_EQ(
static_cast<int64_t
>(time.getSeconds()) * 1000000 + time.getMicroSeconds(),
107 sec * 1000000 + usec);
109 if (usec >= 0 && usec < 1000000)
111 EXPECT_EQ(time.getSeconds(), sec);
112 EXPECT_EQ(time.getMicroSeconds(), usec);
120 TEST(GetNanoSeconds, getNanoSeconds)
125 for (int64_t nsec = -100000000000; nsec <= 100000000000; nsec += 100000000)
127 const TimeStamp time(nsec);
128 EXPECT_EQ(time.getNanoSeconds(), nsec);
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)
140 for (int64_t b_nsec = -max_nsec; b_nsec <= max_nsec; b_nsec += step_nsec)
142 const TimeStamp a(a_nsec);
143 const TimeStamp b(b_nsec);
145 EXPECT_EQ((a + b).getNanoSeconds(), a_nsec + b_nsec);
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)
158 for (int64_t b_nsec = -max_nsec; b_nsec <= max_nsec; b_nsec += step_nsec)
160 const TimeStamp a(a_nsec);
161 const TimeStamp b(b_nsec);
163 EXPECT_EQ((a - b).getNanoSeconds(), a_nsec - b_nsec);