test/time.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008, Willow Garage, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of Willow Garage, Inc. nor the names of its
14  * contributors may be used to endorse or promote products derived from
15  * this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include <limits>
31 #include <vector>
32 
33 #include <gtest/gtest.h>
34 #include <ros/rate.h>
35 #include <ros/time.h>
36 
37 #if !defined(_WIN32)
38 #include <sys/time.h>
39 #endif
40 
41 #include <boost/date_time/posix_time/ptime.hpp>
42 
43 using namespace ros;
44 
46 
47 double epsilon = 1e-9;
48 
49 void seed_rand()
50 {
51  //Seed random number generator with current microseond count
52 #if !defined(_WIN32)
53  timeval temp_time_struct;
54  gettimeofday(&temp_time_struct,NULL);
55  srand(temp_time_struct.tv_usec);
56 #else
57  srand(time(nullptr));
58 #endif
59 };
60 
61 void generate_rand_times(uint32_t range, uint64_t runs, std::vector<ros::Time>& values1, std::vector<ros::Time>& values2)
62 {
63  seed_rand();
64  values1.clear();
65  values2.clear();
66  values1.reserve(runs);
67  values2.reserve(runs);
68  for ( uint32_t i = 0; i < runs ; i++ )
69  {
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)));
72  }
73 }
74 
75 void generate_rand_durations(uint32_t range, uint64_t runs, std::vector<ros::Duration>& values1, std::vector<ros::Duration>& values2)
76 {
77  seed_rand();
78  values1.clear();
79  values2.clear();
80  values1.reserve(runs * 4);
81  values2.reserve(runs * 4);
82  for ( uint32_t i = 0; i < runs ; i++ )
83  {
84  // positive durations
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)));
87  EXPECT_GE(values1.back(), ros::Duration(0,0));
88  EXPECT_GE(values2.back(), ros::Duration(0,0));
89 
90  // negative durations
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)));
93  EXPECT_LE(values1.back(), ros::Duration(0,0));
94  EXPECT_LE(values2.back(), ros::Duration(0,0));
95 
96  // positive and negative durations
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)));
99  EXPECT_GE(values1.back(), ros::Duration(0,0));
100  EXPECT_LE(values2.back(), ros::Duration(0,0));
101 
102  // negative and positive durations
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)));
105  EXPECT_LE(values1.back(), ros::Duration(0,0));
106  EXPECT_GE(values2.back(), ros::Duration(0,0));
107  }
108 }
109 
110 TEST(Time, size)
111 {
112  ASSERT_EQ(sizeof(Time), 8);
113  ASSERT_EQ(sizeof(Duration), 8);
114 }
115 
116 TEST(Time, Comparitors)
117 {
118  std::vector<ros::Time> v1;
119  std::vector<ros::Time> v2;
120  generate_rand_times(100, 1000, v1,v2);
121 
122  for (uint32_t i = 0; i < v1.size(); i++)
123  {
124  if (v1[i].sec * 1000000000ULL + v1[i].nsec < v2[i].sec * 1000000000ULL + v2[i].nsec)
125  {
126  EXPECT_LT(v1[i], v2[i]);
127  // printf("%f %d ", v1[i].toSec(), v1[i].sec * 1000000000ULL + v1[i].nsec);
128  //printf("vs %f %d\n", v2[i].toSec(), v2[i].sec * 1000000000ULL + v2[i].nsec);
129  EXPECT_LE(v1[i], v2[i]);
130  EXPECT_NE(v1[i], v2[i]);
131  }
132  else if (v1[i].sec * 1000000000ULL + v1[i].nsec > v2[i].sec * 1000000000ULL + v2[i].nsec)
133  {
134  EXPECT_GT(v1[i], v2[i]);
135  EXPECT_GE(v1[i], v2[i]);
136  EXPECT_NE(v1[i], v2[i]);
137  }
138  else
139  {
140  EXPECT_EQ(v1[i], v2[i]);
141  EXPECT_LE(v1[i], v2[i]);
142  EXPECT_GE(v1[i], v2[i]);
143  }
144 
145  }
146 
147 }
148 
149 TEST(Time, ToFromDouble)
150 {
151  std::vector<ros::Time> v1;
152  std::vector<ros::Time> v2;
153  generate_rand_times(100, 1000, v1,v2);
154 
155  for (uint32_t i = 0; i < v1.size(); i++)
156  {
157  EXPECT_EQ(v1[i].toSec(), v1[i].fromSec(v1[i].toSec()).toSec());
158 
159  }
160 
161 }
162 
163 TEST(Time, RoundingError)
164 {
165  double someInt = 1031.0; // some integer
166  double t = std::nextafter(someInt, 0); // someint - epsilon
167  // t should be 1031.000000
168 
169  ros::Time exampleTime;
170  exampleTime.fromSec(t);
171 
172  // if rounded incorrectly, sec may be 1030
173  // and nsec may be 1e9.
174  EXPECT_EQ(exampleTime.sec, 1031);
175  EXPECT_EQ(exampleTime.nsec, 0);
176 }
177 
178 TEST(Time, OperatorPlus)
179 {
180  Time t(100, 0);
181  Duration d(100, 0);
182  Time r = t + d;
183  EXPECT_EQ(r.sec, 200UL);
184  EXPECT_EQ(r.nsec, 0UL);
185 
186  t = Time(0, 100000UL);
187  d = Duration(0, 100UL);
188  r = t + d;
189  EXPECT_EQ(r.sec, 0UL);
190  EXPECT_EQ(r.nsec, 100100UL);
191 
192  t = Time(0, 0);
193  d = Duration(10, 2000003000UL);
194  r = t + d;
195  EXPECT_EQ(r.sec, 12UL);
196  EXPECT_EQ(r.nsec, 3000UL);
197 }
198 
199 TEST(Time, OperatorMinus)
200 {
201  Time t(100, 0);
202  Duration d(100, 0);
203  Time r = t - d;
204  EXPECT_EQ(r.sec, 0UL);
205  EXPECT_EQ(r.nsec, 0UL);
206 
207  t = Time(0, 100000UL);
208  d = Duration(0, 100UL);
209  r = t - d;
210  EXPECT_EQ(r.sec, 0UL);
211  EXPECT_EQ(r.nsec, 99900UL);
212 
213  t = Time(30, 0);
214  d = Duration(10, 2000003000UL);
215  r = t - d;
216  EXPECT_EQ(r.sec, 17UL);
217  EXPECT_EQ(r.nsec, 999997000ULL);
218 }
219 
220 TEST(Time, OperatorPlusEquals)
221 {
222  Time t(100, 0);
223  Duration d(100, 0);
224  t += d;
225  EXPECT_EQ(t.sec, 200UL);
226  EXPECT_EQ(t.nsec, 0UL);
227 
228  t = Time(0, 100000UL);
229  d = Duration(0, 100UL);
230  t += d;
231  EXPECT_EQ(t.sec, 0UL);
232  EXPECT_EQ(t.nsec, 100100UL);
233 
234  t = Time(0, 0);
235  d = Duration(10, 2000003000UL);
236  t += d;
237  EXPECT_EQ(t.sec, 12UL);
238  EXPECT_EQ(t.nsec, 3000UL);
239 }
240 
241 TEST(Time, OperatorMinusEquals)
242 {
243  Time t(100, 0);
244  Duration d(100, 0);
245  t -= d;
246  EXPECT_EQ(t.sec, 0UL);
247  EXPECT_EQ(t.nsec, 0UL);
248 
249  t = Time(0, 100000UL);
250  d = Duration(0, 100UL);
251  t -= d;
252  EXPECT_EQ(t.sec, 0UL);
253  EXPECT_EQ(t.nsec, 99900UL);
254 
255  t = Time(30, 0);
256  d = Duration(10, 2000003000UL);
257  t -= d;
258  EXPECT_EQ(t.sec, 17UL);
259  EXPECT_EQ(t.nsec, 999997000ULL);
260 }
261 
262 TEST(Time, SecNSecConstructor)
263 {
264  Time t(100, 2000003000UL);
265  EXPECT_EQ(t.sec, 102UL);
266  EXPECT_EQ(t.nsec, 3000UL);
267 }
268 
269 TEST(Time, DontMungeStreamState)
270 {
271  std::ostringstream oss;
272  Time t(100, 2000003000UL);
273  oss << std::setfill('N');
274  oss << std::setw(13);
275  oss << t;
276 
277  EXPECT_EQ(oss.width(), 13);
278  EXPECT_EQ(oss.fill(), 'N');
279 }
280 
281 TEST(Time, ToFromBoost)
282 {
283  std::vector<ros::Time> v1;
284  std::vector<ros::Time> v2;
285  generate_rand_times(100, 1000, v1,v2);
286 
287  for (uint32_t i = 0; i < v1.size(); i++)
288  {
289  Time t = v1[i];
290  // dont assume that nanosecond are available
291  t.nsec = uint32_t(t.nsec / 1000.0) * 1000;
292  boost::posix_time::ptime b = t.toBoost();
293  Time tt = Time::fromBoost(b);
294  EXPECT_EQ(t, tt);
295  }
296 }
297 
298 TEST(Time, CastFromDoubleExceptions)
299 {
300  ros::Time::init();
301 
302  Time t1, t2, t3, t4, t5, t6, t7, t8;
303  // Valid values to cast, must not throw exceptions
304  EXPECT_NO_THROW(t1.fromSec(4294967295.0));
305  EXPECT_NO_THROW(t2.fromSec(4294967295.999));
306  EXPECT_NO_THROW(t3.fromSec(0.0000001));
307 
308  // The next casts all incorrect.
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);
315  // max int64 value is 9223372036854775807
316  EXPECT_THROW(t7.fromSec(9223372036854775808.0), std::runtime_error);
317  EXPECT_THROW(t8.fromSec(-9223372036854775809.0), std::runtime_error);
318 }
319 
320 TEST(Time, OperatorMinusExceptions)
321 {
322  ros::Time::init();
323 
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);
329  Time t6(0, 1);
330 
331  Duration d1(2147483647, 999999999);
332  Duration d2(-2147483648, 0);
333  Duration d3(-2147483648, 1);
334  Duration d4(0, 1);
335 
336  EXPECT_NO_THROW(t1 - t2);
337  EXPECT_NO_THROW(t3 - t2);
338  EXPECT_NO_THROW(t4 - t5);
339 
340  EXPECT_NO_THROW(t1 - d1);
341  EXPECT_NO_THROW(t5 - d1);
342 
343  EXPECT_THROW(t4 - t6, std::runtime_error);
344  EXPECT_THROW(t4 - t3, std::runtime_error);
345 
346  EXPECT_THROW(t1 - d2, std::runtime_error);
347  EXPECT_THROW(t2 - d2, std::runtime_error);
348  EXPECT_THROW(t4 - d3, std::runtime_error);
349 }
350 
351 TEST(Time, OperatorPlusExceptions)
352 {
353  ros::Time::init();
354 
355  Time t1(2147483648, 0);
356  Time t2(2147483647, 999999999);
357  Time t4(4294967295, 999999999);
358  Time t5(4294967295, 999999998);
359 
360  Duration d1(2147483647, 999999999);
361  Duration d2(-2147483648, 1);
362  Duration d3(0, 2);
363  Duration d4(0, 1);
364 
365  EXPECT_NO_THROW(t2 + d2);
366  EXPECT_NO_THROW(t1 + d1);
367 
368  EXPECT_THROW(t4 + d4, std::runtime_error);
369  EXPECT_THROW(t4 + d1, std::runtime_error);
370  EXPECT_THROW(t5 + d3, std::runtime_error);
371 }
372 
373 TEST(Time, Constants)
374 {
375  EXPECT_EQ(Time::MAX.sec, static_cast<uint32_t>(-1));
376  EXPECT_EQ(Time::MAX.nsec, 999999999);
377  EXPECT_EQ(Time::MIN.sec, 0);
378  EXPECT_EQ(Time::MIN.nsec, 1);
379  EXPECT_EQ(Time::ZERO.sec, 0);
380  EXPECT_EQ(Time::ZERO.nsec, 0);
381  EXPECT_EQ(Time::UNINITIALIZED.sec, 0);
382  EXPECT_EQ(Time::UNINITIALIZED.nsec, 0);
383 
384  EXPECT_EQ(WallTime::MAX.sec, static_cast<uint32_t>(-1));
385  EXPECT_EQ(WallTime::MAX.nsec, 999999999);
386  EXPECT_EQ(WallTime::MIN.sec, 0);
387  EXPECT_EQ(WallTime::MIN.nsec, 1);
388  EXPECT_EQ(WallTime::ZERO.sec, 0);
389  EXPECT_EQ(WallTime::ZERO.nsec, 0);
390  EXPECT_EQ(WallTime::UNINITIALIZED.sec, 0);
391  EXPECT_EQ(WallTime::UNINITIALIZED.nsec, 0);
392 
393  EXPECT_EQ(SteadyTime::MAX.sec, static_cast<uint32_t>(-1));
394  EXPECT_EQ(SteadyTime::MAX.nsec, 999999999);
395  EXPECT_EQ(SteadyTime::MIN.sec, 0);
396  EXPECT_EQ(SteadyTime::MIN.nsec, 1);
397  EXPECT_EQ(SteadyTime::ZERO.sec, 0);
398  EXPECT_EQ(SteadyTime::ZERO.nsec, 0);
399  EXPECT_EQ(SteadyTime::UNINITIALIZED.sec, 0);
400  EXPECT_EQ(SteadyTime::UNINITIALIZED.nsec, 0);
401 }
402 
403 /************************************* Duration Tests *****************/
404 
405 TEST(Duration, Comparitors)
406 {
407  std::vector<ros::Duration> v1;
408  std::vector<ros::Duration> v2;
409  generate_rand_durations(100, 1000, v1,v2);
410 
411  for (uint32_t i = 0; i < v1.size(); i++)
412  {
413  if (v1[i].sec * 1000000000LL + v1[i].nsec < v2[i].sec * 1000000000LL + v2[i].nsec)
414  {
415  EXPECT_LT(v1[i], v2[i]);
416 // printf("%f %lld ", v1[i].toSec(), v1[i].sec * 1000000000LL + v1[i].nsec);
417 // printf("vs %f %lld\n", v2[i].toSec(), v2[i].sec * 1000000000LL + v2[i].nsec);
418  EXPECT_LE(v1[i], v2[i]);
419  EXPECT_NE(v1[i], v2[i]);
420  }
421  else if (v1[i].sec * 1000000000LL + v1[i].nsec > v2[i].sec * 1000000000LL + v2[i].nsec)
422  {
423  EXPECT_GT(v1[i], v2[i]);
424 // printf("%f %lld ", v1[i].toSec(), v1[i].sec * 1000000000LL + v1[i].nsec);
425 // printf("vs %f %lld\n", v2[i].toSec(), v2[i].sec * 1000000000LL + v2[i].nsec);
426  EXPECT_GE(v1[i], v2[i]);
427  EXPECT_NE(v1[i], v2[i]);
428  }
429  else
430  {
431  EXPECT_EQ(v1[i], v2[i]);
432  EXPECT_LE(v1[i], v2[i]);
433  EXPECT_GE(v1[i], v2[i]);
434  }
435 
436  }
437 }
438 
439 TEST(Duration, ToFromSec)
440 {
441  std::vector<ros::Duration> v1;
442  std::vector<ros::Duration> v2;
443  generate_rand_durations(100, 1000, v1,v2);
444 
445  for (uint32_t i = 0; i < v1.size(); i++)
446  {
447  EXPECT_EQ(v1[i].toSec(), v1[i].fromSec(v1[i].toSec()).toSec());
448  EXPECT_GE(ros::Duration(v1[i].toSec()).nsec, 0);
449  }
450 
451  EXPECT_EQ(ros::Duration(-0.5), ros::Duration(-1LL, 500000000LL));
452  EXPECT_EQ(ros::Duration(-0.5), ros::Duration(0, -500000000LL));
453 }
454 
455 TEST(Duration, FromNSec)
456 {
457  ros::Duration t;
458  t.fromNSec(-500000000LL);
459  EXPECT_EQ(ros::Duration(-0.5), t);
460 
461  t.fromNSec(-1500000000LL);
462  EXPECT_EQ(ros::Duration(-1.5), t);
463 
464  t.fromNSec(500000000LL);
465  EXPECT_EQ(ros::Duration(0.5), t);
466 
467  t.fromNSec(1500000000LL);
468  EXPECT_EQ(ros::Duration(1.5), t);
469 }
470 
471 TEST(Duration, OperatorPlus)
472 {
473  std::vector<ros::Duration> v1;
474  std::vector<ros::Duration> v2;
475  generate_rand_durations(100, 1000, v1,v2);
476 
477  for (uint32_t i = 0; i < v1.size(); i++)
478  {
479  EXPECT_NEAR(v1[i].toSec() + v2[i].toSec(), (v1[i] + v2[i]).toSec(), epsilon);
480  ros::Duration temp = v1[i];
481  EXPECT_NEAR(v1[i].toSec() + v2[i].toSec(), (temp += v2[i]).toSec(), epsilon);
482 
483  }
484 
485 }
486 
487 TEST(Duration, OperatorMinus)
488 {
489  std::vector<ros::Duration> v1;
490  std::vector<ros::Duration> v2;
491  generate_rand_durations(100, 1000, v1,v2);
492 
493  for (uint32_t i = 0; i < v1.size(); i++)
494  {
495  EXPECT_NEAR(v1[i].toSec() - v2[i].toSec(), (v1[i] - v2[i]).toSec(), epsilon);
496  ros::Duration temp = v1[i];
497  EXPECT_NEAR(v1[i].toSec() - v2[i].toSec(), (temp -= v2[i]).toSec(), epsilon);
498 
499  EXPECT_NEAR(- v2[i].toSec(), (-v2[i]).toSec(), epsilon);
500 
501  }
502 
503  ros::Time t1(1.1);
504  ros::Time t2(1.3);
505  ros::Duration time_diff = t1 - t2; //=-0.2
506 
507  EXPECT_NEAR(time_diff.toSec(), -0.2, epsilon);
508  EXPECT_LE(time_diff, ros::Duration(-0.19));
509  EXPECT_GE(time_diff, ros::Duration(-0.21));
510 }
511 
512 TEST(Duration, OperatorTimes)
513 {
514  std::vector<ros::Duration> v1;
515  std::vector<ros::Duration> v2;
516  generate_rand_durations(100, 1000, v1,v2);
517 
518  for (uint32_t i = 0; i < v1.size(); i++)
519  {
520  EXPECT_NEAR(v1[i].toSec() * v2[i].toSec(), (v1[i] * v2[i].toSec()).toSec(), epsilon);
521  ros::Duration temp = v1[i];
522  EXPECT_NEAR(v1[i].toSec() * v2[i].toSec(), (temp *= v2[i].toSec()).toSec(), epsilon);
523 
524  }
525 
526 }
527 
528 TEST(Duration, OperatorPlusEquals)
529 {
530  Duration t(100, 0);
531  Duration d(100, 0);
532  t += d;
533  EXPECT_EQ(t.sec, 200L);
534  EXPECT_EQ(t.nsec, 0L);
535 
536  t = Duration(0, 100000L);
537  d = Duration(0, 100L);
538  t += d;
539  EXPECT_EQ(t.sec, 0L);
540  EXPECT_EQ(t.nsec, 100100L);
541 
542  t = Duration(0, 0);
543  d = Duration(10, 2000003000L);
544  t += d;
545  EXPECT_EQ(t.sec, 12L);
546  EXPECT_EQ(t.nsec, 3000L);
547 }
548 
549 TEST(Duration, OperatorMinusEquals)
550 {
551  Duration t(100, 0);
552  Duration d(100, 0);
553  t -= d;
554  EXPECT_EQ(t.sec, 0L);
555  EXPECT_EQ(t.nsec, 0L);
556 
557  t = Duration(0, 100000L);
558  d = Duration(0, 100L);
559  t -= d;
560  EXPECT_EQ(t.sec, 0L);
561  EXPECT_EQ(t.nsec, 99900L);
562 
563  t = Duration(30, 0);
564  d = Duration(10, 2000003000L);
565  t -= d;
566  EXPECT_EQ(t.sec, 17L);
567  EXPECT_EQ(t.nsec, 999997000L);
568 }
569 
570 void alarmHandler(int sig)
571 {
572 
573 }
574 
575 TEST(Duration, sleepWithSignal)
576 {
577 #if !defined(_WIN32)
578  signal(SIGALRM, alarmHandler);
579  alarm(1);
580 #endif
581 
582  Time start = Time::now();
583  Duration d(2.0);
584  bool rc = d.sleep();
585  Time end = Time::now();
586 
587  ASSERT_GT(end - start, d);
588  ASSERT_TRUE(rc);
589 }
590 
591 TEST(Duration, Constants)
592 {
593  EXPECT_EQ(Duration::MAX.sec, std::numeric_limits<int32_t>::max());
594  EXPECT_EQ(Duration::MAX.nsec, 999999999);
595  EXPECT_EQ(Duration::MIN.sec, std::numeric_limits<int32_t>::min());
596  EXPECT_EQ(Duration::MIN.nsec, 0);
597  EXPECT_EQ(Duration::ZERO.sec, 0);
598  EXPECT_EQ(Duration::ZERO.nsec, 0);
599  EXPECT_EQ(Duration::NANOSECOND.sec, 0);
600  EXPECT_EQ(Duration::NANOSECOND.nsec, 1);
601  EXPECT_EQ(Duration::MICROSECOND.sec, 0);
602  EXPECT_EQ(Duration::MICROSECOND.nsec, 1000);
603  EXPECT_EQ(Duration::MILLISECOND.sec, 0);
604  EXPECT_EQ(Duration::MILLISECOND.nsec, 1000000);
605  EXPECT_EQ(Duration::SECOND.sec, 1);
606  EXPECT_EQ(Duration::SECOND.nsec, 0);
607  EXPECT_EQ(Duration::MINUTE.sec, 60);
608  EXPECT_EQ(Duration::MINUTE.nsec, 0);
609  EXPECT_EQ(Duration::HOUR.sec, 60 * 60);
610  EXPECT_EQ(Duration::HOUR.nsec, 0);
611  EXPECT_EQ(Duration::DAY.sec, 60 * 60 * 24);
612  EXPECT_EQ(Duration::DAY.nsec, 0);
613 
614  EXPECT_EQ(WallDuration::MAX.sec, std::numeric_limits<int32_t>::max());
615  EXPECT_EQ(WallDuration::MAX.nsec, 999999999);
616  EXPECT_EQ(WallDuration::MIN.sec, std::numeric_limits<int32_t>::min());
617  EXPECT_EQ(WallDuration::MIN.nsec, 0);
618  EXPECT_EQ(WallDuration::ZERO.sec, 0);
619  EXPECT_EQ(WallDuration::ZERO.nsec, 0);
620  EXPECT_EQ(WallDuration::NANOSECOND.sec, 0);
621  EXPECT_EQ(WallDuration::NANOSECOND.nsec, 1);
622  EXPECT_EQ(WallDuration::MICROSECOND.sec, 0);
623  EXPECT_EQ(WallDuration::MICROSECOND.nsec, 1000);
624  EXPECT_EQ(WallDuration::MILLISECOND.sec, 0);
625  EXPECT_EQ(WallDuration::MILLISECOND.nsec, 1000000);
626  EXPECT_EQ(WallDuration::SECOND.sec, 1);
627  EXPECT_EQ(WallDuration::SECOND.nsec, 0);
628  EXPECT_EQ(WallDuration::MINUTE.sec, 60);
629  EXPECT_EQ(WallDuration::MINUTE.nsec, 0);
630  EXPECT_EQ(WallDuration::HOUR.sec, 60 * 60);
631  EXPECT_EQ(WallDuration::HOUR.nsec, 0);
632  EXPECT_EQ(WallDuration::DAY.sec, 60 * 60 * 24);
633  EXPECT_EQ(WallDuration::DAY.nsec, 0);
634 }
635 
636 TEST(Rate, constructFromDuration){
637  Duration d(4, 0);
638  Rate r(d);
639  EXPECT_EQ(r.expectedCycleTime(), d);
640 }
641 
642 TEST(Rate, constructFromDouble){
643  Rate r(0.5);
644  EXPECT_EQ(r.expectedCycleTime(), ros::Duration(2, 0));
645 
646  Rate r2(-0.5);
647  EXPECT_EQ(r2.expectedCycleTime(), ros::Duration(-2, 0));
648 
649  Rate r3(std::numeric_limits<double>::infinity());
650  EXPECT_EQ(r3.expectedCycleTime(), ros::Duration(0, 0));
651 
652  EXPECT_THROW(Rate(0.0), std::runtime_error);
653 }
654 
655 TEST(Rate, sleep_return_value_true){
656  Rate r(Duration(0.2));
657  Duration(r.expectedCycleTime() * 0.5).sleep();
658  EXPECT_TRUE(r.sleep());
659 }
660 
661 TEST(Rate, sleep_return_value_false){
662  Rate r(Duration(0.2));
663  Duration(r.expectedCycleTime() * 2).sleep();
664  EXPECT_FALSE(r.sleep()); // requested rate cannot be achieved
665 }
666 
667 TEST(WallRate, constructFromDuration){
668  Duration d(4, 0);
669  WallRate r(d);
670  WallDuration wd(4, 0);
671  EXPECT_EQ(r.expectedCycleTime(), wd);
672 }
673 
674 TEST(WallRate, constructFromDouble){
675  WallRate r(0.5);
676  EXPECT_EQ(r.expectedCycleTime(), ros::WallDuration(2, 0));
677 
678  WallRate r2(-0.5);
679  EXPECT_EQ(r2.expectedCycleTime(), ros::WallDuration(-2, 0));
680 
681  WallRate r3(std::numeric_limits<double>::infinity());
682  EXPECT_EQ(r3.expectedCycleTime(), ros::WallDuration(0, 0));
683 
684  EXPECT_THROW(WallRate(0.0), std::runtime_error);
685 }
686 
688 // WallTime/WallDuration
690 
692 // SteadyTime/WallDuration
694 
695 TEST(SteadyTime, sleep){
696  SteadyTime start = SteadyTime::now();
697  WallDuration d(2.0);
698  bool rc = d.sleep();
699  SteadyTime end = SteadyTime::now();
700 
701  ASSERT_GT(end - start, d);
702  ASSERT_TRUE(rc);
703 }
704 
705 TEST(SteadyTime, sleepUntil){
706  SteadyTime start = SteadyTime::now();
707  SteadyTime end = start + WallDuration(2.0);
708  bool rc = SteadyTime::sleepUntil(end);
709  SteadyTime finished = SteadyTime::now();
710 
711  ASSERT_GT(finished, end);
712  ASSERT_TRUE(rc);
713 }
714 
715 int main(int argc, char **argv){
716  testing::InitGoogleTest(&argc, argv);
717  ros::Time::init();
718  return RUN_ALL_TESTS();
719 }
ros::DurationBase< Duration >::HOUR
static const Duration HOUR
One hour duration.
Definition: duration.h:102
ros::WallDuration::sleep
bool sleep() const
sleep for the amount of time specified by this Duration. If a signal interrupts the sleep,...
Definition: src/time.cpp:509
ros::TimeBase::fromSec
T & fromSec(double t)
Definition: impl/time.h:114
main
int main(int argc, char **argv)
Definition: test/time.cpp:715
ros::Rate::expectedCycleTime
Duration expectedCycleTime() const
Get the expected cycle time – one over the frequency passed in to the constructor.
Definition: rate.h:116
epsilon
double epsilon
Definition: test/time.cpp:47
seed_rand
void seed_rand()
Definition: test/time.cpp:49
alarmHandler
void alarmHandler(int sig)
Definition: test/time.cpp:570
TEST
TEST(Time, size)
Definition: test/time.cpp:110
generate_rand_times
void generate_rand_times(uint32_t range, uint64_t runs, std::vector< ros::Time > &values1, std::vector< ros::Time > &values2)
Definition: test/time.cpp:61
ros::WallRate::expectedCycleTime
WallDuration expectedCycleTime() const
Get the expected cycle time – one over the frequency passed in to the constructor.
Definition: rate.h:157
ros
ros::DurationBase< Duration >::DAY
static const Duration DAY
One day duration.
Definition: duration.h:101
ros::DurationBase::nsec
int32_t nsec
Definition: duration.h:75
rate.h
generate_rand_durations
void generate_rand_durations(uint32_t range, uint64_t runs, std::vector< ros::Duration > &values1, std::vector< ros::Duration > &values2)
Definition: test/time.cpp:75
ros::DurationBase< Duration >::MICROSECOND
static const Duration MICROSECOND
One microsecond duration.
Definition: duration.h:106
ros::TimeBase< Time, Duration >::MAX
static const Time MAX
Maximum representable time.
Definition: time.h:166
ros::DurationBase< Duration >::MILLISECOND
static const Duration MILLISECOND
One millisecond duration.
Definition: duration.h:105
ros::DurationBase< Duration >::MIN
static const Duration MIN
Minimum representable duration (negative)
Definition: duration.h:98
ros::DurationBase< Duration >::ZERO
static const Duration ZERO
Zero duration.
Definition: duration.h:100
ros::SteadyTime::now
static SteadyTime now()
Returns the current steady (monotonic) clock time.
Definition: src/time.cpp:487
ros::SteadyTime
Time representation. Always steady-clock time.
Definition: time.h:273
time.h
ros::WallRate
Class to help run loops at a desired frequency. This version always uses wall-clock time.
Definition: rate.h:127
ros::TimeBase< Time, Duration >::ZERO
static const Time ZERO
Zero (invalid) time.
Definition: time.h:167
ros::TimeBase::toBoost
boost::posix_time::ptime toBoost() const
Definition: impl/time.h:221
ros::DurationBase< Duration >::SECOND
static const Duration SECOND
One second duration.
Definition: duration.h:104
ros::SteadyTime::sleepUntil
static bool sleepUntil(const SteadyTime &end)
Sleep until a specific time has been reached.
Definition: src/time.cpp:412
ros::DurationBase< Duration >::NANOSECOND
static const Duration NANOSECOND
One nanosecond duration.
Definition: duration.h:107
ros::TimeBase::sec
uint32_t sec
Definition: time.h:135
ros::Rate::sleep
bool sleep()
Sleeps for any leftover time in a cycle. Calculated from the last time sleep, reset,...
Definition: rate.cpp:89
ros::TimeBase::nsec
uint32_t nsec
Definition: time.h:135
ros::DurationBase< Duration >::MAX
static const Duration MAX
Maximum representable duration.
Definition: duration.h:99
ros::Time::init
static void init()
Definition: src/time.cpp:288
ros::Time
Time representation. May either represent wall clock time or ROS clock time.
Definition: time.h:174
ros::TimeBase< Time, Duration >::MIN
static const Time MIN
Minimum representable time.
Definition: time.h:165
ros::TimeBase< Time, Duration >::UNINITIALIZED
static const Time UNINITIALIZED
Uninitialized time.
Definition: time.h:168
ros::DurationBase< Duration >::MINUTE
static const Duration MINUTE
One minute duration.
Definition: duration.h:103
ros::Rate
Class to help run loops at a desired frequency.
Definition: rate.h:86
ros::Duration::sleep
bool sleep() const
sleep for the amount of time specified by this Duration. If a signal interrupts the sleep,...
Definition: src/time.cpp:423
ros::DurationBase::toSec
double toSec() const
Definition: duration.h:92
ros::WallDuration
Duration representation for use with the WallTime class.
Definition: duration.h:155
ros::Duration
Duration representation for use with the Time class.
Definition: duration.h:117
ros::DurationBase::sec
int32_t sec
Definition: duration.h:75
ros::Time::fromBoost
static Time fromBoost(const boost::posix_time::ptime &t)
Definition: src/time.cpp:331
ros::Time::now
static Time now()
Retrieve the current time. If ROS clock time is in use, this returns the time according to the ROS cl...
Definition: src/time.cpp:260
ros::DurationBase::fromNSec
T & fromNSec(int64_t t)
Definition: impl/duration.h:109


rostime
Author(s): Josh Faust, Dirk Thomas
autogenerated on Sat Jun 17 2023 02:32:37