2 # Copyright (C) 2019 EfficiOS Inc.
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
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.
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.
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
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
)
32 stream_class
= tc
.create_stream_class(
33 packet_context_field_class
=packet_context_fc
, supports_packets
=True
36 stream
= trace
.create_stream(stream_class
)
40 def _create_const_field_class(tc
, field_class
, value_setter_fn
):
41 field_name
= 'const field'
43 class MyIter(bt2
._UserMessageIterator
):
44 def __init__(self
, config
, self_port_output
):
46 nonlocal value_setter_fn
47 stream
= _create_stream(tc
, [(field_name
, field_class
)])
48 packet
= stream
.create_packet()
50 value_setter_fn(packet
.context_field
[field_name
])
53 self
._create
_stream
_beginning
_message
(stream
),
54 self
._create
_packet
_beginning
_message
(packet
),
58 if len(self
._msgs
) == 0:
61 return self
._msgs
.pop(0)
63 class MySrc(bt2
._UserSourceComponent
, message_iterator_class
=MyIter
):
64 def __init__(self
, config
, params
, obj
):
65 self
._add
_output
_port
('out', params
)
68 src_comp
= graph
.add_component(MySrc
, 'my_source', None)
69 msg_iter
= TestOutputPortMessageIterator(graph
, src_comp
.output_ports
['out'])
71 # Ignore first message, stream beginning
73 packet_beg_msg
= next(msg_iter
)
75 return packet_beg_msg
.packet
.context_field
[field_name
].cls
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
)
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
)
89 def test_create_invalid_user_attributes(self
):
90 with self
.assertRaises(TypeError):
91 self
._create
_default
_field
_class
(user_attributes
=object())
93 def test_create_invalid_user_attributes_value_type(self
):
94 with self
.assertRaises(TypeError):
95 self
._create
_default
_field
_class
(user_attributes
=23)
98 class BoolFieldClassTestCase(_TestFieldClass
, unittest
.TestCase
):
100 def _const_value_setter(field
):
103 def _create_default_field_class(self
, **kwargs
):
104 tc
= get_default_trace_class()
105 return tc
.create_bool_field_class(**kwargs
)
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
)
113 self
._fc
= self
._create
_default
_field
_class
()
114 self
._fc
_const
= self
._create
_default
_const
_field
_class
()
116 def test_create_default(self
):
117 self
.assertIsNotNone(self
._fc
)
118 self
.assertEqual(len(self
._fc
.user_attributes
), 0)
121 class BitArrayFieldClassTestCase(_TestFieldClass
, unittest
.TestCase
):
123 def _const_value_setter(field
):
126 def _create_field_class(self
, *args
, **kwargs
):
127 tc
= get_default_trace_class()
128 return tc
.create_bit_array_field_class(*args
, **kwargs
)
130 def _create_default_field_class(self
, **kwargs
):
131 return self
._create
_field
_class
(17, **kwargs
)
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
)
139 self
._fc
= self
._create
_default
_field
_class
()
141 def test_create_default(self
):
142 self
.assertIsNotNone(self
._fc
)
143 self
.assertEqual(len(self
._fc
.user_attributes
), 0)
145 def test_create_length_out_of_range(self
):
146 with self
.assertRaises(ValueError):
147 self
._create
_field
_class
(65)
149 def test_create_length_zero(self
):
150 with self
.assertRaises(ValueError):
151 self
._create
_field
_class
(0)
153 def test_create_length_invalid_type(self
):
154 with self
.assertRaises(TypeError):
155 self
._create
_field
_class
('lel')
157 def test_length_prop(self
):
158 self
.assertEqual(self
._fc
.length
, 17)
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)
168 def test_create_range(self
):
169 fc
= self
._create
_field
_class
(field_value_range
=35)
170 self
.assertEqual(fc
.field_value_range
, 35)
172 fc
= self
._create
_field
_class
(36)
173 self
.assertEqual(fc
.field_value_range
, 36)
175 def test_create_invalid_range(self
):
176 with self
.assertRaises(TypeError):
177 self
._create
_field
_class
('yes')
179 with self
.assertRaises(TypeError):
180 self
._create
_field
_class
(field_value_range
='yes')
182 with self
.assertRaises(ValueError):
183 self
._create
_field
_class
(field_value_range
=-2)
185 with self
.assertRaises(ValueError):
186 self
._create
_field
_class
(field_value_range
=0)
188 def test_create_base(self
):
189 fc
= self
._create
_field
_class
(
190 preferred_display_base
=bt2
.IntegerDisplayBase
.HEXADECIMAL
192 self
.assertEqual(fc
.preferred_display_base
, bt2
.IntegerDisplayBase
.HEXADECIMAL
)
194 def test_create_invalid_base_type(self
):
195 with self
.assertRaises(TypeError):
196 self
._create
_field
_class
(preferred_display_base
='yes')
198 def test_create_invalid_base_value(self
):
199 with self
.assertRaises(ValueError):
200 self
._create
_field
_class
(preferred_display_base
=444)
202 def test_create_full(self
):
203 fc
= self
._create
_field
_class
(
204 24, preferred_display_base
=bt2
.IntegerDisplayBase
.OCTAL
206 self
.assertEqual(fc
.field_value_range
, 24)
207 self
.assertEqual(fc
.preferred_display_base
, bt2
.IntegerDisplayBase
.OCTAL
)
210 class SignedIntegerFieldClassTestCase(
211 _TestIntegerFieldClassProps
, _TestFieldClass
, unittest
.TestCase
214 def _const_value_setter(field
):
217 def _create_field_class(self
, *args
, **kwargs
):
218 tc
= get_default_trace_class()
219 return tc
.create_signed_integer_field_class(*args
, **kwargs
)
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
)
226 _create_default_field_class
= _create_field_class
229 class UnsignedIntegerFieldClassTestCase(
230 _TestIntegerFieldClassProps
, _TestFieldClass
, unittest
.TestCase
233 def _const_value_setter(field
):
236 def _create_field_class(self
, *args
, **kwargs
):
237 tc
= get_default_trace_class()
238 return tc
.create_unsigned_integer_field_class(*args
, **kwargs
)
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
)
245 _create_default_field_class
= _create_field_class
248 class SingleRealFieldClassTestCase(_TestFieldClass
, unittest
.TestCase
):
250 def _const_value_setter(field
):
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
)
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
)
262 _create_default_field_class
= _create_field_class
264 def test_create_default(self
):
265 fc
= self
._create
_field
_class
()
266 self
.assertEqual(len(fc
.user_attributes
), 0)
269 class DoubleRealFieldClassTestCase(_TestFieldClass
, unittest
.TestCase
):
271 def _const_value_setter(field
):
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
)
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
)
283 _create_default_field_class
= _create_field_class
285 def test_create_default(self
):
286 fc
= self
._create
_field
_class
()
287 self
.assertEqual(len(fc
.user_attributes
), 0)
290 # Converts an _EnumerationFieldClassMapping to a list of ranges:
292 # [(lower0, upper0), (lower1, upper1), ...]
295 def enum_mapping_to_set(mapping
):
296 return {(x
.lower
, x
.upper
) for x
in mapping
.ranges
}
299 class _EnumerationFieldClassTestCase(_TestIntegerFieldClassProps
):
302 self
._fc
= self
._create
_default
_field
_class
()
303 self
._fc
_const
= self
._create
_default
_const
_field
_class
()
305 def test_create_from_invalid_type(self
):
306 with self
.assertRaises(TypeError):
307 self
._create
_field
_class
('coucou')
309 def test_add_mapping_simple(self
):
310 self
._fc
.add_mapping('hello', self
._ranges
1)
311 mapping
= self
._fc
['hello']
312 self
.assertEqual(mapping
.label
, 'hello')
313 self
.assertEqual(mapping
.ranges
, self
._ranges
1)
315 def test_const_add_mapping(self
):
316 with self
.assertRaises(AttributeError):
317 self
._fc
_const
.add_mapping('hello', self
._ranges
1)
319 def test_add_mapping_simple_kwargs(self
):
320 self
._fc
.add_mapping(label
='hello', ranges
=self
._ranges
1)
321 mapping
= self
._fc
['hello']
322 self
.assertEqual(mapping
.label
, 'hello')
323 self
.assertEqual(mapping
.ranges
, self
._ranges
1)
325 def test_add_mapping_invalid_name(self
):
326 with self
.assertRaises(TypeError):
327 self
._fc
.add_mapping(17, self
._ranges
1)
329 def test_add_mapping_invalid_range(self
):
330 with self
.assertRaises(TypeError):
331 self
._fc
.add_mapping('allo', 'meow')
333 def test_add_mapping_dup_label(self
):
334 with self
.assertRaises(ValueError):
335 self
._fc
.add_mapping('a', self
._ranges
1)
336 self
._fc
.add_mapping('a', self
._ranges
2)
338 def test_add_mapping_invalid_ranges_signedness(self
):
339 with self
.assertRaises(TypeError):
340 self
._fc
.add_mapping('allo', self
._inval
_ranges
)
343 self
._fc
.add_mapping('c', self
._ranges
1)
345 self
._fc
+= [('d', self
._ranges
2), ('e', self
._ranges
3)]
347 self
.assertEqual(len(self
._fc
), 3)
348 self
.assertEqual(self
._fc
['c'].label
, 'c')
349 self
.assertEqual(self
._fc
['c'].ranges
, self
._ranges
1)
350 self
.assertEqual(self
._fc
['d'].label
, 'd')
351 self
.assertEqual(self
._fc
['d'].ranges
, self
._ranges
2)
352 self
.assertEqual(self
._fc
['e'].label
, 'e')
353 self
.assertEqual(self
._fc
['e'].ranges
, self
._ranges
3)
355 def test_const_iadd(self
):
356 with self
.assertRaises(TypeError):
357 self
._fc
_const
+= [('d', self
._ranges
2), ('e', self
._ranges
3)]
359 def test_bool_op(self
):
360 self
.assertFalse(self
._fc
)
361 self
._fc
.add_mapping('a', self
._ranges
1)
362 self
.assertTrue(self
._fc
)
365 self
._fc
.add_mapping('a', self
._ranges
1)
366 self
._fc
.add_mapping('b', self
._ranges
2)
367 self
._fc
.add_mapping('c', self
._ranges
3)
368 self
.assertEqual(len(self
._fc
), 3)
370 def test_getitem(self
):
371 self
._fc
.add_mapping('a', self
._ranges
1)
372 self
._fc
.add_mapping('b', self
._ranges
2)
373 self
._fc
.add_mapping('c', self
._ranges
3)
374 mapping
= self
._fc
['a']
375 self
.assertEqual(mapping
.label
, 'a')
376 self
.assertEqual(mapping
.ranges
, self
._ranges
1)
377 self
.assertIs(type(mapping
), self
._MAPPING
_CLASS
)
378 self
.assertIs(type(mapping
.ranges
), self
._CONST
_RANGE
_SET
_CLASS
)
380 def test_getitem_nonexistent(self
):
381 with self
.assertRaises(KeyError):
382 self
._fc
['doesnotexist']
385 self
._fc
.add_mapping('a', self
._ranges
1)
386 self
._fc
.add_mapping('b', self
._ranges
2)
387 self
._fc
.add_mapping('c', self
._ranges
3)
389 # This exercises iteration.
390 labels
= sorted(self
._fc
)
392 self
.assertEqual(labels
, ['a', 'b', 'c'])
394 def test_find_by_value(self
):
395 self
._fc
.add_mapping('a', self
._ranges
1)
396 self
._fc
.add_mapping('b', self
._ranges
2)
397 self
._fc
.add_mapping('c', self
._ranges
3)
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
)
404 class UnsignedEnumerationFieldClassTestCase(
405 _EnumerationFieldClassTestCase
, _TestFieldClass
, unittest
.TestCase
407 _MAPPING_CLASS
= bt2_field_class
._UnsignedEnumerationFieldClassMappingConst
408 _RANGE_SET_CLASS
= bt2
.UnsignedIntegerRangeSet
409 _CONST_RANGE_SET_CLASS
= bt2
._UnsignedIntegerRangeSetConst
411 def _spec_set_up(self
):
412 self
._ranges
1 = bt2
.UnsignedIntegerRangeSet([(1, 4), (18, 47)])
413 self
._ranges
2 = bt2
.UnsignedIntegerRangeSet([(5, 5)])
414 self
._ranges
3 = 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
419 def _const_value_setter(field
):
422 def _create_field_class(self
, *args
, **kwargs
):
423 tc
= get_default_trace_class()
424 return tc
.create_unsigned_enumeration_field_class(*args
, **kwargs
)
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
)
431 _create_default_field_class
= _create_field_class
434 class SignedEnumerationFieldClassTestCase(
435 _EnumerationFieldClassTestCase
, _TestFieldClass
, unittest
.TestCase
437 _MAPPING_CLASS
= bt2_field_class
._SignedEnumerationFieldClassMappingConst
438 _RANGE_SET_CLASS
= bt2
.SignedIntegerRangeSet
439 _CONST_RANGE_SET_CLASS
= bt2
._SignedIntegerRangeSetConst
441 def _spec_set_up(self
):
442 self
._ranges
1 = bt2
.SignedIntegerRangeSet([(-10, -4), (18, 47)])
443 self
._ranges
2 = bt2
.SignedIntegerRangeSet([(-3, -3)])
444 self
._ranges
3 = 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
449 def _const_value_setter(field
):
452 def _create_field_class(self
, *args
, **kwargs
):
453 tc
= get_default_trace_class()
454 return tc
.create_signed_enumeration_field_class(*args
, **kwargs
)
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
)
461 _create_default_field_class
= _create_field_class
464 class StringFieldClassTestCase(_TestFieldClass
, unittest
.TestCase
):
466 def _const_value_setter(field
):
467 field
.value
= 'chaine'
469 def _create_field_class(self
, *args
, **kwargs
):
470 tc
= get_default_trace_class()
471 return tc
.create_string_field_class(*args
, **kwargs
)
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
)
478 _create_default_field_class
= _create_field_class
481 self
._fc
= self
._create
_default
_field
_class
()
483 def test_create_default(self
):
484 self
.assertIsNotNone(self
._fc
)
485 self
.assertEqual(len(self
._fc
.user_attributes
), 0)
488 class _TestElementContainer
:
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
()
494 def test_create_default(self
):
495 self
.assertIsNotNone(self
._fc
)
496 self
.assertEqual(len(self
._fc
.user_attributes
), 0)
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
)
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
)
510 def test_append_element_invalid_name(self
):
511 sub_fc
= self
._tc
.create_string_field_class()
513 with self
.assertRaises(TypeError):
514 self
._append
_element
_method
(self
._fc
, 23, sub_fc
)
516 def test_append_element_invalid_field_class(self
):
517 with self
.assertRaises(TypeError):
518 self
._append
_element
_method
(self
._fc
, 'yes', object())
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()
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
)
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
533 self
.assertIs(type(field_class
), bt2_field_class
._SignedIntegerFieldClass
)
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
542 field_class
= const_fc
['int32'].field_class
544 self
.assertIs(type(field_class
), bt2_field_class
._SignedIntegerFieldClassConst
)
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(
555 e_field_class
= self
._tc
.create_structure_field_class()
557 ('c_string', c_field_class
),
558 ('d_enum', d_field_class
),
559 ('e_struct', e_field_class
),
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')
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
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
)
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)
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')
598 def test_getitem_invalid_key_type(self
):
599 with self
.assertRaises(TypeError):
602 def test_getitem_invalid_key(self
):
603 with self
.assertRaises(KeyError):
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
)
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
))
617 for elem
in elements
:
618 self
._append
_element
_method
(self
._fc
, *elem
)
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)
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')
637 def test_at_index_invalid(self
):
638 self
._append
_element
_method
(
639 self
._fc
, 'c', self
._tc
.create_signed_integer_field_class(32)
642 with self
.assertRaises(TypeError):
643 self
._at
_index
_method
(self
._fc
, 'yes')
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)
650 with self
.assertRaises(IndexError):
651 self
._at
_index
_method
(self
._fc
, len(self
._fc
))
653 def test_user_attributes(self
):
654 self
._append
_element
_method
(
657 self
._tc
.create_string_field_class(),
658 user_attributes
={'salut': 23},
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
)
664 def test_invalid_user_attributes(self
):
665 with self
.assertRaises(TypeError):
666 self
._append
_element
_method
(
669 self
._tc
.create_string_field_class(),
670 user_attributes
=object(),
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
680 class StructureFieldClassTestCase(
681 _TestFieldClass
, _TestElementContainer
, unittest
.TestCase
683 _append_element_method
= staticmethod(bt2
._StructureFieldClass
.append_member
)
684 _at_index_method
= staticmethod(bt2
._StructureFieldClass
.member_at_index
)
687 def _const_value_setter(field
):
690 def _create_field_class(self
, *args
, **kwargs
):
691 tc
= get_default_trace_class()
692 return tc
.create_structure_field_class(*args
, **kwargs
)
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
)
699 _create_default_field_class
= _create_field_class
701 def test_const_member_field_class(self
):
702 def _real_value_setter(field
):
703 field
.value
= {'real': 0}
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
)
712 type(const_fc
['real'].field_class
),
713 bt2_field_class
._SinglePrecisionRealFieldClassConst
,
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
)
723 type(fc
['real'].field_class
), bt2_field_class
._SinglePrecisionRealFieldClass
727 class OptionWithoutSelectorFieldClassTestCase(_TestFieldClass
, unittest
.TestCase
):
729 def _const_value_setter(field
):
730 field
.has_field
= True
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
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
742 return _create_const_field_class(self
._tc
, fc
, self
._const
_value
_setter
)
745 self
._tc
= get_default_trace_class()
746 self
._content
_fc
= self
._tc
.create_signed_integer_field_class(23)
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)
753 def test_create_invalid_field_class(self
):
754 with self
.assertRaises(TypeError):
755 self
._tc
.create_option_without_selector_field_class(object())
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
)
761 def test_const_attr_field_class(self
):
762 fc
= self
._create
_default
_const
_field
_class
()
764 type(fc
.field_class
), bt2_field_class
._SignedIntegerFieldClassConst
768 class _OptionWithSelectorFieldClassTestCase(_TestFieldClass
):
770 def _const_value_setter(field
):
771 field
['opt'].has_field
= True
772 field
['opt'].value
= 12
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
)
782 return _create_const_field_class(self
._tc
, struct_fc
, self
._const
_value
_setter
)[
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
()
791 def _create_field_class_for_field_path_test(self
):
792 fc
= self
._create
_default
_field
_class
()
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()
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
)
804 opt_struct_array_fc
= self
._tc
.create_option_without_selector_field_class(
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
)
812 # The path to the selector field class is resolved when the
813 # option field class is actually used, for example in a packet
815 self
._tc
.create_stream_class(
816 packet_context_field_class
=outer_struct_fc
, supports_packets
=True
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)
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
)
829 self
.assertEqual(len(path_items
), 3)
831 self
.assertIsInstance(path_items
[0], bt2
._IndexFieldPathItem
)
832 self
.assertEqual(path_items
[0].index
, 1)
834 self
.assertIsInstance(path_items
[1], bt2
._CurrentOptionContentFieldPathItem
)
836 self
.assertIsInstance(path_items
[2], bt2
._IndexFieldPathItem
)
837 self
.assertEqual(path_items
[2].index
, 2)
839 def test_field_path_root_scope(self
):
840 fc
= self
._create
_field
_class
_for
_field
_path
_test
()
842 fc
.selector_field_path
.root_scope
, bt2
.FieldPathScope
.PACKET_CONTEXT
846 class OptionWithBoolSelectorFieldClassTestCase(
847 _OptionWithSelectorFieldClassTestCase
, unittest
.TestCase
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
854 def _create_tag_fc(self
):
855 return self
._tc
.create_bool_field_class()
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)
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)
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)
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
)
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
)
880 class _OptionWithIntegerSelectorFieldClassTestCase(
881 _OptionWithSelectorFieldClassTestCase
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
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)
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
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
)()
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)
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
)
915 def test_const_attr_ranges(self
):
916 fc
= self
._create
_default
_const
_field
_class
()
917 self
.assertEqual(fc
.ranges
, self
._ranges
)
920 class OptionWithUnsignedIntegerSelectorFieldClassTestCase(
921 _OptionWithIntegerSelectorFieldClassTestCase
, unittest
.TestCase
924 self
._ranges
= bt2
.UnsignedIntegerRangeSet([(1, 3), (18, 44)])
927 def _create_tag_fc(self
):
928 return self
._tc
.create_unsigned_integer_field_class()
931 class VariantFieldClassWithoutSelectorTestCase(
932 _TestFieldClass
, _TestElementContainer
, unittest
.TestCase
934 _append_element_method
= staticmethod(
935 bt2
._VariantFieldClassWithoutSelector
.append_option
937 _at_index_method
= staticmethod(
938 bt2
._VariantFieldClassWithoutSelector
.option_at_index
942 def _const_value_setter(variant_field
):
943 variant_field
.selected_option_index
= 0
944 variant_field
.value
= 12
946 def _create_field_class(self
, *args
, **kwargs
):
947 tc
= get_default_trace_class()
948 return tc
.create_variant_field_class(*args
, **kwargs
)
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
)
956 return _create_const_field_class(tc
, fc
, self
._const
_value
_setter
)
958 _create_default_field_class
= _create_field_class
961 class _VariantFieldClassWithIntegerSelectorTestCase
:
963 def _const_value_setter(field
):
964 field
['variant'].selected_option_index
= 0
965 field
['variant'] = 12
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
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
980 variant_fc
.append_option(
981 'a', self
._tc
.create_signed_integer_field_class(32), self
._ranges
1
983 struct_fc
.append_member('variant', variant_fc
, **kwargs
)
985 return _create_const_field_class(self
._tc
, struct_fc
, self
._const
_value
_setter
)[
990 self
._tc
= get_default_trace_class()
992 self
._fc
= self
._create
_default
_field
_class
()
994 def test_create_default(self
):
995 self
.assertIsNotNone(self
._fc
)
996 self
.assertEqual(len(self
._fc
.user_attributes
), 0)
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
._ranges
1)
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
._ranges
1.addr
)
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
._ranges
1)
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
._ranges
1
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
._ranges
1.addr
)
1022 def test_append_element_invalid_name(self
):
1023 sub_fc
= self
._tc
.create_string_field_class()
1025 with self
.assertRaises(TypeError):
1026 self
._fc
.append_option(self
._fc
, 23, sub_fc
)
1028 def test_append_element_invalid_field_class(self
):
1029 with self
.assertRaises(TypeError):
1030 self
._fc
.append_option(self
._fc
, 'yes', object())
1032 def test_append_element_invalid_ranges(self
):
1033 sub_fc
= self
._tc
.create_string_field_class()
1035 with self
.assertRaises(TypeError):
1036 self
._fc
.append_option(self
._fc
, sub_fc
, 'lel')
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()
1042 with self
.assertRaises(ValueError):
1043 self
._fc
.append_option('yes', sub_fc1
, self
._ranges
1)
1044 self
._fc
.append_option('yes', sub_fc2
, self
._ranges
2)
1046 def test_append_element_invalid_ranges_signedness(self
):
1047 sub_fc
= self
._tc
.create_string_field_class()
1049 with self
.assertRaises(TypeError):
1050 self
._fc
.append_option(self
._fc
, sub_fc
, self
._inval
_ranges
)
1052 def test_user_attributes(self
):
1053 self
._fc
.append_option(
1055 self
._tc
.create_string_field_class(),
1057 user_attributes
={'salut': 23},
1059 self
.assertEqual(self
._fc
['c'].user_attributes
, {'salut': 23})
1060 self
.assertIs(type(self
._fc
.user_attributes
), bt2_value
.MapValue
)
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
)
1066 def test_invalid_user_attributes(self
):
1067 with self
.assertRaises(TypeError):
1068 self
._fc
.append_option(
1070 self
._tc
.create_string_field_class(),
1072 user_attributes
=object(),
1075 def test_invalid_user_attributes_value_type(self
):
1076 with self
.assertRaises(TypeError):
1077 self
._fc
.append_option(
1079 self
._tc
.create_string_field_class(),
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
._ranges
1)
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
1092 ('c_string', c_field_class
, self
._ranges
2),
1093 ('d_enum', d_field_class
, self
._ranges
3),
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
._ranges
1)
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
._ranges
2)
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
._ranges
3)
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
._ranges
1)]
1111 def test_bool_op(self
):
1112 self
.assertFalse(self
._fc
)
1113 self
._fc
.append_option('a', self
._tc
.create_string_field_class(), self
._ranges
1)
1114 self
.assertTrue(self
._fc
)
1117 self
._fc
.append_option('a', self
._tc
.create_string_field_class(), self
._ranges
1)
1118 self
._fc
.append_option('b', self
._tc
.create_string_field_class(), self
._ranges
2)
1119 self
._fc
.append_option('c', self
._tc
.create_string_field_class(), self
._ranges
3)
1120 self
.assertEqual(len(self
._fc
), 3)
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
._ranges
1)
1127 self
._fc
.append_option('b', b_fc
, self
._ranges
2)
1128 self
._fc
.append_option('c', c_fc
, self
._ranges
3)
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
._ranges
2.addr
)
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
._ranges
1)
1137 type(self
._fc
['a'].field_class
), bt2_field_class
._SignedIntegerFieldClass
1140 def test_const_option_field_class(self
):
1141 fc_const
= self
._create
_default
_const
_field
_class
()
1143 type(fc_const
['a'].field_class
),
1144 bt2_field_class
._SignedIntegerFieldClassConst
,
1147 def test_getitem_invalid_key_type(self
):
1148 with self
.assertRaises(TypeError):
1151 def test_getitem_invalid_key(self
):
1152 with self
.assertRaises(KeyError):
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
._ranges
1)
1158 self
.assertTrue('a' in self
._fc
)
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()
1165 ('a', a_fc
, self
._ranges
1),
1166 ('b', b_fc
, self
._ranges
2),
1167 ('c', c_fc
, self
._ranges
3),
1171 self
._fc
.append_option(*opt
)
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
)
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
._ranges
1)
1184 self
._fc
.append_option('a', a_fc
, self
._ranges
2)
1185 self
._fc
.append_option('b', b_fc
, self
._ranges
3)
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
._ranges
2.addr
)
1190 def test_at_index_invalid(self
):
1191 self
._fc
.append_option(
1192 'c', self
._tc
.create_signed_integer_field_class(32), self
._ranges
3
1195 with self
.assertRaises(TypeError):
1196 self
._fc
.option_at_index('yes')
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
._ranges
3
1203 with self
.assertRaises(IndexError):
1204 self
._fc
.option_at_index(len(self
._fc
))
1206 def _fill_default_fc_for_field_path_test(self
):
1207 # Create something equivalent to:
1209 # struct outer_struct_fc {
1211 # struct inner_struct_fc {
1212 # [u]int64_t selector;
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
1220 # } inner_struct[2];
1222 self
._fc
.append_option(
1223 'a', self
._tc
.create_single_precision_real_field_class(), self
._ranges
1
1225 self
._fc
.append_option(
1226 'b', self
._tc
.create_signed_integer_field_class(21), self
._ranges
2
1228 self
._fc
.append_option(
1229 'c', self
._tc
.create_unsigned_integer_field_class(34), self
._ranges
3
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()
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
)
1242 inner_struct_array_fc
= self
._tc
.create_static_array_field_class(
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
)
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
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)
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
)
1264 self
.assertEqual(len(path_items
), 3)
1266 self
.assertIsInstance(path_items
[0], bt2
._IndexFieldPathItem
)
1267 self
.assertEqual(path_items
[0].index
, 1)
1269 self
.assertIsInstance(path_items
[1], bt2
._CurrentArrayElementFieldPathItem
)
1271 self
.assertIsInstance(path_items
[2], bt2
._IndexFieldPathItem
)
1272 self
.assertEqual(path_items
[2].index
, 0)
1274 def test_selector_field_path_root_scope(self
):
1275 self
._fill
_default
_fc
_for
_field
_path
_test
()
1277 self
._fc
.selector_field_path
.root_scope
, bt2
.FieldPathScope
.PACKET_CONTEXT
1281 class VariantFieldClassWithUnsignedSelectorTestCase(
1282 _VariantFieldClassWithIntegerSelectorTestCase
, unittest
.TestCase
1284 def _spec_set_up(self
):
1285 self
._ranges
1 = bt2
.UnsignedIntegerRangeSet([(1, 4), (18, 47)])
1286 self
._ranges
2 = bt2
.UnsignedIntegerRangeSet([(5, 5)])
1287 self
._ranges
3 = 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()
1292 class VariantFieldClassWithSignedSelectorTestCase(
1293 _VariantFieldClassWithIntegerSelectorTestCase
, unittest
.TestCase
1295 def _spec_set_up(self
):
1296 self
._ranges
1 = bt2
.SignedIntegerRangeSet([(-10, -4), (18, 47)])
1297 self
._ranges
2 = bt2
.SignedIntegerRangeSet([(-3, -3)])
1298 self
._ranges
3 = 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()
1303 class _ArrayFieldClassTestCase
:
1304 def test_attr_element_field_class(self
):
1305 fc
= self
._create
_array
()
1307 type(fc
.element_field_class
), bt2_field_class
._SignedIntegerFieldClass
1311 class _ArrayFieldClassConstTestCase
:
1312 def test_const_attr_element_field_class(self
):
1313 fc
= self
._create
_const
_array
()
1315 type(fc
.element_field_class
), bt2_field_class
._SignedIntegerFieldClassConst
1319 class StaticArrayFieldClassTestCase(
1320 _ArrayFieldClassTestCase
, _ArrayFieldClassConstTestCase
, unittest
.TestCase
1323 def _const_value_setter(field
):
1324 field
.value
= [9] * 45
1326 def _create_array(self
):
1327 return self
._tc
.create_static_array_field_class(self
._elem
_fc
, 45)
1329 def _create_const_array(self
):
1330 fc
= self
._tc
.create_static_array_field_class(self
._elem
_fc
, 45)
1331 return _create_const_field_class(self
._tc
, fc
, self
._const
_value
_setter
)
1334 self
._tc
= get_default_trace_class()
1335 self
._elem
_fc
= self
._tc
.create_signed_integer_field_class(23)
1337 def test_create_default(self
):
1338 fc
= self
._tc
.create_static_array_field_class(self
._elem
_fc
, 45)
1339 self
.assertEqual(fc
.element_field_class
.addr
, self
._elem
_fc
.addr
)
1340 self
.assertEqual(fc
.length
, 45)
1341 self
.assertEqual(len(fc
.user_attributes
), 0)
1343 def test_create_invalid_elem_field_class(self
):
1344 with self
.assertRaises(TypeError):
1345 self
._tc
.create_static_array_field_class(object(), 45)
1347 def test_create_invalid_length(self
):
1348 with self
.assertRaises(ValueError):
1349 self
._tc
.create_static_array_field_class(
1350 self
._tc
.create_string_field_class(), -17
1353 def test_create_invalid_length_type(self
):
1354 with self
.assertRaises(TypeError):
1355 self
._tc
.create_static_array_field_class(
1356 self
._tc
.create_string_field_class(), 'the length'
1360 class DynamicArrayFieldClassTestCase(
1361 _ArrayFieldClassTestCase
, _ArrayFieldClassConstTestCase
, unittest
.TestCase
1364 def _const_value_setter(field
):
1367 def _create_array(self
):
1368 return self
._tc
.create_dynamic_array_field_class(self
._elem
_fc
)
1370 def _create_const_array(self
):
1371 fc
= self
._tc
.create_dynamic_array_field_class(self
._elem
_fc
)
1372 return _create_const_field_class(self
._tc
, fc
, self
._const
_value
_setter
)
1375 self
._tc
= get_default_trace_class()
1376 self
._elem
_fc
= self
._tc
.create_signed_integer_field_class(23)
1378 def test_create_default(self
):
1379 fc
= self
._tc
.create_dynamic_array_field_class(self
._elem
_fc
)
1380 self
.assertEqual(fc
.element_field_class
.addr
, self
._elem
_fc
.addr
)
1381 self
.assertEqual(len(fc
.user_attributes
), 0)
1383 def test_create_invalid_field_class(self
):
1384 with self
.assertRaises(TypeError):
1385 self
._tc
.create_dynamic_array_field_class(object())
1388 class DynamicArrayWithLengthFieldFieldClassTestCase(
1389 _ArrayFieldClassTestCase
, unittest
.TestCase
1392 def _const_value_setter(field
):
1395 def _create_array(self
):
1396 return self
._tc
.create_dynamic_array_field_class(self
._elem
_fc
, self
._len
_fc
)
1399 self
._tc
= get_default_trace_class()
1400 self
._elem
_fc
= self
._tc
.create_signed_integer_field_class(23)
1401 self
._len
_fc
= self
._tc
.create_unsigned_integer_field_class(12)
1403 def test_create_default(self
):
1404 fc
= self
._create
_array
()
1405 self
.assertEqual(fc
.element_field_class
.addr
, self
._elem
_fc
.addr
)
1406 self
.assertIsNone(fc
.length_field_path
, None)
1407 self
.assertEqual(len(fc
.user_attributes
), 0)
1409 def _create_field_class_for_field_path_test(self
):
1410 # Create something a field class that is equivalent to:
1412 # struct outer_struct_fc {
1414 # struct inner_struct_fc {
1418 # uint23_t dyn_array[len];
1419 # } inner_struct[2];
1422 fc
= self
._create
_array
()
1424 foo_fc
= self
._tc
.create_single_precision_real_field_class()
1425 bar_fc
= self
._tc
.create_string_field_class()
1426 baz_fc
= self
._tc
.create_string_field_class()
1428 inner_struct_fc
= self
._tc
.create_structure_field_class()
1429 inner_struct_fc
.append_member('bar', bar_fc
)
1430 inner_struct_fc
.append_member('baz', baz_fc
)
1431 inner_struct_fc
.append_member('len', self
._len
_fc
)
1432 inner_struct_fc
.append_member('dyn_array', fc
)
1434 inner_struct_array_fc
= self
._tc
.create_static_array_field_class(
1438 outer_struct_fc
= self
._tc
.create_structure_field_class()
1439 outer_struct_fc
.append_member('foo', foo_fc
)
1440 outer_struct_fc
.append_member('inner_struct', inner_struct_array_fc
)
1442 # The path to the length field is resolved when the sequence is
1443 # actually used, for example in a packet context.
1444 self
._tc
.create_stream_class(
1445 packet_context_field_class
=outer_struct_fc
, supports_packets
=True
1450 def test_field_path_len(self
):
1451 fc
= self
._create
_field
_class
_for
_field
_path
_test
()
1452 self
.assertEqual(len(fc
.length_field_path
), 3)
1454 def test_field_path_iter(self
):
1455 fc
= self
._create
_field
_class
_for
_field
_path
_test
()
1456 path_items
= list(fc
.length_field_path
)
1458 self
.assertEqual(len(path_items
), 3)
1460 self
.assertIsInstance(path_items
[0], bt2
._IndexFieldPathItem
)
1461 self
.assertEqual(path_items
[0].index
, 1)
1463 self
.assertIsInstance(path_items
[1], bt2
._CurrentArrayElementFieldPathItem
)
1465 self
.assertIsInstance(path_items
[2], bt2
._IndexFieldPathItem
)
1466 self
.assertEqual(path_items
[2].index
, 2)
1468 def test_field_path_root_scope(self
):
1469 fc
= self
._create
_field
_class
_for
_field
_path
_test
()
1471 fc
.length_field_path
.root_scope
, bt2
.FieldPathScope
.PACKET_CONTEXT
1474 def test_create_invalid_length_type(self
):
1475 with self
.assertRaises(TypeError):
1476 self
._tc
.create_dynamic_array_field_class(
1477 self
._tc
.create_string_field_class(), 17
1481 if __name__
== "__main__":