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


rosbridge_library
Author(s): Jonathan Mace
autogenerated on Fri Oct 21 2022 02:45:18