test_rospy_rostime.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 # Software License Agreement (BSD License)
00003 #
00004 # Copyright (c) 2008, Willow Garage, Inc.
00005 # All rights reserved.
00006 #
00007 # Redistribution and use in source and binary forms, with or without
00008 # modification, are permitted provided that the following conditions
00009 # are met:
00010 #
00011 #  * Redistributions of source code must retain the above copyright
00012 #    notice, this list of conditions and the following disclaimer.
00013 #  * Redistributions in binary form must reproduce the above
00014 #    copyright notice, this list of conditions and the following
00015 #    disclaimer in the documentation and/or other materials provided
00016 #    with the distribution.
00017 #  * Neither the name of Willow Garage, Inc. nor the names of its
00018 #    contributors may be used to endorse or promote products derived
00019 #    from this software without specific prior written permission.
00020 #
00021 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032 # POSSIBILITY OF SUCH DAMAGE.
00033 
00034 import os
00035 import sys
00036 import struct
00037 import unittest
00038 try:
00039     from cStringIO import StringIO
00040 except ImportError:
00041     from io import StringIO
00042 import time
00043 import random
00044 
00045 import rospy.rostime
00046 
00047 try:
00048     import cPickle as pickle
00049 except ImportError:
00050     import pickle    
00051 
00052 # currently tests rospy.rostime, rospy.simtime, and some parts of rospy.client
00053 
00054 class TestRospyTime(unittest.TestCase):
00055     
00056     def setUp(self):
00057         rospy.rostime.set_rostime_initialized(True)
00058 
00059     def test_import_simtime(self):
00060         # trip wire test, make sure the module is loading
00061         import rospy.impl.simtime
00062         # can't actually do unit tests of simtime, requires rosunit
00063 
00064     def test_switch_to_wallclock(self):
00065         rospy.rostime.switch_to_wallclock()
00066         self.assertAlmostEqual(time.time(), rospy.get_time(), 1)
00067 
00068     def test_Time_get_setstate(self):
00069         # use deepcopy to test getstate/setstate
00070         import copy, random
00071         a = rospy.Time(random.randint(0, 10000), random.randint(0, 10000))
00072         b = copy.deepcopy(a)
00073         self.assertEquals(a.secs, b.secs)
00074         self.assertEquals(a.nsecs, b.nsecs)
00075 
00076         buff = StringIO()
00077         pickle.dump(a, buff)
00078         buff.seek(0)
00079         c = pickle.load(buff)    
00080         self.assertEquals(a.secs, c.secs)
00081         self.assertEquals(a.nsecs, c.nsecs)
00082                                  
00083     def test_Duration_get_setstate(self):
00084         # use deepcopy to test getstate/setstate
00085         import copy, random
00086         a = rospy.Duration(random.randint(0, 10000), random.randint(0, 10000))
00087         b = copy.deepcopy(a)
00088         self.assertEquals(a.secs, b.secs)
00089         self.assertEquals(a.nsecs, b.nsecs)
00090         
00091         buff = StringIO()
00092         pickle.dump(a, buff)
00093         buff.seek(0)
00094         c = pickle.load(buff)    
00095         self.assertEquals(a.secs, c.secs)
00096         self.assertEquals(a.nsecs, c.nsecs)
00097 
00098     def test_Time(self):
00099         # This is a copy of test_roslib_rostime
00100         from rospy import Time, Duration
00101         # #1600 Duration > Time should fail
00102         failed = False
00103         try:
00104             v = Duration.from_sec(0.1) > Time.from_sec(0.5)
00105             failed = True
00106         except: pass
00107         self.failIf(failed, "should have failed to compare")
00108         try:
00109             v = Time.from_sec(0.4) > Duration.from_sec(0.1)
00110             failed = True        
00111         except: pass
00112         self.failIf(failed, "should have failed to compare")
00113 
00114 
00115         try: # neg time fails
00116             Time(-1)
00117             failed = True
00118         except: pass
00119         self.failIf(failed, "negative time not allowed")
00120         try:
00121             Time(1, -1000000001)
00122             failed = True
00123         except: pass
00124         self.failIf(failed, "negative time not allowed")
00125 
00126         # test Time.now() is within 10 seconds of actual time (really generous)
00127         import time
00128         t = time.time()
00129         v = Time.from_sec(t)
00130         self.assertEquals(v.to_sec(), t)
00131         # test from_sec()
00132         self.assertEquals(Time.from_sec(0), Time())
00133         self.assertEquals(Time.from_sec(1.), Time(1))
00134         self.assertEquals(Time.from_sec(v.to_sec()), v)
00135         self.assertEquals(v.from_sec(v.to_sec()), v)
00136 
00137         # test to_time()
00138         self.assertEquals(v.to_sec(), v.to_time())
00139 
00140         # test addition
00141         # - time + time fails
00142         try:
00143             v = Time(1,0) + Time(1, 0)
00144             failed = True
00145         except: pass
00146         self.failIf(failed, "Time + Time must fail")
00147 
00148         # - time + duration
00149         v = Time(1,0) + Duration(1, 0)
00150         self.assertEquals(Time(2, 0), v)
00151         v = Duration(1, 0) + Time(1,0)
00152         self.assertEquals(Time(2, 0), v)
00153         v = Time(1,1) + Duration(1, 1)
00154         self.assertEquals(Time(2, 2), v)
00155         v = Duration(1, 1) + Time(1,1)
00156         self.assertEquals(Time(2, 2), v)
00157 
00158         v = Time(1) + Duration(0, 1000000000)
00159         self.assertEquals(Time(2), v)
00160         v = Duration(1) + Time(0, 1000000000)
00161         self.assertEquals(Time(2), v)
00162 
00163         v = Time(100, 100) + Duration(300)
00164         self.assertEquals(Time(400, 100), v)
00165         v = Duration(300) + Time(100, 100)
00166         self.assertEquals(Time(400, 100), v)
00167 
00168         v = Time(100, 100) + Duration(300, 300)
00169         self.assertEquals(Time(400, 400), v)
00170         v = Duration(300, 300) + Time(100, 100)
00171         self.assertEquals(Time(400, 400), v)
00172 
00173         v = Time(100, 100) + Duration(300, -101)
00174         self.assertEquals(Time(399, 999999999), v)
00175         v =  Duration(300, -101) + Time(100, 100)
00176         self.assertEquals(Time(399, 999999999), v)
00177 
00178         # test subtraction
00179         try:
00180             v = Time(1,0) - 1
00181             failed = True
00182         except: pass
00183         self.failIf(failed, "Time - non Duration must fail")
00184         class Foob(object): pass      
00185         try:
00186             v = Time(1,0) - Foob()
00187             failed = True          
00188         except: pass
00189         self.failIf(failed, "Time - non TVal must fail")
00190 
00191         # - Time - Duration
00192         v = Time(1,0) - Duration(1, 0)
00193         self.assertEquals(Time(), v)
00194 
00195         v = Time(1,1) - Duration(-1, -1)
00196         self.assertEquals(Time(2, 2), v)
00197         v = Time(1) - Duration(0, 1000000000)
00198         self.assertEquals(Time(), v)
00199         v = Time(2) - Duration(0, 1000000000)
00200         self.assertEquals(Time(1), v)
00201         v = Time(400, 100) - Duration(300)
00202         self.assertEquals(Time(100, 100), v)
00203         v = Time(100, 100) - Duration(0, 101)
00204         self.assertEquals(Time(99, 999999999), v)
00205 
00206         # - Time - Time = Duration      
00207         v = Time(100, 100) - Time(100, 100)
00208         self.assertEquals(Duration(), v)
00209         v = Time(100, 100) - Time(100)
00210         self.assertEquals(Duration(0,100), v)
00211         v = Time(100) - Time(200)
00212         self.assertEquals(Duration(-100), v)
00213 
00214     def test_Duration(self):
00215         Duration = rospy.Duration
00216 
00217         # test from_sec
00218         v = Duration(1000)
00219         self.assertEquals(v, Duration.from_sec(v.to_sec()))
00220         self.assertEquals(v, v.from_sec(v.to_sec()))
00221         v = Duration(0,1000)
00222         self.assertEquals(v, Duration.from_sec(v.to_sec()))
00223         self.assertEquals(v, v.from_sec(v.to_sec()))
00224       
00225         # test neg
00226         v = -Duration(1, -1)
00227         self.assertEquals(-1, v.secs)
00228         self.assertEquals(1, v.nsecs)
00229         v = -Duration(-1, -1)
00230         self.assertEquals(1, v.secs)
00231         self.assertEquals(1, v.nsecs)
00232         v = -Duration(-1, 1)
00233         self.assertEquals(0, v.secs)
00234         self.assertEquals(999999999, v.nsecs)
00235       
00236         # test addition
00237         failed = False
00238         try:
00239             v = Duration(1,0) + Time(1, 0)
00240             failed = True
00241         except: pass
00242         self.failIf(failed, "Duration + Time must fail")
00243         try:
00244             v = Duration(1,0) + 1
00245             failed = True
00246         except: pass
00247         self.failIf(failed, "Duration + int must fail")
00248           
00249         v = Duration(1,0) + Duration(1, 0)
00250         self.assertEquals(2, v.secs)
00251         self.assertEquals(0, v.nsecs)
00252         self.assertEquals(Duration(2, 0), v)
00253         v = Duration(-1,-1) + Duration(1, 1)
00254         self.assertEquals(0, v.secs)
00255         self.assertEquals(0, v.nsecs)
00256         self.assertEquals(Duration(), v)
00257         v = Duration(1) + Duration(0, 1000000000)
00258         self.assertEquals(2, v.secs)
00259         self.assertEquals(0, v.nsecs)
00260         self.assertEquals(Duration(2), v)
00261         v = Duration(100, 100) + Duration(300)
00262         self.assertEquals(Duration(400, 100), v)
00263         v = Duration(100, 100) + Duration(300, 300)
00264         self.assertEquals(Duration(400, 400), v)
00265         v = Duration(100, 100) + Duration(300, -101)
00266         self.assertEquals(Duration(399, 999999999), v)
00267         
00268         # test subtraction
00269         try:
00270             v = Duration(1,0) - 1
00271             failed = True
00272         except: pass
00273         self.failIf(failed, "Duration - non duration must fail")
00274         try:
00275             v = Duration(1, 0) - Time(1,0)
00276             failed = True          
00277         except: pass
00278         self.failIf(failed, "Duration - Time must fail")
00279         
00280         v = Duration(1,0) - Duration(1, 0)
00281         self.assertEquals(Duration(), v)
00282         v = Duration(-1,-1) - Duration(1, 1)
00283         self.assertEquals(Duration(-3, 999999998), v)
00284         v = Duration(1) - Duration(0, 1000000000)
00285         self.assertEquals(Duration(), v)
00286         v = Duration(2) - Duration(0, 1000000000)
00287         self.assertEquals(Duration(1), v)
00288         v = Duration(100, 100) - Duration(300)
00289         self.assertEquals(Duration(-200, 100), v)
00290         v = Duration(100, 100) - Duration(300, 101)
00291         self.assertEquals(Duration(-201, 999999999), v)
00292 
00293         # test abs
00294         self.assertEquals(abs(Duration()), Duration())
00295         self.assertEquals(abs(Duration(1)), Duration(1))      
00296         self.assertEquals(abs(Duration(-1)), Duration(1))
00297         self.assertEquals(abs(Duration(0,-1)), Duration(0,1))
00298         self.assertEquals(abs(Duration(-1,-1)), Duration(1,1))
00299       
00300     def test_set_rostime(self):
00301         from rospy.rostime import _set_rostime
00302         from rospy import Time 
00303 
00304         self.assertAlmostEqual(time.time(), rospy.get_time(), 1)
00305 
00306         for t in [Time.from_sec(1.0), Time.from_sec(4.0)]:
00307             _set_rostime(t)
00308             self.assertEquals(t, rospy.get_rostime())
00309             self.assertEquals(t.to_time(), rospy.get_time())        
00310 
00311     def test_get_rostime(self):
00312         rospy.rostime.switch_to_wallclock()
00313         self.assertAlmostEqual(time.time(), rospy.get_time(), 1)
00314         self.assertAlmostEqual(time.time(), rospy.get_rostime().to_time(), 1)
00315         #rest of get_rostime implicitly tested by update_rostime tests
00316 
00317     def test_sleep(self):
00318         # test wallclock sleep
00319         rospy.rostime.switch_to_wallclock()
00320         rospy.sleep(0.1)
00321         rospy.sleep(rospy.Duration.from_sec(0.1))
00322         
00323         from rospy.rostime import _set_rostime
00324         from rospy import Time 
00325 
00326         t = Time.from_sec(1.0)
00327         _set_rostime(t)
00328         self.assertEquals(t, rospy.get_rostime())
00329         self.assertEquals(t.to_time(), rospy.get_time())        
00330 
00331         import threading
00332 
00333         #start sleeper
00334         self.failIf(test_sleep_done)
00335         sleepthread = threading.Thread(target=sleeper, args=())
00336         sleepthread.setDaemon(True)
00337         sleepthread.start()
00338         time.sleep(1.0) #make sure thread is spun up
00339         self.failIf(test_sleep_done)
00340 
00341         t = Time.from_sec(1000000.0)
00342         _set_rostime(t)
00343         time.sleep(0.5) #give sleeper time to wakeup
00344         self.assert_(test_sleep_done, "sleeper did not wake up")
00345 
00346         #start duration sleeper
00347         self.failIf(test_duration_sleep_done)      
00348         dursleepthread = threading.Thread(target=duration_sleeper, args=())
00349         dursleepthread.setDaemon(True)
00350         dursleepthread.start()
00351         time.sleep(1.0) #make sure thread is spun up
00352         self.failIf(test_duration_sleep_done)
00353 
00354         t = Time.from_sec(2000000.0)
00355         _set_rostime(t)
00356         time.sleep(0.5) #give sleeper time to wakeup
00357         self.assert_(test_sleep_done, "sleeper did not wake up")
00358 
00359         #start backwards sleeper
00360         self.failIf(test_backwards_sleep_done)
00361         backsleepthread = threading.Thread(target=backwards_sleeper, args=())
00362         backsleepthread.setDaemon(True)
00363         backsleepthread.start()
00364         time.sleep(1.0) #make sure thread is spun up
00365         self.failIf(test_backwards_sleep_done)
00366 
00367         t = Time.from_sec(1.0)
00368         _set_rostime(t)
00369         time.sleep(0.5) #give sleeper time to wakeup
00370         self.assert_(test_backwards_sleep_done, "backwards sleeper was not given an exception")
00371     
00372 test_duration_sleep_done = False
00373 def duration_sleeper():
00374     global test_duration_sleep_done
00375     rospy.sleep(rospy.Duration(10000.0))
00376     test_duration_sleep_done = True
00377 
00378 test_sleep_done = False
00379 def sleeper():
00380     global test_sleep_done
00381     rospy.sleep(10000.0)
00382     test_sleep_done = True
00383 test_backwards_sleep_done = False    
00384 def backwards_sleeper():
00385     global test_backwards_sleep_done
00386     try:
00387         rospy.sleep(10000.0)
00388     except rospy.ROSException:
00389         test_backwards_sleep_done = True


test_rospy
Author(s): Ken Conley
autogenerated on Thu Jun 6 2019 21:10:57