-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_py_stuff_sack.py
354 lines (265 loc) · 10.7 KB
/
test_py_stuff_sack.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
import ctypes
import os
import unittest
import test.test_message_def as msg_def
class TestBitfield(unittest.TestCase):
@staticmethod
def get_test_message():
msg = msg_def.Bitfield4BytesTest()
msg.bitfield.field0 = 6
msg.bitfield.field1 = 27
msg.bitfield.field2 = 264
buf = msg_def.Bitfield4BytesTest.get_buffer()
buf[6:] = [0x00, 0x01, 0x08, 0xde]
return msg, buf
def test_size(self):
self.assertEqual(8, msg_def.Bitfield2BytesTest.packed_size)
self.assertEqual(10, msg_def.Bitfield4BytesTest.packed_size)
def test_pack_unpack(self):
msg, buf = self.get_test_message()
packed = msg.pack()
self.assertEqual(buf[6:], packed[6:])
unpacked = msg_def.Bitfield4BytesTest.unpack(packed)
self.assertEqual(msg.bitfield.field0, unpacked.bitfield.field0)
self.assertEqual(msg.bitfield.field1, unpacked.bitfield.field1)
self.assertEqual(msg.bitfield.field2, unpacked.bitfield.field2)
def test_description(self):
self.assertEqual('This is a bitfield description.', msg_def.Bitfield4Bytes.description)
def test_field_description(self):
self.assertEqual('This is a bitfield field description.',
msg_def.Bitfield4Bytes.field_descriptions[0])
class TestEnum(unittest.TestCase):
@staticmethod
def get_test_message():
msg = msg_def.Enum2BytesTest()
msg.enumeration = len(msg_def.Enum2Bytes)
buf = msg_def.Enum2BytesTest.get_buffer()
buf[6:] = [0x00, 0x80]
return msg, buf
def test_size(self):
self.assertEqual(127, len(msg_def.Enum1Bytes))
self.assertEqual(7, msg_def.Enum1BytesTest.packed_size)
self.assertEqual(128, len(msg_def.Enum2Bytes))
self.assertEqual(8, msg_def.Enum2BytesTest.packed_size)
self.assertEqual(ctypes.sizeof(ctypes.c_int),
ctypes.sizeof(msg_def.Enum1BytesTest().enumeration))
self.assertEqual(ctypes.sizeof(ctypes.c_int),
ctypes.sizeof(msg_def.Enum2BytesTest().enumeration))
def test_pack_unpack(self):
msg, buf = self.get_test_message()
packed = msg.pack()
self.assertEqual(buf[6:], packed[6:])
unpacked = msg_def.Enum2BytesTest.unpack(packed)
self.assertEqual(msg.enumeration, unpacked.enumeration)
def test_enum_behavior(self):
# Test int comparison.
self.assertEqual(msg_def.Enum1Bytes(1), 1)
# Test length.
self.assertEqual(127, len(msg_def.Enum1Bytes))
# Test __contains__.
self.assertIn('Value0', msg_def.Enum1Bytes)
self.assertNotIn('Value-1', msg_def.Enum1Bytes)
self.assertIn(0, msg_def.Enum1Bytes)
self.assertNotIn(-1, msg_def.Enum1Bytes)
self.assertIn(msg_def.Enum1Bytes(0), msg_def.Enum1Bytes)
self.assertNotIn(msg_def.Enum1Bytes(-1), msg_def.Enum1Bytes)
# Test class attributes.
self.assertIsInstance(msg_def.Enum1Bytes.Value0, msg_def.Enum1Bytes)
self.assertEqual(msg_def.Enum1Bytes.Value1, msg_def.Enum1Bytes(1))
with self.assertRaises(AttributeError):
msg_def.Enum1Bytes.NoValue
# Test string representation.
self.assertEqual('<Enum1Bytes.Value0: 0>', str(msg_def.Enum1Bytes(0)))
self.assertEqual('<Enum1Bytes.InvalidEnumValue: -1>', str(msg_def.Enum1Bytes(-1)))
# Test name.
e = msg_def.Enum1Bytes(0)
self.assertEqual('Value0', e.name)
e.value = 1
self.assertEqual('Value1', e.name)
e.value = -1
self.assertEqual('InvalidEnumValue', e.name)
# Test iterator.
self.assertEqual([msg_def.Enum1Bytes(x) for x in range(len(msg_def.Enum1Bytes))],
list(msg_def.Enum1Bytes))
# Test key access.
self.assertIsInstance(msg_def.Enum1Bytes['Value0'], msg_def.Enum1Bytes)
self.assertEqual(msg_def.Enum1Bytes.Value1, msg_def.Enum1Bytes['Value1'])
with self.assertRaises(KeyError):
msg_def.Enum1Bytes['NoValue']
# Test valid.
self.assertTrue(msg_def.Enum1Bytes(1).is_valid())
self.assertFalse(msg_def.Enum1Bytes(-1).is_valid())
# Test in structure.
s = msg_def.Enum1BytesTest()
self.assertIsInstance(s.enumeration, msg_def.Enum1Bytes)
s.enumeration = 1
self.assertEqual('Value1', s.enumeration.name)
s.enumeration.value = 2
self.assertEqual('Value2', s.enumeration.name)
s.enumeration = msg_def.Enum1Bytes.Value10
self.assertEqual('Value10', s.enumeration.name)
def test_description(self):
self.assertEqual('This is an enum description.', msg_def.Enum2Bytes.description)
def test_value_description(self):
self.assertEqual('This is a enum value description', msg_def.Enum2Bytes.value_descriptions[0])
class TestPrimitive(unittest.TestCase):
@staticmethod
def get_test_message():
msg = msg_def.PrimitiveTest()
msg.uint8 = 0x01
msg.uint16 = 0x0201
msg.uint32 = 0x04030201
msg.uint64 = 0x0807060504030201
msg.int8 = 0x01
msg.int16 = 0x0201
msg.int32 = 0x04030201
msg.int64 = 0x0807060504030201
msg.boolean = True
msg.float_type = 3.1415926
msg.double_type = 3.1415926
buf = msg_def.PrimitiveTest.get_buffer()
buf[6:] = [
0x01, # uint8, 5
0x02, 0x01, # uint16, 7
0x04, 0x03, 0x02, 0x01, # uint32, 9
0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, # uint64, 13
0x01, # int8, 21
0x02, 0x01, # int16, 22
0x04, 0x03, 0x02, 0x01, # int32, 24
0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, # int64, 28
0x01, # bool, 36
0x40, 0x49, 0x0f, 0xda, # float, 37
0x40, 0x09, 0x21, 0xFB, 0x4D, 0x12, 0xD8, 0x4A, # double, 41
] # yapf: disable
return msg, buf
def test_pack_unpack(self):
msg, buf = self.get_test_message()
packed = msg.pack()
self.assertEqual(buf[6:], packed[6:])
unpacked = msg_def.PrimitiveTest.unpack(packed)
self.assertEqual(msg.uint8, unpacked.uint8)
self.assertEqual(msg.uint16, unpacked.uint16)
self.assertEqual(msg.uint32, unpacked.uint32)
self.assertEqual(msg.uint64, unpacked.uint64)
self.assertEqual(msg.int8, unpacked.int8)
self.assertEqual(msg.int16, unpacked.int16)
self.assertEqual(msg.int32, unpacked.int32)
self.assertEqual(msg.int64, unpacked.int64)
self.assertEqual(msg.boolean, unpacked.boolean)
self.assertEqual(msg.float_type, unpacked.float_type)
self.assertEqual(msg.double_type, unpacked.double_type)
class TestArray(unittest.TestCase):
@staticmethod
def get_test_message():
msg = msg_def.ArrayTest()
for i, s in enumerate(msg.array_1d):
s.field1 = i
for i, a in enumerate(msg.array_2d):
for j, s in enumerate(a):
s.field1 = j + 3 * i
for i, a in enumerate(msg.array_3d):
for j, a in enumerate(a):
for k, s in enumerate(a):
s.field1 = k + 3 * j + 6 * i
buf = msg_def.ArrayTest.get_buffer()
buf[6:] = [
0x00, 0x00, 0x00,
0x00, 0x00, 0x01,
0x00, 0x00, 0x02,
0x00, 0x00, 0x00,
0x00, 0x00, 0x01,
0x00, 0x00, 0x02,
0x00, 0x00, 0x03,
0x00, 0x00, 0x04,
0x00, 0x00, 0x05,
0x00, 0x00, 0x00,
0x00, 0x00, 0x01,
0x00, 0x00, 0x02,
0x00, 0x00, 0x03,
0x00, 0x00, 0x04,
0x00, 0x00, 0x05,
] # yapf: disable
return msg, buf
def test_pack_unpack(self):
msg, buf = self.get_test_message()
packed = msg.pack()
self.assertEqual(buf[6:], packed[6:])
unpacked = msg_def.ArrayTest.unpack(packed)
for i, s in enumerate(msg.array_1d):
with self.subTest(msg='1D Array.', i=i):
self.assertEqual(s.field0, unpacked.array_1d[i].field0)
self.assertEqual(s.field1, unpacked.array_1d[i].field1)
for i, a in enumerate(msg.array_2d):
for j, s in enumerate(a):
with self.subTest(msg='2D Array.', i=i, j=j):
self.assertEqual(s.field0, unpacked.array_2d[i][j].field0)
self.assertEqual(s.field1, unpacked.array_2d[i][j].field1)
for i, a in enumerate(msg.array_3d):
for j, a in enumerate(a):
for k, s in enumerate(a):
with self.subTest(msg='3D Array.', i=i, j=j, k=k):
self.assertEqual(s.field0, unpacked.array_3d[i][j][k].field0)
self.assertEqual(s.field1, unpacked.array_3d[i][j][k].field1)
def test_description(self):
self.assertEqual('This is a struct description.', msg_def.ArrayElem.description)
def test_field_description(self):
self.assertEqual('This is a struct field description.', msg_def.ArrayElem.field_descriptions[0])
def test_message_description(self):
self.assertEqual('This is a message description.', msg_def.ArrayTest.description)
class TestAlias(unittest.TestCase):
@staticmethod
def get_test_message():
msg = msg_def.AliasTest()
msg.position[0] = 1.2
msg.position[1] = 2.3
msg.position[2] = 3.4
msg.velocity.x = 4.5
msg.velocity.y = 5.6
msg.velocity.z = 6.7
buf = msg_def.AliasTest.get_buffer()
buf[6:] = [
0x3f, 0x99, 0x99, 0x9a, 0x40, 0x13, 0x33, 0x33, 0x40, 0x59, 0x99, 0x9a, 0x40, 0x90, 0x00,
0x00, 0x40, 0xb3, 0x33, 0x33, 0x40, 0xd6, 0x66, 0x66,
] # yapf: disable
return msg, buf
def test_pack_unpack(self):
msg, buf = self.get_test_message()
packed = msg.pack()
self.assertEqual(buf[6:], packed[6:])
unpacked = msg_def.AliasTest.unpack(packed)
self.assertEqual(msg.position[0], unpacked.position[0])
self.assertEqual(msg.position[1], unpacked.position[1])
self.assertEqual(msg.position[2], unpacked.position[2])
self.assertEqual(msg.velocity.x, unpacked.velocity.x)
self.assertEqual(msg.velocity.y, unpacked.velocity.y)
self.assertEqual(msg.velocity.z, unpacked.velocity.z)
class UnpackTest(unittest.TestCase):
def test_unpack(self):
msg = msg_def.PrimitiveTest()
buf = msg.pack()
self.assertIsInstance(msg_def.unpack_message(buf), msg_def.PrimitiveTest)
buf_bytes = bytes(x for x in buf)
self.assertIsInstance(msg_def.unpack_message(buf_bytes), msg_def.PrimitiveTest)
buf_list = [x for x in buf]
self.assertIsInstance(msg_def.unpack_message(buf_list), msg_def.PrimitiveTest)
buf[:4] = [0x00, 0x00, 0x00, 0x00]
with self.assertRaises(msg_def.UnknownMessage):
msg_def.unpack_message(buf)
with self.assertRaises(msg_def.IncorrectBufferSize):
msg_def.PrimitiveTest.unpack(buf[1:])
with self.assertRaises(msg_def.InvalidUid):
msg_def.PrimitiveTest.unpack(buf)
buf = msg.pack()
buf[5:7] = [0x00, 0x00]
with self.assertRaises(msg_def.InvalidLen):
msg_def.PrimitiveTest.unpack(buf)
class LoggingTest(unittest.TestCase):
def test_logging(self):
tmp_dir = os.environ['TEST_TMPDIR']
temp_log = os.path.join(tmp_dir, 'test.ss')
with msg_def.Logger(temp_log) as logger:
logger.log(msg_def.PrimitiveTest())
with open(temp_log, 'rb') as f:
self.assertIn(b'SsLogFileDelimiter', f.read())
if __name__ == '__main__':
unittest.main()