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
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
00053
00054
00055
00056 self.assertEquals(PythonKeyword._md5sum, "1330d6bbfad8e75334346fec949d5133")
00057
00058
00059
00060
00061
00062
00063
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
00074 def test_test_rospy_TransitiveImport(self):
00075 from test_rospy.msg import TransitiveImport
00076 m = TransitiveImport()
00077
00078
00079
00080
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
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
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
00195 self.assertEquals(False, Bool().data)
00196
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
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
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
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
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
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
00352
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())