$search
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 using namespace ros; 00037 00039 00040 double epsilon = 1e-9; 00041 00042 void seed_rand() 00043 { 00044 //Seed random number generator with current microseond count 00045 timeval temp_time_struct; 00046 gettimeofday(&temp_time_struct,NULL); 00047 srand(temp_time_struct.tv_usec); 00048 }; 00049 00050 void generate_rand_times(uint32_t range, uint64_t runs, std::vector<ros::Time>& values1, std::vector<ros::Time>& values2) 00051 { 00052 seed_rand(); 00053 values1.clear(); 00054 values2.clear(); 00055 values1.reserve(runs); 00056 values2.reserve(runs); 00057 for ( uint32_t i = 0; i < runs ; i++ ) 00058 { 00059 values1.push_back(ros::Time( (rand() * range / RAND_MAX), (rand() * 1000000000ULL/RAND_MAX))); 00060 values2.push_back(ros::Time( (rand() * range / RAND_MAX), (rand() * 1000000000ULL/RAND_MAX))); 00061 } 00062 } 00063 00064 void generate_rand_durations(uint32_t range, uint64_t runs, std::vector<ros::Duration>& values1, std::vector<ros::Duration>& values2) 00065 { 00066 seed_rand(); 00067 values1.clear(); 00068 values2.clear(); 00069 values1.reserve(runs); 00070 values2.reserve(runs); 00071 for ( uint32_t i = 0; i < runs ; i++ ) 00072 { 00073 values1.push_back(ros::Duration( (rand() * range / RAND_MAX), (rand() * 1000000000ULL/RAND_MAX))); 00074 values2.push_back(ros::Duration( (rand() * range / RAND_MAX), (rand() * 1000000000ULL/RAND_MAX))); 00075 } 00076 } 00077 00078 TEST(Time, size) 00079 { 00080 ASSERT_EQ(sizeof(Time), 8); 00081 ASSERT_EQ(sizeof(Duration), 8); 00082 } 00083 00084 TEST(Time, Comparitors) 00085 { 00086 std::vector<ros::Time> v1; 00087 std::vector<ros::Time> v2; 00088 generate_rand_times(100, 1000, v1,v2); 00089 00090 for (uint32_t i = 0; i < v1.size(); i++) 00091 { 00092 if (v1[i].sec * 1000000000ULL + v1[i].nsec < v2[i].sec * 1000000000ULL + v2[i].nsec) 00093 { 00094 EXPECT_LT(v1[i], v2[i]); 00095 // printf("%f %d ", v1[i].toSec(), v1[i].sec * 1000000000ULL + v1[i].nsec); 00096 //printf("vs %f %d\n", v2[i].toSec(), v2[i].sec * 1000000000ULL + v2[i].nsec); 00097 EXPECT_LE(v1[i], v2[i]); 00098 EXPECT_NE(v1[i], v2[i]); 00099 } 00100 else if (v1[i].sec * 1000000000ULL + v1[i].nsec > v2[i].sec * 1000000000ULL + v2[i].nsec) 00101 { 00102 EXPECT_GT(v1[i], v2[i]); 00103 EXPECT_GE(v1[i], v2[i]); 00104 EXPECT_NE(v1[i], v2[i]); 00105 } 00106 else 00107 { 00108 EXPECT_EQ(v1[i], v2[i]); 00109 EXPECT_LE(v1[i], v2[i]); 00110 EXPECT_GE(v1[i], v2[i]); 00111 } 00112 00113 } 00114 00115 } 00116 00117 TEST(Time, ToFromDouble) 00118 { 00119 std::vector<ros::Time> v1; 00120 std::vector<ros::Time> v2; 00121 generate_rand_times(100, 1000, v1,v2); 00122 00123 for (uint32_t i = 0; i < v1.size(); i++) 00124 { 00125 EXPECT_EQ(v1[i].toSec(), v1[i].fromSec(v1[i].toSec()).toSec()); 00126 00127 } 00128 00129 } 00130 00131 TEST(Time, OperatorPlus) 00132 { 00133 Time t(100, 0); 00134 Duration d(100, 0); 00135 Time r = t + d; 00136 EXPECT_EQ(r.sec, 200UL); 00137 EXPECT_EQ(r.nsec, 0UL); 00138 00139 t = Time(0, 100000UL); 00140 d = Duration(0, 100UL); 00141 r = t + d; 00142 EXPECT_EQ(r.sec, 0UL); 00143 EXPECT_EQ(r.nsec, 100100UL); 00144 00145 t = Time(0, 0); 00146 d = Duration(10, 2000003000UL); 00147 r = t + d; 00148 EXPECT_EQ(r.sec, 12UL); 00149 EXPECT_EQ(r.nsec, 3000UL); 00150 } 00151 00152 TEST(Time, OperatorMinus) 00153 { 00154 Time t(100, 0); 00155 Duration d(100, 0); 00156 Time r = t - d; 00157 EXPECT_EQ(r.sec, 0UL); 00158 EXPECT_EQ(r.nsec, 0UL); 00159 00160 t = Time(0, 100000UL); 00161 d = Duration(0, 100UL); 00162 r = t - d; 00163 EXPECT_EQ(r.sec, 0UL); 00164 EXPECT_EQ(r.nsec, 99900UL); 00165 00166 t = Time(30, 0); 00167 d = Duration(10, 2000003000UL); 00168 r = t - d; 00169 EXPECT_EQ(r.sec, 17UL); 00170 EXPECT_EQ(r.nsec, 999997000ULL); 00171 } 00172 00173 TEST(Time, OperatorPlusEquals) 00174 { 00175 Time t(100, 0); 00176 Duration d(100, 0); 00177 t += d; 00178 EXPECT_EQ(t.sec, 200UL); 00179 EXPECT_EQ(t.nsec, 0UL); 00180 00181 t = Time(0, 100000UL); 00182 d = Duration(0, 100UL); 00183 t += d; 00184 EXPECT_EQ(t.sec, 0UL); 00185 EXPECT_EQ(t.nsec, 100100UL); 00186 00187 t = Time(0, 0); 00188 d = Duration(10, 2000003000UL); 00189 t += d; 00190 EXPECT_EQ(t.sec, 12UL); 00191 EXPECT_EQ(t.nsec, 3000UL); 00192 } 00193 00194 TEST(Time, OperatorMinusEquals) 00195 { 00196 Time t(100, 0); 00197 Duration d(100, 0); 00198 t -= d; 00199 EXPECT_EQ(t.sec, 0UL); 00200 EXPECT_EQ(t.nsec, 0UL); 00201 00202 t = Time(0, 100000UL); 00203 d = Duration(0, 100UL); 00204 t -= d; 00205 EXPECT_EQ(t.sec, 0UL); 00206 EXPECT_EQ(t.nsec, 99900UL); 00207 00208 t = Time(30, 0); 00209 d = Duration(10, 2000003000UL); 00210 t -= d; 00211 EXPECT_EQ(t.sec, 17UL); 00212 EXPECT_EQ(t.nsec, 999997000ULL); 00213 } 00214 00215 TEST(Time, SecNSecConstructor) 00216 { 00217 Time t(100, 2000003000UL); 00218 EXPECT_EQ(t.sec, 102UL); 00219 EXPECT_EQ(t.nsec, 3000UL); 00220 } 00221 00222 TEST(Time, DontMungeStreamState) 00223 { 00224 std::ostringstream oss; 00225 Time t(100, 2000003000UL); 00226 oss << std::setfill('N'); 00227 oss << std::setw(13); 00228 oss << t; 00229 00230 EXPECT_EQ(oss.width(), 13); 00231 EXPECT_EQ(oss.fill(), 'N'); 00232 } 00233 00234 /************************************* Duration Tests *****************/ 00235 00236 TEST(Duration, Comparitors) 00237 { 00238 std::vector<ros::Duration> v1; 00239 std::vector<ros::Duration> v2; 00240 generate_rand_durations(100, 1000, v1,v2); 00241 00242 for (uint32_t i = 0; i < v1.size(); i++) 00243 { 00244 if (v1[i].sec * 1000000000ULL + v1[i].nsec < v2[i].sec * 1000000000ULL + v2[i].nsec) 00245 { 00246 EXPECT_LT(v1[i], v2[i]); 00247 // printf("%f %d ", v1[i].toSec(), v1[i].sec * 1000000000ULL + v1[i].nsec); 00248 //printf("vs %f %d\n", v2[i].toSec(), v2[i].sec * 1000000000ULL + v2[i].nsec); 00249 EXPECT_LE(v1[i], v2[i]); 00250 EXPECT_NE(v1[i], v2[i]); 00251 } 00252 else if (v1[i].sec * 1000000000ULL + v1[i].nsec > v2[i].sec * 1000000000ULL + v2[i].nsec) 00253 { 00254 EXPECT_GT(v1[i], v2[i]); 00255 EXPECT_GE(v1[i], v2[i]); 00256 EXPECT_NE(v1[i], v2[i]); 00257 } 00258 else 00259 { 00260 EXPECT_EQ(v1[i], v2[i]); 00261 EXPECT_LE(v1[i], v2[i]); 00262 EXPECT_GE(v1[i], v2[i]); 00263 } 00264 00265 } 00266 00267 } 00268 00269 TEST(Duration, ToFromSec) 00270 { 00271 std::vector<ros::Duration> v1; 00272 std::vector<ros::Duration> v2; 00273 generate_rand_durations(100, 1000, v1,v2); 00274 00275 for (uint32_t i = 0; i < v1.size(); i++) 00276 { 00277 EXPECT_EQ(v1[i].toSec(), v1[i].fromSec(v1[i].toSec()).toSec()); 00278 00279 } 00280 00281 } 00282 00283 00284 TEST(Duration, OperatorPlus) 00285 { 00286 std::vector<ros::Duration> v1; 00287 std::vector<ros::Duration> v2; 00288 generate_rand_durations(100, 1000, v1,v2); 00289 00290 for (uint32_t i = 0; i < v1.size(); i++) 00291 { 00292 EXPECT_NEAR(v1[i].toSec() + v2[i].toSec(), (v1[i] + v2[i]).toSec(), epsilon); 00293 ros::Duration temp = v1[i]; 00294 EXPECT_NEAR(v1[i].toSec() + v2[i].toSec(), (temp += v2[i]).toSec(), epsilon); 00295 00296 } 00297 00298 } 00299 00300 TEST(Duration, OperatorMinus) 00301 { 00302 std::vector<ros::Duration> v1; 00303 std::vector<ros::Duration> v2; 00304 generate_rand_durations(100, 1000, v1,v2); 00305 00306 for (uint32_t i = 0; i < v1.size(); i++) 00307 { 00308 EXPECT_NEAR(v1[i].toSec() - v2[i].toSec(), (v1[i] - v2[i]).toSec(), epsilon); 00309 ros::Duration temp = v1[i]; 00310 EXPECT_NEAR(v1[i].toSec() - v2[i].toSec(), (temp -= v2[i]).toSec(), epsilon); 00311 00312 EXPECT_NEAR(- v2[i].toSec(), (-v2[i]).toSec(), epsilon); 00313 00314 } 00315 00316 } 00317 00318 TEST(Duration, OperatorTimes) 00319 { 00320 std::vector<ros::Duration> v1; 00321 std::vector<ros::Duration> v2; 00322 generate_rand_durations(100, 1000, v1,v2); 00323 00324 for (uint32_t i = 0; i < v1.size(); i++) 00325 { 00326 EXPECT_NEAR(v1[i].toSec() * v2[i].toSec(), (v1[i] * v2[i].toSec()).toSec(), epsilon); 00327 ros::Duration temp = v1[i]; 00328 EXPECT_NEAR(v1[i].toSec() * v2[i].toSec(), (temp *= v2[i].toSec()).toSec(), epsilon); 00329 00330 } 00331 00332 } 00333 00334 TEST(Duration, OperatorPlusEquals) 00335 { 00336 Duration t(100, 0); 00337 Duration d(100, 0); 00338 t += d; 00339 EXPECT_EQ(t.sec, 200L); 00340 EXPECT_EQ(t.nsec, 0L); 00341 00342 t = Duration(0, 100000L); 00343 d = Duration(0, 100L); 00344 t += d; 00345 EXPECT_EQ(t.sec, 0L); 00346 EXPECT_EQ(t.nsec, 100100L); 00347 00348 t = Duration(0, 0); 00349 d = Duration(10, 2000003000L); 00350 t += d; 00351 EXPECT_EQ(t.sec, 12L); 00352 EXPECT_EQ(t.nsec, 3000L); 00353 } 00354 00355 TEST(Duration, OperatorMinusEquals) 00356 { 00357 Duration t(100, 0); 00358 Duration d(100, 0); 00359 t -= d; 00360 EXPECT_EQ(t.sec, 0L); 00361 EXPECT_EQ(t.nsec, 0L); 00362 00363 t = Duration(0, 100000L); 00364 d = Duration(0, 100L); 00365 t -= d; 00366 EXPECT_EQ(t.sec, 0L); 00367 EXPECT_EQ(t.nsec, 99900L); 00368 00369 t = Duration(30, 0); 00370 d = Duration(10, 2000003000L); 00371 t -= d; 00372 EXPECT_EQ(t.sec, 17L); 00373 EXPECT_EQ(t.nsec, 999997000L); 00374 } 00375 00376 void alarmHandler(int sig) 00377 { 00378 00379 } 00380 00381 TEST(Duration, sleepWithSignal) 00382 { 00383 signal(SIGALRM, alarmHandler); 00384 alarm(1); 00385 Time start = Time::now(); 00386 Duration d(2.0); 00387 d.sleep(); 00388 Time end = Time::now(); 00389 00390 ASSERT_GT(end - start, d); 00391 } 00392 00394 // WallTime/WallDuration 00396 00397 00398 int main(int argc, char **argv){ 00399 testing::InitGoogleTest(&argc, argv); 00400 ros::Time::init(); 00401 return RUN_ALL_TESTS(); 00402 }