d980548f675aeee04dd8736a5f75ccca1943d8d9
[babeltrace.git] / tests / bindings / python / bt2 / test_field_class.py
1 #
2 # Copyright (C) 2019 EfficiOS Inc.
3 #
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; only version 2
7 # of the License.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 #
18
19 import unittest
20 import bt2
21 from utils import get_default_trace_class, TestOutputPortMessageIterator
22 from bt2 import value as bt2_value
23 from bt2 import field_class as bt2_field_class
24
25
26 def _create_stream(tc, ctx_field_classes):
27 packet_context_fc = tc.create_structure_field_class()
28 for name, fc in ctx_field_classes:
29 packet_context_fc.append_member(name, fc)
30
31 trace = tc()
32 stream_class = tc.create_stream_class(
33 packet_context_field_class=packet_context_fc, supports_packets=True
34 )
35
36 stream = trace.create_stream(stream_class)
37 return stream
38
39
40 def _create_const_field_class(tc, field_class, value_setter_fn):
41 field_name = 'const field'
42
43 class MyIter(bt2._UserMessageIterator):
44 def __init__(self, config, self_port_output):
45 nonlocal field_class
46 nonlocal value_setter_fn
47 stream = _create_stream(tc, [(field_name, field_class)])
48 packet = stream.create_packet()
49
50 value_setter_fn(packet.context_field[field_name])
51
52 self._msgs = [
53 self._create_stream_beginning_message(stream),
54 self._create_packet_beginning_message(packet),
55 ]
56
57 def __next__(self):
58 if len(self._msgs) == 0:
59 raise StopIteration
60
61 return self._msgs.pop(0)
62
63 class MySrc(bt2._UserSourceComponent, message_iterator_class=MyIter):
64 def __init__(self, config, params, obj):
65 self._add_output_port('out', params)
66
67 graph = bt2.Graph()
68 src_comp = graph.add_component(MySrc, 'my_source', None)
69 msg_iter = TestOutputPortMessageIterator(graph, src_comp.output_ports['out'])
70
71 # Ignore first message, stream beginning
72 _ = next(msg_iter)
73 packet_beg_msg = next(msg_iter)
74
75 return packet_beg_msg.packet.context_field[field_name].cls
76
77
78 class _TestFieldClass:
79 def test_create_user_attributes(self):
80 fc = self._create_default_field_class(user_attributes={'salut': 23})
81 self.assertEqual(fc.user_attributes, {'salut': 23})
82 self.assertIs(type(fc.user_attributes), bt2_value.MapValue)
83
84 def test_const_create_user_attributes(self):
85 fc = self._create_default_const_field_class(user_attributes={'salut': 23})
86 self.assertEqual(fc.user_attributes, {'salut': 23})
87 self.assertIs(type(fc.user_attributes), bt2_value._MapValueConst)
88
89 def test_create_invalid_user_attributes(self):
90 with self.assertRaises(TypeError):
91 self._create_default_field_class(user_attributes=object())
92
93 def test_create_invalid_user_attributes_value_type(self):
94 with self.assertRaises(TypeError):
95 self._create_default_field_class(user_attributes=23)
96
97
98 class BoolFieldClassTestCase(_TestFieldClass, unittest.TestCase):
99 @staticmethod
100 def _const_value_setter(field):
101 field.value = False
102
103 def _create_default_field_class(self, **kwargs):
104 tc = get_default_trace_class()
105 return tc.create_bool_field_class(**kwargs)
106
107 def _create_default_const_field_class(self, *args, **kwargs):
108 tc = get_default_trace_class()
109 fc = tc.create_bool_field_class(*args, **kwargs)
110 return _create_const_field_class(tc, fc, self._const_value_setter)
111
112 def setUp(self):
113 self._fc = self._create_default_field_class()
114 self._fc_const = self._create_default_const_field_class()
115
116 def test_create_default(self):
117 self.assertIsNotNone(self._fc)
118 self.assertEqual(len(self._fc.user_attributes), 0)
119
120
121 class BitArrayFieldClassTestCase(_TestFieldClass, unittest.TestCase):
122 @staticmethod
123 def _const_value_setter(field):
124 field.value = []
125
126 def _create_field_class(self, *args, **kwargs):
127 tc = get_default_trace_class()
128 return tc.create_bit_array_field_class(*args, **kwargs)
129
130 def _create_default_field_class(self, **kwargs):
131 return self._create_field_class(17, **kwargs)
132
133 def _create_default_const_field_class(self, *args, **kwargs):
134 tc = get_default_trace_class()
135 fc = tc.create_bit_array_field_class(17, **kwargs)
136 return _create_const_field_class(tc, fc, self._const_value_setter)
137
138 def setUp(self):
139 self._fc = self._create_default_field_class()
140
141 def test_create_default(self):
142 self.assertIsNotNone(self._fc)
143 self.assertEqual(len(self._fc.user_attributes), 0)
144
145 def test_create_length_out_of_range(self):
146 with self.assertRaises(ValueError):
147 self._create_field_class(65)
148
149 def test_create_length_zero(self):
150 with self.assertRaises(ValueError):
151 self._create_field_class(0)
152
153 def test_create_length_invalid_type(self):
154 with self.assertRaises(TypeError):
155 self._create_field_class('lel')
156
157 def test_length_prop(self):
158 self.assertEqual(self._fc.length, 17)
159
160
161 class _TestIntegerFieldClassProps:
162 def test_create_default(self):
163 fc = self._create_default_field_class()
164 self.assertEqual(fc.field_value_range, 64)
165 self.assertEqual(fc.preferred_display_base, bt2.IntegerDisplayBase.DECIMAL)
166 self.assertEqual(len(fc.user_attributes), 0)
167
168 def test_create_range(self):
169 fc = self._create_field_class(field_value_range=35)
170 self.assertEqual(fc.field_value_range, 35)
171
172 fc = self._create_field_class(36)
173 self.assertEqual(fc.field_value_range, 36)
174
175 def test_create_invalid_range(self):
176 with self.assertRaises(TypeError):
177 self._create_field_class('yes')
178
179 with self.assertRaises(TypeError):
180 self._create_field_class(field_value_range='yes')
181
182 with self.assertRaises(ValueError):
183 self._create_field_class(field_value_range=-2)
184
185 with self.assertRaises(ValueError):
186 self._create_field_class(field_value_range=0)
187
188 def test_create_base(self):
189 fc = self._create_field_class(
190 preferred_display_base=bt2.IntegerDisplayBase.HEXADECIMAL
191 )
192 self.assertEqual(fc.preferred_display_base, bt2.IntegerDisplayBase.HEXADECIMAL)
193
194 def test_create_invalid_base_type(self):
195 with self.assertRaises(TypeError):
196 self._create_field_class(preferred_display_base='yes')
197
198 def test_create_invalid_base_value(self):
199 with self.assertRaises(ValueError):
200 self._create_field_class(preferred_display_base=444)
201
202 def test_create_full(self):
203 fc = self._create_field_class(
204 24, preferred_display_base=bt2.IntegerDisplayBase.OCTAL
205 )
206 self.assertEqual(fc.field_value_range, 24)
207 self.assertEqual(fc.preferred_display_base, bt2.IntegerDisplayBase.OCTAL)
208
209
210 class SignedIntegerFieldClassTestCase(
211 _TestIntegerFieldClassProps, _TestFieldClass, unittest.TestCase
212 ):
213 @staticmethod
214 def _const_value_setter(field):
215 field.value = -18
216
217 def _create_field_class(self, *args, **kwargs):
218 tc = get_default_trace_class()
219 return tc.create_signed_integer_field_class(*args, **kwargs)
220
221 def _create_default_const_field_class(self, *args, **kwargs):
222 tc = get_default_trace_class()
223 fc = tc.create_signed_integer_field_class(*args, **kwargs)
224 return _create_const_field_class(tc, fc, self._const_value_setter)
225
226 _create_default_field_class = _create_field_class
227
228
229 class UnsignedIntegerFieldClassTestCase(
230 _TestIntegerFieldClassProps, _TestFieldClass, unittest.TestCase
231 ):
232 @staticmethod
233 def _const_value_setter(field):
234 field.value = 18
235
236 def _create_field_class(self, *args, **kwargs):
237 tc = get_default_trace_class()
238 return tc.create_unsigned_integer_field_class(*args, **kwargs)
239
240 def _create_default_const_field_class(self, *args, **kwargs):
241 tc = get_default_trace_class()
242 fc = tc.create_signed_integer_field_class(*args, **kwargs)
243 return _create_const_field_class(tc, fc, self._const_value_setter)
244
245 _create_default_field_class = _create_field_class
246
247
248 class SingleRealFieldClassTestCase(_TestFieldClass, unittest.TestCase):
249 @staticmethod
250 def _const_value_setter(field):
251 field.value = -18.0
252
253 def _create_field_class(self, *args, **kwargs):
254 tc = get_default_trace_class()
255 return tc.create_single_precision_real_field_class(*args, **kwargs)
256
257 def _create_default_const_field_class(self, *args, **kwargs):
258 tc = get_default_trace_class()
259 fc = tc.create_single_precision_real_field_class(*args, **kwargs)
260 return _create_const_field_class(tc, fc, self._const_value_setter)
261
262 _create_default_field_class = _create_field_class
263
264 def test_create_default(self):
265 fc = self._create_field_class()
266 self.assertEqual(len(fc.user_attributes), 0)
267
268
269 class DoubleRealFieldClassTestCase(_TestFieldClass, unittest.TestCase):
270 @staticmethod
271 def _const_value_setter(field):
272 field.value = -18.0
273
274 def _create_field_class(self, *args, **kwargs):
275 tc = get_default_trace_class()
276 return tc.create_double_precision_real_field_class(*args, **kwargs)
277
278 def _create_default_const_field_class(self, *args, **kwargs):
279 tc = get_default_trace_class()
280 fc = tc.create_double_precision_real_field_class(*args, **kwargs)
281 return _create_const_field_class(tc, fc, self._const_value_setter)
282
283 _create_default_field_class = _create_field_class
284
285 def test_create_default(self):
286 fc = self._create_field_class()
287 self.assertEqual(len(fc.user_attributes), 0)
288
289
290 # Converts an _EnumerationFieldClassMapping to a list of ranges:
291 #
292 # [(lower0, upper0), (lower1, upper1), ...]
293
294
295 def enum_mapping_to_set(mapping):
296 return {(x.lower, x.upper) for x in mapping.ranges}
297
298
299 class _EnumerationFieldClassTestCase(_TestIntegerFieldClassProps):
300 def setUp(self):
301 self._spec_set_up()
302 self._fc = self._create_default_field_class()
303 self._fc_const = self._create_default_const_field_class()
304
305 def test_create_from_invalid_type(self):
306 with self.assertRaises(TypeError):
307 self._create_field_class('coucou')
308
309 def test_add_mapping_simple(self):
310 self._fc.add_mapping('hello', self._ranges1)
311 mapping = self._fc['hello']
312 self.assertEqual(mapping.label, 'hello')
313 self.assertEqual(mapping.ranges, self._ranges1)
314
315 def test_const_add_mapping(self):
316 with self.assertRaises(AttributeError):
317 self._fc_const.add_mapping('hello', self._ranges1)
318
319 def test_add_mapping_simple_kwargs(self):
320 self._fc.add_mapping(label='hello', ranges=self._ranges1)
321 mapping = self._fc['hello']
322 self.assertEqual(mapping.label, 'hello')
323 self.assertEqual(mapping.ranges, self._ranges1)
324
325 def test_add_mapping_invalid_name(self):
326 with self.assertRaises(TypeError):
327 self._fc.add_mapping(17, self._ranges1)
328
329 def test_add_mapping_invalid_range(self):
330 with self.assertRaises(TypeError):
331 self._fc.add_mapping('allo', 'meow')
332
333 def test_add_mapping_dup_label(self):
334 with self.assertRaises(ValueError):
335 self._fc.add_mapping('a', self._ranges1)
336 self._fc.add_mapping('a', self._ranges2)
337
338 def test_add_mapping_invalid_ranges_signedness(self):
339 with self.assertRaises(TypeError):
340 self._fc.add_mapping('allo', self._inval_ranges)
341
342 def test_iadd(self):
343 self._fc.add_mapping('c', self._ranges1)
344
345 self._fc += [('d', self._ranges2), ('e', self._ranges3)]
346
347 self.assertEqual(len(self._fc), 3)
348 self.assertEqual(self._fc['c'].label, 'c')
349 self.assertEqual(self._fc['c'].ranges, self._ranges1)
350 self.assertEqual(self._fc['d'].label, 'd')
351 self.assertEqual(self._fc['d'].ranges, self._ranges2)
352 self.assertEqual(self._fc['e'].label, 'e')
353 self.assertEqual(self._fc['e'].ranges, self._ranges3)
354
355 def test_const_iadd(self):
356 with self.assertRaises(TypeError):
357 self._fc_const += [('d', self._ranges2), ('e', self._ranges3)]
358
359 def test_bool_op(self):
360 self.assertFalse(self._fc)
361 self._fc.add_mapping('a', self._ranges1)
362 self.assertTrue(self._fc)
363
364 def test_len(self):
365 self._fc.add_mapping('a', self._ranges1)
366 self._fc.add_mapping('b', self._ranges2)
367 self._fc.add_mapping('c', self._ranges3)
368 self.assertEqual(len(self._fc), 3)
369
370 def test_getitem(self):
371 self._fc.add_mapping('a', self._ranges1)
372 self._fc.add_mapping('b', self._ranges2)
373 self._fc.add_mapping('c', self._ranges3)
374 mapping = self._fc['a']
375 self.assertEqual(mapping.label, 'a')
376 self.assertEqual(mapping.ranges, self._ranges1)
377 self.assertIs(type(mapping), self._MAPPING_CLASS)
378 self.assertIs(type(mapping.ranges), self._CONST_RANGE_SET_CLASS)
379
380 def test_getitem_nonexistent(self):
381 with self.assertRaises(KeyError):
382 self._fc['doesnotexist']
383
384 def test_iter(self):
385 self._fc.add_mapping('a', self._ranges1)
386 self._fc.add_mapping('b', self._ranges2)
387 self._fc.add_mapping('c', self._ranges3)
388
389 # This exercises iteration.
390 labels = sorted(self._fc)
391
392 self.assertEqual(labels, ['a', 'b', 'c'])
393
394 def test_find_by_value(self):
395 self._fc.add_mapping('a', self._ranges1)
396 self._fc.add_mapping('b', self._ranges2)
397 self._fc.add_mapping('c', self._ranges3)
398 mappings = self._fc.mappings_for_value(self._value_in_range_1_and_3)
399 labels = set([mapping.label for mapping in mappings])
400 expected_labels = set(['a', 'c'])
401 self.assertEqual(labels, expected_labels)
402
403
404 class UnsignedEnumerationFieldClassTestCase(
405 _EnumerationFieldClassTestCase, _TestFieldClass, unittest.TestCase
406 ):
407 _MAPPING_CLASS = bt2_field_class._UnsignedEnumerationFieldClassMappingConst
408 _RANGE_SET_CLASS = bt2.UnsignedIntegerRangeSet
409 _CONST_RANGE_SET_CLASS = bt2._UnsignedIntegerRangeSetConst
410
411 def _spec_set_up(self):
412 self._ranges1 = bt2.UnsignedIntegerRangeSet([(1, 4), (18, 47)])
413 self._ranges2 = bt2.UnsignedIntegerRangeSet([(5, 5)])
414 self._ranges3 = bt2.UnsignedIntegerRangeSet([(8, 22), (48, 99)])
415 self._inval_ranges = bt2.SignedIntegerRangeSet([(-8, -5), (48, 1928)])
416 self._value_in_range_1_and_3 = 20
417
418 @staticmethod
419 def _const_value_setter(field):
420 field.value = 0
421
422 def _create_field_class(self, *args, **kwargs):
423 tc = get_default_trace_class()
424 return tc.create_unsigned_enumeration_field_class(*args, **kwargs)
425
426 def _create_default_const_field_class(self, *args, **kwargs):
427 tc = get_default_trace_class()
428 fc = tc.create_unsigned_enumeration_field_class(*args, **kwargs)
429 return _create_const_field_class(tc, fc, self._const_value_setter)
430
431 _create_default_field_class = _create_field_class
432
433
434 class SignedEnumerationFieldClassTestCase(
435 _EnumerationFieldClassTestCase, _TestFieldClass, unittest.TestCase
436 ):
437 _MAPPING_CLASS = bt2_field_class._SignedEnumerationFieldClassMappingConst
438 _RANGE_SET_CLASS = bt2.SignedIntegerRangeSet
439 _CONST_RANGE_SET_CLASS = bt2._SignedIntegerRangeSetConst
440
441 def _spec_set_up(self):
442 self._ranges1 = bt2.SignedIntegerRangeSet([(-10, -4), (18, 47)])
443 self._ranges2 = bt2.SignedIntegerRangeSet([(-3, -3)])
444 self._ranges3 = bt2.SignedIntegerRangeSet([(-100, -1), (8, 16), (48, 99)])
445 self._inval_ranges = bt2.UnsignedIntegerRangeSet([(8, 16), (48, 99)])
446 self._value_in_range_1_and_3 = -7
447
448 @staticmethod
449 def _const_value_setter(field):
450 field.value = 0
451
452 def _create_field_class(self, *args, **kwargs):
453 tc = get_default_trace_class()
454 return tc.create_signed_enumeration_field_class(*args, **kwargs)
455
456 def _create_default_const_field_class(self, *args, **kwargs):
457 tc = get_default_trace_class()
458 fc = tc.create_signed_enumeration_field_class(*args, **kwargs)
459 return _create_const_field_class(tc, fc, self._const_value_setter)
460
461 _create_default_field_class = _create_field_class
462
463
464 class StringFieldClassTestCase(_TestFieldClass, unittest.TestCase):
465 @staticmethod
466 def _const_value_setter(field):
467 field.value = 'chaine'
468
469 def _create_field_class(self, *args, **kwargs):
470 tc = get_default_trace_class()
471 return tc.create_string_field_class(*args, **kwargs)
472
473 def _create_default_const_field_class(self, *args, **kwargs):
474 tc = get_default_trace_class()
475 fc = tc.create_string_field_class(*args, **kwargs)
476 return _create_const_field_class(tc, fc, self._const_value_setter)
477
478 _create_default_field_class = _create_field_class
479
480 def setUp(self):
481 self._fc = self._create_default_field_class()
482
483 def test_create_default(self):
484 self.assertIsNotNone(self._fc)
485 self.assertEqual(len(self._fc.user_attributes), 0)
486
487
488 class _TestElementContainer:
489 def setUp(self):
490 self._tc = get_default_trace_class()
491 self._fc = self._create_default_field_class()
492 self._fc_const = self._create_default_const_field_class()
493
494 def test_create_default(self):
495 self.assertIsNotNone(self._fc)
496 self.assertEqual(len(self._fc.user_attributes), 0)
497
498 def test_append_element(self):
499 int_field_class = self._tc.create_signed_integer_field_class(32)
500 self._append_element_method(self._fc, 'int32', int_field_class)
501 field_class = self._fc['int32'].field_class
502 self.assertEqual(field_class.addr, int_field_class.addr)
503
504 def test_append_element_kwargs(self):
505 int_field_class = self._tc.create_signed_integer_field_class(32)
506 self._append_element_method(self._fc, name='int32', field_class=int_field_class)
507 field_class = self._fc['int32'].field_class
508 self.assertEqual(field_class.addr, int_field_class.addr)
509
510 def test_append_element_invalid_name(self):
511 sub_fc = self._tc.create_string_field_class()
512
513 with self.assertRaises(TypeError):
514 self._append_element_method(self._fc, 23, sub_fc)
515
516 def test_append_element_invalid_field_class(self):
517 with self.assertRaises(TypeError):
518 self._append_element_method(self._fc, 'yes', object())
519
520 def test_append_element_dup_name(self):
521 sub_fc1 = self._tc.create_string_field_class()
522 sub_fc2 = self._tc.create_string_field_class()
523
524 with self.assertRaises(ValueError):
525 self._append_element_method(self._fc, 'yes', sub_fc1)
526 self._append_element_method(self._fc, 'yes', sub_fc2)
527
528 def test_attr_field_class(self):
529 int_field_class = self._tc.create_signed_integer_field_class(32)
530 self._append_element_method(self._fc, 'int32', int_field_class)
531 field_class = self._fc['int32'].field_class
532
533 self.assertIs(type(field_class), bt2_field_class._SignedIntegerFieldClass)
534
535 def test_const_attr_field_class(self):
536 int_field_class = self._tc.create_signed_integer_field_class(32)
537 self._append_element_method(self._fc, 'int32', int_field_class)
538 field_class = self._fc['int32'].field_class
539 const_fc = _create_const_field_class(
540 self._tc, self._fc, self._const_value_setter
541 )
542 field_class = const_fc['int32'].field_class
543
544 self.assertIs(type(field_class), bt2_field_class._SignedIntegerFieldClassConst)
545
546 def test_iadd(self):
547 a_field_class = self._tc.create_single_precision_real_field_class()
548 b_field_class = self._tc.create_signed_integer_field_class(17)
549 self._append_element_method(self._fc, 'a_float', a_field_class)
550 self._append_element_method(self._fc, 'b_int', b_field_class)
551 c_field_class = self._tc.create_string_field_class()
552 d_field_class = self._tc.create_signed_enumeration_field_class(
553 field_value_range=32
554 )
555 e_field_class = self._tc.create_structure_field_class()
556 self._fc += [
557 ('c_string', c_field_class),
558 ('d_enum', d_field_class),
559 ('e_struct', e_field_class),
560 ]
561 self.assertEqual(self._fc['a_float'].field_class.addr, a_field_class.addr)
562 self.assertEqual(self._fc['a_float'].name, 'a_float')
563 self.assertEqual(self._fc['b_int'].field_class.addr, b_field_class.addr)
564 self.assertEqual(self._fc['b_int'].name, 'b_int')
565 self.assertEqual(self._fc['c_string'].field_class.addr, c_field_class.addr)
566 self.assertEqual(self._fc['c_string'].name, 'c_string')
567 self.assertEqual(self._fc['d_enum'].field_class.addr, d_field_class.addr)
568 self.assertEqual(self._fc['d_enum'].name, 'd_enum')
569 self.assertEqual(self._fc['e_struct'].field_class.addr, e_field_class.addr)
570 self.assertEqual(self._fc['e_struct'].name, 'e_struct')
571
572 def test_const_iadd(self):
573 a_field_class = self._tc.create_single_precision_real_field_class()
574 with self.assertRaises(TypeError):
575 self._fc_const += a_field_class
576
577 def test_bool_op(self):
578 self.assertFalse(self._fc)
579 self._append_element_method(self._fc, 'a', self._tc.create_string_field_class())
580 self.assertTrue(self._fc)
581
582 def test_len(self):
583 self._append_element_method(self._fc, 'a', self._tc.create_string_field_class())
584 self._append_element_method(self._fc, 'b', self._tc.create_string_field_class())
585 self._append_element_method(self._fc, 'c', self._tc.create_string_field_class())
586 self.assertEqual(len(self._fc), 3)
587
588 def test_getitem(self):
589 a_fc = self._tc.create_signed_integer_field_class(32)
590 b_fc = self._tc.create_string_field_class()
591 c_fc = self._tc.create_single_precision_real_field_class()
592 self._append_element_method(self._fc, 'a', a_fc)
593 self._append_element_method(self._fc, 'b', b_fc)
594 self._append_element_method(self._fc, 'c', c_fc)
595 self.assertEqual(self._fc['b'].field_class.addr, b_fc.addr)
596 self.assertEqual(self._fc['b'].name, 'b')
597
598 def test_getitem_invalid_key_type(self):
599 with self.assertRaises(TypeError):
600 self._fc[0]
601
602 def test_getitem_invalid_key(self):
603 with self.assertRaises(KeyError):
604 self._fc['no way']
605
606 def test_contains(self):
607 self.assertFalse('a' in self._fc)
608 self._append_element_method(self._fc, 'a', self._tc.create_string_field_class())
609 self.assertTrue('a' in self._fc)
610
611 def test_iter(self):
612 a_fc = self._tc.create_signed_integer_field_class(32)
613 b_fc = self._tc.create_string_field_class()
614 c_fc = self._tc.create_single_precision_real_field_class()
615 elements = (('a', a_fc), ('b', b_fc), ('c', c_fc))
616
617 for elem in elements:
618 self._append_element_method(self._fc, *elem)
619
620 for (name, element), test_elem in zip(self._fc.items(), elements):
621 self.assertEqual(element.name, test_elem[0])
622 self.assertEqual(name, element.name)
623 self.assertEqual(element.field_class.addr, test_elem[1].addr)
624 self.assertEqual(len(element.user_attributes), 0)
625
626 def test_at_index(self):
627 a_fc = self._tc.create_signed_integer_field_class(32)
628 b_fc = self._tc.create_string_field_class()
629 c_fc = self._tc.create_single_precision_real_field_class()
630 self._append_element_method(self._fc, 'c', c_fc)
631 self._append_element_method(self._fc, 'a', a_fc)
632 self._append_element_method(self._fc, 'b', b_fc)
633 elem = self._at_index_method(self._fc, 1)
634 self.assertEqual(elem.field_class.addr, a_fc.addr)
635 self.assertEqual(elem.name, 'a')
636
637 def test_at_index_invalid(self):
638 self._append_element_method(
639 self._fc, 'c', self._tc.create_signed_integer_field_class(32)
640 )
641
642 with self.assertRaises(TypeError):
643 self._at_index_method(self._fc, 'yes')
644
645 def test_at_index_out_of_bounds_after(self):
646 self._append_element_method(
647 self._fc, 'c', self._tc.create_signed_integer_field_class(32)
648 )
649
650 with self.assertRaises(IndexError):
651 self._at_index_method(self._fc, len(self._fc))
652
653 def test_user_attributes(self):
654 self._append_element_method(
655 self._fc,
656 'c',
657 self._tc.create_string_field_class(),
658 user_attributes={'salut': 23},
659 )
660 self.assertEqual(self._fc['c'].user_attributes, {'salut': 23})
661 self.assertIs(type(self._fc.user_attributes), bt2_value.MapValue)
662 self.assertIs(type(self._fc['c'].user_attributes), bt2_value.MapValue)
663
664 def test_invalid_user_attributes(self):
665 with self.assertRaises(TypeError):
666 self._append_element_method(
667 self._fc,
668 'c',
669 self._tc.create_string_field_class(),
670 user_attributes=object(),
671 )
672
673 def test_invalid_user_attributes_value_type(self):
674 with self.assertRaises(TypeError):
675 self._append_element_method(
676 self._fc, 'c', self._tc.create_string_field_class(), user_attributes=23
677 )
678
679
680 class StructureFieldClassTestCase(
681 _TestFieldClass, _TestElementContainer, unittest.TestCase
682 ):
683 _append_element_method = staticmethod(bt2._StructureFieldClass.append_member)
684 _at_index_method = staticmethod(bt2._StructureFieldClass.member_at_index)
685
686 @staticmethod
687 def _const_value_setter(field):
688 field.value = {}
689
690 def _create_field_class(self, *args, **kwargs):
691 tc = get_default_trace_class()
692 return tc.create_structure_field_class(*args, **kwargs)
693
694 def _create_default_const_field_class(self, *args, **kwargs):
695 tc = get_default_trace_class()
696 fc = tc.create_structure_field_class(*args, **kwargs)
697 return _create_const_field_class(tc, fc, self._const_value_setter)
698
699 _create_default_field_class = _create_field_class
700
701 def test_const_member_field_class(self):
702 def _real_value_setter(field):
703 field.value = {'real': 0}
704
705 tc = get_default_trace_class()
706 fc = tc.create_structure_field_class()
707 member_fc = self._tc.create_single_precision_real_field_class()
708 fc.append_member('real', member_fc)
709 const_fc = _create_const_field_class(tc, fc, _real_value_setter)
710
711 self.assertIs(
712 type(const_fc['real'].field_class),
713 bt2_field_class._SinglePrecisionRealFieldClassConst,
714 )
715
716 def test_member_field_class(self):
717 tc = get_default_trace_class()
718 fc = tc.create_structure_field_class()
719 member_fc = self._tc.create_single_precision_real_field_class()
720 fc.append_member('real', member_fc)
721
722 self.assertIs(
723 type(fc['real'].field_class), bt2_field_class._SinglePrecisionRealFieldClass
724 )
725
726
727 class OptionWithoutSelectorFieldClassTestCase(_TestFieldClass, unittest.TestCase):
728 @staticmethod
729 def _const_value_setter(field):
730 field.has_field = True
731 field.value = 12
732
733 def _create_default_field_class(self, *args, **kwargs):
734 return self._tc.create_option_without_selector_field_class(
735 self._content_fc, *args, **kwargs
736 )
737
738 def _create_default_const_field_class(self, *args, **kwargs):
739 fc = self._tc.create_option_without_selector_field_class(
740 self._content_fc, *args, **kwargs
741 )
742 return _create_const_field_class(self._tc, fc, self._const_value_setter)
743
744 def setUp(self):
745 self._tc = get_default_trace_class()
746 self._content_fc = self._tc.create_signed_integer_field_class(23)
747
748 def test_create_default(self):
749 fc = self._create_default_field_class()
750 self.assertEqual(fc.field_class.addr, self._content_fc.addr)
751 self.assertEqual(len(fc.user_attributes), 0)
752
753 def test_create_invalid_field_class(self):
754 with self.assertRaises(TypeError):
755 self._tc.create_option_without_selector_field_class(object())
756
757 def test_attr_field_class(self):
758 fc = self._create_default_field_class()
759 self.assertIs(type(fc.field_class), bt2_field_class._SignedIntegerFieldClass)
760
761 def test_const_attr_field_class(self):
762 fc = self._create_default_const_field_class()
763 self.assertIs(
764 type(fc.field_class), bt2_field_class._SignedIntegerFieldClassConst
765 )
766
767
768 class _OptionWithSelectorFieldClassTestCase(_TestFieldClass):
769 @staticmethod
770 def _const_value_setter(field):
771 field['opt'].has_field = True
772 field['opt'].value = 12
773
774 def _create_default_const_field_class(self, *args, **kwargs):
775 # Create a struct to contain the option and its selector else we can't
776 # create the non-const field necessary to get the the const field_class
777 struct_fc = self._tc.create_structure_field_class()
778 struct_fc.append_member('selecteux', self._tag_fc)
779 opt_fc = self._create_default_field_class(*args, **kwargs)
780 struct_fc.append_member('opt', opt_fc)
781
782 return _create_const_field_class(self._tc, struct_fc, self._const_value_setter)[
783 'opt'
784 ].field_class
785
786 def setUp(self):
787 self._tc = get_default_trace_class()
788 self._content_fc = self._tc.create_signed_integer_field_class(23)
789 self._tag_fc = self._create_tag_fc()
790
791 def _create_field_class_for_field_path_test(self):
792 fc = self._create_default_field_class()
793
794 foo_fc = self._tc.create_single_precision_real_field_class()
795 bar_fc = self._tc.create_string_field_class()
796 baz_fc = self._tc.create_string_field_class()
797
798 inner_struct_fc = self._tc.create_structure_field_class()
799 inner_struct_fc.append_member('bar', bar_fc)
800 inner_struct_fc.append_member('baz', baz_fc)
801 inner_struct_fc.append_member('tag', self._tag_fc)
802 inner_struct_fc.append_member('opt', fc)
803
804 opt_struct_array_fc = self._tc.create_option_without_selector_field_class(
805 inner_struct_fc
806 )
807
808 outer_struct_fc = self._tc.create_structure_field_class()
809 outer_struct_fc.append_member('foo', foo_fc)
810 outer_struct_fc.append_member('inner_opt', opt_struct_array_fc)
811
812 # The path to the selector field class is resolved when the
813 # option field class is actually used, for example in a packet
814 # context.
815 self._tc.create_stream_class(
816 packet_context_field_class=outer_struct_fc, supports_packets=True
817 )
818
819 return fc
820
821 def test_field_path_len(self):
822 fc = self._create_field_class_for_field_path_test()
823 self.assertEqual(len(fc.selector_field_path), 3)
824
825 def test_field_path_iter(self):
826 fc = self._create_field_class_for_field_path_test()
827 path_items = list(fc.selector_field_path)
828
829 self.assertEqual(len(path_items), 3)
830
831 self.assertIsInstance(path_items[0], bt2._IndexFieldPathItem)
832 self.assertEqual(path_items[0].index, 1)
833
834 self.assertIsInstance(path_items[1], bt2._CurrentOptionContentFieldPathItem)
835
836 self.assertIsInstance(path_items[2], bt2._IndexFieldPathItem)
837 self.assertEqual(path_items[2].index, 2)
838
839 def test_field_path_root_scope(self):
840 fc = self._create_field_class_for_field_path_test()
841 self.assertEqual(
842 fc.selector_field_path.root_scope, bt2.FieldPathScope.PACKET_CONTEXT
843 )
844
845
846 class OptionWithBoolSelectorFieldClassTestCase(
847 _OptionWithSelectorFieldClassTestCase, unittest.TestCase
848 ):
849 def _create_default_field_class(self, *args, **kwargs):
850 return self._tc.create_option_with_bool_selector_field_class(
851 self._content_fc, self._tag_fc, *args, **kwargs
852 )
853
854 def _create_tag_fc(self):
855 return self._tc.create_bool_field_class()
856
857 def test_create_default(self):
858 fc = self._create_default_field_class()
859 self.assertEqual(fc.field_class.addr, self._content_fc.addr)
860 self.assertFalse(fc.selector_is_reversed)
861 self.assertEqual(len(fc.user_attributes), 0)
862
863 def test_create_selector_is_reversed_wrong_type(self):
864 with self.assertRaises(TypeError):
865 self._create_default_field_class(selector_is_reversed=23)
866
867 def test_create_invalid_selector_type(self):
868 with self.assertRaises(TypeError):
869 self._tc.create_option_with_bool_selector_field_class(self._content_fc, 17)
870
871 def test_attr_selector_is_reversed(self):
872 fc = self._create_default_field_class(selector_is_reversed=True)
873 self.assertTrue(fc.selector_is_reversed)
874
875 def test_const_attr_selector_is_reversed(self):
876 fc = self._create_default_const_field_class(selector_is_reversed=True)
877 self.assertTrue(fc.selector_is_reversed)
878
879
880 class _OptionWithIntegerSelectorFieldClassTestCase(
881 _OptionWithSelectorFieldClassTestCase
882 ):
883 def _create_default_field_class(self, *args, **kwargs):
884 return self._tc.create_option_with_integer_selector_field_class(
885 self._content_fc, self._tag_fc, self._ranges, *args, **kwargs
886 )
887
888 def test_create_default(self):
889 fc = self._create_default_field_class()
890 self.assertEqual(fc.field_class.addr, self._content_fc.addr)
891 self.assertEqual(fc.ranges, self._ranges)
892 self.assertEqual(len(fc.user_attributes), 0)
893
894 def test_create_ranges_wrong_type(self):
895 with self.assertRaises(TypeError):
896 self._tc.create_option_with_integer_selector_field_class(
897 self._content_fc, self._tag_fc, 23
898 )
899
900 def test_create_ranges_empty(self):
901 with self.assertRaises(ValueError):
902 self._tc.create_option_with_integer_selector_field_class(
903 self._content_fc, self._tag_fc, type(self._ranges)()
904 )
905
906 def test_create_invalid_selector_type(self):
907 with self.assertRaises(TypeError):
908 self._tc.create_option_with_bool_selector_field_class(self._content_fc, 17)
909
910 def test_attr_ranges(self):
911 fc = self._create_default_field_class()
912 print(type(fc.ranges), type(self._ranges))
913 self.assertEqual(fc.ranges, self._ranges)
914
915 def test_const_attr_ranges(self):
916 fc = self._create_default_const_field_class()
917 self.assertEqual(fc.ranges, self._ranges)
918
919
920 class OptionWithUnsignedIntegerSelectorFieldClassTestCase(
921 _OptionWithIntegerSelectorFieldClassTestCase, unittest.TestCase
922 ):
923 def setUp(self):
924 self._ranges = bt2.UnsignedIntegerRangeSet([(1, 3), (18, 44)])
925 super().setUp()
926
927 def _create_tag_fc(self):
928 return self._tc.create_unsigned_integer_field_class()
929
930
931 class VariantFieldClassWithoutSelectorTestCase(
932 _TestFieldClass, _TestElementContainer, unittest.TestCase
933 ):
934 _append_element_method = staticmethod(
935 bt2._VariantFieldClassWithoutSelector.append_option
936 )
937 _at_index_method = staticmethod(
938 bt2._VariantFieldClassWithoutSelector.option_at_index
939 )
940
941 @staticmethod
942 def _const_value_setter(variant_field):
943 variant_field.selected_option_index = 0
944 variant_field.value = 12
945
946 def _create_field_class(self, *args, **kwargs):
947 tc = get_default_trace_class()
948 return tc.create_variant_field_class(*args, **kwargs)
949
950 def _create_default_const_field_class(self, *args, **kwargs):
951 tc = get_default_trace_class()
952 fc = tc.create_variant_field_class(*args, **kwargs)
953 int_field_class = self._tc.create_signed_integer_field_class(32)
954 fc.append_option('int32', int_field_class)
955
956 return _create_const_field_class(tc, fc, self._const_value_setter)
957
958 _create_default_field_class = _create_field_class
959
960
961 class _VariantFieldClassWithIntegerSelectorTestCase:
962 @staticmethod
963 def _const_value_setter(field):
964 field['variant'].selected_option_index = 0
965 field['variant'] = 12
966
967 def _create_default_field_class(self, *args, **kwargs):
968 return self._tc.create_variant_field_class(
969 *args, selector_fc=self._selector_fc, **kwargs
970 )
971
972 def _create_default_const_field_class(self, *args, **kwargs):
973 # Create a struct to contain the variant and its selector else we can't
974 # create the non-const field necessary to get the the const field_class
975 struct_fc = self._tc.create_structure_field_class()
976 struct_fc.append_member('selecteux', self._selector_fc)
977 variant_fc = self._tc.create_variant_field_class(
978 *args, selector_fc=self._selector_fc
979 )
980 variant_fc.append_option(
981 'a', self._tc.create_signed_integer_field_class(32), self._ranges1
982 )
983 struct_fc.append_member('variant', variant_fc, **kwargs)
984
985 return _create_const_field_class(self._tc, struct_fc, self._const_value_setter)[
986 'variant'
987 ].field_class
988
989 def setUp(self):
990 self._tc = get_default_trace_class()
991 self._spec_set_up()
992 self._fc = self._create_default_field_class()
993
994 def test_create_default(self):
995 self.assertIsNotNone(self._fc)
996 self.assertEqual(len(self._fc.user_attributes), 0)
997
998 def test_append_element(self):
999 str_field_class = self._tc.create_string_field_class()
1000 self._fc.append_option('str', str_field_class, self._ranges1)
1001 opt = self._fc['str']
1002 self.assertEqual(opt.field_class.addr, str_field_class.addr)
1003 self.assertEqual(opt.name, 'str')
1004 self.assertEqual(opt.ranges.addr, self._ranges1.addr)
1005
1006 def test_const_append(self):
1007 fc_const = self._create_default_const_field_class()
1008 str_field_class = self._tc.create_string_field_class()
1009 with self.assertRaises(AttributeError):
1010 fc_const.append_option('str', str_field_class, self._ranges1)
1011
1012 def test_append_element_kwargs(self):
1013 int_field_class = self._tc.create_signed_integer_field_class(32)
1014 self._fc.append_option(
1015 name='int32', field_class=int_field_class, ranges=self._ranges1
1016 )
1017 opt = self._fc['int32']
1018 self.assertEqual(opt.field_class.addr, int_field_class.addr)
1019 self.assertEqual(opt.name, 'int32')
1020 self.assertEqual(opt.ranges.addr, self._ranges1.addr)
1021
1022 def test_append_element_invalid_name(self):
1023 sub_fc = self._tc.create_string_field_class()
1024
1025 with self.assertRaises(TypeError):
1026 self._fc.append_option(self._fc, 23, sub_fc)
1027
1028 def test_append_element_invalid_field_class(self):
1029 with self.assertRaises(TypeError):
1030 self._fc.append_option(self._fc, 'yes', object())
1031
1032 def test_append_element_invalid_ranges(self):
1033 sub_fc = self._tc.create_string_field_class()
1034
1035 with self.assertRaises(TypeError):
1036 self._fc.append_option(self._fc, sub_fc, 'lel')
1037
1038 def test_append_element_dup_name(self):
1039 sub_fc1 = self._tc.create_string_field_class()
1040 sub_fc2 = self._tc.create_string_field_class()
1041
1042 with self.assertRaises(ValueError):
1043 self._fc.append_option('yes', sub_fc1, self._ranges1)
1044 self._fc.append_option('yes', sub_fc2, self._ranges2)
1045
1046 def test_append_element_invalid_ranges_signedness(self):
1047 sub_fc = self._tc.create_string_field_class()
1048
1049 with self.assertRaises(TypeError):
1050 self._fc.append_option(self._fc, sub_fc, self._inval_ranges)
1051
1052 def test_user_attributes(self):
1053 self._fc.append_option(
1054 'c',
1055 self._tc.create_string_field_class(),
1056 self._ranges1,
1057 user_attributes={'salut': 23},
1058 )
1059 self.assertEqual(self._fc['c'].user_attributes, {'salut': 23})
1060 self.assertIs(type(self._fc.user_attributes), bt2_value.MapValue)
1061
1062 def test_const_user_attributes(self):
1063 fc_const = self._create_default_const_field_class()
1064 self.assertIs(type(fc_const.user_attributes), bt2_value._MapValueConst)
1065
1066 def test_invalid_user_attributes(self):
1067 with self.assertRaises(TypeError):
1068 self._fc.append_option(
1069 'c',
1070 self._tc.create_string_field_class(),
1071 self._ranges1,
1072 user_attributes=object(),
1073 )
1074
1075 def test_invalid_user_attributes_value_type(self):
1076 with self.assertRaises(TypeError):
1077 self._fc.append_option(
1078 'c',
1079 self._tc.create_string_field_class(),
1080 self._ranges1,
1081 user_attributes=23,
1082 )
1083
1084 def test_iadd(self):
1085 a_field_class = self._tc.create_single_precision_real_field_class()
1086 self._fc.append_option('a_float', a_field_class, self._ranges1)
1087 c_field_class = self._tc.create_string_field_class()
1088 d_field_class = self._tc.create_signed_enumeration_field_class(
1089 field_value_range=32
1090 )
1091 self._fc += [
1092 ('c_string', c_field_class, self._ranges2),
1093 ('d_enum', d_field_class, self._ranges3),
1094 ]
1095 self.assertEqual(self._fc['a_float'].field_class.addr, a_field_class.addr)
1096 self.assertEqual(self._fc['a_float'].name, 'a_float')
1097 self.assertEqual(self._fc['a_float'].ranges, self._ranges1)
1098 self.assertEqual(self._fc['c_string'].field_class.addr, c_field_class.addr)
1099 self.assertEqual(self._fc['c_string'].name, 'c_string')
1100 self.assertEqual(self._fc['c_string'].ranges, self._ranges2)
1101 self.assertEqual(self._fc['d_enum'].field_class.addr, d_field_class.addr)
1102 self.assertEqual(self._fc['d_enum'].name, 'd_enum')
1103 self.assertEqual(self._fc['d_enum'].ranges, self._ranges3)
1104
1105 def test_const_iadd(self):
1106 fc_const = self._create_default_const_field_class()
1107 a_field_class = self._tc.create_single_precision_real_field_class()
1108 with self.assertRaises(TypeError):
1109 fc_const += [('a_float', a_field_class, self._ranges1)]
1110
1111 def test_bool_op(self):
1112 self.assertFalse(self._fc)
1113 self._fc.append_option('a', self._tc.create_string_field_class(), self._ranges1)
1114 self.assertTrue(self._fc)
1115
1116 def test_len(self):
1117 self._fc.append_option('a', self._tc.create_string_field_class(), self._ranges1)
1118 self._fc.append_option('b', self._tc.create_string_field_class(), self._ranges2)
1119 self._fc.append_option('c', self._tc.create_string_field_class(), self._ranges3)
1120 self.assertEqual(len(self._fc), 3)
1121
1122 def test_getitem(self):
1123 a_fc = self._tc.create_signed_integer_field_class(32)
1124 b_fc = self._tc.create_string_field_class()
1125 c_fc = self._tc.create_single_precision_real_field_class()
1126 self._fc.append_option('a', a_fc, self._ranges1)
1127 self._fc.append_option('b', b_fc, self._ranges2)
1128 self._fc.append_option('c', c_fc, self._ranges3)
1129 self.assertEqual(self._fc['b'].field_class.addr, b_fc.addr)
1130 self.assertEqual(self._fc['b'].name, 'b')
1131 self.assertEqual(self._fc['b'].ranges.addr, self._ranges2.addr)
1132
1133 def test_option_field_class(self):
1134 a_fc = self._tc.create_signed_integer_field_class(32)
1135 self._fc.append_option('a', a_fc, self._ranges1)
1136 self.assertIs(
1137 type(self._fc['a'].field_class), bt2_field_class._SignedIntegerFieldClass
1138 )
1139
1140 def test_const_option_field_class(self):
1141 fc_const = self._create_default_const_field_class()
1142 self.assertIs(
1143 type(fc_const['a'].field_class),
1144 bt2_field_class._SignedIntegerFieldClassConst,
1145 )
1146
1147 def test_getitem_invalid_key_type(self):
1148 with self.assertRaises(TypeError):
1149 self._fc[0]
1150
1151 def test_getitem_invalid_key(self):
1152 with self.assertRaises(KeyError):
1153 self._fc['no way']
1154
1155 def test_contains(self):
1156 self.assertFalse('a' in self._fc)
1157 self._fc.append_option('a', self._tc.create_string_field_class(), self._ranges1)
1158 self.assertTrue('a' in self._fc)
1159
1160 def test_iter(self):
1161 a_fc = self._tc.create_signed_integer_field_class(32)
1162 b_fc = self._tc.create_string_field_class()
1163 c_fc = self._tc.create_single_precision_real_field_class()
1164 opts = (
1165 ('a', a_fc, self._ranges1),
1166 ('b', b_fc, self._ranges2),
1167 ('c', c_fc, self._ranges3),
1168 )
1169
1170 for opt in opts:
1171 self._fc.append_option(*opt)
1172
1173 for (name, opt), test_opt in zip(self._fc.items(), opts):
1174 self.assertEqual(opt.name, test_opt[0])
1175 self.assertEqual(name, opt.name)
1176 self.assertEqual(opt.field_class.addr, test_opt[1].addr)
1177 self.assertEqual(opt.ranges.addr, test_opt[2].addr)
1178
1179 def test_at_index(self):
1180 a_fc = self._tc.create_signed_integer_field_class(32)
1181 b_fc = self._tc.create_string_field_class()
1182 c_fc = self._tc.create_single_precision_real_field_class()
1183 self._fc.append_option('c', c_fc, self._ranges1)
1184 self._fc.append_option('a', a_fc, self._ranges2)
1185 self._fc.append_option('b', b_fc, self._ranges3)
1186 self.assertEqual(self._fc.option_at_index(1).field_class.addr, a_fc.addr)
1187 self.assertEqual(self._fc.option_at_index(1).name, 'a')
1188 self.assertEqual(self._fc.option_at_index(1).ranges.addr, self._ranges2.addr)
1189
1190 def test_at_index_invalid(self):
1191 self._fc.append_option(
1192 'c', self._tc.create_signed_integer_field_class(32), self._ranges3
1193 )
1194
1195 with self.assertRaises(TypeError):
1196 self._fc.option_at_index('yes')
1197
1198 def test_at_index_out_of_bounds_after(self):
1199 self._fc.append_option(
1200 'c', self._tc.create_signed_integer_field_class(32), self._ranges3
1201 )
1202
1203 with self.assertRaises(IndexError):
1204 self._fc.option_at_index(len(self._fc))
1205
1206 def _fill_default_fc_for_field_path_test(self):
1207 # Create something equivalent to:
1208 #
1209 # struct outer_struct_fc {
1210 # real foo;
1211 # struct inner_struct_fc {
1212 # [u]int64_t selector;
1213 # string bar;
1214 # string baz;
1215 # variant <selector> {
1216 # real a; // selected with self._ranges1
1217 # int21_t b; // selected with self._ranges2
1218 # uint34_t c; // selected with self._ranges3
1219 # } variant;
1220 # } inner_struct[2];
1221 # };
1222 self._fc.append_option(
1223 'a', self._tc.create_single_precision_real_field_class(), self._ranges1
1224 )
1225 self._fc.append_option(
1226 'b', self._tc.create_signed_integer_field_class(21), self._ranges2
1227 )
1228 self._fc.append_option(
1229 'c', self._tc.create_unsigned_integer_field_class(34), self._ranges3
1230 )
1231
1232 foo_fc = self._tc.create_single_precision_real_field_class()
1233 bar_fc = self._tc.create_string_field_class()
1234 baz_fc = self._tc.create_string_field_class()
1235
1236 inner_struct_fc = self._tc.create_structure_field_class()
1237 inner_struct_fc.append_member('selector', self._selector_fc)
1238 inner_struct_fc.append_member('bar', bar_fc)
1239 inner_struct_fc.append_member('baz', baz_fc)
1240 inner_struct_fc.append_member('variant', self._fc)
1241
1242 inner_struct_array_fc = self._tc.create_static_array_field_class(
1243 inner_struct_fc, 2
1244 )
1245
1246 outer_struct_fc = self._tc.create_structure_field_class()
1247 outer_struct_fc.append_member('foo', foo_fc)
1248 outer_struct_fc.append_member('inner_struct', inner_struct_array_fc)
1249
1250 # The path to the selector field is resolved when the sequence is
1251 # actually used, for example in a packet context.
1252 self._tc.create_stream_class(
1253 supports_packets=True, packet_context_field_class=outer_struct_fc
1254 )
1255
1256 def test_selector_field_path_length(self):
1257 self._fill_default_fc_for_field_path_test()
1258 self.assertEqual(len(self._fc.selector_field_path), 3)
1259
1260 def test_selector_field_path_iter(self):
1261 self._fill_default_fc_for_field_path_test()
1262 path_items = list(self._fc.selector_field_path)
1263
1264 self.assertEqual(len(path_items), 3)
1265
1266 self.assertIsInstance(path_items[0], bt2._IndexFieldPathItem)
1267 self.assertEqual(path_items[0].index, 1)
1268
1269 self.assertIsInstance(path_items[1], bt2._CurrentArrayElementFieldPathItem)
1270
1271 self.assertIsInstance(path_items[2], bt2._IndexFieldPathItem)
1272 self.assertEqual(path_items[2].index, 0)
1273
1274 def test_selector_field_path_root_scope(self):
1275 self._fill_default_fc_for_field_path_test()
1276 self.assertEqual(
1277 self._fc.selector_field_path.root_scope, bt2.FieldPathScope.PACKET_CONTEXT
1278 )
1279
1280
1281 class VariantFieldClassWithUnsignedSelectorTestCase(
1282 _VariantFieldClassWithIntegerSelectorTestCase, unittest.TestCase
1283 ):
1284 def _spec_set_up(self):
1285 self._ranges1 = bt2.UnsignedIntegerRangeSet([(1, 4), (18, 47)])
1286 self._ranges2 = bt2.UnsignedIntegerRangeSet([(5, 5)])
1287 self._ranges3 = bt2.UnsignedIntegerRangeSet([(8, 16), (48, 99)])
1288 self._inval_ranges = bt2.SignedIntegerRangeSet([(-8, 16), (48, 99)])
1289 self._selector_fc = self._tc.create_unsigned_integer_field_class()
1290
1291
1292 class VariantFieldClassWithSignedSelectorTestCase(
1293 _VariantFieldClassWithIntegerSelectorTestCase, unittest.TestCase
1294 ):
1295 def _spec_set_up(self):
1296 self._ranges1 = bt2.SignedIntegerRangeSet([(-10, -4), (18, 47)])
1297 self._ranges2 = bt2.SignedIntegerRangeSet([(-3, -3)])
1298 self._ranges3 = bt2.SignedIntegerRangeSet([(8, 16), (48, 99)])
1299 self._inval_ranges = bt2.UnsignedIntegerRangeSet([(8, 16), (48, 99)])
1300 self._selector_fc = self._tc.create_signed_integer_field_class()
1301
1302
1303 class _ArrayFieldClassTestCase:
1304 def test_attr_element_field_class(self):
1305 fc = self._create_array()
1306 self.assertIs(
1307 type(fc.element_field_class), bt2_field_class._SignedIntegerFieldClass
1308 )
1309
1310 def test_const_attr_element_field_class(self):
1311 fc = self._create_const_array()
1312 self.assertIs(
1313 type(fc.element_field_class), bt2_field_class._SignedIntegerFieldClassConst
1314 )
1315
1316
1317 class StaticArrayFieldClassTestCase(_ArrayFieldClassTestCase, unittest.TestCase):
1318 @staticmethod
1319 def _const_value_setter(field):
1320 field.value = [9] * 45
1321
1322 def _create_array(self):
1323 return self._tc.create_static_array_field_class(self._elem_fc, 45)
1324
1325 def _create_const_array(self):
1326 fc = self._tc.create_static_array_field_class(self._elem_fc, 45)
1327 return _create_const_field_class(self._tc, fc, self._const_value_setter)
1328
1329 def setUp(self):
1330 self._tc = get_default_trace_class()
1331 self._elem_fc = self._tc.create_signed_integer_field_class(23)
1332
1333 def test_create_default(self):
1334 fc = self._tc.create_static_array_field_class(self._elem_fc, 45)
1335 self.assertEqual(fc.element_field_class.addr, self._elem_fc.addr)
1336 self.assertEqual(fc.length, 45)
1337 self.assertEqual(len(fc.user_attributes), 0)
1338
1339 def test_create_invalid_elem_field_class(self):
1340 with self.assertRaises(TypeError):
1341 self._tc.create_static_array_field_class(object(), 45)
1342
1343 def test_create_invalid_length(self):
1344 with self.assertRaises(ValueError):
1345 self._tc.create_static_array_field_class(
1346 self._tc.create_string_field_class(), -17
1347 )
1348
1349 def test_create_invalid_length_type(self):
1350 with self.assertRaises(TypeError):
1351 self._tc.create_static_array_field_class(
1352 self._tc.create_string_field_class(), 'the length'
1353 )
1354
1355
1356 class DynamicArrayFieldClassTestCase(_ArrayFieldClassTestCase, unittest.TestCase):
1357 @staticmethod
1358 def _const_value_setter(field):
1359 field.value = []
1360
1361 def _create_array(self):
1362 return self._tc.create_dynamic_array_field_class(self._elem_fc)
1363
1364 def _create_const_array(self):
1365 fc = self._tc.create_dynamic_array_field_class(self._elem_fc)
1366 return _create_const_field_class(self._tc, fc, self._const_value_setter)
1367
1368 def setUp(self):
1369 self._tc = get_default_trace_class()
1370 self._elem_fc = self._tc.create_signed_integer_field_class(23)
1371 self._len_fc = self._tc.create_unsigned_integer_field_class(12)
1372
1373 def test_create_default(self):
1374 fc = self._tc.create_dynamic_array_field_class(self._elem_fc)
1375 self.assertEqual(fc.element_field_class.addr, self._elem_fc.addr)
1376 self.assertIsNone(fc.length_field_path, None)
1377 self.assertEqual(len(fc.user_attributes), 0)
1378
1379 def _create_field_class_for_field_path_test(self):
1380 # Create something a field class that is equivalent to:
1381 #
1382 # struct outer_struct_fc {
1383 # real foo;
1384 # struct inner_struct_fc {
1385 # string bar;
1386 # string baz;
1387 # uint12_t len;
1388 # uint23_t dyn_array[len];
1389 # } inner_struct[2];
1390 # };
1391
1392 fc = self._tc.create_dynamic_array_field_class(self._elem_fc, self._len_fc)
1393
1394 foo_fc = self._tc.create_single_precision_real_field_class()
1395 bar_fc = self._tc.create_string_field_class()
1396 baz_fc = self._tc.create_string_field_class()
1397
1398 inner_struct_fc = self._tc.create_structure_field_class()
1399 inner_struct_fc.append_member('bar', bar_fc)
1400 inner_struct_fc.append_member('baz', baz_fc)
1401 inner_struct_fc.append_member('len', self._len_fc)
1402 inner_struct_fc.append_member('dyn_array', fc)
1403
1404 inner_struct_array_fc = self._tc.create_static_array_field_class(
1405 inner_struct_fc, 2
1406 )
1407
1408 outer_struct_fc = self._tc.create_structure_field_class()
1409 outer_struct_fc.append_member('foo', foo_fc)
1410 outer_struct_fc.append_member('inner_struct', inner_struct_array_fc)
1411
1412 # The path to the length field is resolved when the sequence is
1413 # actually used, for example in a packet context.
1414 self._tc.create_stream_class(
1415 packet_context_field_class=outer_struct_fc, supports_packets=True
1416 )
1417
1418 return fc
1419
1420 def test_field_path_len(self):
1421 fc = self._create_field_class_for_field_path_test()
1422 self.assertEqual(len(fc.length_field_path), 3)
1423
1424 def test_field_path_iter(self):
1425 fc = self._create_field_class_for_field_path_test()
1426 path_items = list(fc.length_field_path)
1427
1428 self.assertEqual(len(path_items), 3)
1429
1430 self.assertIsInstance(path_items[0], bt2._IndexFieldPathItem)
1431 self.assertEqual(path_items[0].index, 1)
1432
1433 self.assertIsInstance(path_items[1], bt2._CurrentArrayElementFieldPathItem)
1434
1435 self.assertIsInstance(path_items[2], bt2._IndexFieldPathItem)
1436 self.assertEqual(path_items[2].index, 2)
1437
1438 def test_field_path_root_scope(self):
1439 fc = self._create_field_class_for_field_path_test()
1440 self.assertEqual(
1441 fc.length_field_path.root_scope, bt2.FieldPathScope.PACKET_CONTEXT
1442 )
1443
1444 def test_create_invalid_field_class(self):
1445 with self.assertRaises(TypeError):
1446 self._tc.create_dynamic_array_field_class(object())
1447
1448 def test_create_invalid_length_type(self):
1449 with self.assertRaises(TypeError):
1450 self._tc.create_dynamic_array_field_class(
1451 self._tc.create_string_field_class(), 17
1452 )
1453
1454
1455 if __name__ == "__main__":
1456 unittest.main()
This page took 0.115282 seconds and 3 git commands to generate.