6 PYTHON2 = sys.version_info < (3, 0)
19 Int8, Int16, Int32, Int64,
20 UInt8, UInt16, UInt32, UInt64,
23 Int8MultiArray, Int16MultiArray, Int32MultiArray, Int64MultiArray,
24 UInt8MultiArray, UInt16MultiArray, UInt32MultiArray, UInt64MultiArray,
25 Float32MultiArray, Float64MultiArray,
28 MultiArrayLayout, MultiArrayDimension,
32 PKG =
'rosbridge_library'
33 NAME =
'test_cbor_conversion'
38 msg = String(data=
"foo")
41 self.assertEqual(extracted[
'data'], msg.data)
43 self.assertEqual(type(extracted[
'data']), unicode)
45 self.assertEqual(type(extracted[
'data']), str)
48 for val
in [
True,
False]:
52 self.assertEqual(extracted[
'data'], msg.data,
'val={}'.format(val))
53 self.assertEqual(type(extracted[
'data']), bool,
'val={}'.format(val))
56 for msg_type
in [Int8, Int16, Int32, Int64]:
57 msg = msg_type(data=-5)
60 self.assertEqual(extracted[
'data'], msg.data,
'type={}'.format(msg_type))
61 self.assertEqual(type(extracted[
'data']), int,
'type={}'.format(msg_type))
63 for msg_type
in [UInt8, UInt16, UInt32, UInt64]:
64 msg = msg_type(data=5)
67 self.assertEqual(extracted[
'data'], msg.data,
'type={}'.format(msg_type))
68 self.assertEqual(type(extracted[
'data']), int,
'type={}'.format(msg_type))
70 for msg_type
in [Float32, Float64]:
71 msg = msg_type(data=2.3)
74 self.assertEqual(extracted[
'data'], msg.data,
'type={}'.format(msg_type))
75 self.assertEqual(type(extracted[
'data']), float,
'type={}'.format(msg_type))
78 for msg_type
in [Time, Duration]:
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))
88 msg = UInt8MultiArray(data=[0, 1, 2])
91 data = extracted[
'data']
92 self.assertEqual(type(data), bytes)
93 for i, val
in enumerate(msg.data):
95 self.assertEqual(ord(data[i]), val)
97 self.assertEqual(data[i], val)
100 for msg_type
in [Int8MultiArray, Int16MultiArray, Int32MultiArray, Int64MultiArray,
101 UInt16MultiArray, UInt32MultiArray, UInt64MultiArray,
102 Float32MultiArray, Float64MultiArray]:
103 msg = msg_type(data=[0, 1, 2])
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))
111 array_type = msg._slot_types[1]
113 expected_tag = TAGGED_ARRAY_FORMATS[array_type][0]
114 self.assertEqual(tag.tag, expected_tag,
'type={}'.format(msg_type))
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))
120 self.assertEqual(unpacked, msg.data,
'type={}'.format(msg_type))
123 msg = UInt8MultiArray(layout=MultiArrayLayout(
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)
150 msg = String(data=
"foo")
153 keys = extracted.keys()
156 self.assertEqual(type(key), unicode)
158 self.assertEqual(type(key), str)
161 if __name__ ==
'__main__':
162 rosunit.unitrun(PKG, NAME, TestCBORConversion)