$search
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 # Revision $Id$ 00035 00036 import roslib; roslib.load_manifest('test_rospy') 00037 00038 import os 00039 import sys 00040 import struct 00041 import unittest 00042 from cStringIO import StringIO 00043 import time 00044 import random 00045 import math 00046 00047 from roslib.message import SerializationError 00048 00049 class TestGenmsgPy(unittest.TestCase): 00050 00051 def test_PythonKeyword(self): 00052 from test_rospy.msg import PythonKeyword 00053 # the md5sum is pulled from the c++ message generator. The 00054 # test here is that the Python msg generator didn't 00055 # accidentally mutate a md5sum based on a message that has its 00056 # fieldname remapped. 00057 self.assertEquals(PythonKeyword._md5sum, "1330d6bbfad8e75334346fec949d5133") 00058 00059 ## Utility for testing roundtrip serialization 00060 ## @param orig Message to test roundtrip serialization of 00061 ## @param blank Uninitialized instance of message to deserialize into 00062 ## @param float bool: if True, use almostEquals instead of equals 00063 ## comparison. This variant assumes only data field is named 00064 ## 'data' 00065 def _test_ser_deser(self, orig, blank, float=False): 00066 b = StringIO() 00067 orig.serialize(b) 00068 blank.deserialize(b.getvalue()) 00069 if not float: 00070 self.assertEquals(orig, blank, str(orig)+" != "+str(blank)) 00071 else: 00072 self.assertAlmostEquals(orig.data, blank.data, 5) 00073 00074 ## #2133/2152 00075 def test_test_rospy_TransitiveImport(self): 00076 from test_rospy.msg import TransitiveImport 00077 m = TransitiveImport() 00078 # invoking serialize should be enough to expose issue. The bug 00079 # was that genmsg_py was failing to include the imports of 00080 # embedded messages. Because messages are flattened, this 00081 # causes ImportErrors. 00082 self._test_ser_deser(m, TransitiveImport()) 00083 00084 def test_test_rospy_TestFixedArray(self): 00085 from test_rospy.msg import TestFixedArray 00086 m = TestFixedArray() 00087 self.assertEquals([0.], m.f32_1) 00088 self.assertEquals([0., 0., 0.], m.f32_3) 00089 self.assertEquals([0.], m.f64_1) 00090 self.assertEquals([0., 0., 0.], m.f64_3) 00091 self.assertEquals([0], m.i8_1) 00092 self.assertEquals([0, 0, 0], m.i8_3) 00093 self.assertEquals(chr(0), m.u8_1) 00094 self.assertEquals(chr(0)*3, m.u8_3) 00095 self.assertEquals([0], m.i32_1) 00096 self.assertEquals([0, 0, 0], m.i32_3) 00097 self.assertEquals([0], m.u32_1) 00098 self.assertEquals([0, 0, 0], m.u32_3) 00099 self.assertEquals([''], m.s_1) 00100 self.assertEquals(['', '', ''], m.s_3) 00101 00102 self._test_ser_deser(m, TestFixedArray()) 00103 00104 m = TestFixedArray(i32_1 = [1]) 00105 c = TestFixedArray() 00106 self._test_ser_deser(m, c) 00107 self.assertEquals((1,), c.i32_1) 00108 00109 m = TestFixedArray(i32_3 = [-3, 2, 10]) 00110 c = TestFixedArray() 00111 self._test_ser_deser(m, c) 00112 self.assertEquals((-3, 2, 10), c.i32_3) 00113 00114 m = TestFixedArray(u32_1 = [1234]) 00115 c = TestFixedArray() 00116 self._test_ser_deser(m, c) 00117 self.assertEquals((1234,), c.u32_1) 00118 00119 m = TestFixedArray(u32_3 = [3, 2, 10]) 00120 c = TestFixedArray() 00121 self._test_ser_deser(m, c) 00122 self.assertEquals((3, 2, 10), c.u32_3) 00123 00124 # this could potentially fail due to floating point lossiness 00125 m,c = TestFixedArray(f32_1 = [2.]), TestFixedArray() 00126 self._test_ser_deser(m, c) 00127 self.assertEquals((2.,), c.f32_1) 00128 00129 m,c = TestFixedArray(f32_3 = [1., 2., 3.]), TestFixedArray() 00130 self._test_ser_deser(m, c) 00131 self.assertEquals((1., 2., 3.), c.f32_3) 00132 00133 m,c = TestFixedArray(u8_1 = 'x'), TestFixedArray() 00134 self._test_ser_deser(m, c) 00135 self.assertEquals('x', c.u8_1) 00136 00137 m,c = TestFixedArray(u8_3 = 'xyz'), TestFixedArray() 00138 self._test_ser_deser(m, c) 00139 self.assertEquals('xyz', c.u8_3) 00140 00141 m,c = TestFixedArray(s_1 = ['']), TestFixedArray() 00142 self._test_ser_deser(m, c) 00143 self.assertEquals([''], c.s_1) 00144 00145 m,c = TestFixedArray(s_1 = ['blah blah blah']), TestFixedArray() 00146 self._test_ser_deser(m, c) 00147 self.assertEquals(['blah blah blah',], c.s_1) 00148 00149 m = TestFixedArray(s_3 = ['', 'x', 'xyz']) 00150 c = TestFixedArray() 00151 self._test_ser_deser(m, c) 00152 self.assertEquals(['', 'x', 'xyz'], c.s_3) 00153 00154 for v in [True, False]: 00155 m = TestFixedArray(b_1 = [v]) 00156 c = TestFixedArray() 00157 self._test_ser_deser(m, c) 00158 self.assertEquals([v], c.b_1) 00159 00160 m = TestFixedArray(b_3 = [True, False, True]) 00161 c = TestFixedArray() 00162 self._test_ser_deser(m, c) 00163 self.assertEquals([True, False, True], c.b_3) 00164 00165 #TODO: enable tests for auto-convert of uint8[] to string 00166 00167 def test_test_rospy_TestConstants(self): 00168 from test_rospy.msg import TestConstants 00169 self.assertEquals(-123.0, TestConstants.A) 00170 self.assertEquals(124.0, TestConstants.B) 00171 self.assertEquals(125.0, TestConstants.C) 00172 self.assertEquals(123, TestConstants.X) 00173 self.assertEquals(-123, TestConstants.Y) 00174 self.assertEquals(124, TestConstants.Z) 00175 self.assertEquals("'hi", TestConstants.SINGLEQUOTE) 00176 self.assertEquals('"hello" there', TestConstants.DOUBLEQUOTE) 00177 self.assertEquals('"hello" \'goodbye\'', TestConstants.MULTIQUOTE) 00178 self.assertEquals('foo', TestConstants.FOO) 00179 self.assertEquals('"#comments" are ignored, and leading and trailing whitespace removed',TestConstants.EXAMPLE) 00180 self.assertEquals('strip', TestConstants.WHITESPACE) 00181 self.assertEquals('', TestConstants.EMPTY) 00182 00183 self.assertEquals(True, TestConstants.TRUE) 00184 self.assertEquals(False, TestConstants.FALSE) 00185 00186 def test_std_msgs_empty(self): 00187 from std_msgs.msg import Empty 00188 self.assertEquals(Empty(), Empty()) 00189 self._test_ser_deser(Empty(), Empty()) 00190 00191 def test_std_msgs_Bool(self): 00192 from std_msgs.msg import Bool 00193 self.assertEquals(Bool(), Bool()) 00194 self._test_ser_deser(Bool(), Bool()) 00195 # default value should be False 00196 self.assertEquals(False, Bool().data) 00197 # test various constructor permutations 00198 for v in [True, False]: 00199 self.assertEquals(Bool(v), Bool(v)) 00200 self.assertEquals(Bool(v), Bool(data=v)) 00201 self.assertEquals(Bool(data=v), Bool(data=v)) 00202 self.assertNotEquals(Bool(True), Bool(False)) 00203 00204 self._test_ser_deser(Bool(True), Bool()) 00205 self._test_ser_deser(Bool(False), Bool()) 00206 00207 # validate type cast to bool 00208 blank = Bool() 00209 b = StringIO() 00210 Bool(True).serialize(b) 00211 blank.deserialize(b.getvalue()) 00212 self.assert_(blank.data) 00213 self.assert_(type(blank.data) == bool) 00214 00215 b = StringIO() 00216 Bool(True).serialize(b) 00217 blank.deserialize(b.getvalue()) 00218 self.assert_(blank.data) 00219 self.assert_(type(blank.data) == bool) 00220 00221 00222 def test_std_msgs_String(self): 00223 from std_msgs.msg import String 00224 self.assertEquals(String(), String()) 00225 self.assertEquals('', String().data) 00226 # default value should be empty string 00227 self.assertEquals(String(''), String()) 00228 self.assertEquals(String(''), String('')) 00229 self.assertEquals(String('foo'), String('foo')) 00230 self.assertEquals(String('foo'), String(data='foo')) 00231 self.assertEquals(String(data='foo'), String(data='foo')) 00232 00233 self.assertNotEquals(String('foo'), String('bar')) 00234 self.assertNotEquals(String('foo'), String(data='bar')) 00235 self.assertNotEquals(String(data='foo'), String(data='bar')) 00236 00237 self._test_ser_deser(String(''), String()) 00238 self._test_ser_deser(String('a man a plan a canal panama'), String()) 00239 00240 def test_std_msgs_SignedInt(self): 00241 from std_msgs.msg import Byte, Int8, Int16, Int32, Int64 00242 for cls in [Byte, Int8, Int16, Int32, Int64]: 00243 v = random.randint(1, 127) 00244 self.assertEquals(cls(), cls()) 00245 self.assertEquals(0, cls().data) 00246 self.assertEquals(cls(), cls(0)) 00247 self.assertEquals(cls(0), cls(0)) 00248 self.assertEquals(cls(v), cls(v)) 00249 self.assertEquals(cls(-v), cls(-v)) 00250 self.assertEquals(cls(v), cls(data=v)) 00251 self.assertEquals(cls(data=v), cls(data=v)) 00252 00253 self.assertNotEquals(cls(v), cls()) 00254 self.assertNotEquals(cls(data=v), cls(data=-v)) 00255 self.assertNotEquals(cls(data=v), cls(data=v-1)) 00256 self.assertNotEquals(cls(data=v), cls(v-1)) 00257 self.assertNotEquals(cls(v), cls(v-1)) 00258 00259 self._test_ser_deser(cls(), cls()) 00260 self._test_ser_deser(cls(0), cls()) 00261 self._test_ser_deser(cls(-v), cls()) 00262 self._test_ser_deser(cls(v), cls()) 00263 00264 # rospy currently does not spot negative overflow due to the fact that Python's struct doesn't either 00265 widths = [(8, Byte), (8, Int8), (16, Int16), (32, Int32), (64, Int64)] 00266 for w, cls in widths: 00267 maxp = long(math.pow(2, w-1)) - 1 00268 maxn = -long(math.pow(2, w-1)) + 1 00269 self._test_ser_deser(cls(maxp), cls()) 00270 self._test_ser_deser(cls(maxn), cls()) 00271 try: 00272 cls(maxp+1)._check_types() 00273 self.fail("check_types should have noted width error[%s]: %s, %s"%(w, maxp+1, cls.__name__)) 00274 except SerializationError: pass 00275 try: 00276 cls(maxn-1)._check_types() 00277 self.fail("check_types should have noted width error[%s]: %s, %s"%(w, maxn-1, cls.__name__)) 00278 except SerializationError: pass 00279 00280 def test_std_msgs_UnsignedInt(self): 00281 from std_msgs.msg import Char, UInt8, UInt16, UInt32, UInt64 00282 for cls in [Char, UInt8, UInt16, UInt32, UInt64]: 00283 v = random.randint(1, 127) 00284 self.assertEquals(cls(), cls()) 00285 self.assertEquals(0, cls().data) 00286 self.assertEquals(cls(), cls(0)) 00287 self.assertEquals(cls(0), cls(0)) 00288 self.assertEquals(cls(v), cls(v)) 00289 self.assertEquals(cls(v), cls(data=v)) 00290 self.assertEquals(cls(data=v), cls(data=v)) 00291 00292 self.assertNotEquals(cls(v), cls()) 00293 self.assertNotEquals(cls(data=v), cls(data=-v)) 00294 self.assertNotEquals(cls(data=v), cls(data=v-1)) 00295 self.assertNotEquals(cls(data=v), cls(v-1)) 00296 self.assertNotEquals(cls(v), cls(v-1)) 00297 00298 self._test_ser_deser(cls(), cls()) 00299 self._test_ser_deser(cls(0), cls()) 00300 self._test_ser_deser(cls(v), cls()) 00301 00302 try: 00303 cls(-1)._check_types() 00304 self.fail("check_types should have noted sign error[%s]: %s"%(w, cls.__name__)) 00305 except SerializationError: pass 00306 00307 # rospy currently does not spot negative overflow due to the fact that Python's struct doesn't either 00308 widths = [(8, Char), (8, UInt8), (16, UInt16), (32, UInt32), (64, UInt64)] 00309 for w, cls in widths: 00310 maxp = long(math.pow(2, w)) - 1 00311 self._test_ser_deser(cls(maxp), cls()) 00312 try: 00313 cls(maxp+1)._check_types() 00314 self.fail("check_types should have noted width error[%s]: %s, %s"%(w, maxp+1, cls.__name__)) 00315 except SerializationError: pass 00316 00317 def test_std_msgs_Float(self): 00318 from std_msgs.msg import Float32, Float64 00319 for cls in [Float32, Float64]: 00320 self.assertEquals(cls(), cls()) 00321 self.assertEquals(0., cls().data) 00322 self.assertEquals(cls(), cls(0.)) 00323 self.assertEquals(cls(0.), cls(0.)) 00324 self.assertEquals(cls(1.), cls(1.)) 00325 self.assertEquals(cls(1.), cls(data=1.)) 00326 self.assertEquals(cls(data=1.), cls(data=1.)) 00327 self.assertEquals(cls(math.pi), cls(math.pi)) 00328 self.assertEquals(cls(math.pi), cls(data=math.pi)) 00329 self.assertEquals(cls(data=math.pi), cls(data=math.pi)) 00330 00331 self.assertNotEquals(cls(1.), cls()) 00332 self.assertNotEquals(cls(math.pi), cls()) 00333 self.assertNotEquals(cls(data=math.pi), cls(data=-math.pi)) 00334 self.assertNotEquals(cls(data=math.pi), cls(data=math.pi-1)) 00335 self.assertNotEquals(cls(data=math.pi), cls(math.pi-1)) 00336 self.assertNotEquals(cls(math.pi), cls(math.pi-1)) 00337 00338 self._test_ser_deser(cls(), cls()) 00339 self._test_ser_deser(cls(0.), cls()) 00340 self._test_ser_deser(cls(1.), cls(), float=True) 00341 self._test_ser_deser(cls(math.pi), cls(), float=True) 00342 00343 def test_std_msgs_MultiArray(self): 00344 # multiarray is good test of embed plus array type 00345 from std_msgs.msg import Int32MultiArray, MultiArrayDimension, MultiArrayLayout, UInt8MultiArray 00346 00347 dims = [MultiArrayDimension('foo', 1, 2), MultiArrayDimension('bar', 3, 4),\ 00348 MultiArrayDimension('foo2', 5, 6), MultiArrayDimension('bar2', 7, 8)] 00349 for d in dims: 00350 self.assertEquals(d, d) 00351 00352 # there was a bug with UInt8 arrays, so this is a regression 00353 # test. the buff was with the uint8[] type consistency 00354 buff = StringIO() 00355 self.assertEquals(UInt8MultiArray(),UInt8MultiArray()) 00356 self.assertEquals('',UInt8MultiArray().data) 00357 UInt8MultiArray().serialize(buff) 00358 self.assertEquals(UInt8MultiArray(layout=MultiArrayLayout()),UInt8MultiArray()) 00359 UInt8MultiArray(layout=MultiArrayLayout()).serialize(buff) 00360 data = ''.join([chr(i) for i in range(0, 100)]) 00361 v = UInt8MultiArray(data=data) 00362 self._test_ser_deser(UInt8MultiArray(data=data),UInt8MultiArray()) 00363 00364 self.assertEquals(Int32MultiArray(),Int32MultiArray()) 00365 self.assertEquals(Int32MultiArray(layout=MultiArrayLayout()),Int32MultiArray()) 00366 self.assertEquals(Int32MultiArray(layout=MultiArrayLayout(), data=[1, 2, 3]),Int32MultiArray(data=[1, 2, 3])) 00367 self.assertEquals(Int32MultiArray(layout=MultiArrayLayout(), data=[1, 2, 3]),\ 00368 Int32MultiArray(layout=MultiArrayLayout(),data=[1, 2, 3])) 00369 self.assertEquals(Int32MultiArray(layout=MultiArrayLayout(dim=[]), data=[1, 2, 3]),\ 00370 Int32MultiArray(layout=MultiArrayLayout(),data=[1, 2, 3])) 00371 self.assertEquals(Int32MultiArray(layout=MultiArrayLayout([], 0), data=[1, 2, 3]),\ 00372 Int32MultiArray(layout=MultiArrayLayout(),data=[1, 2, 3])) 00373 self.assertEquals(Int32MultiArray(layout=MultiArrayLayout(dim=[], data_offset=0), data=[1, 2, 3]),\ 00374 Int32MultiArray(layout=MultiArrayLayout(),data=[1, 2, 3])) 00375 self.assertEquals(Int32MultiArray(layout=MultiArrayLayout(dim=dims, data_offset=0), data=[1, 2, 3]),\ 00376 Int32MultiArray(layout=MultiArrayLayout(dim=dims),data=[1, 2, 3])) 00377 self.assertEquals(Int32MultiArray(layout=MultiArrayLayout(dims, 10), data=[1, 2, 3]),\ 00378 Int32MultiArray(layout=MultiArrayLayout(dim=dims,data_offset=10),data=[1, 2, 3])) 00379 00380 00381 self.assertNotEquals(Int32MultiArray(data=[1, 2, 3]),Int32MultiArray(data=[4,5,6])) 00382 self.assertNotEquals(Int32MultiArray(layout=MultiArrayLayout([], 1), data=[1, 2, 3]),\ 00383 Int32MultiArray(layout=MultiArrayLayout([], 0),data=[1, 2, 3])) 00384 self.assertNotEquals(Int32MultiArray(layout=MultiArrayLayout([], 1), data=[1, 2, 3]),\ 00385 Int32MultiArray(layout=MultiArrayLayout(dim=[]),data=[1, 2, 3])) 00386 self.assertNotEquals(Int32MultiArray(layout=MultiArrayLayout(dims, 10), data=[1, 2, 3]),\ 00387 Int32MultiArray(layout=MultiArrayLayout(dim=dims,data_offset=11),data=[1, 2, 3])) 00388 self.assertNotEquals(Int32MultiArray(layout=MultiArrayLayout(dim=dims, data_offset=10), data=[1, 2, 3]),\ 00389 Int32MultiArray(layout=MultiArrayLayout(dim=dims[1:],data_offset=10),data=[1, 2, 3])) 00390 00391 00392 self._test_ser_deser(Int32MultiArray(),Int32MultiArray()) 00393 self._test_ser_deser(Int32MultiArray(layout=MultiArrayLayout()),Int32MultiArray()) 00394 self._test_ser_deser(Int32MultiArray(data=[1, 2, 3]),Int32MultiArray()) 00395 00396 00397 if __name__ == '__main__': 00398 import rostest 00399 rostest.unitrun('test_rospy', sys.argv[0], TestGenmsgPy)