Go to the documentation of this file.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 try:
00037 from cStringIO import StringIO
00038 except ImportError:
00039 from io import StringIO
00040 import struct
00041 import unittest
00042 import time
00043
00044 try:
00045 import numpy
00046 disable = False
00047 except ImportError:
00048 print("cannot import numpy, test is disabled")
00049 disable = True
00050
00051
00052 from test_rospy.msg import Floats
00053
00054
00055 class TestRospyNumpy(unittest.TestCase):
00056
00057 def test_floats(self):
00058 if disable:
00059 return
00060 vals = [1.0, 2.1, 3.2, 4.3, 5.4, 6.5]
00061 b = StringIO()
00062 f = Floats(numpy.array([1.0, 2.1, 3.2, 4.3, 5.4, 6.5], dtype=numpy.float32))
00063 f.serialize(b)
00064
00065
00066 f2 = Floats()
00067 self.assert_(type(f2.data) == list)
00068 f2.deserialize(b.getvalue())
00069 for x, y in zip(f2.data, vals):
00070 self.assertAlmostEquals(x, y, 2)
00071
00072 from rospy.numpy_msg import numpy_msg
00073 f3 = numpy_msg(Floats)()
00074 if 0:
00075
00076 self.assert_(isinstance(f3.data, numpy.ndarray), type(f3.data))
00077 f3.deserialize(b.getvalue())
00078 self.assert_(isinstance(f3.data, numpy.ndarray), type(f3.data))
00079 v = numpy.equal(f3.data, numpy.array([1.0, 2.1, 3.2, 4.3, 5.4, 6.5], dtype=numpy.float32))
00080 self.assert_(v.all())
00081
00082 def test_class_identity(self):
00083 from rospy.numpy_msg import numpy_msg
00084 self.assert_(isinstance(numpy_msg(Floats)(), numpy_msg(Floats)))
00085
00086 FloatsNP = numpy_msg(Floats)
00087 FloatsNP2 = numpy_msg(Floats)
00088
00089 self.assert_(FloatsNP is FloatsNP2)