32 #include <gtest/gtest.h> 39 #include <boost/date_time/posix_time/ptime.hpp> 51 timeval temp_time_struct;
52 gettimeofday(&temp_time_struct,NULL);
53 srand(temp_time_struct.tv_usec);
59 void generate_rand_times(uint32_t range, uint64_t runs, std::vector<ros::Time>& values1, std::vector<ros::Time>& values2)
64 values1.reserve(runs);
65 values2.reserve(runs);
66 for ( uint32_t i = 0; i < runs ; i++ )
68 values1.push_back(
ros::Time( (rand() * range / RAND_MAX), (rand() * 1000000000ULL/RAND_MAX)));
69 values2.push_back(
ros::Time( (rand() * range / RAND_MAX), (rand() * 1000000000ULL/RAND_MAX)));
73 void generate_rand_durations(uint32_t range, uint64_t runs, std::vector<ros::Duration>& values1, std::vector<ros::Duration>& values2)
78 values1.reserve(runs * 4);
79 values2.reserve(runs * 4);
80 for ( uint32_t i = 0; i < runs ; i++ )
83 values1.push_back(
ros::Duration( (rand() * range / RAND_MAX), (rand() * 1000000000ULL/RAND_MAX)));
84 values2.push_back(
ros::Duration( (rand() * range / RAND_MAX), (rand() * 1000000000ULL/RAND_MAX)));
89 values1.push_back(
ros::Duration( -(rand() * range / RAND_MAX), -(rand() * 1000000000ULL/RAND_MAX)));
90 values2.push_back(
ros::Duration( -(rand() * range / RAND_MAX), -(rand() * 1000000000ULL/RAND_MAX)));
95 values1.push_back(
ros::Duration( (rand() * range / RAND_MAX), (rand() * 1000000000ULL/RAND_MAX)));
96 values2.push_back(
ros::Duration( -(rand() * range / RAND_MAX), -(rand() * 1000000000ULL/RAND_MAX)));
101 values1.push_back(
ros::Duration( -(rand() * range / RAND_MAX), -(rand() * 1000000000ULL/RAND_MAX)));
102 values2.push_back(
ros::Duration( (rand() * range / RAND_MAX), (rand() * 1000000000ULL/RAND_MAX)));
110 ASSERT_EQ(
sizeof(
Time), 8);
116 std::vector<ros::Time> v1;
117 std::vector<ros::Time> v2;
120 for (uint32_t i = 0; i < v1.size(); i++)
122 if (v1[i].sec * 1000000000ULL + v1[i].nsec < v2[i].sec * 1000000000ULL + v2[i].nsec)
124 EXPECT_LT(v1[i], v2[i]);
127 EXPECT_LE(v1[i], v2[i]);
128 EXPECT_NE(v1[i], v2[i]);
130 else if (v1[i].sec * 1000000000ULL + v1[i].nsec > v2[i].sec * 1000000000ULL + v2[i].nsec)
132 EXPECT_GT(v1[i], v2[i]);
133 EXPECT_GE(v1[i], v2[i]);
134 EXPECT_NE(v1[i], v2[i]);
138 EXPECT_EQ(v1[i], v2[i]);
139 EXPECT_LE(v1[i], v2[i]);
140 EXPECT_GE(v1[i], v2[i]);
149 std::vector<ros::Time> v1;
150 std::vector<ros::Time> v2;
153 for (uint32_t i = 0; i < v1.size(); i++)
155 EXPECT_EQ(v1[i].toSec(), v1[i].fromSec(v1[i].toSec()).toSec());
163 double someInt = 1031.0;
164 double t = std::nextafter(someInt, 0);
172 EXPECT_EQ(exampleTime.
sec, 1031);
173 EXPECT_EQ(exampleTime.
nsec, 0);
181 EXPECT_EQ(r.
sec, 200UL);
182 EXPECT_EQ(r.
nsec, 0UL);
184 t =
Time(0, 100000UL);
187 EXPECT_EQ(r.
sec, 0UL);
188 EXPECT_EQ(r.
nsec, 100100UL);
193 EXPECT_EQ(r.
sec, 12UL);
194 EXPECT_EQ(r.
nsec, 3000UL);
202 EXPECT_EQ(r.
sec, 0UL);
203 EXPECT_EQ(r.
nsec, 0UL);
205 t =
Time(0, 100000UL);
208 EXPECT_EQ(r.
sec, 0UL);
209 EXPECT_EQ(r.
nsec, 99900UL);
214 EXPECT_EQ(r.
sec, 17UL);
215 EXPECT_EQ(r.
nsec, 999997000ULL);
223 EXPECT_EQ(t.
sec, 200UL);
224 EXPECT_EQ(t.
nsec, 0UL);
226 t =
Time(0, 100000UL);
229 EXPECT_EQ(t.
sec, 0UL);
230 EXPECT_EQ(t.
nsec, 100100UL);
235 EXPECT_EQ(t.
sec, 12UL);
236 EXPECT_EQ(t.
nsec, 3000UL);
244 EXPECT_EQ(t.
sec, 0UL);
245 EXPECT_EQ(t.
nsec, 0UL);
247 t =
Time(0, 100000UL);
250 EXPECT_EQ(t.
sec, 0UL);
251 EXPECT_EQ(t.
nsec, 99900UL);
256 EXPECT_EQ(t.
sec, 17UL);
257 EXPECT_EQ(t.
nsec, 999997000ULL);
262 Time t(100, 2000003000UL);
263 EXPECT_EQ(t.
sec, 102UL);
264 EXPECT_EQ(t.
nsec, 3000UL);
269 std::ostringstream oss;
270 Time t(100, 2000003000UL);
271 oss << std::setfill(
'N');
272 oss << std::setw(13);
275 EXPECT_EQ(oss.width(), 13);
276 EXPECT_EQ(oss.fill(),
'N');
281 std::vector<ros::Time> v1;
282 std::vector<ros::Time> v2;
285 for (uint32_t i = 0; i < v1.size(); i++)
289 t.
nsec = uint32_t(t.
nsec / 1000.0) * 1000;
290 boost::posix_time::ptime b = t.
toBoost();
302 EXPECT_NO_THROW(t1.
fromSec(4294967295.0));
303 EXPECT_NO_THROW(t2.
fromSec(4294967295.999));
304 EXPECT_NO_THROW(t3.
fromSec(0.0000001));
307 EXPECT_THROW(t1.
fromSec(4294967296.0), std::runtime_error);
308 EXPECT_THROW(t2.
fromSec(-0.0001), std::runtime_error);
309 EXPECT_THROW(t3.
fromSec(-4294967296.0), std::runtime_error);
316 Time t1(2147483648, 0);
317 Time t2(2147483647, 999999999);
318 Time t3(2147483647, 999999998);
319 Time t4(4294967295, 999999999);
320 Time t5(4294967295, 999999998);
328 EXPECT_NO_THROW(t1 - t2);
329 EXPECT_NO_THROW(t3 - t2);
330 EXPECT_NO_THROW(t4 - t5);
332 EXPECT_NO_THROW(t1 - d1);
333 EXPECT_NO_THROW(t5 - d1);
335 EXPECT_THROW(t4 - t6, std::runtime_error);
336 EXPECT_THROW(t4 - t3, std::runtime_error);
338 EXPECT_THROW(t1 - d2, std::runtime_error);
339 EXPECT_THROW(t2 - d2, std::runtime_error);
340 EXPECT_THROW(t4 - d3, std::runtime_error);
347 Time t1(2147483648, 0);
348 Time t2(2147483647, 999999999);
349 Time t4(4294967295, 999999999);
350 Time t5(4294967295, 999999998);
357 EXPECT_NO_THROW(t2 + d2);
358 EXPECT_NO_THROW(t1 + d1);
360 EXPECT_THROW(t4 + d4, std::runtime_error);
361 EXPECT_THROW(t4 + d1, std::runtime_error);
362 EXPECT_THROW(t5 + d3, std::runtime_error);
370 std::vector<ros::Duration> v1;
371 std::vector<ros::Duration> v2;
374 for (uint32_t i = 0; i < v1.size(); i++)
376 if (v1[i].sec * 1000000000LL + v1[i].nsec < v2[i].sec * 1000000000LL + v2[i].nsec)
378 EXPECT_LT(v1[i], v2[i]);
381 EXPECT_LE(v1[i], v2[i]);
382 EXPECT_NE(v1[i], v2[i]);
384 else if (v1[i].sec * 1000000000LL + v1[i].nsec > v2[i].sec * 1000000000LL + v2[i].nsec)
386 EXPECT_GT(v1[i], v2[i]);
389 EXPECT_GE(v1[i], v2[i]);
390 EXPECT_NE(v1[i], v2[i]);
394 EXPECT_EQ(v1[i], v2[i]);
395 EXPECT_LE(v1[i], v2[i]);
396 EXPECT_GE(v1[i], v2[i]);
404 std::vector<ros::Duration> v1;
405 std::vector<ros::Duration> v2;
408 for (uint32_t i = 0; i < v1.size(); i++)
410 EXPECT_EQ(v1[i].toSec(), v1[i].fromSec(v1[i].toSec()).toSec());
436 std::vector<ros::Duration> v1;
437 std::vector<ros::Duration> v2;
440 for (uint32_t i = 0; i < v1.size(); i++)
442 EXPECT_NEAR(v1[i].toSec() + v2[i].toSec(), (v1[i] + v2[i]).toSec(),
epsilon);
444 EXPECT_NEAR(v1[i].toSec() + v2[i].toSec(), (temp += v2[i]).toSec(),
epsilon);
452 std::vector<ros::Duration> v1;
453 std::vector<ros::Duration> v2;
456 for (uint32_t i = 0; i < v1.size(); i++)
458 EXPECT_NEAR(v1[i].toSec() - v2[i].toSec(), (v1[i] - v2[i]).toSec(),
epsilon);
460 EXPECT_NEAR(v1[i].toSec() - v2[i].toSec(), (temp -= v2[i]).toSec(),
epsilon);
462 EXPECT_NEAR(- v2[i].toSec(), (-v2[i]).toSec(),
epsilon);
477 std::vector<ros::Duration> v1;
478 std::vector<ros::Duration> v2;
481 for (uint32_t i = 0; i < v1.size(); i++)
483 EXPECT_NEAR(v1[i].toSec() * v2[i].toSec(), (v1[i] * v2[i].toSec()).toSec(),
epsilon);
485 EXPECT_NEAR(v1[i].toSec() * v2[i].toSec(), (temp *= v2[i].toSec()).toSec(),
epsilon);
496 EXPECT_EQ(t.
sec, 200L);
497 EXPECT_EQ(t.
nsec, 0L);
502 EXPECT_EQ(t.
sec, 0L);
503 EXPECT_EQ(t.
nsec, 100100L);
508 EXPECT_EQ(t.
sec, 12L);
509 EXPECT_EQ(t.
nsec, 3000L);
517 EXPECT_EQ(t.
sec, 0L);
518 EXPECT_EQ(t.
nsec, 0L);
523 EXPECT_EQ(t.
sec, 0L);
524 EXPECT_EQ(t.
nsec, 99900L);
529 EXPECT_EQ(t.
sec, 17L);
530 EXPECT_EQ(t.
nsec, 999997000L);
550 ASSERT_GT(end - start, d);
563 EXPECT_TRUE(r.
sleep());
569 EXPECT_FALSE(r.
sleep());
594 ASSERT_GT(end - start, d);
604 ASSERT_GT(finished, end);
608 int main(
int argc,
char **argv){
609 testing::InitGoogleTest(&argc, argv);
611 return RUN_ALL_TESTS();
Time representation. May either represent wall clock time or ROS clock time.
bool sleep() const
sleep for the amount of time specified by this Duration. If a signal interrupts the sleep...
Class to help run loops at a desired frequency.
Duration representation for use with the WallTime class.
void alarmHandler(int sig)
Class to help run loops at a desired frequency. This version always uses wall-clock time...
void generate_rand_durations(uint32_t range, uint64_t runs, std::vector< ros::Duration > &values1, std::vector< ros::Duration > &values2)
bool sleep() const
sleep for the amount of time specified by this Duration. If a signal interrupts the sleep...
static bool sleepUntil(const SteadyTime &end)
Sleep until a specific time has been reached.
static SteadyTime now()
Returns the current steady (monotonic) clock time.
Duration expectedCycleTime() const
Get the expected cycle time – one over the frequency passed in to the constructor.
Time representation. Always steady-clock time.
bool sleep()
Sleeps for any leftover time in a cycle. Calculated from the last time sleep, reset, or the constructor was called.
Duration representation for use with the Time class.
WallDuration expectedCycleTime() const
Get the expected cycle time – one over the frequency passed in to the constructor.
static Time now()
Retrieve the current time. If ROS clock time is in use, this returns the time according to the ROS cl...
static Time fromBoost(const boost::posix_time::ptime &t)
boost::posix_time::ptime toBoost() const
void generate_rand_times(uint32_t range, uint64_t runs, std::vector< ros::Time > &values1, std::vector< ros::Time > &values2)
int main(int argc, char **argv)