33 #include <gtest/gtest.h>
41 #include <boost/date_time/posix_time/ptime.hpp>
53 timeval temp_time_struct;
54 gettimeofday(&temp_time_struct,NULL);
55 srand(temp_time_struct.tv_usec);
61 void generate_rand_times(uint32_t range, uint64_t runs, std::vector<ros::Time>& values1, std::vector<ros::Time>& values2)
66 values1.reserve(runs);
67 values2.reserve(runs);
68 for ( uint32_t i = 0; i < runs ; i++ )
70 values1.push_back(
ros::Time( (rand() * range / RAND_MAX), (rand() * 1000000000ULL/RAND_MAX)));
71 values2.push_back(
ros::Time( (rand() * range / RAND_MAX), (rand() * 1000000000ULL/RAND_MAX)));
75 void generate_rand_durations(uint32_t range, uint64_t runs, std::vector<ros::Duration>& values1, std::vector<ros::Duration>& values2)
80 values1.reserve(runs * 4);
81 values2.reserve(runs * 4);
82 for ( uint32_t i = 0; i < runs ; i++ )
85 values1.push_back(
ros::Duration( (rand() * range / RAND_MAX), (rand() * 1000000000ULL/RAND_MAX)));
86 values2.push_back(
ros::Duration( (rand() * range / RAND_MAX), (rand() * 1000000000ULL/RAND_MAX)));
91 values1.push_back(
ros::Duration( -(rand() * range / RAND_MAX), -(rand() * 1000000000ULL/RAND_MAX)));
92 values2.push_back(
ros::Duration( -(rand() * range / RAND_MAX), -(rand() * 1000000000ULL/RAND_MAX)));
97 values1.push_back(
ros::Duration( (rand() * range / RAND_MAX), (rand() * 1000000000ULL/RAND_MAX)));
98 values2.push_back(
ros::Duration( -(rand() * range / RAND_MAX), -(rand() * 1000000000ULL/RAND_MAX)));
103 values1.push_back(
ros::Duration( -(rand() * range / RAND_MAX), -(rand() * 1000000000ULL/RAND_MAX)));
104 values2.push_back(
ros::Duration( (rand() * range / RAND_MAX), (rand() * 1000000000ULL/RAND_MAX)));
112 ASSERT_EQ(
sizeof(
Time), 8);
118 std::vector<ros::Time> v1;
119 std::vector<ros::Time> v2;
122 for (uint32_t i = 0; i < v1.size(); i++)
124 if (v1[i].sec * 1000000000ULL + v1[i].nsec < v2[i].sec * 1000000000ULL + v2[i].nsec)
126 EXPECT_LT(v1[i], v2[i]);
129 EXPECT_LE(v1[i], v2[i]);
130 EXPECT_NE(v1[i], v2[i]);
132 else if (v1[i].sec * 1000000000ULL + v1[i].nsec > v2[i].sec * 1000000000ULL + v2[i].nsec)
134 EXPECT_GT(v1[i], v2[i]);
135 EXPECT_GE(v1[i], v2[i]);
136 EXPECT_NE(v1[i], v2[i]);
140 EXPECT_EQ(v1[i], v2[i]);
141 EXPECT_LE(v1[i], v2[i]);
142 EXPECT_GE(v1[i], v2[i]);
151 std::vector<ros::Time> v1;
152 std::vector<ros::Time> v2;
155 for (uint32_t i = 0; i < v1.size(); i++)
157 EXPECT_EQ(v1[i].toSec(), v1[i].fromSec(v1[i].toSec()).toSec());
165 double someInt = 1031.0;
166 double t = std::nextafter(someInt, 0);
174 EXPECT_EQ(exampleTime.
sec, 1031);
175 EXPECT_EQ(exampleTime.
nsec, 0);
183 EXPECT_EQ(r.
sec, 200UL);
184 EXPECT_EQ(r.
nsec, 0UL);
186 t =
Time(0, 100000UL);
189 EXPECT_EQ(r.
sec, 0UL);
190 EXPECT_EQ(r.
nsec, 100100UL);
195 EXPECT_EQ(r.
sec, 12UL);
196 EXPECT_EQ(r.
nsec, 3000UL);
204 EXPECT_EQ(r.
sec, 0UL);
205 EXPECT_EQ(r.
nsec, 0UL);
207 t =
Time(0, 100000UL);
210 EXPECT_EQ(r.
sec, 0UL);
211 EXPECT_EQ(r.
nsec, 99900UL);
216 EXPECT_EQ(r.
sec, 17UL);
217 EXPECT_EQ(r.
nsec, 999997000ULL);
225 EXPECT_EQ(t.
sec, 200UL);
226 EXPECT_EQ(t.
nsec, 0UL);
228 t =
Time(0, 100000UL);
231 EXPECT_EQ(t.
sec, 0UL);
232 EXPECT_EQ(t.
nsec, 100100UL);
237 EXPECT_EQ(t.
sec, 12UL);
238 EXPECT_EQ(t.
nsec, 3000UL);
246 EXPECT_EQ(t.
sec, 0UL);
247 EXPECT_EQ(t.
nsec, 0UL);
249 t =
Time(0, 100000UL);
252 EXPECT_EQ(t.
sec, 0UL);
253 EXPECT_EQ(t.
nsec, 99900UL);
258 EXPECT_EQ(t.
sec, 17UL);
259 EXPECT_EQ(t.
nsec, 999997000ULL);
264 Time t(100, 2000003000UL);
265 EXPECT_EQ(t.
sec, 102UL);
266 EXPECT_EQ(t.
nsec, 3000UL);
271 std::ostringstream oss;
272 Time t(100, 2000003000UL);
273 oss << std::setfill(
'N');
274 oss << std::setw(13);
277 EXPECT_EQ(oss.width(), 13);
278 EXPECT_EQ(oss.fill(),
'N');
283 std::vector<ros::Time> v1;
284 std::vector<ros::Time> v2;
287 for (uint32_t i = 0; i < v1.size(); i++)
291 t.
nsec = uint32_t(t.
nsec / 1000.0) * 1000;
292 boost::posix_time::ptime b = t.
toBoost();
302 Time t1, t2, t3, t4, t5, t6, t7, t8;
304 EXPECT_NO_THROW(t1.
fromSec(4294967295.0));
305 EXPECT_NO_THROW(t2.
fromSec(4294967295.999));
306 EXPECT_NO_THROW(t3.
fromSec(0.0000001));
309 EXPECT_THROW(t1.
fromSec(4294967296.0), std::runtime_error);
310 EXPECT_THROW(t2.
fromSec(-0.0001), std::runtime_error);
311 EXPECT_THROW(t3.
fromSec(-4294967296.0), std::runtime_error);
312 EXPECT_THROW(t4.
fromSec(std::numeric_limits<double>::infinity()), std::runtime_error);
313 EXPECT_THROW(t5.
fromSec(-std::numeric_limits<double>::infinity()), std::runtime_error);
314 EXPECT_THROW(t6.
fromSec(std::numeric_limits<double>::quiet_NaN()), std::runtime_error);
316 EXPECT_THROW(t7.
fromSec(9223372036854775808.0), std::runtime_error);
317 EXPECT_THROW(t8.
fromSec(-9223372036854775809.0), std::runtime_error);
324 Time t1(2147483648, 0);
325 Time t2(2147483647, 999999999);
326 Time t3(2147483647, 999999998);
327 Time t4(4294967295, 999999999);
328 Time t5(4294967295, 999999998);
336 EXPECT_NO_THROW(t1 - t2);
337 EXPECT_NO_THROW(t3 - t2);
338 EXPECT_NO_THROW(t4 - t5);
340 EXPECT_NO_THROW(t1 - d1);
341 EXPECT_NO_THROW(t5 - d1);
343 EXPECT_THROW(t4 - t6, std::runtime_error);
344 EXPECT_THROW(t4 - t3, std::runtime_error);
346 EXPECT_THROW(t1 - d2, std::runtime_error);
347 EXPECT_THROW(t2 - d2, std::runtime_error);
348 EXPECT_THROW(t4 - d3, std::runtime_error);
355 Time t1(2147483648, 0);
356 Time t2(2147483647, 999999999);
357 Time t4(4294967295, 999999999);
358 Time t5(4294967295, 999999998);
365 EXPECT_NO_THROW(t2 + d2);
366 EXPECT_NO_THROW(t1 + d1);
368 EXPECT_THROW(t4 + d4, std::runtime_error);
369 EXPECT_THROW(t4 + d1, std::runtime_error);
370 EXPECT_THROW(t5 + d3, std::runtime_error);
375 EXPECT_EQ(
Time::MAX.sec,
static_cast<uint32_t
>(-1));
407 std::vector<ros::Duration> v1;
408 std::vector<ros::Duration> v2;
411 for (uint32_t i = 0; i < v1.size(); i++)
413 if (v1[i].sec * 1000000000LL + v1[i].nsec < v2[i].sec * 1000000000LL + v2[i].nsec)
415 EXPECT_LT(v1[i], v2[i]);
418 EXPECT_LE(v1[i], v2[i]);
419 EXPECT_NE(v1[i], v2[i]);
421 else if (v1[i].sec * 1000000000LL + v1[i].nsec > v2[i].sec * 1000000000LL + v2[i].nsec)
423 EXPECT_GT(v1[i], v2[i]);
426 EXPECT_GE(v1[i], v2[i]);
427 EXPECT_NE(v1[i], v2[i]);
431 EXPECT_EQ(v1[i], v2[i]);
432 EXPECT_LE(v1[i], v2[i]);
433 EXPECT_GE(v1[i], v2[i]);
441 std::vector<ros::Duration> v1;
442 std::vector<ros::Duration> v2;
445 for (uint32_t i = 0; i < v1.size(); i++)
447 EXPECT_EQ(v1[i].toSec(), v1[i].fromSec(v1[i].toSec()).toSec());
473 std::vector<ros::Duration> v1;
474 std::vector<ros::Duration> v2;
477 for (uint32_t i = 0; i < v1.size(); i++)
479 EXPECT_NEAR(v1[i].toSec() + v2[i].toSec(), (v1[i] + v2[i]).toSec(),
epsilon);
481 EXPECT_NEAR(v1[i].toSec() + v2[i].toSec(), (temp += v2[i]).toSec(),
epsilon);
489 std::vector<ros::Duration> v1;
490 std::vector<ros::Duration> v2;
493 for (uint32_t i = 0; i < v1.size(); i++)
495 EXPECT_NEAR(v1[i].toSec() - v2[i].toSec(), (v1[i] - v2[i]).toSec(),
epsilon);
497 EXPECT_NEAR(v1[i].toSec() - v2[i].toSec(), (temp -= v2[i]).toSec(),
epsilon);
499 EXPECT_NEAR(- v2[i].toSec(), (-v2[i]).toSec(),
epsilon);
514 std::vector<ros::Duration> v1;
515 std::vector<ros::Duration> v2;
518 for (uint32_t i = 0; i < v1.size(); i++)
520 EXPECT_NEAR(v1[i].toSec() * v2[i].toSec(), (v1[i] * v2[i].toSec()).toSec(),
epsilon);
522 EXPECT_NEAR(v1[i].toSec() * v2[i].toSec(), (temp *= v2[i].toSec()).toSec(),
epsilon);
533 EXPECT_EQ(t.
sec, 200L);
534 EXPECT_EQ(t.
nsec, 0L);
539 EXPECT_EQ(t.
sec, 0L);
540 EXPECT_EQ(t.
nsec, 100100L);
545 EXPECT_EQ(t.
sec, 12L);
546 EXPECT_EQ(t.
nsec, 3000L);
554 EXPECT_EQ(t.
sec, 0L);
555 EXPECT_EQ(t.
nsec, 0L);
560 EXPECT_EQ(t.
sec, 0L);
561 EXPECT_EQ(t.
nsec, 99900L);
566 EXPECT_EQ(t.
sec, 17L);
567 EXPECT_EQ(t.
nsec, 999997000L);
587 ASSERT_GT(end - start, d);
593 EXPECT_EQ(
Duration::MAX.sec, std::numeric_limits<int32_t>::max());
595 EXPECT_EQ(
Duration::MIN.sec, std::numeric_limits<int32_t>::min());
649 Rate r3(std::numeric_limits<double>::infinity());
652 EXPECT_THROW(
Rate(0.0), std::runtime_error);
658 EXPECT_TRUE(r.
sleep());
664 EXPECT_FALSE(r.
sleep());
681 WallRate r3(std::numeric_limits<double>::infinity());
684 EXPECT_THROW(
WallRate(0.0), std::runtime_error);
701 ASSERT_GT(end - start, d);
711 ASSERT_GT(finished, end);
715 int main(
int argc,
char **argv){
716 testing::InitGoogleTest(&argc, argv);
718 return RUN_ALL_TESTS();