test_cbor_conversion.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 import rostest
3 import sys
4 import unittest
5 
6 PYTHON2 = sys.version_info < (3, 0)
7 
8 import struct
9 from rosbridge_library.internal.cbor_conversion import extract_cbor_values, TAGGED_ARRAY_FORMATS
10 
11 try:
12  from cbor import Tag
13 except ImportError:
14  from rosbridge_library.util.cbor import Tag
15 
16 from std_msgs.msg import (
17  Bool, String,
18  Byte, Char,
19  Int8, Int16, Int32, Int64,
20  UInt8, UInt16, UInt32, UInt64,
21  Float32, Float64,
22  ByteMultiArray,
23  Int8MultiArray, Int16MultiArray, Int32MultiArray, Int64MultiArray,
24  UInt8MultiArray, UInt16MultiArray, UInt32MultiArray, UInt64MultiArray,
25  Float32MultiArray, Float64MultiArray,
26  Time, Duration,
27  Empty,
28  MultiArrayLayout, MultiArrayDimension,
29 )
30 
31 
32 class TestCBORConversion(unittest.TestCase):
33  def test_string(self):
34  msg = String(data="foo")
35  extracted = extract_cbor_values(msg)
36 
37  self.assertEqual(extracted['data'], msg.data)
38  if PYTHON2:
39  self.assertEqual(type(extracted['data']), unicode)
40  else:
41  self.assertEqual(type(extracted['data']), str)
42 
43  def test_bool(self):
44  for val in [True, False]:
45  msg = Bool(data=val)
46  extracted = extract_cbor_values(msg)
47 
48  self.assertEqual(extracted['data'], msg.data, 'val={}'.format(val))
49  self.assertEqual(type(extracted['data']), bool, 'val={}'.format(val))
50 
51  def test_numbers(self):
52  for msg_type in [Int8, Int16, Int32, Int64]:
53  msg = msg_type(data=-5)
54  extracted = extract_cbor_values(msg)
55 
56  self.assertEqual(extracted['data'], msg.data, 'type={}'.format(msg_type))
57  self.assertEqual(type(extracted['data']), int, 'type={}'.format(msg_type))
58 
59  for msg_type in [UInt8, UInt16, UInt32, UInt64]:
60  msg = msg_type(data=5)
61  extracted = extract_cbor_values(msg)
62 
63  self.assertEqual(extracted['data'], msg.data, 'type={}'.format(msg_type))
64  self.assertEqual(type(extracted['data']), int, 'type={}'.format(msg_type))
65 
66  for msg_type in [Float32, Float64]:
67  msg = msg_type(data=2.3)
68  extracted = extract_cbor_values(msg)
69 
70  self.assertEqual(extracted['data'], msg.data, 'type={}'.format(msg_type))
71  self.assertEqual(type(extracted['data']), float, 'type={}'.format(msg_type))
72 
73  def test_time(self):
74  for msg_type in [Time, Duration]:
75  msg = msg_type()
76  extracted = extract_cbor_values(msg)
77 
78  self.assertEqual(extracted['data']['secs'], msg.data.secs, 'type={}'.format(msg_type))
79  self.assertEqual(extracted['data']['nsecs'], msg.data.nsecs, 'type={}'.format(msg_type))
80  self.assertEqual(type(extracted['data']['secs']), int, 'type={}'.format(msg_type))
81  self.assertEqual(type(extracted['data']['nsecs']), int, 'type={}'.format(msg_type))
82 
83  def test_byte_array(self):
84  msg = UInt8MultiArray(data=[0, 1, 2])
85  extracted = extract_cbor_values(msg)
86 
87  data = extracted['data']
88  self.assertEqual(type(data), bytes)
89  for i, val in enumerate(msg.data):
90  if PYTHON2:
91  self.assertEqual(ord(data[i]), val)
92  else:
93  self.assertEqual(data[i], val)
94 
95  def test_numeric_array(self):
96  for msg_type in [Int8MultiArray, Int16MultiArray, Int32MultiArray, Int64MultiArray,
97  UInt16MultiArray, UInt32MultiArray, UInt64MultiArray,
98  Float32MultiArray, Float64MultiArray]:
99  msg = msg_type(data=[0, 1, 2])
100  extracted = extract_cbor_values(msg)
101 
102  tag = extracted['data']
103  self.assertEqual(type(tag), Tag, 'type={}'.format(msg_type))
104  self.assertEqual(type(tag.value), bytes, 'type={}'.format(msg_type))
105 
106  # This is as consistent as the message defs..
107  array_type = msg._slot_types[1]
108 
109  expected_tag = TAGGED_ARRAY_FORMATS[array_type][0]
110  self.assertEqual(tag.tag, expected_tag, 'type={}'.format(msg_type))
111 
112  fmt = TAGGED_ARRAY_FORMATS[array_type][1]
113  fmt_to_length = fmt.format(len(msg.data))
114  unpacked = list(struct.unpack(fmt_to_length, tag.value))
115 
116  self.assertEqual(unpacked, msg.data, 'type={}'.format(msg_type))
117 
119  msg = UInt8MultiArray(layout=MultiArrayLayout(
120  data_offset=5,
121  dim=[
122  MultiArrayDimension(
123  label="foo",
124  size=4,
125  stride=4,
126  ),
127  MultiArrayDimension(
128  label="bar",
129  size=8,
130  stride=8,
131  ),
132  ]
133  ))
134  extracted = extract_cbor_values(msg)
135 
136  ex_layout = extracted['layout']
137  self.assertEqual(type(ex_layout), dict)
138  self.assertEqual(ex_layout['data_offset'], msg.layout.data_offset)
139  self.assertEqual(len(ex_layout['dim']), len(msg.layout.dim))
140  for i, val in enumerate(msg.layout.dim):
141  self.assertEqual(ex_layout['dim'][i]['label'], val.label)
142  self.assertEqual(ex_layout['dim'][i]['size'], val.size)
143  self.assertEqual(ex_layout['dim'][i]['stride'], val.stride)
144 
145  def test_unicode_keys(self):
146  msg = String(data="foo")
147  extracted = extract_cbor_values(msg)
148 
149  keys = extracted.keys()
150  for key in keys:
151  if PYTHON2:
152  self.assertEqual(type(key), unicode)
153  else:
154  self.assertEqual(type(key), str)
155 
156 
157 PKG = 'rosbridge_library'
158 NAME = 'test_cbor_conversion'
159 if __name__ == '__main__':
160  rostest.unitrun(PKG, NAME, TestCBORConversion)


rosbridge_library
Author(s): Jonathan Mace
autogenerated on Fri May 10 2019 02:17:02