Commit | Line | Data |
---|---|---|
9cf643d1 PP |
1 | from bt2 import values |
2 | import unittest | |
3 | import copy | |
4 | import uuid | |
5 | import bt2 | |
6 | ||
7 | ||
8 | class TraceTestCase(unittest.TestCase): | |
9 | def setUp(self): | |
10 | self._sc = self._create_stream_class('sc1', 3) | |
11 | self._tc = bt2.Trace() | |
12 | ||
13 | def _create_stream_class(self, name, id): | |
14 | ec1, ec2 = self._create_event_classes() | |
15 | packet_context_ft = bt2.StructureFieldType() | |
16 | packet_context_ft.append_field('menu', bt2.FloatingPointNumberFieldType()) | |
17 | packet_context_ft.append_field('sticker', bt2.StringFieldType()) | |
18 | event_header_ft = bt2.StructureFieldType() | |
19 | event_header_ft.append_field('id', bt2.IntegerFieldType(19)) | |
20 | event_context_ft = bt2.StructureFieldType() | |
21 | event_context_ft.append_field('msg', bt2.StringFieldType()) | |
22 | return bt2.StreamClass(name=name, id=id, | |
23 | packet_context_field_type=packet_context_ft, | |
24 | event_header_field_type=event_header_ft, | |
25 | event_context_field_type=event_context_ft, | |
26 | event_classes=(ec1, ec2)) | |
27 | ||
28 | def _create_event_classes(self): | |
29 | context_ft = bt2.StructureFieldType() | |
30 | context_ft.append_field('allo', bt2.StringFieldType()) | |
31 | context_ft.append_field('zola', bt2.IntegerFieldType(18)) | |
32 | payload_ft = bt2.StructureFieldType() | |
33 | payload_ft.append_field('zoom', bt2.StringFieldType()) | |
34 | ec1 = bt2.EventClass('event23', id=23, context_field_type=context_ft, | |
35 | payload_field_type=payload_ft) | |
36 | ec2 = bt2.EventClass('event17', id=17, context_field_type=payload_ft, | |
37 | payload_field_type=context_ft) | |
38 | return ec1, ec2 | |
39 | ||
40 | def test_create_default(self): | |
41 | self.assertEqual(len(self._tc), 0) | |
42 | ||
43 | def test_create_full(self): | |
44 | header_ft = bt2.StructureFieldType() | |
45 | header_ft.append_field('magic', bt2.IntegerFieldType(32)) | |
46 | clock_classes = bt2.ClockClass('cc1'), bt2.ClockClass('cc2') | |
47 | sc = self._create_stream_class('sc1', 3) | |
48 | tc = bt2.Trace(name='my name', | |
49 | native_byte_order=bt2.ByteOrder.LITTLE_ENDIAN, | |
50 | env={'the_string': 'value', 'the_int': 23}, | |
51 | packet_header_field_type=header_ft, | |
52 | clock_classes=clock_classes, | |
53 | stream_classes=(sc,)) | |
54 | self.assertEqual(tc.name, 'my name') | |
55 | self.assertEqual(tc.native_byte_order, bt2.ByteOrder.LITTLE_ENDIAN) | |
56 | self.assertEqual(tc.env['the_string'], 'value') | |
57 | self.assertEqual(tc.env['the_int'], 23) | |
58 | self.assertEqual(tc.packet_header_field_type, header_ft) | |
59 | self.assertEqual(tc.clock_classes['cc1'], clock_classes[0]) | |
60 | self.assertEqual(tc.clock_classes['cc2'], clock_classes[1]) | |
61 | self.assertEqual(tc[3], sc) | |
62 | ||
63 | def test_assign_name(self): | |
64 | self._tc.name = 'lel' | |
65 | self.assertEqual(self._tc.name, 'lel') | |
66 | ||
67 | def test_assign_invalid_name(self): | |
68 | with self.assertRaises(TypeError): | |
69 | self._tc.name = 17 | |
70 | ||
71 | def test_assign_native_byte_order(self): | |
72 | self._tc.native_byte_order = bt2.ByteOrder.BIG_ENDIAN | |
73 | self.assertEqual(self._tc.native_byte_order, bt2.ByteOrder.BIG_ENDIAN) | |
74 | ||
75 | def test_assign_invalid_native_byte_order(self): | |
76 | with self.assertRaises(TypeError): | |
77 | self._tc.native_byte_order = 'lel' | |
78 | ||
79 | def test_assign_packet_header_field_type(self): | |
80 | header_ft = bt2.StructureFieldType() | |
81 | header_ft.append_field('magic', bt2.IntegerFieldType(32)) | |
82 | self._tc.packet_header_field_type = header_ft | |
83 | self.assertEqual(self._tc.packet_header_field_type, header_ft) | |
84 | ||
85 | def test_assign_no_packet_header_field_type(self): | |
86 | self._tc.packet_header_field_type = None | |
87 | self.assertIsNone(self._tc.packet_header_field_type) | |
88 | ||
89 | def _test_copy(self, cpy): | |
90 | self.assertIsNot(cpy, self._tc) | |
91 | self.assertNotEqual(cpy.addr, self._tc.addr) | |
92 | self.assertEqual(cpy, self._tc) | |
93 | self.assertEqual(len(self._tc), len(cpy)) | |
94 | ||
95 | def _pre_copy(self): | |
96 | self._tc.name = 'the trace class' | |
97 | sc1 = self._create_stream_class('sc1', 3) | |
98 | sc2 = self._create_stream_class('sc2', 9) | |
99 | sc3 = self._create_stream_class('sc3', 17) | |
100 | self._tc.add_clock_class(bt2.ClockClass('cc1')) | |
101 | self._tc.add_clock_class(bt2.ClockClass('cc2')) | |
102 | self._tc.env['allo'] = 'bateau' | |
103 | self._tc.env['bateau'] = 'cart' | |
104 | self._tc.add_stream_class(sc1) | |
105 | self._tc.add_stream_class(sc2) | |
106 | self._tc.add_stream_class(sc3) | |
107 | ||
108 | def test_copy(self): | |
109 | self._pre_copy() | |
110 | cpy = copy.copy(self._tc) | |
111 | self._test_copy(cpy) | |
112 | self.assertEqual(self._tc.packet_header_field_type.addr, cpy.packet_header_field_type.addr) | |
113 | self.assertEqual(self._tc.clock_classes['cc1'].addr, cpy.clock_classes['cc1'].addr) | |
114 | self.assertEqual(self._tc.clock_classes['cc2'].addr, cpy.clock_classes['cc2'].addr) | |
115 | self.assertEqual(self._tc.env['allo'].addr, cpy.env['allo'].addr) | |
116 | self.assertEqual(self._tc.env['bateau'].addr, cpy.env['bateau'].addr) | |
117 | ||
118 | def test_deepcopy(self): | |
119 | self._pre_copy() | |
120 | cpy = copy.deepcopy(self._tc) | |
121 | self._test_copy(cpy) | |
122 | self.assertNotEqual(self._tc.packet_header_field_type.addr, cpy.packet_header_field_type.addr) | |
123 | self.assertNotEqual(self._tc.clock_classes['cc1'].addr, cpy.clock_classes['cc1'].addr) | |
124 | self.assertNotEqual(self._tc.clock_classes['cc2'].addr, cpy.clock_classes['cc2'].addr) | |
125 | self.assertNotEqual(self._tc.env['allo'].addr, cpy.env['allo'].addr) | |
126 | self.assertNotEqual(self._tc.env['bateau'].addr, cpy.env['bateau'].addr) | |
127 | ||
128 | def test_getitem(self): | |
129 | self._tc.add_stream_class(self._sc) | |
130 | self.assertEqual(self._tc[3].addr, self._sc.addr) | |
131 | ||
132 | def test_getitem_wrong_key_type(self): | |
133 | self._tc.add_stream_class(self._sc) | |
134 | with self.assertRaises(TypeError): | |
135 | self._tc['hello'] | |
136 | ||
137 | def test_getitem_wrong_key(self): | |
138 | self._tc.add_stream_class(self._sc) | |
139 | with self.assertRaises(KeyError): | |
140 | self._tc[4] | |
141 | ||
142 | def test_len(self): | |
143 | self.assertEqual(len(self._tc), 0) | |
144 | self._tc.add_stream_class(self._sc) | |
145 | self.assertEqual(len(self._tc), 1) | |
146 | ||
147 | def test_iter(self): | |
148 | sc1 = self._create_stream_class('sc1', 3) | |
149 | sc2 = self._create_stream_class('sc2', 9) | |
150 | sc3 = self._create_stream_class('sc3', 17) | |
151 | self._tc.add_stream_class(sc1) | |
152 | self._tc.add_stream_class(sc2) | |
153 | self._tc.add_stream_class(sc3) | |
154 | ||
155 | for sid, stream_class in self._tc.items(): | |
156 | self.assertIsInstance(stream_class, bt2.StreamClass) | |
157 | ||
158 | if sid == 3: | |
159 | self.assertEqual(stream_class.addr, sc1.addr) | |
160 | elif sid == 9: | |
161 | self.assertEqual(stream_class.addr, sc2.addr) | |
162 | elif sid == 17: | |
163 | self.assertEqual(stream_class.addr, sc3.addr) | |
164 | ||
165 | def test_env_getitem_wrong_key(self): | |
166 | with self.assertRaises(KeyError): | |
167 | self._tc.env['lel'] | |
168 | ||
169 | def test_clock_classes_getitem_wrong_key(self): | |
170 | with self.assertRaises(KeyError): | |
171 | self._tc.clock_classes['lel'] | |
172 | ||
173 | def _test_eq_create_objects(self): | |
174 | cc1_uuid = uuid.UUID('bc7f2f2d-2ee4-4e03-ab1f-2e0e1304e94f') | |
175 | cc1 = bt2.ClockClass('cc1', uuid=cc1_uuid) | |
176 | cc2_uuid = uuid.UUID('da7d6b6f-3108-4706-89bd-ab554732611b') | |
177 | cc2 = bt2.ClockClass('cc2', uuid=cc2_uuid) | |
178 | sc1 = self._create_stream_class('sc1', 3) | |
179 | sc2 = self._create_stream_class('sc2', 9) | |
180 | header_ft = bt2.StructureFieldType() | |
181 | header_ft.append_field('magic', bt2.IntegerFieldType(32)) | |
182 | return cc1, cc2, sc1, sc2, header_ft | |
183 | ||
184 | def test_eq(self): | |
185 | cc1, cc2, sc1, sc2, header_ft = self._test_eq_create_objects() | |
186 | tc1 = bt2.Trace(name='my name', | |
187 | native_byte_order=bt2.ByteOrder.LITTLE_ENDIAN, | |
188 | env={'the_string': 'value', 'the_int': 23}, | |
189 | packet_header_field_type=header_ft, | |
190 | clock_classes=(cc1, cc2), | |
191 | stream_classes=(sc1, sc2)) | |
192 | cc1, cc2, sc1, sc2, header_ft = self._test_eq_create_objects() | |
193 | tc2 = bt2.Trace(name='my name', | |
194 | native_byte_order=bt2.ByteOrder.LITTLE_ENDIAN, | |
195 | env={'the_string': 'value', 'the_int': 23}, | |
196 | packet_header_field_type=header_ft, | |
197 | clock_classes=(cc1, cc2), | |
198 | stream_classes=(sc1, sc2)) | |
199 | self.assertEqual(tc1, tc2) | |
200 | ||
201 | def test_ne_name(self): | |
202 | cc1, cc2, sc1, sc2, header_ft = self._test_eq_create_objects() | |
203 | tc1 = bt2.Trace(name='my name2', | |
204 | native_byte_order=bt2.ByteOrder.LITTLE_ENDIAN, | |
205 | env={'the_string': 'value', 'the_int': 23}, | |
206 | packet_header_field_type=header_ft, | |
207 | clock_classes=(cc1, cc2), | |
208 | stream_classes=(sc1, sc2)) | |
209 | cc1, cc2, sc1, sc2, header_ft = self._test_eq_create_objects() | |
210 | tc2 = bt2.Trace(name='my name', | |
211 | native_byte_order=bt2.ByteOrder.LITTLE_ENDIAN, | |
212 | env={'the_string': 'value', 'the_int': 23}, | |
213 | packet_header_field_type=header_ft, | |
214 | clock_classes=(cc1, cc2), | |
215 | stream_classes=(sc1, sc2)) | |
216 | self.assertNotEqual(tc1, tc2) | |
217 | ||
218 | def test_ne_packet_header_field_type(self): | |
219 | cc1, cc2, sc1, sc2, header_ft = self._test_eq_create_objects() | |
220 | tc1 = bt2.Trace(name='my name', | |
221 | native_byte_order=bt2.ByteOrder.LITTLE_ENDIAN, | |
222 | env={'the_string': 'value', 'the_int': 23}, | |
223 | packet_header_field_type=header_ft, | |
224 | clock_classes=(cc1, cc2), | |
225 | stream_classes=(sc1, sc2)) | |
226 | cc1, cc2, sc1, sc2, header_ft = self._test_eq_create_objects() | |
227 | header_ft.append_field('yes', bt2.StringFieldType()) | |
228 | tc2 = bt2.Trace(name='my name', | |
229 | native_byte_order=bt2.ByteOrder.LITTLE_ENDIAN, | |
230 | env={'the_string': 'value', 'the_int': 23}, | |
231 | packet_header_field_type=header_ft, | |
232 | clock_classes=(cc1, cc2), | |
233 | stream_classes=(sc1, sc2)) | |
234 | self.assertNotEqual(tc1, tc2) | |
235 | ||
236 | def test_ne_native_byte_order(self): | |
237 | cc1, cc2, sc1, sc2, header_ft = self._test_eq_create_objects() | |
238 | tc1 = bt2.Trace(name='my name', | |
239 | native_byte_order=bt2.ByteOrder.LITTLE_ENDIAN, | |
240 | env={'the_string': 'value', 'the_int': 23}, | |
241 | packet_header_field_type=header_ft, | |
242 | clock_classes=(cc1, cc2), | |
243 | stream_classes=(sc1, sc2)) | |
244 | cc1, cc2, sc1, sc2, header_ft = self._test_eq_create_objects() | |
245 | tc2 = bt2.Trace(name='my name', | |
246 | native_byte_order=bt2.ByteOrder.BIG_ENDIAN, | |
247 | env={'the_string': 'value', 'the_int': 23}, | |
248 | packet_header_field_type=header_ft, | |
249 | clock_classes=(cc1, cc2), | |
250 | stream_classes=(sc1, sc2)) | |
251 | self.assertNotEqual(tc1, tc2) | |
252 | ||
253 | def test_ne_env(self): | |
254 | cc1, cc2, sc1, sc2, header_ft = self._test_eq_create_objects() | |
255 | tc1 = bt2.Trace(name='my name', | |
256 | native_byte_order=bt2.ByteOrder.LITTLE_ENDIAN, | |
257 | env={'the_string': 'value', 'the_int2': 23}, | |
258 | packet_header_field_type=header_ft, | |
259 | clock_classes=(cc1, cc2), | |
260 | stream_classes=(sc1, sc2)) | |
261 | cc1, cc2, sc1, sc2, header_ft = self._test_eq_create_objects() | |
262 | tc2 = bt2.Trace(name='my name', | |
263 | native_byte_order=bt2.ByteOrder.LITTLE_ENDIAN, | |
264 | env={'the_string': 'value', 'the_int': 23}, | |
265 | packet_header_field_type=header_ft, | |
266 | clock_classes=(cc1, cc2), | |
267 | stream_classes=(sc1, sc2)) | |
268 | self.assertNotEqual(tc1, tc2) | |
269 | ||
270 | def test_ne_clock_classes(self): | |
271 | cc1, cc2, sc1, sc2, header_ft = self._test_eq_create_objects() | |
272 | tc1 = bt2.Trace(name='my name', | |
273 | native_byte_order=bt2.ByteOrder.LITTLE_ENDIAN, | |
274 | env={'the_string': 'value', 'the_int': 23}, | |
275 | packet_header_field_type=header_ft, | |
276 | clock_classes=(cc1, cc2), | |
277 | stream_classes=(sc1, sc2)) | |
278 | cc1, cc2, sc1, sc2, header_ft = self._test_eq_create_objects() | |
279 | cc2.frequency = 1234 | |
280 | tc2 = bt2.Trace(name='my name', | |
281 | native_byte_order=bt2.ByteOrder.LITTLE_ENDIAN, | |
282 | env={'the_string': 'value', 'the_int': 23}, | |
283 | packet_header_field_type=header_ft, | |
284 | clock_classes=(cc1, cc2), | |
285 | stream_classes=(sc1, sc2)) | |
286 | self.assertNotEqual(tc1, tc2) | |
287 | ||
288 | def test_ne_stream_classes(self): | |
289 | cc1, cc2, sc1, sc2, header_ft = self._test_eq_create_objects() | |
290 | tc1 = bt2.Trace(name='my name', | |
291 | native_byte_order=bt2.ByteOrder.LITTLE_ENDIAN, | |
292 | env={'the_string': 'value', 'the_int': 23}, | |
293 | packet_header_field_type=header_ft, | |
294 | clock_classes=(cc1, cc2), | |
295 | stream_classes=(sc1, sc2)) | |
296 | cc1, cc2, sc1, sc2, header_ft = self._test_eq_create_objects() | |
297 | sc2.id = 72632 | |
298 | tc2 = bt2.Trace(name='my name', | |
299 | native_byte_order=bt2.ByteOrder.LITTLE_ENDIAN, | |
300 | env={'the_string': 'value', 'the_int': 23}, | |
301 | packet_header_field_type=header_ft, | |
302 | clock_classes=(cc1, cc2), | |
303 | stream_classes=(sc1, sc2)) | |
304 | self.assertNotEqual(tc1, tc2) | |
305 | ||
306 | def test_eq_invalid(self): | |
307 | self.assertFalse(self._tc == 23) |