time.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2008, Willow Garage, Inc.
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions are met:
00007  *
00008  *     * Redistributions of source code must retain the above copyright
00009  *       notice, this list of conditions and the following disclaimer.
00010  *     * Redistributions in binary form must reproduce the above copyright
00011  *       notice, this list of conditions and the following disclaimer in the
00012  *       documentation and/or other materials provided with the distribution.
00013  *     * Neither the name of Willow Garage, Inc. nor the names of its
00014  *       contributors may be used to endorse or promote products derived from
00015  *       this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00018  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00021  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00022  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00023  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00024  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00025  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00026  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00027  * POSSIBILITY OF SUCH DAMAGE.
00028  */
00029 
00030 #include <vector>
00031 
00032 #include <gtest/gtest.h>
00033 #include <ros/time.h>
00034 #include <sys/time.h>
00035 
00036 #include <boost/date_time/posix_time/ptime.hpp>
00037 
00038 using namespace ros;
00039 
00041 
00042 double epsilon = 1e-9;
00043 
00044 void seed_rand()
00045 {
00046   //Seed random number generator with current microseond count
00047   timeval temp_time_struct;
00048   gettimeofday(&temp_time_struct,NULL);
00049   srand(temp_time_struct.tv_usec);
00050 };
00051 
00052 void generate_rand_times(uint32_t range, uint64_t runs, std::vector<ros::Time>& values1, std::vector<ros::Time>& values2)
00053 {
00054   seed_rand();
00055   values1.clear();
00056   values2.clear();
00057   values1.reserve(runs);
00058   values2.reserve(runs);
00059   for ( uint32_t i = 0; i < runs ; i++ )
00060   {
00061     values1.push_back(ros::Time( (rand() * range / RAND_MAX), (rand() * 1000000000ULL/RAND_MAX)));
00062     values2.push_back(ros::Time( (rand() * range / RAND_MAX), (rand() * 1000000000ULL/RAND_MAX)));
00063   }
00064 }
00065 
00066 void generate_rand_durations(uint32_t range, uint64_t runs, std::vector<ros::Duration>& values1, std::vector<ros::Duration>& values2)
00067 {
00068   seed_rand();
00069   values1.clear();
00070   values2.clear();
00071   values1.reserve(runs * 4);
00072   values2.reserve(runs * 4);
00073   for ( uint32_t i = 0; i < runs ; i++ )
00074   {
00075     // positive durations
00076     values1.push_back(ros::Duration(  (rand() * range / RAND_MAX),  (rand() * 1000000000ULL/RAND_MAX)));
00077     values2.push_back(ros::Duration(  (rand() * range / RAND_MAX),  (rand() * 1000000000ULL/RAND_MAX)));
00078     EXPECT_GE(values1.back(), ros::Duration(0,0));
00079     EXPECT_GE(values2.back(), ros::Duration(0,0));
00080 
00081     // negative durations
00082     values1.push_back(ros::Duration( -(rand() * range / RAND_MAX), -(rand() * 1000000000ULL/RAND_MAX)));
00083     values2.push_back(ros::Duration( -(rand() * range / RAND_MAX), -(rand() * 1000000000ULL/RAND_MAX)));
00084     EXPECT_LE(values1.back(), ros::Duration(0,0));
00085     EXPECT_LE(values2.back(), ros::Duration(0,0));
00086 
00087     // positive and negative durations
00088     values1.push_back(ros::Duration(  (rand() * range / RAND_MAX),  (rand() * 1000000000ULL/RAND_MAX)));
00089     values2.push_back(ros::Duration( -(rand() * range / RAND_MAX), -(rand() * 1000000000ULL/RAND_MAX)));
00090     EXPECT_GE(values1.back(), ros::Duration(0,0));
00091     EXPECT_LE(values2.back(), ros::Duration(0,0));
00092 
00093     // negative and positive durations
00094     values1.push_back(ros::Duration( -(rand() * range / RAND_MAX), -(rand() * 1000000000ULL/RAND_MAX)));
00095     values2.push_back(ros::Duration(  (rand() * range / RAND_MAX),  (rand() * 1000000000ULL/RAND_MAX)));
00096     EXPECT_LE(values1.back(), ros::Duration(0,0));
00097     EXPECT_GE(values2.back(), ros::Duration(0,0));
00098   }
00099 }
00100 
00101 TEST(Time, size)
00102 {
00103   ASSERT_EQ(sizeof(Time), 8);
00104   ASSERT_EQ(sizeof(Duration), 8);
00105 }
00106 
00107 TEST(Time, Comparitors)
00108 {
00109   std::vector<ros::Time> v1;
00110   std::vector<ros::Time> v2;
00111   generate_rand_times(100, 1000, v1,v2);
00112 
00113   for (uint32_t i = 0; i < v1.size(); i++)
00114   {
00115     if (v1[i].sec * 1000000000ULL + v1[i].nsec < v2[i].sec * 1000000000ULL + v2[i].nsec)
00116     {
00117       EXPECT_LT(v1[i], v2[i]);
00118       //      printf("%f %d ", v1[i].toSec(), v1[i].sec * 1000000000ULL + v1[i].nsec);
00119       //printf("vs %f %d\n", v2[i].toSec(), v2[i].sec * 1000000000ULL + v2[i].nsec);
00120       EXPECT_LE(v1[i], v2[i]);
00121       EXPECT_NE(v1[i], v2[i]);
00122     }
00123     else if (v1[i].sec * 1000000000ULL + v1[i].nsec > v2[i].sec * 1000000000ULL + v2[i].nsec)
00124     {
00125       EXPECT_GT(v1[i], v2[i]);
00126       EXPECT_GE(v1[i], v2[i]);
00127       EXPECT_NE(v1[i], v2[i]);
00128     }
00129     else
00130     {
00131       EXPECT_EQ(v1[i], v2[i]);
00132       EXPECT_LE(v1[i], v2[i]);
00133       EXPECT_GE(v1[i], v2[i]);
00134     }
00135 
00136   }
00137 
00138 }
00139 
00140 TEST(Time, ToFromDouble)
00141 {
00142   std::vector<ros::Time> v1;
00143   std::vector<ros::Time> v2;
00144   generate_rand_times(100, 1000, v1,v2);
00145 
00146   for (uint32_t i = 0; i < v1.size(); i++)
00147   {
00148     EXPECT_EQ(v1[i].toSec(), v1[i].fromSec(v1[i].toSec()).toSec());
00149 
00150   }
00151 
00152 }
00153 
00154 TEST(Time, OperatorPlus)
00155 {
00156   Time t(100, 0);
00157   Duration d(100, 0);
00158   Time r = t + d;
00159   EXPECT_EQ(r.sec, 200UL);
00160   EXPECT_EQ(r.nsec, 0UL);
00161 
00162   t = Time(0, 100000UL);
00163   d = Duration(0, 100UL);
00164   r = t + d;
00165   EXPECT_EQ(r.sec, 0UL);
00166   EXPECT_EQ(r.nsec, 100100UL);
00167 
00168   t = Time(0, 0);
00169   d = Duration(10, 2000003000UL);
00170   r = t + d;
00171   EXPECT_EQ(r.sec, 12UL);
00172   EXPECT_EQ(r.nsec, 3000UL);
00173 }
00174 
00175 TEST(Time, OperatorMinus)
00176 {
00177   Time t(100, 0);
00178   Duration d(100, 0);
00179   Time r = t - d;
00180   EXPECT_EQ(r.sec, 0UL);
00181   EXPECT_EQ(r.nsec, 0UL);
00182 
00183   t = Time(0, 100000UL);
00184   d = Duration(0, 100UL);
00185   r = t - d;
00186   EXPECT_EQ(r.sec, 0UL);
00187   EXPECT_EQ(r.nsec, 99900UL);
00188 
00189   t = Time(30, 0);
00190   d = Duration(10, 2000003000UL);
00191   r = t - d;
00192   EXPECT_EQ(r.sec, 17UL);
00193   EXPECT_EQ(r.nsec, 999997000ULL);
00194 }
00195 
00196 TEST(Time, OperatorPlusEquals)
00197 {
00198   Time t(100, 0);
00199   Duration d(100, 0);
00200   t += d;
00201   EXPECT_EQ(t.sec, 200UL);
00202   EXPECT_EQ(t.nsec, 0UL);
00203 
00204   t = Time(0, 100000UL);
00205   d = Duration(0, 100UL);
00206   t += d;
00207   EXPECT_EQ(t.sec, 0UL);
00208   EXPECT_EQ(t.nsec, 100100UL);
00209 
00210   t = Time(0, 0);
00211   d = Duration(10, 2000003000UL);
00212   t += d;
00213   EXPECT_EQ(t.sec, 12UL);
00214   EXPECT_EQ(t.nsec, 3000UL);
00215 }
00216 
00217 TEST(Time, OperatorMinusEquals)
00218 {
00219   Time t(100, 0);
00220   Duration d(100, 0);
00221   t -= d;
00222   EXPECT_EQ(t.sec, 0UL);
00223   EXPECT_EQ(t.nsec, 0UL);
00224 
00225   t = Time(0, 100000UL);
00226   d = Duration(0, 100UL);
00227   t -= d;
00228   EXPECT_EQ(t.sec, 0UL);
00229   EXPECT_EQ(t.nsec, 99900UL);
00230 
00231   t = Time(30, 0);
00232   d = Duration(10, 2000003000UL);
00233   t -= d;
00234   EXPECT_EQ(t.sec, 17UL);
00235   EXPECT_EQ(t.nsec, 999997000ULL);
00236 }
00237 
00238 TEST(Time, SecNSecConstructor)
00239 {
00240   Time t(100, 2000003000UL);
00241   EXPECT_EQ(t.sec, 102UL);
00242   EXPECT_EQ(t.nsec, 3000UL);
00243 }
00244 
00245 TEST(Time, DontMungeStreamState)
00246 {
00247   std::ostringstream oss;
00248   Time t(100, 2000003000UL);
00249   oss << std::setfill('N');
00250   oss << std::setw(13);
00251   oss << t;
00252   
00253   EXPECT_EQ(oss.width(), 13);
00254   EXPECT_EQ(oss.fill(), 'N');
00255 }
00256 
00257 TEST(Time, ToFromBoost)
00258 {
00259   std::vector<ros::Time> v1;
00260   std::vector<ros::Time> v2;
00261   generate_rand_times(100, 1000, v1,v2);
00262 
00263   for (uint32_t i = 0; i < v1.size(); i++)
00264   {
00265     Time t = v1[i];
00266     // dont assume that nanosecond are available
00267     t.nsec = uint32_t(t.nsec / 1000.0) * 1000;
00268     boost::posix_time::ptime b = t.toBoost();
00269     Time tt = Time::fromBoost(b);
00270     EXPECT_EQ(t, tt);
00271   }
00272 }
00273 
00274 /************************************* Duration Tests *****************/
00275 
00276 TEST(Duration, Comparitors)
00277 {
00278   std::vector<ros::Duration> v1;
00279   std::vector<ros::Duration> v2;
00280   generate_rand_durations(100, 1000, v1,v2);
00281 
00282   for (uint32_t i = 0; i < v1.size(); i++)
00283   {
00284     if (v1[i].sec * 1000000000LL + v1[i].nsec < v2[i].sec * 1000000000LL + v2[i].nsec)
00285     {
00286       EXPECT_LT(v1[i], v2[i]);
00287 //      printf("%f %lld ", v1[i].toSec(), v1[i].sec * 1000000000LL + v1[i].nsec);
00288 //      printf("vs %f %lld\n", v2[i].toSec(), v2[i].sec * 1000000000LL + v2[i].nsec);
00289       EXPECT_LE(v1[i], v2[i]);
00290       EXPECT_NE(v1[i], v2[i]);
00291     }
00292     else if (v1[i].sec * 1000000000LL + v1[i].nsec > v2[i].sec * 1000000000LL + v2[i].nsec)
00293     {
00294       EXPECT_GT(v1[i], v2[i]);
00295 //      printf("%f %lld ", v1[i].toSec(), v1[i].sec * 1000000000LL + v1[i].nsec);
00296 //      printf("vs %f %lld\n", v2[i].toSec(), v2[i].sec * 1000000000LL + v2[i].nsec);
00297       EXPECT_GE(v1[i], v2[i]);
00298       EXPECT_NE(v1[i], v2[i]);
00299     }
00300     else
00301     {
00302       EXPECT_EQ(v1[i], v2[i]);
00303       EXPECT_LE(v1[i], v2[i]);
00304       EXPECT_GE(v1[i], v2[i]);
00305     }
00306 
00307   }
00308 }
00309 
00310 TEST(Duration, ToFromSec)
00311 {
00312   std::vector<ros::Duration> v1;
00313   std::vector<ros::Duration> v2;
00314   generate_rand_durations(100, 1000, v1,v2);
00315 
00316   for (uint32_t i = 0; i < v1.size(); i++)
00317   {
00318     EXPECT_EQ(v1[i].toSec(), v1[i].fromSec(v1[i].toSec()).toSec());
00319     EXPECT_GE(ros::Duration(v1[i].toSec()).nsec, 0);
00320   }
00321 
00322   EXPECT_EQ(ros::Duration(-0.5), ros::Duration(-1LL, 500000000LL));
00323   EXPECT_EQ(ros::Duration(-0.5), ros::Duration(0, -500000000LL));
00324 }
00325 
00326 
00327 TEST(Duration, OperatorPlus)
00328 {
00329   std::vector<ros::Duration> v1;
00330   std::vector<ros::Duration> v2;
00331   generate_rand_durations(100, 1000, v1,v2);
00332 
00333   for (uint32_t i = 0; i < v1.size(); i++)
00334   {
00335     EXPECT_NEAR(v1[i].toSec() + v2[i].toSec(), (v1[i] + v2[i]).toSec(), epsilon);
00336     ros::Duration temp = v1[i];
00337     EXPECT_NEAR(v1[i].toSec() + v2[i].toSec(), (temp += v2[i]).toSec(), epsilon);
00338 
00339   }
00340 
00341 }
00342 
00343 TEST(Duration, OperatorMinus)
00344 {
00345   std::vector<ros::Duration> v1;
00346   std::vector<ros::Duration> v2;
00347   generate_rand_durations(100, 1000, v1,v2);
00348 
00349   for (uint32_t i = 0; i < v1.size(); i++)
00350   {
00351     EXPECT_NEAR(v1[i].toSec() - v2[i].toSec(), (v1[i] - v2[i]).toSec(), epsilon);
00352     ros::Duration temp = v1[i];
00353     EXPECT_NEAR(v1[i].toSec() - v2[i].toSec(), (temp -= v2[i]).toSec(), epsilon);
00354 
00355     EXPECT_NEAR(- v2[i].toSec(), (-v2[i]).toSec(), epsilon);
00356 
00357   }
00358 
00359 }
00360 
00361 TEST(Duration, OperatorTimes)
00362 {
00363   std::vector<ros::Duration> v1;
00364   std::vector<ros::Duration> v2;
00365   generate_rand_durations(100, 1000, v1,v2);
00366 
00367   for (uint32_t i = 0; i < v1.size(); i++)
00368   {
00369     EXPECT_NEAR(v1[i].toSec() * v2[i].toSec(), (v1[i] * v2[i].toSec()).toSec(), epsilon);
00370     ros::Duration temp = v1[i];
00371     EXPECT_NEAR(v1[i].toSec() * v2[i].toSec(), (temp *= v2[i].toSec()).toSec(), epsilon);
00372 
00373   }
00374 
00375 }
00376 
00377 TEST(Duration, OperatorPlusEquals)
00378 {
00379   Duration t(100, 0);
00380   Duration d(100, 0);
00381   t += d;
00382   EXPECT_EQ(t.sec, 200L);
00383   EXPECT_EQ(t.nsec, 0L);
00384 
00385   t = Duration(0, 100000L);
00386   d = Duration(0, 100L);
00387   t += d;
00388   EXPECT_EQ(t.sec, 0L);
00389   EXPECT_EQ(t.nsec, 100100L);
00390 
00391   t = Duration(0, 0);
00392   d = Duration(10, 2000003000L);
00393   t += d;
00394   EXPECT_EQ(t.sec, 12L);
00395   EXPECT_EQ(t.nsec, 3000L);
00396 }
00397 
00398 TEST(Duration, OperatorMinusEquals)
00399 {
00400   Duration t(100, 0);
00401   Duration d(100, 0);
00402   t -= d;
00403   EXPECT_EQ(t.sec, 0L);
00404   EXPECT_EQ(t.nsec, 0L);
00405 
00406   t = Duration(0, 100000L);
00407   d = Duration(0, 100L);
00408   t -= d;
00409   EXPECT_EQ(t.sec, 0L);
00410   EXPECT_EQ(t.nsec, 99900L);
00411 
00412   t = Duration(30, 0);
00413   d = Duration(10, 2000003000L);
00414   t -= d;
00415   EXPECT_EQ(t.sec, 17L);
00416   EXPECT_EQ(t.nsec, 999997000L);
00417 }
00418 
00419 void alarmHandler(int sig)
00420 {
00421 
00422 }
00423 
00424 TEST(Duration, sleepWithSignal)
00425 {
00426   signal(SIGALRM, alarmHandler);
00427   alarm(1);
00428   Time start = Time::now();
00429   Duration d(2.0);
00430   d.sleep();
00431   Time end = Time::now();
00432 
00433   ASSERT_GT(end - start, d);
00434 }
00435 
00437 // WallTime/WallDuration
00439 
00440 
00441 int main(int argc, char **argv){
00442   testing::InitGoogleTest(&argc, argv);
00443   ros::Time::init();
00444   return RUN_ALL_TESTS();
00445 }


rostime
Author(s): Josh Faust
autogenerated on Fri Aug 28 2015 12:39:10