1 # SPDX-License-Identifier: GPL-2.0-only
3 # Copyright (C) 2019 EfficiOS Inc.
8 from utils
import get_default_trace_class
, TestOutputPortMessageIterator
9 from bt2
import value
as bt2_value
10 from bt2
import field_class
as bt2_field_class
13 def _create_stream(tc
, ctx_field_classes
):
14 packet_context_fc
= tc
.create_structure_field_class()
15 for name
, fc
in ctx_field_classes
:
16 packet_context_fc
.append_member(name
, fc
)
19 stream_class
= tc
.create_stream_class(
20 packet_context_field_class
=packet_context_fc
, supports_packets
=True
23 stream
= trace
.create_stream(stream_class
)
27 def _create_const_field_class(tc
, field_class
, value_setter_fn
):
28 field_name
= 'const field'
30 class MyIter(bt2
._UserMessageIterator
):
31 def __init__(self
, config
, self_port_output
):
33 nonlocal value_setter_fn
34 stream
= _create_stream(tc
, [(field_name
, field_class
)])
35 packet
= stream
.create_packet()
37 value_setter_fn(packet
.context_field
[field_name
])
40 self
._create
_stream
_beginning
_message
(stream
),
41 self
._create
_packet
_beginning
_message
(packet
),
45 if len(self
._msgs
) == 0:
48 return self
._msgs
.pop(0)
50 class MySrc(bt2
._UserSourceComponent
, message_iterator_class
=MyIter
):
51 def __init__(self
, config
, params
, obj
):
52 self
._add
_output
_port
('out', params
)
55 src_comp
= graph
.add_component(MySrc
, 'my_source', None)
56 msg_iter
= TestOutputPortMessageIterator(graph
, src_comp
.output_ports
['out'])
58 # Ignore first message, stream beginning
60 packet_beg_msg
= next(msg_iter
)
62 return packet_beg_msg
.packet
.context_field
[field_name
].cls
65 class _TestFieldClass
:
66 def test_create_user_attributes(self
):
67 fc
= self
._create
_default
_field
_class
(user_attributes
={'salut': 23})
68 self
.assertEqual(fc
.user_attributes
, {'salut': 23})
69 self
.assertIs(type(fc
.user_attributes
), bt2_value
.MapValue
)
71 def test_const_create_user_attributes(self
):
72 fc
= self
._create
_default
_const
_field
_class
(user_attributes
={'salut': 23})
73 self
.assertEqual(fc
.user_attributes
, {'salut': 23})
74 self
.assertIs(type(fc
.user_attributes
), bt2_value
._MapValueConst
)
76 def test_create_invalid_user_attributes(self
):
77 with self
.assertRaises(TypeError):
78 self
._create
_default
_field
_class
(user_attributes
=object())
80 def test_create_invalid_user_attributes_value_type(self
):
81 with self
.assertRaises(TypeError):
82 self
._create
_default
_field
_class
(user_attributes
=23)
85 class BoolFieldClassTestCase(_TestFieldClass
, unittest
.TestCase
):
87 def _const_value_setter(field
):
90 def _create_default_field_class(self
, **kwargs
):
91 tc
= get_default_trace_class()
92 return tc
.create_bool_field_class(**kwargs
)
94 def _create_default_const_field_class(self
, *args
, **kwargs
):
95 tc
= get_default_trace_class()
96 fc
= tc
.create_bool_field_class(*args
, **kwargs
)
97 return _create_const_field_class(tc
, fc
, self
._const
_value
_setter
)
100 self
._fc
= self
._create
_default
_field
_class
()
101 self
._fc
_const
= self
._create
_default
_const
_field
_class
()
103 def test_create_default(self
):
104 self
.assertIsNotNone(self
._fc
)
105 self
.assertEqual(len(self
._fc
.user_attributes
), 0)
108 class BitArrayFieldClassTestCase(_TestFieldClass
, unittest
.TestCase
):
110 def _const_value_setter(field
):
113 def _create_field_class(self
, *args
, **kwargs
):
114 tc
= get_default_trace_class()
115 return tc
.create_bit_array_field_class(*args
, **kwargs
)
117 def _create_default_field_class(self
, **kwargs
):
118 return self
._create
_field
_class
(17, **kwargs
)
120 def _create_default_const_field_class(self
, *args
, **kwargs
):
121 tc
= get_default_trace_class()
122 fc
= tc
.create_bit_array_field_class(17, **kwargs
)
123 return _create_const_field_class(tc
, fc
, self
._const
_value
_setter
)
126 self
._fc
= self
._create
_default
_field
_class
()
128 def test_create_default(self
):
129 self
.assertIsNotNone(self
._fc
)
130 self
.assertEqual(len(self
._fc
.user_attributes
), 0)
132 def test_create_length_out_of_range(self
):
133 with self
.assertRaises(ValueError):
134 self
._create
_field
_class
(65)
136 def test_create_length_zero(self
):
137 with self
.assertRaises(ValueError):
138 self
._create
_field
_class
(0)
140 def test_create_length_invalid_type(self
):
141 with self
.assertRaises(TypeError):
142 self
._create
_field
_class
('lel')
144 def test_length_prop(self
):
145 self
.assertEqual(self
._fc
.length
, 17)
148 class _TestIntegerFieldClassProps
:
149 def test_create_default(self
):
150 fc
= self
._create
_default
_field
_class
()
151 self
.assertEqual(fc
.field_value_range
, 64)
152 self
.assertEqual(fc
.preferred_display_base
, bt2
.IntegerDisplayBase
.DECIMAL
)
153 self
.assertEqual(len(fc
.user_attributes
), 0)
155 def test_create_range(self
):
156 fc
= self
._create
_field
_class
(field_value_range
=35)
157 self
.assertEqual(fc
.field_value_range
, 35)
159 fc
= self
._create
_field
_class
(36)
160 self
.assertEqual(fc
.field_value_range
, 36)
162 def test_create_invalid_range(self
):
163 with self
.assertRaises(TypeError):
164 self
._create
_field
_class
('yes')
166 with self
.assertRaises(TypeError):
167 self
._create
_field
_class
(field_value_range
='yes')
169 with self
.assertRaises(ValueError):
170 self
._create
_field
_class
(field_value_range
=-2)
172 with self
.assertRaises(ValueError):
173 self
._create
_field
_class
(field_value_range
=0)
175 def test_create_base(self
):
176 fc
= self
._create
_field
_class
(
177 preferred_display_base
=bt2
.IntegerDisplayBase
.HEXADECIMAL
179 self
.assertEqual(fc
.preferred_display_base
, bt2
.IntegerDisplayBase
.HEXADECIMAL
)
181 def test_create_invalid_base_type(self
):
182 with self
.assertRaises(TypeError):
183 self
._create
_field
_class
(preferred_display_base
='yes')
185 def test_create_invalid_base_value(self
):
186 with self
.assertRaises(ValueError):
187 self
._create
_field
_class
(preferred_display_base
=444)
189 def test_create_full(self
):
190 fc
= self
._create
_field
_class
(
191 24, preferred_display_base
=bt2
.IntegerDisplayBase
.OCTAL
193 self
.assertEqual(fc
.field_value_range
, 24)
194 self
.assertEqual(fc
.preferred_display_base
, bt2
.IntegerDisplayBase
.OCTAL
)
197 class SignedIntegerFieldClassTestCase(
198 _TestIntegerFieldClassProps
, _TestFieldClass
, unittest
.TestCase
201 def _const_value_setter(field
):
204 def _create_field_class(self
, *args
, **kwargs
):
205 tc
= get_default_trace_class()
206 return tc
.create_signed_integer_field_class(*args
, **kwargs
)
208 def _create_default_const_field_class(self
, *args
, **kwargs
):
209 tc
= get_default_trace_class()
210 fc
= tc
.create_signed_integer_field_class(*args
, **kwargs
)
211 return _create_const_field_class(tc
, fc
, self
._const
_value
_setter
)
213 _create_default_field_class
= _create_field_class
216 class UnsignedIntegerFieldClassTestCase(
217 _TestIntegerFieldClassProps
, _TestFieldClass
, unittest
.TestCase
220 def _const_value_setter(field
):
223 def _create_field_class(self
, *args
, **kwargs
):
224 tc
= get_default_trace_class()
225 return tc
.create_unsigned_integer_field_class(*args
, **kwargs
)
227 def _create_default_const_field_class(self
, *args
, **kwargs
):
228 tc
= get_default_trace_class()
229 fc
= tc
.create_signed_integer_field_class(*args
, **kwargs
)
230 return _create_const_field_class(tc
, fc
, self
._const
_value
_setter
)
232 _create_default_field_class
= _create_field_class
235 class SingleRealFieldClassTestCase(_TestFieldClass
, unittest
.TestCase
):
237 def _const_value_setter(field
):
240 def _create_field_class(self
, *args
, **kwargs
):
241 tc
= get_default_trace_class()
242 return tc
.create_single_precision_real_field_class(*args
, **kwargs
)
244 def _create_default_const_field_class(self
, *args
, **kwargs
):
245 tc
= get_default_trace_class()
246 fc
= tc
.create_single_precision_real_field_class(*args
, **kwargs
)
247 return _create_const_field_class(tc
, fc
, self
._const
_value
_setter
)
249 _create_default_field_class
= _create_field_class
251 def test_create_default(self
):
252 fc
= self
._create
_field
_class
()
253 self
.assertEqual(len(fc
.user_attributes
), 0)
256 class DoubleRealFieldClassTestCase(_TestFieldClass
, unittest
.TestCase
):
258 def _const_value_setter(field
):
261 def _create_field_class(self
, *args
, **kwargs
):
262 tc
= get_default_trace_class()
263 return tc
.create_double_precision_real_field_class(*args
, **kwargs
)
265 def _create_default_const_field_class(self
, *args
, **kwargs
):
266 tc
= get_default_trace_class()
267 fc
= tc
.create_double_precision_real_field_class(*args
, **kwargs
)
268 return _create_const_field_class(tc
, fc
, self
._const
_value
_setter
)
270 _create_default_field_class
= _create_field_class
272 def test_create_default(self
):
273 fc
= self
._create
_field
_class
()
274 self
.assertEqual(len(fc
.user_attributes
), 0)
277 # Converts an _EnumerationFieldClassMapping to a list of ranges:
279 # [(lower0, upper0), (lower1, upper1), ...]
282 def enum_mapping_to_set(mapping
):
283 return {(x
.lower
, x
.upper
) for x
in mapping
.ranges
}
286 class _EnumerationFieldClassTestCase(_TestIntegerFieldClassProps
):
289 self
._fc
= self
._create
_default
_field
_class
()
290 self
._fc
_const
= self
._create
_default
_const
_field
_class
()
292 def test_create_from_invalid_type(self
):
293 with self
.assertRaises(TypeError):
294 self
._create
_field
_class
('coucou')
296 def test_add_mapping_simple(self
):
297 self
._fc
.add_mapping('hello', self
._ranges
1)
298 mapping
= self
._fc
['hello']
299 self
.assertEqual(mapping
.label
, 'hello')
300 self
.assertEqual(mapping
.ranges
, self
._ranges
1)
302 def test_const_add_mapping(self
):
303 with self
.assertRaises(AttributeError):
304 self
._fc
_const
.add_mapping('hello', self
._ranges
1)
306 def test_add_mapping_simple_kwargs(self
):
307 self
._fc
.add_mapping(label
='hello', ranges
=self
._ranges
1)
308 mapping
= self
._fc
['hello']
309 self
.assertEqual(mapping
.label
, 'hello')
310 self
.assertEqual(mapping
.ranges
, self
._ranges
1)
312 def test_add_mapping_invalid_name(self
):
313 with self
.assertRaises(TypeError):
314 self
._fc
.add_mapping(17, self
._ranges
1)
316 def test_add_mapping_invalid_range(self
):
317 with self
.assertRaises(TypeError):
318 self
._fc
.add_mapping('allo', 'meow')
320 def test_add_mapping_dup_label(self
):
321 with self
.assertRaises(ValueError):
322 self
._fc
.add_mapping('a', self
._ranges
1)
323 self
._fc
.add_mapping('a', self
._ranges
2)
325 def test_add_mapping_invalid_ranges_signedness(self
):
326 with self
.assertRaises(TypeError):
327 self
._fc
.add_mapping('allo', self
._inval
_ranges
)
330 self
._fc
.add_mapping('c', self
._ranges
1)
332 self
._fc
+= [('d', self
._ranges
2), ('e', self
._ranges
3)]
334 self
.assertEqual(len(self
._fc
), 3)
335 self
.assertEqual(self
._fc
['c'].label
, 'c')
336 self
.assertEqual(self
._fc
['c'].ranges
, self
._ranges
1)
337 self
.assertEqual(self
._fc
['d'].label
, 'd')
338 self
.assertEqual(self
._fc
['d'].ranges
, self
._ranges
2)
339 self
.assertEqual(self
._fc
['e'].label
, 'e')
340 self
.assertEqual(self
._fc
['e'].ranges
, self
._ranges
3)
342 def test_const_iadd(self
):
343 with self
.assertRaises(TypeError):
344 self
._fc
_const
+= [('d', self
._ranges
2), ('e', self
._ranges
3)]
346 def test_bool_op(self
):
347 self
.assertFalse(self
._fc
)
348 self
._fc
.add_mapping('a', self
._ranges
1)
349 self
.assertTrue(self
._fc
)
352 self
._fc
.add_mapping('a', self
._ranges
1)
353 self
._fc
.add_mapping('b', self
._ranges
2)
354 self
._fc
.add_mapping('c', self
._ranges
3)
355 self
.assertEqual(len(self
._fc
), 3)
357 def test_getitem(self
):
358 self
._fc
.add_mapping('a', self
._ranges
1)
359 self
._fc
.add_mapping('b', self
._ranges
2)
360 self
._fc
.add_mapping('c', self
._ranges
3)
361 mapping
= self
._fc
['a']
362 self
.assertEqual(mapping
.label
, 'a')
363 self
.assertEqual(mapping
.ranges
, self
._ranges
1)
364 self
.assertIs(type(mapping
), self
._MAPPING
_CLASS
)
365 self
.assertIs(type(mapping
.ranges
), self
._CONST
_RANGE
_SET
_CLASS
)
367 def test_getitem_nonexistent(self
):
368 with self
.assertRaises(KeyError):
369 self
._fc
['doesnotexist']
372 self
._fc
.add_mapping('a', self
._ranges
1)
373 self
._fc
.add_mapping('b', self
._ranges
2)
374 self
._fc
.add_mapping('c', self
._ranges
3)
376 # This exercises iteration.
377 labels
= sorted(self
._fc
)
379 self
.assertEqual(labels
, ['a', 'b', 'c'])
381 def test_find_by_value(self
):
382 self
._fc
.add_mapping('a', self
._ranges
1)
383 self
._fc
.add_mapping('b', self
._ranges
2)
384 self
._fc
.add_mapping('c', self
._ranges
3)
385 mappings
= self
._fc
.mappings_for_value(self
._value
_in
_range
_1_and
_3)
386 labels
= set([mapping
.label
for mapping
in mappings
])
387 expected_labels
= set(['a', 'c'])
388 self
.assertEqual(labels
, expected_labels
)
391 class UnsignedEnumerationFieldClassTestCase(
392 _EnumerationFieldClassTestCase
, _TestFieldClass
, unittest
.TestCase
394 _MAPPING_CLASS
= bt2_field_class
._UnsignedEnumerationFieldClassMappingConst
395 _RANGE_SET_CLASS
= bt2
.UnsignedIntegerRangeSet
396 _CONST_RANGE_SET_CLASS
= bt2
._UnsignedIntegerRangeSetConst
398 def _spec_set_up(self
):
399 self
._ranges
1 = bt2
.UnsignedIntegerRangeSet([(1, 4), (18, 47)])
400 self
._ranges
2 = bt2
.UnsignedIntegerRangeSet([(5, 5)])
401 self
._ranges
3 = bt2
.UnsignedIntegerRangeSet([(8, 22), (48, 99)])
402 self
._inval
_ranges
= bt2
.SignedIntegerRangeSet([(-8, -5), (48, 1928)])
403 self
._value
_in
_range
_1_and
_3 = 20
406 def _const_value_setter(field
):
409 def _create_field_class(self
, *args
, **kwargs
):
410 tc
= get_default_trace_class()
411 return tc
.create_unsigned_enumeration_field_class(*args
, **kwargs
)
413 def _create_default_const_field_class(self
, *args
, **kwargs
):
414 tc
= get_default_trace_class()
415 fc
= tc
.create_unsigned_enumeration_field_class(*args
, **kwargs
)
416 return _create_const_field_class(tc
, fc
, self
._const
_value
_setter
)
418 _create_default_field_class
= _create_field_class
421 class SignedEnumerationFieldClassTestCase(
422 _EnumerationFieldClassTestCase
, _TestFieldClass
, unittest
.TestCase
424 _MAPPING_CLASS
= bt2_field_class
._SignedEnumerationFieldClassMappingConst
425 _RANGE_SET_CLASS
= bt2
.SignedIntegerRangeSet
426 _CONST_RANGE_SET_CLASS
= bt2
._SignedIntegerRangeSetConst
428 def _spec_set_up(self
):
429 self
._ranges
1 = bt2
.SignedIntegerRangeSet([(-10, -4), (18, 47)])
430 self
._ranges
2 = bt2
.SignedIntegerRangeSet([(-3, -3)])
431 self
._ranges
3 = bt2
.SignedIntegerRangeSet([(-100, -1), (8, 16), (48, 99)])
432 self
._inval
_ranges
= bt2
.UnsignedIntegerRangeSet([(8, 16), (48, 99)])
433 self
._value
_in
_range
_1_and
_3 = -7
436 def _const_value_setter(field
):
439 def _create_field_class(self
, *args
, **kwargs
):
440 tc
= get_default_trace_class()
441 return tc
.create_signed_enumeration_field_class(*args
, **kwargs
)
443 def _create_default_const_field_class(self
, *args
, **kwargs
):
444 tc
= get_default_trace_class()
445 fc
= tc
.create_signed_enumeration_field_class(*args
, **kwargs
)
446 return _create_const_field_class(tc
, fc
, self
._const
_value
_setter
)
448 _create_default_field_class
= _create_field_class
451 class StringFieldClassTestCase(_TestFieldClass
, unittest
.TestCase
):
453 def _const_value_setter(field
):
454 field
.value
= 'chaine'
456 def _create_field_class(self
, *args
, **kwargs
):
457 tc
= get_default_trace_class()
458 return tc
.create_string_field_class(*args
, **kwargs
)
460 def _create_default_const_field_class(self
, *args
, **kwargs
):
461 tc
= get_default_trace_class()
462 fc
= tc
.create_string_field_class(*args
, **kwargs
)
463 return _create_const_field_class(tc
, fc
, self
._const
_value
_setter
)
465 _create_default_field_class
= _create_field_class
468 self
._fc
= self
._create
_default
_field
_class
()
470 def test_create_default(self
):
471 self
.assertIsNotNone(self
._fc
)
472 self
.assertEqual(len(self
._fc
.user_attributes
), 0)
475 class _TestElementContainer
:
477 self
._tc
= get_default_trace_class()
478 self
._fc
= self
._create
_default
_field
_class
()
479 self
._fc
_const
= self
._create
_default
_const
_field
_class
()
481 def test_create_default(self
):
482 self
.assertIsNotNone(self
._fc
)
483 self
.assertEqual(len(self
._fc
.user_attributes
), 0)
485 def test_append_element(self
):
486 int_field_class
= self
._tc
.create_signed_integer_field_class(32)
487 self
._append
_element
_method
(self
._fc
, 'int32', int_field_class
)
488 field_class
= self
._fc
['int32'].field_class
489 self
.assertEqual(field_class
.addr
, int_field_class
.addr
)
491 def test_append_element_kwargs(self
):
492 int_field_class
= self
._tc
.create_signed_integer_field_class(32)
493 self
._append
_element
_method
(self
._fc
, name
='int32', field_class
=int_field_class
)
494 field_class
= self
._fc
['int32'].field_class
495 self
.assertEqual(field_class
.addr
, int_field_class
.addr
)
497 def test_append_element_invalid_name(self
):
498 sub_fc
= self
._tc
.create_string_field_class()
500 with self
.assertRaises(TypeError):
501 self
._append
_element
_method
(self
._fc
, 23, sub_fc
)
503 def test_append_element_invalid_field_class(self
):
504 with self
.assertRaises(TypeError):
505 self
._append
_element
_method
(self
._fc
, 'yes', object())
507 def test_append_element_dup_name(self
):
508 sub_fc1
= self
._tc
.create_string_field_class()
509 sub_fc2
= self
._tc
.create_string_field_class()
511 with self
.assertRaises(ValueError):
512 self
._append
_element
_method
(self
._fc
, 'yes', sub_fc1
)
513 self
._append
_element
_method
(self
._fc
, 'yes', sub_fc2
)
515 def test_attr_field_class(self
):
516 int_field_class
= self
._tc
.create_signed_integer_field_class(32)
517 self
._append
_element
_method
(self
._fc
, 'int32', int_field_class
)
518 field_class
= self
._fc
['int32'].field_class
520 self
.assertIs(type(field_class
), bt2_field_class
._SignedIntegerFieldClass
)
522 def test_const_attr_field_class(self
):
523 int_field_class
= self
._tc
.create_signed_integer_field_class(32)
524 self
._append
_element
_method
(self
._fc
, 'int32', int_field_class
)
525 field_class
= self
._fc
['int32'].field_class
526 const_fc
= _create_const_field_class(
527 self
._tc
, self
._fc
, self
._const
_value
_setter
529 field_class
= const_fc
['int32'].field_class
531 self
.assertIs(type(field_class
), bt2_field_class
._SignedIntegerFieldClassConst
)
534 a_field_class
= self
._tc
.create_single_precision_real_field_class()
535 b_field_class
= self
._tc
.create_signed_integer_field_class(17)
536 self
._append
_element
_method
(self
._fc
, 'a_float', a_field_class
)
537 self
._append
_element
_method
(self
._fc
, 'b_int', b_field_class
)
538 c_field_class
= self
._tc
.create_string_field_class()
539 d_field_class
= self
._tc
.create_signed_enumeration_field_class(
542 e_field_class
= self
._tc
.create_structure_field_class()
544 ('c_string', c_field_class
),
545 ('d_enum', d_field_class
),
546 ('e_struct', e_field_class
),
548 self
.assertEqual(self
._fc
['a_float'].field_class
.addr
, a_field_class
.addr
)
549 self
.assertEqual(self
._fc
['a_float'].name
, 'a_float')
550 self
.assertEqual(self
._fc
['b_int'].field_class
.addr
, b_field_class
.addr
)
551 self
.assertEqual(self
._fc
['b_int'].name
, 'b_int')
552 self
.assertEqual(self
._fc
['c_string'].field_class
.addr
, c_field_class
.addr
)
553 self
.assertEqual(self
._fc
['c_string'].name
, 'c_string')
554 self
.assertEqual(self
._fc
['d_enum'].field_class
.addr
, d_field_class
.addr
)
555 self
.assertEqual(self
._fc
['d_enum'].name
, 'd_enum')
556 self
.assertEqual(self
._fc
['e_struct'].field_class
.addr
, e_field_class
.addr
)
557 self
.assertEqual(self
._fc
['e_struct'].name
, 'e_struct')
559 def test_const_iadd(self
):
560 a_field_class
= self
._tc
.create_single_precision_real_field_class()
561 with self
.assertRaises(TypeError):
562 self
._fc
_const
+= a_field_class
564 def test_bool_op(self
):
565 self
.assertFalse(self
._fc
)
566 self
._append
_element
_method
(self
._fc
, 'a', self
._tc
.create_string_field_class())
567 self
.assertTrue(self
._fc
)
570 self
._append
_element
_method
(self
._fc
, 'a', self
._tc
.create_string_field_class())
571 self
._append
_element
_method
(self
._fc
, 'b', self
._tc
.create_string_field_class())
572 self
._append
_element
_method
(self
._fc
, 'c', self
._tc
.create_string_field_class())
573 self
.assertEqual(len(self
._fc
), 3)
575 def test_getitem(self
):
576 a_fc
= self
._tc
.create_signed_integer_field_class(32)
577 b_fc
= self
._tc
.create_string_field_class()
578 c_fc
= self
._tc
.create_single_precision_real_field_class()
579 self
._append
_element
_method
(self
._fc
, 'a', a_fc
)
580 self
._append
_element
_method
(self
._fc
, 'b', b_fc
)
581 self
._append
_element
_method
(self
._fc
, 'c', c_fc
)
582 self
.assertEqual(self
._fc
['b'].field_class
.addr
, b_fc
.addr
)
583 self
.assertEqual(self
._fc
['b'].name
, 'b')
585 def test_getitem_invalid_key_type(self
):
586 with self
.assertRaises(TypeError):
589 def test_getitem_invalid_key(self
):
590 with self
.assertRaises(KeyError):
593 def test_contains(self
):
594 self
.assertFalse('a' in self
._fc
)
595 self
._append
_element
_method
(self
._fc
, 'a', self
._tc
.create_string_field_class())
596 self
.assertTrue('a' in self
._fc
)
599 a_fc
= self
._tc
.create_signed_integer_field_class(32)
600 b_fc
= self
._tc
.create_string_field_class()
601 c_fc
= self
._tc
.create_single_precision_real_field_class()
602 elements
= (('a', a_fc
), ('b', b_fc
), ('c', c_fc
))
604 for elem
in elements
:
605 self
._append
_element
_method
(self
._fc
, *elem
)
607 for (name
, element
), test_elem
in zip(self
._fc
.items(), elements
):
608 self
.assertEqual(element
.name
, test_elem
[0])
609 self
.assertEqual(name
, element
.name
)
610 self
.assertEqual(element
.field_class
.addr
, test_elem
[1].addr
)
611 self
.assertEqual(len(element
.user_attributes
), 0)
613 def test_at_index(self
):
614 a_fc
= self
._tc
.create_signed_integer_field_class(32)
615 b_fc
= self
._tc
.create_string_field_class()
616 c_fc
= self
._tc
.create_single_precision_real_field_class()
617 self
._append
_element
_method
(self
._fc
, 'c', c_fc
)
618 self
._append
_element
_method
(self
._fc
, 'a', a_fc
)
619 self
._append
_element
_method
(self
._fc
, 'b', b_fc
)
620 elem
= self
._at
_index
_method
(self
._fc
, 1)
621 self
.assertEqual(elem
.field_class
.addr
, a_fc
.addr
)
622 self
.assertEqual(elem
.name
, 'a')
624 def test_at_index_invalid(self
):
625 self
._append
_element
_method
(
626 self
._fc
, 'c', self
._tc
.create_signed_integer_field_class(32)
629 with self
.assertRaises(TypeError):
630 self
._at
_index
_method
(self
._fc
, 'yes')
632 def test_at_index_out_of_bounds_after(self
):
633 self
._append
_element
_method
(
634 self
._fc
, 'c', self
._tc
.create_signed_integer_field_class(32)
637 with self
.assertRaises(IndexError):
638 self
._at
_index
_method
(self
._fc
, len(self
._fc
))
640 def test_user_attributes(self
):
641 self
._append
_element
_method
(
644 self
._tc
.create_string_field_class(),
645 user_attributes
={'salut': 23},
647 self
.assertEqual(self
._fc
['c'].user_attributes
, {'salut': 23})
648 self
.assertIs(type(self
._fc
.user_attributes
), bt2_value
.MapValue
)
649 self
.assertIs(type(self
._fc
['c'].user_attributes
), bt2_value
.MapValue
)
651 def test_invalid_user_attributes(self
):
652 with self
.assertRaises(TypeError):
653 self
._append
_element
_method
(
656 self
._tc
.create_string_field_class(),
657 user_attributes
=object(),
660 def test_invalid_user_attributes_value_type(self
):
661 with self
.assertRaises(TypeError):
662 self
._append
_element
_method
(
663 self
._fc
, 'c', self
._tc
.create_string_field_class(), user_attributes
=23
667 class StructureFieldClassTestCase(
668 _TestFieldClass
, _TestElementContainer
, unittest
.TestCase
670 _append_element_method
= staticmethod(bt2
._StructureFieldClass
.append_member
)
671 _at_index_method
= staticmethod(bt2
._StructureFieldClass
.member_at_index
)
674 def _const_value_setter(field
):
677 def _create_field_class(self
, *args
, **kwargs
):
678 tc
= get_default_trace_class()
679 return tc
.create_structure_field_class(*args
, **kwargs
)
681 def _create_default_const_field_class(self
, *args
, **kwargs
):
682 tc
= get_default_trace_class()
683 fc
= tc
.create_structure_field_class(*args
, **kwargs
)
684 return _create_const_field_class(tc
, fc
, self
._const
_value
_setter
)
686 _create_default_field_class
= _create_field_class
688 def test_const_member_field_class(self
):
689 def _real_value_setter(field
):
690 field
.value
= {'real': 0}
692 tc
= get_default_trace_class()
693 fc
= tc
.create_structure_field_class()
694 member_fc
= self
._tc
.create_single_precision_real_field_class()
695 fc
.append_member('real', member_fc
)
696 const_fc
= _create_const_field_class(tc
, fc
, _real_value_setter
)
699 type(const_fc
['real'].field_class
),
700 bt2_field_class
._SinglePrecisionRealFieldClassConst
,
703 def test_member_field_class(self
):
704 tc
= get_default_trace_class()
705 fc
= tc
.create_structure_field_class()
706 member_fc
= self
._tc
.create_single_precision_real_field_class()
707 fc
.append_member('real', member_fc
)
710 type(fc
['real'].field_class
), bt2_field_class
._SinglePrecisionRealFieldClass
714 class OptionWithoutSelectorFieldClassTestCase(_TestFieldClass
, unittest
.TestCase
):
716 def _const_value_setter(field
):
717 field
.has_field
= True
720 def _create_default_field_class(self
, *args
, **kwargs
):
721 return self
._tc
.create_option_without_selector_field_class(
722 self
._content
_fc
, *args
, **kwargs
725 def _create_default_const_field_class(self
, *args
, **kwargs
):
726 fc
= self
._tc
.create_option_without_selector_field_class(
727 self
._content
_fc
, *args
, **kwargs
729 return _create_const_field_class(self
._tc
, fc
, self
._const
_value
_setter
)
732 self
._tc
= get_default_trace_class()
733 self
._content
_fc
= self
._tc
.create_signed_integer_field_class(23)
735 def test_create_default(self
):
736 fc
= self
._create
_default
_field
_class
()
737 self
.assertEqual(fc
.field_class
.addr
, self
._content
_fc
.addr
)
738 self
.assertEqual(len(fc
.user_attributes
), 0)
740 def test_create_invalid_field_class(self
):
741 with self
.assertRaises(TypeError):
742 self
._tc
.create_option_without_selector_field_class(object())
744 def test_attr_field_class(self
):
745 fc
= self
._create
_default
_field
_class
()
746 self
.assertIs(type(fc
.field_class
), bt2_field_class
._SignedIntegerFieldClass
)
748 def test_const_attr_field_class(self
):
749 fc
= self
._create
_default
_const
_field
_class
()
751 type(fc
.field_class
), bt2_field_class
._SignedIntegerFieldClassConst
755 class _OptionWithSelectorFieldClassTestCase(_TestFieldClass
):
757 def _const_value_setter(field
):
758 field
['opt'].has_field
= True
759 field
['opt'].value
= 12
761 def _create_default_const_field_class(self
, *args
, **kwargs
):
762 # Create a struct to contain the option and its selector else we can't
763 # create the non-const field necessary to get the the const field_class
764 struct_fc
= self
._tc
.create_structure_field_class()
765 struct_fc
.append_member('selecteux', self
._tag
_fc
)
766 opt_fc
= self
._create
_default
_field
_class
(*args
, **kwargs
)
767 struct_fc
.append_member('opt', opt_fc
)
769 return _create_const_field_class(self
._tc
, struct_fc
, self
._const
_value
_setter
)[
774 self
._tc
= get_default_trace_class()
775 self
._content
_fc
= self
._tc
.create_signed_integer_field_class(23)
776 self
._tag
_fc
= self
._create
_tag
_fc
()
778 def _create_field_class_for_field_path_test(self
):
779 fc
= self
._create
_default
_field
_class
()
781 foo_fc
= self
._tc
.create_single_precision_real_field_class()
782 bar_fc
= self
._tc
.create_string_field_class()
783 baz_fc
= self
._tc
.create_string_field_class()
785 inner_struct_fc
= self
._tc
.create_structure_field_class()
786 inner_struct_fc
.append_member('bar', bar_fc
)
787 inner_struct_fc
.append_member('baz', baz_fc
)
788 inner_struct_fc
.append_member('tag', self
._tag
_fc
)
789 inner_struct_fc
.append_member('opt', fc
)
791 opt_struct_array_fc
= self
._tc
.create_option_without_selector_field_class(
795 outer_struct_fc
= self
._tc
.create_structure_field_class()
796 outer_struct_fc
.append_member('foo', foo_fc
)
797 outer_struct_fc
.append_member('inner_opt', opt_struct_array_fc
)
799 # The path to the selector field class is resolved when the
800 # option field class is actually used, for example in a packet
802 self
._tc
.create_stream_class(
803 packet_context_field_class
=outer_struct_fc
, supports_packets
=True
808 def test_field_path_len(self
):
809 fc
= self
._create
_field
_class
_for
_field
_path
_test
()
810 self
.assertEqual(len(fc
.selector_field_path
), 3)
812 def test_field_path_iter(self
):
813 fc
= self
._create
_field
_class
_for
_field
_path
_test
()
814 path_items
= list(fc
.selector_field_path
)
816 self
.assertEqual(len(path_items
), 3)
818 self
.assertIsInstance(path_items
[0], bt2
._IndexFieldPathItem
)
819 self
.assertEqual(path_items
[0].index
, 1)
821 self
.assertIsInstance(path_items
[1], bt2
._CurrentOptionContentFieldPathItem
)
823 self
.assertIsInstance(path_items
[2], bt2
._IndexFieldPathItem
)
824 self
.assertEqual(path_items
[2].index
, 2)
826 def test_field_path_root_scope(self
):
827 fc
= self
._create
_field
_class
_for
_field
_path
_test
()
829 fc
.selector_field_path
.root_scope
, bt2
.FieldPathScope
.PACKET_CONTEXT
833 class OptionWithBoolSelectorFieldClassTestCase(
834 _OptionWithSelectorFieldClassTestCase
, unittest
.TestCase
836 def _create_default_field_class(self
, *args
, **kwargs
):
837 return self
._tc
.create_option_with_bool_selector_field_class(
838 self
._content
_fc
, self
._tag
_fc
, *args
, **kwargs
841 def _create_tag_fc(self
):
842 return self
._tc
.create_bool_field_class()
844 def test_create_default(self
):
845 fc
= self
._create
_default
_field
_class
()
846 self
.assertEqual(fc
.field_class
.addr
, self
._content
_fc
.addr
)
847 self
.assertFalse(fc
.selector_is_reversed
)
848 self
.assertEqual(len(fc
.user_attributes
), 0)
850 def test_create_selector_is_reversed_wrong_type(self
):
851 with self
.assertRaises(TypeError):
852 self
._create
_default
_field
_class
(selector_is_reversed
=23)
854 def test_create_invalid_selector_type(self
):
855 with self
.assertRaises(TypeError):
856 self
._tc
.create_option_with_bool_selector_field_class(self
._content
_fc
, 17)
858 def test_attr_selector_is_reversed(self
):
859 fc
= self
._create
_default
_field
_class
(selector_is_reversed
=True)
860 self
.assertTrue(fc
.selector_is_reversed
)
862 def test_const_attr_selector_is_reversed(self
):
863 fc
= self
._create
_default
_const
_field
_class
(selector_is_reversed
=True)
864 self
.assertTrue(fc
.selector_is_reversed
)
867 class _OptionWithIntegerSelectorFieldClassTestCase(
868 _OptionWithSelectorFieldClassTestCase
870 def _create_default_field_class(self
, *args
, **kwargs
):
871 return self
._tc
.create_option_with_integer_selector_field_class(
872 self
._content
_fc
, self
._tag
_fc
, self
._ranges
, *args
, **kwargs
875 def test_create_default(self
):
876 fc
= self
._create
_default
_field
_class
()
877 self
.assertEqual(fc
.field_class
.addr
, self
._content
_fc
.addr
)
878 self
.assertEqual(fc
.ranges
, self
._ranges
)
879 self
.assertEqual(len(fc
.user_attributes
), 0)
881 def test_create_ranges_wrong_type(self
):
882 with self
.assertRaises(TypeError):
883 self
._tc
.create_option_with_integer_selector_field_class(
884 self
._content
_fc
, self
._tag
_fc
, 23
887 def test_create_ranges_empty(self
):
888 with self
.assertRaises(ValueError):
889 self
._tc
.create_option_with_integer_selector_field_class(
890 self
._content
_fc
, self
._tag
_fc
, type(self
._ranges
)()
893 def test_create_invalid_selector_type(self
):
894 with self
.assertRaises(TypeError):
895 self
._tc
.create_option_with_bool_selector_field_class(self
._content
_fc
, 17)
897 def test_attr_ranges(self
):
898 fc
= self
._create
_default
_field
_class
()
899 print(type(fc
.ranges
), type(self
._ranges
))
900 self
.assertEqual(fc
.ranges
, self
._ranges
)
902 def test_const_attr_ranges(self
):
903 fc
= self
._create
_default
_const
_field
_class
()
904 self
.assertEqual(fc
.ranges
, self
._ranges
)
907 class OptionWithUnsignedIntegerSelectorFieldClassTestCase(
908 _OptionWithIntegerSelectorFieldClassTestCase
, unittest
.TestCase
911 self
._ranges
= bt2
.UnsignedIntegerRangeSet([(1, 3), (18, 44)])
914 def _create_tag_fc(self
):
915 return self
._tc
.create_unsigned_integer_field_class()
918 class VariantFieldClassWithoutSelectorTestCase(
919 _TestFieldClass
, _TestElementContainer
, unittest
.TestCase
921 _append_element_method
= staticmethod(
922 bt2
._VariantFieldClassWithoutSelector
.append_option
924 _at_index_method
= staticmethod(
925 bt2
._VariantFieldClassWithoutSelector
.option_at_index
929 def _const_value_setter(variant_field
):
930 variant_field
.selected_option_index
= 0
931 variant_field
.value
= 12
933 def _create_field_class(self
, *args
, **kwargs
):
934 tc
= get_default_trace_class()
935 return tc
.create_variant_field_class(*args
, **kwargs
)
937 def _create_default_const_field_class(self
, *args
, **kwargs
):
938 tc
= get_default_trace_class()
939 fc
= tc
.create_variant_field_class(*args
, **kwargs
)
940 int_field_class
= self
._tc
.create_signed_integer_field_class(32)
941 fc
.append_option('int32', int_field_class
)
943 return _create_const_field_class(tc
, fc
, self
._const
_value
_setter
)
945 _create_default_field_class
= _create_field_class
948 class _VariantFieldClassWithIntegerSelectorTestCase
:
950 def _const_value_setter(field
):
951 field
['variant'].selected_option_index
= 0
952 field
['variant'] = 12
954 def _create_default_field_class(self
, *args
, **kwargs
):
955 return self
._tc
.create_variant_field_class(
956 *args
, selector_fc
=self
._selector
_fc
, **kwargs
959 def _create_default_const_field_class(self
, *args
, **kwargs
):
960 # Create a struct to contain the variant and its selector else we can't
961 # create the non-const field necessary to get the the const field_class
962 struct_fc
= self
._tc
.create_structure_field_class()
963 struct_fc
.append_member('selecteux', self
._selector
_fc
)
964 variant_fc
= self
._tc
.create_variant_field_class(
965 *args
, selector_fc
=self
._selector
_fc
967 variant_fc
.append_option(
968 'a', self
._tc
.create_signed_integer_field_class(32), self
._ranges
1
970 struct_fc
.append_member('variant', variant_fc
, **kwargs
)
972 return _create_const_field_class(self
._tc
, struct_fc
, self
._const
_value
_setter
)[
977 self
._tc
= get_default_trace_class()
979 self
._fc
= self
._create
_default
_field
_class
()
981 def test_create_default(self
):
982 self
.assertIsNotNone(self
._fc
)
983 self
.assertEqual(len(self
._fc
.user_attributes
), 0)
985 def test_append_element(self
):
986 str_field_class
= self
._tc
.create_string_field_class()
987 self
._fc
.append_option('str', str_field_class
, self
._ranges
1)
988 opt
= self
._fc
['str']
989 self
.assertEqual(opt
.field_class
.addr
, str_field_class
.addr
)
990 self
.assertEqual(opt
.name
, 'str')
991 self
.assertEqual(opt
.ranges
.addr
, self
._ranges
1.addr
)
993 def test_const_append(self
):
994 fc_const
= self
._create
_default
_const
_field
_class
()
995 str_field_class
= self
._tc
.create_string_field_class()
996 with self
.assertRaises(AttributeError):
997 fc_const
.append_option('str', str_field_class
, self
._ranges
1)
999 def test_append_element_kwargs(self
):
1000 int_field_class
= self
._tc
.create_signed_integer_field_class(32)
1001 self
._fc
.append_option(
1002 name
='int32', field_class
=int_field_class
, ranges
=self
._ranges
1
1004 opt
= self
._fc
['int32']
1005 self
.assertEqual(opt
.field_class
.addr
, int_field_class
.addr
)
1006 self
.assertEqual(opt
.name
, 'int32')
1007 self
.assertEqual(opt
.ranges
.addr
, self
._ranges
1.addr
)
1009 def test_append_element_invalid_name(self
):
1010 sub_fc
= self
._tc
.create_string_field_class()
1012 with self
.assertRaises(TypeError):
1013 self
._fc
.append_option(self
._fc
, 23, sub_fc
)
1015 def test_append_element_invalid_field_class(self
):
1016 with self
.assertRaises(TypeError):
1017 self
._fc
.append_option(self
._fc
, 'yes', object())
1019 def test_append_element_invalid_ranges(self
):
1020 sub_fc
= self
._tc
.create_string_field_class()
1022 with self
.assertRaises(TypeError):
1023 self
._fc
.append_option(self
._fc
, sub_fc
, 'lel')
1025 def test_append_element_dup_name(self
):
1026 sub_fc1
= self
._tc
.create_string_field_class()
1027 sub_fc2
= self
._tc
.create_string_field_class()
1029 with self
.assertRaises(ValueError):
1030 self
._fc
.append_option('yes', sub_fc1
, self
._ranges
1)
1031 self
._fc
.append_option('yes', sub_fc2
, self
._ranges
2)
1033 def test_append_element_invalid_ranges_signedness(self
):
1034 sub_fc
= self
._tc
.create_string_field_class()
1036 with self
.assertRaises(TypeError):
1037 self
._fc
.append_option(self
._fc
, sub_fc
, self
._inval
_ranges
)
1039 def test_user_attributes(self
):
1040 self
._fc
.append_option(
1042 self
._tc
.create_string_field_class(),
1044 user_attributes
={'salut': 23},
1046 self
.assertEqual(self
._fc
['c'].user_attributes
, {'salut': 23})
1047 self
.assertIs(type(self
._fc
.user_attributes
), bt2_value
.MapValue
)
1049 def test_const_user_attributes(self
):
1050 fc_const
= self
._create
_default
_const
_field
_class
()
1051 self
.assertIs(type(fc_const
.user_attributes
), bt2_value
._MapValueConst
)
1053 def test_invalid_user_attributes(self
):
1054 with self
.assertRaises(TypeError):
1055 self
._fc
.append_option(
1057 self
._tc
.create_string_field_class(),
1059 user_attributes
=object(),
1062 def test_invalid_user_attributes_value_type(self
):
1063 with self
.assertRaises(TypeError):
1064 self
._fc
.append_option(
1066 self
._tc
.create_string_field_class(),
1071 def test_iadd(self
):
1072 a_field_class
= self
._tc
.create_single_precision_real_field_class()
1073 self
._fc
.append_option('a_float', a_field_class
, self
._ranges
1)
1074 c_field_class
= self
._tc
.create_string_field_class()
1075 d_field_class
= self
._tc
.create_signed_enumeration_field_class(
1076 field_value_range
=32
1079 ('c_string', c_field_class
, self
._ranges
2),
1080 ('d_enum', d_field_class
, self
._ranges
3),
1082 self
.assertEqual(self
._fc
['a_float'].field_class
.addr
, a_field_class
.addr
)
1083 self
.assertEqual(self
._fc
['a_float'].name
, 'a_float')
1084 self
.assertEqual(self
._fc
['a_float'].ranges
, self
._ranges
1)
1085 self
.assertEqual(self
._fc
['c_string'].field_class
.addr
, c_field_class
.addr
)
1086 self
.assertEqual(self
._fc
['c_string'].name
, 'c_string')
1087 self
.assertEqual(self
._fc
['c_string'].ranges
, self
._ranges
2)
1088 self
.assertEqual(self
._fc
['d_enum'].field_class
.addr
, d_field_class
.addr
)
1089 self
.assertEqual(self
._fc
['d_enum'].name
, 'd_enum')
1090 self
.assertEqual(self
._fc
['d_enum'].ranges
, self
._ranges
3)
1092 def test_const_iadd(self
):
1093 fc_const
= self
._create
_default
_const
_field
_class
()
1094 a_field_class
= self
._tc
.create_single_precision_real_field_class()
1095 with self
.assertRaises(TypeError):
1096 fc_const
+= [('a_float', a_field_class
, self
._ranges
1)]
1098 def test_bool_op(self
):
1099 self
.assertFalse(self
._fc
)
1100 self
._fc
.append_option('a', self
._tc
.create_string_field_class(), self
._ranges
1)
1101 self
.assertTrue(self
._fc
)
1104 self
._fc
.append_option('a', self
._tc
.create_string_field_class(), self
._ranges
1)
1105 self
._fc
.append_option('b', self
._tc
.create_string_field_class(), self
._ranges
2)
1106 self
._fc
.append_option('c', self
._tc
.create_string_field_class(), self
._ranges
3)
1107 self
.assertEqual(len(self
._fc
), 3)
1109 def test_getitem(self
):
1110 a_fc
= self
._tc
.create_signed_integer_field_class(32)
1111 b_fc
= self
._tc
.create_string_field_class()
1112 c_fc
= self
._tc
.create_single_precision_real_field_class()
1113 self
._fc
.append_option('a', a_fc
, self
._ranges
1)
1114 self
._fc
.append_option('b', b_fc
, self
._ranges
2)
1115 self
._fc
.append_option('c', c_fc
, self
._ranges
3)
1116 self
.assertEqual(self
._fc
['b'].field_class
.addr
, b_fc
.addr
)
1117 self
.assertEqual(self
._fc
['b'].name
, 'b')
1118 self
.assertEqual(self
._fc
['b'].ranges
.addr
, self
._ranges
2.addr
)
1120 def test_option_field_class(self
):
1121 a_fc
= self
._tc
.create_signed_integer_field_class(32)
1122 self
._fc
.append_option('a', a_fc
, self
._ranges
1)
1124 type(self
._fc
['a'].field_class
), bt2_field_class
._SignedIntegerFieldClass
1127 def test_const_option_field_class(self
):
1128 fc_const
= self
._create
_default
_const
_field
_class
()
1130 type(fc_const
['a'].field_class
),
1131 bt2_field_class
._SignedIntegerFieldClassConst
,
1134 def test_getitem_invalid_key_type(self
):
1135 with self
.assertRaises(TypeError):
1138 def test_getitem_invalid_key(self
):
1139 with self
.assertRaises(KeyError):
1142 def test_contains(self
):
1143 self
.assertFalse('a' in self
._fc
)
1144 self
._fc
.append_option('a', self
._tc
.create_string_field_class(), self
._ranges
1)
1145 self
.assertTrue('a' in self
._fc
)
1147 def test_iter(self
):
1148 a_fc
= self
._tc
.create_signed_integer_field_class(32)
1149 b_fc
= self
._tc
.create_string_field_class()
1150 c_fc
= self
._tc
.create_single_precision_real_field_class()
1152 ('a', a_fc
, self
._ranges
1),
1153 ('b', b_fc
, self
._ranges
2),
1154 ('c', c_fc
, self
._ranges
3),
1158 self
._fc
.append_option(*opt
)
1160 for (name
, opt
), test_opt
in zip(self
._fc
.items(), opts
):
1161 self
.assertEqual(opt
.name
, test_opt
[0])
1162 self
.assertEqual(name
, opt
.name
)
1163 self
.assertEqual(opt
.field_class
.addr
, test_opt
[1].addr
)
1164 self
.assertEqual(opt
.ranges
.addr
, test_opt
[2].addr
)
1166 def test_at_index(self
):
1167 a_fc
= self
._tc
.create_signed_integer_field_class(32)
1168 b_fc
= self
._tc
.create_string_field_class()
1169 c_fc
= self
._tc
.create_single_precision_real_field_class()
1170 self
._fc
.append_option('c', c_fc
, self
._ranges
1)
1171 self
._fc
.append_option('a', a_fc
, self
._ranges
2)
1172 self
._fc
.append_option('b', b_fc
, self
._ranges
3)
1173 self
.assertEqual(self
._fc
.option_at_index(1).field_class
.addr
, a_fc
.addr
)
1174 self
.assertEqual(self
._fc
.option_at_index(1).name
, 'a')
1175 self
.assertEqual(self
._fc
.option_at_index(1).ranges
.addr
, self
._ranges
2.addr
)
1177 def test_at_index_invalid(self
):
1178 self
._fc
.append_option(
1179 'c', self
._tc
.create_signed_integer_field_class(32), self
._ranges
3
1182 with self
.assertRaises(TypeError):
1183 self
._fc
.option_at_index('yes')
1185 def test_at_index_out_of_bounds_after(self
):
1186 self
._fc
.append_option(
1187 'c', self
._tc
.create_signed_integer_field_class(32), self
._ranges
3
1190 with self
.assertRaises(IndexError):
1191 self
._fc
.option_at_index(len(self
._fc
))
1193 def _fill_default_fc_for_field_path_test(self
):
1194 # Create something equivalent to:
1196 # struct outer_struct_fc {
1198 # struct inner_struct_fc {
1199 # [u]int64_t selector;
1202 # variant <selector> {
1203 # real a; // selected with self._ranges1
1204 # int21_t b; // selected with self._ranges2
1205 # uint34_t c; // selected with self._ranges3
1207 # } inner_struct[2];
1209 self
._fc
.append_option(
1210 'a', self
._tc
.create_single_precision_real_field_class(), self
._ranges
1
1212 self
._fc
.append_option(
1213 'b', self
._tc
.create_signed_integer_field_class(21), self
._ranges
2
1215 self
._fc
.append_option(
1216 'c', self
._tc
.create_unsigned_integer_field_class(34), self
._ranges
3
1219 foo_fc
= self
._tc
.create_single_precision_real_field_class()
1220 bar_fc
= self
._tc
.create_string_field_class()
1221 baz_fc
= self
._tc
.create_string_field_class()
1223 inner_struct_fc
= self
._tc
.create_structure_field_class()
1224 inner_struct_fc
.append_member('selector', self
._selector
_fc
)
1225 inner_struct_fc
.append_member('bar', bar_fc
)
1226 inner_struct_fc
.append_member('baz', baz_fc
)
1227 inner_struct_fc
.append_member('variant', self
._fc
)
1229 inner_struct_array_fc
= self
._tc
.create_static_array_field_class(
1233 outer_struct_fc
= self
._tc
.create_structure_field_class()
1234 outer_struct_fc
.append_member('foo', foo_fc
)
1235 outer_struct_fc
.append_member('inner_struct', inner_struct_array_fc
)
1237 # The path to the selector field is resolved when the sequence is
1238 # actually used, for example in a packet context.
1239 self
._tc
.create_stream_class(
1240 supports_packets
=True, packet_context_field_class
=outer_struct_fc
1243 def test_selector_field_path_length(self
):
1244 self
._fill
_default
_fc
_for
_field
_path
_test
()
1245 self
.assertEqual(len(self
._fc
.selector_field_path
), 3)
1247 def test_selector_field_path_iter(self
):
1248 self
._fill
_default
_fc
_for
_field
_path
_test
()
1249 path_items
= list(self
._fc
.selector_field_path
)
1251 self
.assertEqual(len(path_items
), 3)
1253 self
.assertIsInstance(path_items
[0], bt2
._IndexFieldPathItem
)
1254 self
.assertEqual(path_items
[0].index
, 1)
1256 self
.assertIsInstance(path_items
[1], bt2
._CurrentArrayElementFieldPathItem
)
1258 self
.assertIsInstance(path_items
[2], bt2
._IndexFieldPathItem
)
1259 self
.assertEqual(path_items
[2].index
, 0)
1261 def test_selector_field_path_root_scope(self
):
1262 self
._fill
_default
_fc
_for
_field
_path
_test
()
1264 self
._fc
.selector_field_path
.root_scope
, bt2
.FieldPathScope
.PACKET_CONTEXT
1268 class VariantFieldClassWithUnsignedSelectorTestCase(
1269 _VariantFieldClassWithIntegerSelectorTestCase
, unittest
.TestCase
1271 def _spec_set_up(self
):
1272 self
._ranges
1 = bt2
.UnsignedIntegerRangeSet([(1, 4), (18, 47)])
1273 self
._ranges
2 = bt2
.UnsignedIntegerRangeSet([(5, 5)])
1274 self
._ranges
3 = bt2
.UnsignedIntegerRangeSet([(8, 16), (48, 99)])
1275 self
._inval
_ranges
= bt2
.SignedIntegerRangeSet([(-8, 16), (48, 99)])
1276 self
._selector
_fc
= self
._tc
.create_unsigned_integer_field_class()
1279 class VariantFieldClassWithSignedSelectorTestCase(
1280 _VariantFieldClassWithIntegerSelectorTestCase
, unittest
.TestCase
1282 def _spec_set_up(self
):
1283 self
._ranges
1 = bt2
.SignedIntegerRangeSet([(-10, -4), (18, 47)])
1284 self
._ranges
2 = bt2
.SignedIntegerRangeSet([(-3, -3)])
1285 self
._ranges
3 = bt2
.SignedIntegerRangeSet([(8, 16), (48, 99)])
1286 self
._inval
_ranges
= bt2
.UnsignedIntegerRangeSet([(8, 16), (48, 99)])
1287 self
._selector
_fc
= self
._tc
.create_signed_integer_field_class()
1290 class _ArrayFieldClassTestCase
:
1291 def test_attr_element_field_class(self
):
1292 fc
= self
._create
_array
()
1294 type(fc
.element_field_class
), bt2_field_class
._SignedIntegerFieldClass
1298 class _ArrayFieldClassConstTestCase
:
1299 def test_const_attr_element_field_class(self
):
1300 fc
= self
._create
_const
_array
()
1302 type(fc
.element_field_class
), bt2_field_class
._SignedIntegerFieldClassConst
1306 class StaticArrayFieldClassTestCase(
1307 _ArrayFieldClassTestCase
, _ArrayFieldClassConstTestCase
, unittest
.TestCase
1310 def _const_value_setter(field
):
1311 field
.value
= [9] * 45
1313 def _create_array(self
):
1314 return self
._tc
.create_static_array_field_class(self
._elem
_fc
, 45)
1316 def _create_const_array(self
):
1317 fc
= self
._tc
.create_static_array_field_class(self
._elem
_fc
, 45)
1318 return _create_const_field_class(self
._tc
, fc
, self
._const
_value
_setter
)
1321 self
._tc
= get_default_trace_class()
1322 self
._elem
_fc
= self
._tc
.create_signed_integer_field_class(23)
1324 def test_create_default(self
):
1325 fc
= self
._tc
.create_static_array_field_class(self
._elem
_fc
, 45)
1326 self
.assertEqual(fc
.element_field_class
.addr
, self
._elem
_fc
.addr
)
1327 self
.assertEqual(fc
.length
, 45)
1328 self
.assertEqual(len(fc
.user_attributes
), 0)
1330 def test_create_invalid_elem_field_class(self
):
1331 with self
.assertRaises(TypeError):
1332 self
._tc
.create_static_array_field_class(object(), 45)
1334 def test_create_invalid_length(self
):
1335 with self
.assertRaises(ValueError):
1336 self
._tc
.create_static_array_field_class(
1337 self
._tc
.create_string_field_class(), -17
1340 def test_create_invalid_length_type(self
):
1341 with self
.assertRaises(TypeError):
1342 self
._tc
.create_static_array_field_class(
1343 self
._tc
.create_string_field_class(), 'the length'
1347 class DynamicArrayFieldClassTestCase(
1348 _ArrayFieldClassTestCase
, _ArrayFieldClassConstTestCase
, unittest
.TestCase
1351 def _const_value_setter(field
):
1354 def _create_array(self
):
1355 return self
._tc
.create_dynamic_array_field_class(self
._elem
_fc
)
1357 def _create_const_array(self
):
1358 fc
= self
._tc
.create_dynamic_array_field_class(self
._elem
_fc
)
1359 return _create_const_field_class(self
._tc
, fc
, self
._const
_value
_setter
)
1362 self
._tc
= get_default_trace_class()
1363 self
._elem
_fc
= self
._tc
.create_signed_integer_field_class(23)
1365 def test_create_default(self
):
1366 fc
= self
._tc
.create_dynamic_array_field_class(self
._elem
_fc
)
1367 self
.assertEqual(fc
.element_field_class
.addr
, self
._elem
_fc
.addr
)
1368 self
.assertEqual(len(fc
.user_attributes
), 0)
1370 def test_create_invalid_field_class(self
):
1371 with self
.assertRaises(TypeError):
1372 self
._tc
.create_dynamic_array_field_class(object())
1375 class DynamicArrayWithLengthFieldFieldClassTestCase(
1376 _ArrayFieldClassTestCase
, unittest
.TestCase
1379 def _const_value_setter(field
):
1382 def _create_array(self
):
1383 return self
._tc
.create_dynamic_array_field_class(self
._elem
_fc
, self
._len
_fc
)
1386 self
._tc
= get_default_trace_class()
1387 self
._elem
_fc
= self
._tc
.create_signed_integer_field_class(23)
1388 self
._len
_fc
= self
._tc
.create_unsigned_integer_field_class(12)
1390 def test_create_default(self
):
1391 fc
= self
._create
_array
()
1392 self
.assertEqual(fc
.element_field_class
.addr
, self
._elem
_fc
.addr
)
1393 self
.assertIsNone(fc
.length_field_path
, None)
1394 self
.assertEqual(len(fc
.user_attributes
), 0)
1396 def _create_field_class_for_field_path_test(self
):
1397 # Create something a field class that is equivalent to:
1399 # struct outer_struct_fc {
1401 # struct inner_struct_fc {
1405 # uint23_t dyn_array[len];
1406 # } inner_struct[2];
1409 fc
= self
._create
_array
()
1411 foo_fc
= self
._tc
.create_single_precision_real_field_class()
1412 bar_fc
= self
._tc
.create_string_field_class()
1413 baz_fc
= self
._tc
.create_string_field_class()
1415 inner_struct_fc
= self
._tc
.create_structure_field_class()
1416 inner_struct_fc
.append_member('bar', bar_fc
)
1417 inner_struct_fc
.append_member('baz', baz_fc
)
1418 inner_struct_fc
.append_member('len', self
._len
_fc
)
1419 inner_struct_fc
.append_member('dyn_array', fc
)
1421 inner_struct_array_fc
= self
._tc
.create_static_array_field_class(
1425 outer_struct_fc
= self
._tc
.create_structure_field_class()
1426 outer_struct_fc
.append_member('foo', foo_fc
)
1427 outer_struct_fc
.append_member('inner_struct', inner_struct_array_fc
)
1429 # The path to the length field is resolved when the sequence is
1430 # actually used, for example in a packet context.
1431 self
._tc
.create_stream_class(
1432 packet_context_field_class
=outer_struct_fc
, supports_packets
=True
1437 def test_field_path_len(self
):
1438 fc
= self
._create
_field
_class
_for
_field
_path
_test
()
1439 self
.assertEqual(len(fc
.length_field_path
), 3)
1441 def test_field_path_iter(self
):
1442 fc
= self
._create
_field
_class
_for
_field
_path
_test
()
1443 path_items
= list(fc
.length_field_path
)
1445 self
.assertEqual(len(path_items
), 3)
1447 self
.assertIsInstance(path_items
[0], bt2
._IndexFieldPathItem
)
1448 self
.assertEqual(path_items
[0].index
, 1)
1450 self
.assertIsInstance(path_items
[1], bt2
._CurrentArrayElementFieldPathItem
)
1452 self
.assertIsInstance(path_items
[2], bt2
._IndexFieldPathItem
)
1453 self
.assertEqual(path_items
[2].index
, 2)
1455 def test_field_path_root_scope(self
):
1456 fc
= self
._create
_field
_class
_for
_field
_path
_test
()
1458 fc
.length_field_path
.root_scope
, bt2
.FieldPathScope
.PACKET_CONTEXT
1461 def test_create_invalid_length_type(self
):
1462 with self
.assertRaises(TypeError):
1463 self
._tc
.create_dynamic_array_field_class(
1464 self
._tc
.create_string_field_class(), 17
1468 if __name__
== "__main__":