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


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