00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 import roslib; roslib.load_manifest('test_roslib')
00033
00034 import os
00035 import sys
00036 import unittest
00037
00038 import roslib.rostime
00039 import rosunit
00040
00041 class RostimeTest(unittest.TestCase):
00042
00043 def test_TVal(self, TVal=None, test_neg=True):
00044 if TVal is None:
00045
00046
00047 TVal=roslib.rostime.TVal
00048
00049
00050 v = TVal()
00051 self.assert_(repr(v))
00052 self.assert_(str(v))
00053 self.assertEquals(0, v.secs)
00054 self.assertEquals(0, v.nsecs)
00055 self.failIf(v)
00056 self.assert_(v.is_zero())
00057 self.assertEquals('0', str(v))
00058 self.assertEquals(0, v.to_nsec())
00059 self.assertEquals(0, v.to_sec())
00060
00061 self.assertEquals(v, v)
00062 self.assertEquals(v, TVal())
00063 self.assertEquals(v, TVal(0))
00064 self.assertEquals(v, TVal(0, 0))
00065 self.assertEquals(v.__hash__(), TVal(0, 0).__hash__())
00066
00067 self.assert_(v != TVal(0,1))
00068 self.assert_(v >= TVal())
00069 self.assert_(v <= TVal())
00070 self.assert_(v < TVal(0,1))
00071 self.assert_(TVal(0,1) > v)
00072 v.set(0, 0)
00073 self.assertEquals(0, v.secs)
00074 self.assertEquals(0, v.nsecs)
00075 v.set(1, 0)
00076 self.assertEquals(1, v.secs)
00077 self.assertEquals(0, v.nsecs)
00078 v.set(0, 1)
00079 self.assertEquals(0, v.secs)
00080 self.assertEquals(1, v.nsecs)
00081
00082 v.set(0, 1000000000)
00083 self.assertEquals(0, v.secs)
00084 self.assertEquals(1000000000, v.nsecs)
00085 v.canon()
00086 self.assertEquals(1, v.secs)
00087 self.assertEquals(0, v.nsecs)
00088
00089
00090 v = TVal(1)
00091 self.assertEquals(1, v.secs)
00092 self.assertEquals(0, v.nsecs)
00093 self.assert_(v)
00094 self.failIf(v.is_zero())
00095 self.assertEquals('1000000000', str(v))
00096 self.assertEquals(1000000000, v.to_nsec())
00097 self.assertEquals(v, v)
00098 self.assertEquals(v, TVal(1))
00099 self.assertEquals(v, TVal(1, 0))
00100 self.assertEquals(v, TVal(0,1000000000))
00101 self.assertEquals(v.__hash__(), TVal(0,1000000000).__hash__())
00102 self.assertNotEquals(v, TVal(0, 0))
00103 self.assertNotEquals(v.__hash__(), TVal(0, 0).__hash__())
00104 self.assertEquals(NotImplemented, v.__ge__(0))
00105 class Foo(object): pass
00106 self.assertEquals(NotImplemented, v.__gt__(Foo()))
00107 self.assertEquals(NotImplemented, v.__ge__(Foo()))
00108 self.assertEquals(NotImplemented, v.__le__(Foo()))
00109 self.assertEquals(NotImplemented, v.__lt__(Foo()))
00110 self.failIf(v.__eq__(Foo()))
00111 self.assert_(v.__ne__(Foo()))
00112 self.assert_(v >= TVal())
00113 self.assert_(v <= TVal(1))
00114 self.assert_(v <= TVal(1,0))
00115 self.assert_(v <= TVal(2,0))
00116 self.assert_(v < TVal(2))
00117 self.assert_(v < TVal(1,1))
00118 self.assert_(TVal(1,1) > v)
00119 self.assert_(TVal(2) > v)
00120
00121 v = TVal(0, 1)
00122 self.assertEquals(0, v.secs)
00123 self.assertEquals(1, v.nsecs)
00124 self.assert_(v)
00125 self.failIf(v.is_zero())
00126 self.assertEquals('1', str(v))
00127 self.assertEquals(1, v.to_nsec())
00128 self.assertEquals(v, v)
00129 self.assertEquals(v, TVal(0,1))
00130 self.assertNotEquals(v, TVal(0, 0))
00131 self.assert_(v >= TVal())
00132 self.assert_(v <= TVal(1))
00133 self.assert_(v <= TVal(0,1))
00134 self.assert_(v <= TVal(2,0))
00135 self.assert_(v < TVal(0,2))
00136 self.assert_(v < TVal(1))
00137 self.assert_(TVal(1) > v)
00138 self.assert_(TVal(0,2) > v)
00139
00140 v = TVal(1, 1000000000)
00141 self.assertEquals(2, v.secs)
00142 self.assertEquals(0, v.nsecs)
00143 self.assertEquals(2, v.to_sec())
00144 self.assertEquals(2000000000, v.to_nsec())
00145
00146 v = TVal(1, 1000000001)
00147 self.assertEquals(2, v.secs)
00148 self.assertEquals(1, v.nsecs)
00149 self.assertEquals(2.000000001, v.to_sec())
00150 self.assertEquals(2000000001, v.to_nsec())
00151
00152 v = TVal(1, -1000000000)
00153 self.assertEquals(0, v.secs)
00154 self.assertEquals(0, v.nsecs)
00155 v = TVal(1, -999999999)
00156 self.assertEquals(0, v.secs)
00157 self.assertEquals(1, v.nsecs)
00158 self.assertEquals(0.000000001, v.to_sec())
00159 self.assertEquals(1, v.to_nsec())
00160
00161 if test_neg:
00162 v = TVal(-1, -1000000000)
00163 self.assertEquals(-2, v.secs)
00164 self.assertEquals(0, v.nsecs)
00165 self.assertEquals(-2, v.to_sec())
00166 self.assertEquals(-2000000000, v.to_nsec())
00167
00168 v = TVal(-2, 1000000000)
00169 self.assertEquals(-1, v.secs)
00170 self.assertEquals(0, v.nsecs)
00171 self.assertEquals(-1, v.to_sec())
00172 self.assertEquals(-1000000000, v.to_nsec())
00173
00174
00175
00176 self.assertEquals(TVal(1).__hash__(), TVal(1).__hash__())
00177 self.assertEquals(TVal(1,1).__hash__(), TVal(1,1).__hash__())
00178 self.assertNotEquals(TVal(1).__hash__(), TVal(2).__hash__())
00179 self.assertNotEquals(TVal(1,1).__hash__(), TVal(1,2).__hash__())
00180 self.assertNotEquals(TVal(1,1).__hash__(), TVal(2,1).__hash__())
00181
00182 def test_Time(self):
00183 Time = roslib.rostime.Time
00184 Duration = roslib.rostime.Duration
00185 self.test_TVal(TVal=Time, test_neg=False)
00186
00187
00188 failed = False
00189 try:
00190 v = Duration.from_sec(0.1) > Time.from_sec(0.5)
00191 failed = True
00192 except: pass
00193 self.failIf(failed, "should have failed to compare")
00194 try:
00195 v = Time.from_sec(0.4) > Duration.from_sec(0.1)
00196 failed = True
00197 except: pass
00198 self.failIf(failed, "should have failed to compare")
00199
00200
00201
00202
00203 try:
00204 Time(-1)
00205 failed = True
00206 except: pass
00207 self.failIf(failed, "negative time not allowed")
00208 try:
00209 Time(1, -1000000001)
00210 failed = True
00211 except: pass
00212 self.failIf(failed, "negative time not allowed")
00213
00214
00215 import time
00216 t = time.time()
00217 v = Time.from_sec(t)
00218 self.assertEquals(v.to_sec(), t)
00219
00220 self.assertEquals(Time.from_sec(0), Time())
00221 self.assertEquals(Time.from_sec(1.), Time(1))
00222 self.assertEquals(Time.from_sec(v.to_sec()), v)
00223 self.assertEquals(v.from_sec(v.to_sec()), v)
00224
00225 self.assertEquals(v.to_sec(), v.to_time())
00226
00227
00228
00229 try:
00230 v = Time(1,0) + Time(1, 0)
00231 failed = True
00232 except: pass
00233 self.failIf(failed, "Time + Time must fail")
00234
00235
00236 v = Time(1,0) + Duration(1, 0)
00237 self.assertEquals(Time(2, 0), v)
00238 v = Duration(1, 0) + Time(1,0)
00239 self.assertEquals(Time(2, 0), v)
00240 v = Time(1,1) + Duration(1, 1)
00241 self.assertEquals(Time(2, 2), v)
00242 v = Duration(1, 1) + Time(1,1)
00243 self.assertEquals(Time(2, 2), v)
00244
00245 v = Time(1) + Duration(0, 1000000000)
00246 self.assertEquals(Time(2), v)
00247 v = Duration(1) + Time(0, 1000000000)
00248 self.assertEquals(Time(2), v)
00249
00250 v = Time(100, 100) + Duration(300)
00251 self.assertEquals(Time(400, 100), v)
00252 v = Duration(300) + Time(100, 100)
00253 self.assertEquals(Time(400, 100), v)
00254
00255 v = Time(100, 100) + Duration(300, 300)
00256 self.assertEquals(Time(400, 400), v)
00257 v = Duration(300, 300) + Time(100, 100)
00258 self.assertEquals(Time(400, 400), v)
00259
00260 v = Time(100, 100) + Duration(300, -101)
00261 self.assertEquals(Time(399, 999999999), v)
00262 v = Duration(300, -101) + Time(100, 100)
00263 self.assertEquals(Time(399, 999999999), v)
00264
00265
00266 try:
00267 v = Time(1,0) - 1
00268 failed = True
00269 except: pass
00270 self.failIf(failed, "Time - non Duration must fail")
00271 class Foob(object): pass
00272 try:
00273 v = Time(1,0) - Foob()
00274 failed = True
00275 except: pass
00276 self.failIf(failed, "Time - non TVal must fail")
00277
00278
00279 v = Time(1,0) - Duration(1, 0)
00280 self.assertEquals(Time(), v)
00281
00282 v = Time(1,1) - Duration(-1, -1)
00283 self.assertEquals(Time(2, 2), v)
00284 v = Time(1) - Duration(0, 1000000000)
00285 self.assertEquals(Time(), v)
00286 v = Time(2) - Duration(0, 1000000000)
00287 self.assertEquals(Time(1), v)
00288 v = Time(400, 100) - Duration(300)
00289 self.assertEquals(Time(100, 100), v)
00290 v = Time(100, 100) - Duration(0, 101)
00291 self.assertEquals(Time(99, 999999999), v)
00292
00293
00294 v = Time(100, 100) - Time(100, 100)
00295 self.assertEquals(Duration(), v)
00296 v = Time(100, 100) - Time(100)
00297 self.assertEquals(Duration(0,100), v)
00298 v = Time(100) - Time(200)
00299 self.assertEquals(Duration(-100), v)
00300
00301
00302 self.assertEquals(Time.from_sec(0.5), Time(0.5))
00303 t = Time(0.5)
00304 self.assert_(type(t.secs) == int)
00305 self.assertEquals(0, t.secs)
00306 self.assertEquals(500000000, t.nsecs)
00307
00308 try:
00309 Time(0.5, 0.5)
00310 self.fail("should have thrown value error")
00311 except ValueError: pass
00312
00313 def test_Duration(self):
00314 Duration = roslib.rostime.Duration
00315 self.test_TVal(TVal=Duration, test_neg=True)
00316
00317
00318 v = Duration(1000)
00319 self.assertEquals(v, Duration.from_sec(v.to_sec()))
00320 self.assertEquals(v, v.from_sec(v.to_sec()))
00321 v = Duration(0,1000)
00322 self.assertEquals(v, Duration.from_sec(v.to_sec()))
00323 self.assertEquals(v, v.from_sec(v.to_sec()))
00324
00325
00326 v = -Duration(1, -1)
00327 self.assertEquals(-1, v.secs)
00328 self.assertEquals(1, v.nsecs)
00329 v = -Duration(-1, -1)
00330 self.assertEquals(1, v.secs)
00331 self.assertEquals(1, v.nsecs)
00332 v = -Duration(-1, 1)
00333 self.assertEquals(0, v.secs)
00334 self.assertEquals(999999999, v.nsecs)
00335
00336
00337 failed = False
00338 try:
00339 v = Duration(1,0) + Time(1, 0)
00340 failed = True
00341 except: pass
00342 self.failIf(failed, "Duration + Time must fail")
00343 try:
00344 v = Duration(1,0) + 1
00345 failed = True
00346 except: pass
00347 self.failIf(failed, "Duration + int must fail")
00348
00349 v = Duration(1,0) + Duration(1, 0)
00350 self.assertEquals(2, v.secs)
00351 self.assertEquals(0, v.nsecs)
00352 self.assertEquals(Duration(2, 0), v)
00353 v = Duration(-1,-1) + Duration(1, 1)
00354 self.assertEquals(0, v.secs)
00355 self.assertEquals(0, v.nsecs)
00356 self.assertEquals(Duration(), v)
00357 v = Duration(1) + Duration(0, 1000000000)
00358 self.assertEquals(2, v.secs)
00359 self.assertEquals(0, v.nsecs)
00360 self.assertEquals(Duration(2), v)
00361 v = Duration(100, 100) + Duration(300)
00362 self.assertEquals(Duration(400, 100), v)
00363 v = Duration(100, 100) + Duration(300, 300)
00364 self.assertEquals(Duration(400, 400), v)
00365 v = Duration(100, 100) + Duration(300, -101)
00366 self.assertEquals(Duration(399, 999999999), v)
00367
00368
00369 try:
00370 v = Duration(1,0) - 1
00371 failed = True
00372 except: pass
00373 self.failIf(failed, "Duration - non duration must fail")
00374 try:
00375 v = Duration(1, 0) - Time(1,0)
00376 failed = True
00377 except: pass
00378 self.failIf(failed, "Duration - Time must fail")
00379
00380 v = Duration(1,0) - Duration(1, 0)
00381 self.assertEquals(Duration(), v)
00382 v = Duration(-1,-1) - Duration(1, 1)
00383 self.assertEquals(Duration(-3, 999999998), v)
00384 v = Duration(1) - Duration(0, 1000000000)
00385 self.assertEquals(Duration(), v)
00386 v = Duration(2) - Duration(0, 1000000000)
00387 self.assertEquals(Duration(1), v)
00388 v = Duration(100, 100) - Duration(300)
00389 self.assertEquals(Duration(-200, 100), v)
00390 v = Duration(100, 100) - Duration(300, 101)
00391 self.assertEquals(Duration(-201, 999999999), v)
00392
00393
00394 self.assertEquals(abs(Duration()), Duration())
00395 self.assertEquals(abs(Duration(1)), Duration(1))
00396 self.assertEquals(abs(Duration(-1)), Duration(1))
00397 self.assertEquals(abs(Duration(0,-1)), Duration(0,1))
00398 self.assertEquals(abs(Duration(-1,-1)), Duration(1,1))
00399
00400
00401 self.assertEquals(Duration.from_sec(0.5), Duration(0.5))
00402 t = Duration(0.5)
00403 self.assert_(type(t.secs) == int)
00404 self.assertEquals(0, t.secs)
00405 self.assertEquals(500000000, t.nsecs)
00406
00407 try:
00408 Duration(0.5, 0.5)
00409 self.fail("should have thrown value error")
00410 except ValueError: pass
00411
00412
00413 self.assertEquals(Duration(4), Duration(2) * 2)
00414 self.assertEquals(Duration(4), Duration(2) * 2.)
00415 self.assertEquals(Duration(10), Duration(4) * 2.5)
00416 self.assertEquals(Duration(4, 8), Duration(2, 4) * 2)
00417 v = Duration(4, 8) - (Duration(2, 4) * 2.)
00418 self.assert_(abs(v.to_nsec()) < 100)
00419 v = Duration(5, 10) - (Duration(2, 4) * 2.5)
00420 self.assert_(abs(v.to_nsec()) < 100)
00421
00422
00423 self.assertEquals(Duration(4), Duration(8) / 2)
00424 self.assertEquals(Duration(4), Duration(8) / 2.)
00425 self.assertEquals(Duration(4), Duration(8) // 2)
00426 self.assertEquals(Duration(4), Duration(8) // 2.)
00427 self.assertEquals(Duration(4), Duration(9) // 2)
00428 self.assertEquals(Duration(4), Duration(9) // 2.)
00429 self.assertEquals(Duration(4, 2), Duration(8, 4) / 2)
00430 v = Duration(4, 2) - (Duration(8, 4) / 2.)
00431 self.assert_(abs(v.to_nsec()) < 100)
00432
00433 self.assertEquals(Duration(4, 2), Duration(8, 4) // 2)
00434 self.assertEquals(Duration(4, 2), Duration(9, 5) // 2)
00435 v = Duration(4, 2) - (Duration(9, 5) // 2.)
00436 self.assert_(abs(v.to_nsec()) < 100)
00437
00438 if __name__ == '__main__':
00439 rosunit.unitrun('test_roslib', 'test_rostime', RostimeTest, coverage_packages=['roslib.rostime'])
00440