test_rospy_numpy.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # Software License Agreement (BSD License)
3 #
4 # Copyright (c) 2008, Willow Garage, Inc.
5 # All rights reserved.
6 #
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions
9 # are met:
10 #
11 # * Redistributions of source code must retain the above copyright
12 # notice, this list of conditions and the following disclaimer.
13 # * Redistributions in binary form must reproduce the above
14 # copyright notice, this list of conditions and the following
15 # disclaimer in the documentation and/or other materials provided
16 # with the distribution.
17 # * Neither the name of Willow Garage, Inc. nor the names of its
18 # contributors may be used to endorse or promote products derived
19 # from this software without specific prior written permission.
20 #
21 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 # POSSIBILITY OF SUCH DAMAGE.
33 
34 import os
35 import sys
36 try:
37  from cStringIO import StringIO
38 except ImportError:
39  from io import StringIO
40 import struct
41 import unittest
42 import time
43 
44 try:
45  import numpy
46  disable = False
47 except ImportError:
48  print("cannot import numpy, test is disabled")
49  disable = True
50 
51 # this is partially a teste of the rospy/Tutorials/numpy
52 from test_rospy.msg import Floats
53 
54 # test rospy.names package
55 class TestRospyNumpy(unittest.TestCase):
56 
57  def test_floats(self):
58  if disable:
59  return
60  vals = [1.0, 2.1, 3.2, 4.3, 5.4, 6.5]
61  b = StringIO()
62  f = Floats(numpy.array([1.0, 2.1, 3.2, 4.3, 5.4, 6.5], dtype=numpy.float32))
63  f.serialize(b)
64 
65  # deserialize twice, once with numpy wrappers, once without
66  f2 = Floats()
67  self.assert_(type(f2.data) == list)
68  f2.deserialize(b.getvalue())
69  for x, y in zip(f2.data, vals):
70  self.assertAlmostEquals(x, y, 2)
71 
72  from rospy.numpy_msg import numpy_msg
73  f3 = numpy_msg(Floats)()
74  if 0:
75  # future
76  self.assert_(isinstance(f3.data, numpy.ndarray), type(f3.data))
77  f3.deserialize(b.getvalue())
78  self.assert_(isinstance(f3.data, numpy.ndarray), type(f3.data))
79  v = numpy.equal(f3.data, numpy.array([1.0, 2.1, 3.2, 4.3, 5.4, 6.5], dtype=numpy.float32))
80  self.assert_(v.all())
81 
83  from rospy.numpy_msg import numpy_msg
84  self.assert_(isinstance(numpy_msg(Floats)(), numpy_msg(Floats)))
85 
86  FloatsNP = numpy_msg(Floats)
87  FloatsNP2 = numpy_msg(Floats)
88 
89  self.assert_(FloatsNP is FloatsNP2)


test_rospy
Author(s): Ken Conley
autogenerated on Sun Feb 3 2019 03:30:22