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 OptionFieldClassTestCase(_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_field_class(self
._content
_fc
, **kwargs
)
736 def _create_default_const_field_class(self
, *args
, **kwargs
):
737 fc
= self
._tc
.create_option_field_class(self
._content
_fc
, **kwargs
)
738 return _create_const_field_class(self
._tc
, fc
, self
._const
_value
_setter
)
741 self
._tc
= get_default_trace_class()
742 self
._content
_fc
= self
._tc
.create_signed_integer_field_class(23)
743 self
._tag
_fc
= self
._tc
.create_bool_field_class()
745 def test_create_default(self
):
746 fc
= self
._create
_default
_field
_class
()
747 self
.assertEqual(fc
.field_class
.addr
, self
._content
_fc
.addr
)
748 self
.assertIsNone(fc
.selector_field_path
, None)
749 self
.assertEqual(len(fc
.user_attributes
), 0)
751 def test_attr_field_class(self
):
752 fc
= self
._create
_default
_field
_class
()
753 self
.assertIs(type(fc
.field_class
), bt2_field_class
._SignedIntegerFieldClass
)
755 def test_const_attr_field_class(self
):
756 fc
= self
._create
_default
_const
_field
_class
()
758 type(fc
.field_class
), bt2_field_class
._SignedIntegerFieldClassConst
761 def _create_field_class_for_field_path_test(self
):
762 fc
= self
._create
_default
_field
_class
(selector_fc
=self
._tag
_fc
)
764 foo_fc
= self
._tc
.create_single_precision_real_field_class()
765 bar_fc
= self
._tc
.create_string_field_class()
766 baz_fc
= self
._tc
.create_string_field_class()
768 inner_struct_fc
= self
._tc
.create_structure_field_class()
769 inner_struct_fc
.append_member('bar', bar_fc
)
770 inner_struct_fc
.append_member('baz', baz_fc
)
771 inner_struct_fc
.append_member('tag', self
._tag
_fc
)
772 inner_struct_fc
.append_member('opt', fc
)
774 opt_struct_array_fc
= self
._tc
.create_option_field_class(inner_struct_fc
)
776 outer_struct_fc
= self
._tc
.create_structure_field_class()
777 outer_struct_fc
.append_member('foo', foo_fc
)
778 outer_struct_fc
.append_member('inner_opt', opt_struct_array_fc
)
780 # The path to the selector field class is resolved when the
781 # option field class is actually used, for example in a packet
783 self
._tc
.create_stream_class(
784 packet_context_field_class
=outer_struct_fc
, supports_packets
=True
789 def test_field_path_len(self
):
790 fc
= self
._create
_field
_class
_for
_field
_path
_test
()
791 self
.assertEqual(len(fc
.selector_field_path
), 3)
793 def test_field_path_iter(self
):
794 fc
= self
._create
_field
_class
_for
_field
_path
_test
()
795 path_items
= list(fc
.selector_field_path
)
797 self
.assertEqual(len(path_items
), 3)
799 self
.assertIsInstance(path_items
[0], bt2
._IndexFieldPathItem
)
800 self
.assertEqual(path_items
[0].index
, 1)
802 self
.assertIsInstance(path_items
[1], bt2
._CurrentOptionContentFieldPathItem
)
804 self
.assertIsInstance(path_items
[2], bt2
._IndexFieldPathItem
)
805 self
.assertEqual(path_items
[2].index
, 2)
807 def test_field_path_root_scope(self
):
808 fc
= self
._create
_field
_class
_for
_field
_path
_test
()
810 fc
.selector_field_path
.root_scope
, bt2
.FieldPathScope
.PACKET_CONTEXT
813 def test_create_invalid_field_class(self
):
814 with self
.assertRaises(TypeError):
815 self
._tc
.create_option_field_class(object())
817 def test_create_invalid_selector_type(self
):
818 with self
.assertRaises(TypeError):
819 self
._tc
.create_option_field_class(self
._content
_fc
, 17)
822 class VariantFieldClassWithoutSelectorTestCase(
823 _TestFieldClass
, _TestElementContainer
, unittest
.TestCase
825 _append_element_method
= staticmethod(
826 bt2
._VariantFieldClassWithoutSelector
.append_option
828 _at_index_method
= staticmethod(
829 bt2
._VariantFieldClassWithoutSelector
.option_at_index
833 def _const_value_setter(variant_field
):
834 variant_field
.selected_option_index
= 0
835 variant_field
.value
= 12
837 def _create_field_class(self
, *args
, **kwargs
):
838 tc
= get_default_trace_class()
839 return tc
.create_variant_field_class(*args
, **kwargs
)
841 def _create_default_const_field_class(self
, *args
, **kwargs
):
842 tc
= get_default_trace_class()
843 fc
= tc
.create_variant_field_class(*args
, **kwargs
)
844 int_field_class
= self
._tc
.create_signed_integer_field_class(32)
845 fc
.append_option('int32', int_field_class
)
847 return _create_const_field_class(tc
, fc
, self
._const
_value
_setter
)
849 _create_default_field_class
= _create_field_class
852 class _VariantFieldClassWithSelectorTestCase
:
854 def _const_value_setter(field
):
855 field
['variant'].selected_option_index
= 0
856 field
['variant'] = 12
858 def _create_default_field_class(self
, *args
, **kwargs
):
859 return self
._tc
.create_variant_field_class(
860 *args
, selector_fc
=self
._selector
_fc
, **kwargs
863 def _create_default_const_field_class(self
, *args
, **kwargs
):
864 # Create a struct to contain the variant and its selector else we can't
865 # create the non-const field necessary to get the the const field_class
866 struct_fc
= self
._tc
.create_structure_field_class()
867 struct_fc
.append_member('selecteux', self
._selector
_fc
)
868 variant_fc
= self
._tc
.create_variant_field_class(
869 *args
, selector_fc
=self
._selector
_fc
871 variant_fc
.append_option(
872 'a', self
._tc
.create_signed_integer_field_class(32), self
._ranges
1
874 struct_fc
.append_member('variant', variant_fc
, **kwargs
)
876 return _create_const_field_class(self
._tc
, struct_fc
, self
._const
_value
_setter
)[
881 self
._tc
= get_default_trace_class()
883 self
._fc
= self
._create
_default
_field
_class
()
885 def test_create_default(self
):
886 self
.assertIsNotNone(self
._fc
)
887 self
.assertEqual(len(self
._fc
.user_attributes
), 0)
889 def test_append_element(self
):
890 str_field_class
= self
._tc
.create_string_field_class()
891 self
._fc
.append_option('str', str_field_class
, self
._ranges
1)
892 opt
= self
._fc
['str']
893 self
.assertEqual(opt
.field_class
.addr
, str_field_class
.addr
)
894 self
.assertEqual(opt
.name
, 'str')
895 self
.assertEqual(opt
.ranges
.addr
, self
._ranges
1.addr
)
897 def test_const_append(self
):
898 fc_const
= self
._create
_default
_const
_field
_class
()
899 str_field_class
= self
._tc
.create_string_field_class()
900 with self
.assertRaises(AttributeError):
901 fc_const
.append_option('str', str_field_class
, self
._ranges
1)
903 def test_append_element_kwargs(self
):
904 int_field_class
= self
._tc
.create_signed_integer_field_class(32)
905 self
._fc
.append_option(
906 name
='int32', field_class
=int_field_class
, ranges
=self
._ranges
1
908 opt
= self
._fc
['int32']
909 self
.assertEqual(opt
.field_class
.addr
, int_field_class
.addr
)
910 self
.assertEqual(opt
.name
, 'int32')
911 self
.assertEqual(opt
.ranges
.addr
, self
._ranges
1.addr
)
913 def test_append_element_invalid_name(self
):
914 sub_fc
= self
._tc
.create_string_field_class()
916 with self
.assertRaises(TypeError):
917 self
._fc
.append_option(self
._fc
, 23, sub_fc
)
919 def test_append_element_invalid_field_class(self
):
920 with self
.assertRaises(TypeError):
921 self
._fc
.append_option(self
._fc
, 'yes', object())
923 def test_append_element_invalid_ranges(self
):
924 sub_fc
= self
._tc
.create_string_field_class()
926 with self
.assertRaises(TypeError):
927 self
._fc
.append_option(self
._fc
, sub_fc
, 'lel')
929 def test_append_element_dup_name(self
):
930 sub_fc1
= self
._tc
.create_string_field_class()
931 sub_fc2
= self
._tc
.create_string_field_class()
933 with self
.assertRaises(ValueError):
934 self
._fc
.append_option('yes', sub_fc1
, self
._ranges
1)
935 self
._fc
.append_option('yes', sub_fc2
, self
._ranges
2)
937 def test_append_element_invalid_ranges_signedness(self
):
938 sub_fc
= self
._tc
.create_string_field_class()
940 with self
.assertRaises(TypeError):
941 self
._fc
.append_option(self
._fc
, sub_fc
, self
._inval
_ranges
)
943 def test_user_attributes(self
):
944 self
._fc
.append_option(
946 self
._tc
.create_string_field_class(),
948 user_attributes
={'salut': 23},
950 self
.assertEqual(self
._fc
['c'].user_attributes
, {'salut': 23})
951 self
.assertIs(type(self
._fc
.user_attributes
), bt2_value
.MapValue
)
953 def test_const_user_attributes(self
):
954 fc_const
= self
._create
_default
_const
_field
_class
()
955 self
.assertIs(type(fc_const
.user_attributes
), bt2_value
._MapValueConst
)
957 def test_invalid_user_attributes(self
):
958 with self
.assertRaises(TypeError):
959 self
._fc
.append_option(
961 self
._tc
.create_string_field_class(),
963 user_attributes
=object(),
966 def test_invalid_user_attributes_value_type(self
):
967 with self
.assertRaises(TypeError):
968 self
._fc
.append_option(
970 self
._tc
.create_string_field_class(),
976 a_field_class
= self
._tc
.create_single_precision_real_field_class()
977 self
._fc
.append_option('a_float', a_field_class
, self
._ranges
1)
978 c_field_class
= self
._tc
.create_string_field_class()
979 d_field_class
= self
._tc
.create_signed_enumeration_field_class(
983 ('c_string', c_field_class
, self
._ranges
2),
984 ('d_enum', d_field_class
, self
._ranges
3),
986 self
.assertEqual(self
._fc
['a_float'].field_class
.addr
, a_field_class
.addr
)
987 self
.assertEqual(self
._fc
['a_float'].name
, 'a_float')
988 self
.assertEqual(self
._fc
['a_float'].ranges
, self
._ranges
1)
989 self
.assertEqual(self
._fc
['c_string'].field_class
.addr
, c_field_class
.addr
)
990 self
.assertEqual(self
._fc
['c_string'].name
, 'c_string')
991 self
.assertEqual(self
._fc
['c_string'].ranges
, self
._ranges
2)
992 self
.assertEqual(self
._fc
['d_enum'].field_class
.addr
, d_field_class
.addr
)
993 self
.assertEqual(self
._fc
['d_enum'].name
, 'd_enum')
994 self
.assertEqual(self
._fc
['d_enum'].ranges
, self
._ranges
3)
996 def test_const_iadd(self
):
997 fc_const
= self
._create
_default
_const
_field
_class
()
998 a_field_class
= self
._tc
.create_single_precision_real_field_class()
999 with self
.assertRaises(TypeError):
1000 fc_const
+= [('a_float', a_field_class
, self
._ranges
1)]
1002 def test_bool_op(self
):
1003 self
.assertFalse(self
._fc
)
1004 self
._fc
.append_option('a', self
._tc
.create_string_field_class(), self
._ranges
1)
1005 self
.assertTrue(self
._fc
)
1008 self
._fc
.append_option('a', self
._tc
.create_string_field_class(), self
._ranges
1)
1009 self
._fc
.append_option('b', self
._tc
.create_string_field_class(), self
._ranges
2)
1010 self
._fc
.append_option('c', self
._tc
.create_string_field_class(), self
._ranges
3)
1011 self
.assertEqual(len(self
._fc
), 3)
1013 def test_getitem(self
):
1014 a_fc
= self
._tc
.create_signed_integer_field_class(32)
1015 b_fc
= self
._tc
.create_string_field_class()
1016 c_fc
= self
._tc
.create_single_precision_real_field_class()
1017 self
._fc
.append_option('a', a_fc
, self
._ranges
1)
1018 self
._fc
.append_option('b', b_fc
, self
._ranges
2)
1019 self
._fc
.append_option('c', c_fc
, self
._ranges
3)
1020 self
.assertEqual(self
._fc
['b'].field_class
.addr
, b_fc
.addr
)
1021 self
.assertEqual(self
._fc
['b'].name
, 'b')
1022 self
.assertEqual(self
._fc
['b'].ranges
.addr
, self
._ranges
2.addr
)
1024 def test_option_field_class(self
):
1025 a_fc
= self
._tc
.create_signed_integer_field_class(32)
1026 self
._fc
.append_option('a', a_fc
, self
._ranges
1)
1028 type(self
._fc
['a'].field_class
), bt2_field_class
._SignedIntegerFieldClass
1031 def test_const_option_field_class(self
):
1032 fc_const
= self
._create
_default
_const
_field
_class
()
1034 type(fc_const
['a'].field_class
),
1035 bt2_field_class
._SignedIntegerFieldClassConst
,
1038 def test_getitem_invalid_key_type(self
):
1039 with self
.assertRaises(TypeError):
1042 def test_getitem_invalid_key(self
):
1043 with self
.assertRaises(KeyError):
1046 def test_contains(self
):
1047 self
.assertFalse('a' in self
._fc
)
1048 self
._fc
.append_option('a', self
._tc
.create_string_field_class(), self
._ranges
1)
1049 self
.assertTrue('a' in self
._fc
)
1051 def test_iter(self
):
1052 a_fc
= self
._tc
.create_signed_integer_field_class(32)
1053 b_fc
= self
._tc
.create_string_field_class()
1054 c_fc
= self
._tc
.create_single_precision_real_field_class()
1056 ('a', a_fc
, self
._ranges
1),
1057 ('b', b_fc
, self
._ranges
2),
1058 ('c', c_fc
, self
._ranges
3),
1062 self
._fc
.append_option(*opt
)
1064 for (name
, opt
), test_opt
in zip(self
._fc
.items(), opts
):
1065 self
.assertEqual(opt
.name
, test_opt
[0])
1066 self
.assertEqual(name
, opt
.name
)
1067 self
.assertEqual(opt
.field_class
.addr
, test_opt
[1].addr
)
1068 self
.assertEqual(opt
.ranges
.addr
, test_opt
[2].addr
)
1070 def test_at_index(self
):
1071 a_fc
= self
._tc
.create_signed_integer_field_class(32)
1072 b_fc
= self
._tc
.create_string_field_class()
1073 c_fc
= self
._tc
.create_single_precision_real_field_class()
1074 self
._fc
.append_option('c', c_fc
, self
._ranges
1)
1075 self
._fc
.append_option('a', a_fc
, self
._ranges
2)
1076 self
._fc
.append_option('b', b_fc
, self
._ranges
3)
1077 self
.assertEqual(self
._fc
.option_at_index(1).field_class
.addr
, a_fc
.addr
)
1078 self
.assertEqual(self
._fc
.option_at_index(1).name
, 'a')
1079 self
.assertEqual(self
._fc
.option_at_index(1).ranges
.addr
, self
._ranges
2.addr
)
1081 def test_at_index_invalid(self
):
1082 self
._fc
.append_option(
1083 'c', self
._tc
.create_signed_integer_field_class(32), self
._ranges
3
1086 with self
.assertRaises(TypeError):
1087 self
._fc
.option_at_index('yes')
1089 def test_at_index_out_of_bounds_after(self
):
1090 self
._fc
.append_option(
1091 'c', self
._tc
.create_signed_integer_field_class(32), self
._ranges
3
1094 with self
.assertRaises(IndexError):
1095 self
._fc
.option_at_index(len(self
._fc
))
1097 def _fill_default_fc_for_field_path_test(self
):
1098 # Create something equivalent to:
1100 # struct outer_struct_fc {
1102 # struct inner_struct_fc {
1103 # [u]int64_t selector;
1106 # variant <selector> {
1107 # real a; // selected with self._ranges1
1108 # int21_t b; // selected with self._ranges2
1109 # uint34_t c; // selected with self._ranges3
1111 # } inner_struct[2];
1113 self
._fc
.append_option(
1114 'a', self
._tc
.create_single_precision_real_field_class(), self
._ranges
1
1116 self
._fc
.append_option(
1117 'b', self
._tc
.create_signed_integer_field_class(21), self
._ranges
2
1119 self
._fc
.append_option(
1120 'c', self
._tc
.create_unsigned_integer_field_class(34), self
._ranges
3
1123 foo_fc
= self
._tc
.create_single_precision_real_field_class()
1124 bar_fc
= self
._tc
.create_string_field_class()
1125 baz_fc
= self
._tc
.create_string_field_class()
1127 inner_struct_fc
= self
._tc
.create_structure_field_class()
1128 inner_struct_fc
.append_member('selector', self
._selector
_fc
)
1129 inner_struct_fc
.append_member('bar', bar_fc
)
1130 inner_struct_fc
.append_member('baz', baz_fc
)
1131 inner_struct_fc
.append_member('variant', self
._fc
)
1133 inner_struct_array_fc
= self
._tc
.create_static_array_field_class(
1137 outer_struct_fc
= self
._tc
.create_structure_field_class()
1138 outer_struct_fc
.append_member('foo', foo_fc
)
1139 outer_struct_fc
.append_member('inner_struct', inner_struct_array_fc
)
1141 # The path to the selector field is resolved when the sequence is
1142 # actually used, for example in a packet context.
1143 self
._tc
.create_stream_class(
1144 supports_packets
=True, packet_context_field_class
=outer_struct_fc
1147 def test_selector_field_path_length(self
):
1148 self
._fill
_default
_fc
_for
_field
_path
_test
()
1149 self
.assertEqual(len(self
._fc
.selector_field_path
), 3)
1151 def test_selector_field_path_iter(self
):
1152 self
._fill
_default
_fc
_for
_field
_path
_test
()
1153 path_items
= list(self
._fc
.selector_field_path
)
1155 self
.assertEqual(len(path_items
), 3)
1157 self
.assertIsInstance(path_items
[0], bt2
._IndexFieldPathItem
)
1158 self
.assertEqual(path_items
[0].index
, 1)
1160 self
.assertIsInstance(path_items
[1], bt2
._CurrentArrayElementFieldPathItem
)
1162 self
.assertIsInstance(path_items
[2], bt2
._IndexFieldPathItem
)
1163 self
.assertEqual(path_items
[2].index
, 0)
1165 def test_selector_field_path_root_scope(self
):
1166 self
._fill
_default
_fc
_for
_field
_path
_test
()
1168 self
._fc
.selector_field_path
.root_scope
, bt2
.FieldPathScope
.PACKET_CONTEXT
1172 class VariantFieldClassWithUnsignedSelectorTestCase(
1173 _VariantFieldClassWithSelectorTestCase
, unittest
.TestCase
1175 def _spec_set_up(self
):
1176 self
._ranges
1 = bt2
.UnsignedIntegerRangeSet([(1, 4), (18, 47)])
1177 self
._ranges
2 = bt2
.UnsignedIntegerRangeSet([(5, 5)])
1178 self
._ranges
3 = bt2
.UnsignedIntegerRangeSet([(8, 16), (48, 99)])
1179 self
._inval
_ranges
= bt2
.SignedIntegerRangeSet([(-8, 16), (48, 99)])
1180 self
._selector
_fc
= self
._tc
.create_unsigned_integer_field_class()
1183 class VariantFieldClassWithSignedSelectorTestCase(
1184 _VariantFieldClassWithSelectorTestCase
, unittest
.TestCase
1186 def _spec_set_up(self
):
1187 self
._ranges
1 = bt2
.SignedIntegerRangeSet([(-10, -4), (18, 47)])
1188 self
._ranges
2 = bt2
.SignedIntegerRangeSet([(-3, -3)])
1189 self
._ranges
3 = bt2
.SignedIntegerRangeSet([(8, 16), (48, 99)])
1190 self
._inval
_ranges
= bt2
.UnsignedIntegerRangeSet([(8, 16), (48, 99)])
1191 self
._selector
_fc
= self
._tc
.create_signed_integer_field_class()
1194 class _ArrayFieldClassTestCase
:
1195 def test_attr_element_field_class(self
):
1196 fc
= self
._create
_array
()
1198 type(fc
.element_field_class
), bt2_field_class
._SignedIntegerFieldClass
1201 def test_const_attr_element_field_class(self
):
1202 fc
= self
._create
_const
_array
()
1204 type(fc
.element_field_class
), bt2_field_class
._SignedIntegerFieldClassConst
1208 class StaticArrayFieldClassTestCase(_ArrayFieldClassTestCase
, unittest
.TestCase
):
1210 def _const_value_setter(field
):
1211 field
.value
= [9] * 45
1213 def _create_array(self
):
1214 return self
._tc
.create_static_array_field_class(self
._elem
_fc
, 45)
1216 def _create_const_array(self
):
1217 fc
= self
._tc
.create_static_array_field_class(self
._elem
_fc
, 45)
1218 return _create_const_field_class(self
._tc
, fc
, self
._const
_value
_setter
)
1221 self
._tc
= get_default_trace_class()
1222 self
._elem
_fc
= self
._tc
.create_signed_integer_field_class(23)
1224 def test_create_default(self
):
1225 fc
= self
._tc
.create_static_array_field_class(self
._elem
_fc
, 45)
1226 self
.assertEqual(fc
.element_field_class
.addr
, self
._elem
_fc
.addr
)
1227 self
.assertEqual(fc
.length
, 45)
1228 self
.assertEqual(len(fc
.user_attributes
), 0)
1230 def test_create_invalid_elem_field_class(self
):
1231 with self
.assertRaises(TypeError):
1232 self
._tc
.create_static_array_field_class(object(), 45)
1234 def test_create_invalid_length(self
):
1235 with self
.assertRaises(ValueError):
1236 self
._tc
.create_static_array_field_class(
1237 self
._tc
.create_string_field_class(), -17
1240 def test_create_invalid_length_type(self
):
1241 with self
.assertRaises(TypeError):
1242 self
._tc
.create_static_array_field_class(
1243 self
._tc
.create_string_field_class(), 'the length'
1247 class DynamicArrayFieldClassTestCase(_ArrayFieldClassTestCase
, unittest
.TestCase
):
1249 def _const_value_setter(field
):
1252 def _create_array(self
):
1253 return self
._tc
.create_dynamic_array_field_class(self
._elem
_fc
)
1255 def _create_const_array(self
):
1256 fc
= self
._tc
.create_dynamic_array_field_class(self
._elem
_fc
)
1257 return _create_const_field_class(self
._tc
, fc
, self
._const
_value
_setter
)
1260 self
._tc
= get_default_trace_class()
1261 self
._elem
_fc
= self
._tc
.create_signed_integer_field_class(23)
1262 self
._len
_fc
= self
._tc
.create_unsigned_integer_field_class(12)
1264 def test_create_default(self
):
1265 fc
= self
._tc
.create_dynamic_array_field_class(self
._elem
_fc
)
1266 self
.assertEqual(fc
.element_field_class
.addr
, self
._elem
_fc
.addr
)
1267 self
.assertIsNone(fc
.length_field_path
, None)
1268 self
.assertEqual(len(fc
.user_attributes
), 0)
1270 def _create_field_class_for_field_path_test(self
):
1271 # Create something a field class that is equivalent to:
1273 # struct outer_struct_fc {
1275 # struct inner_struct_fc {
1279 # uint23_t dyn_array[len];
1280 # } inner_struct[2];
1283 fc
= self
._tc
.create_dynamic_array_field_class(self
._elem
_fc
, self
._len
_fc
)
1285 foo_fc
= self
._tc
.create_single_precision_real_field_class()
1286 bar_fc
= self
._tc
.create_string_field_class()
1287 baz_fc
= self
._tc
.create_string_field_class()
1289 inner_struct_fc
= self
._tc
.create_structure_field_class()
1290 inner_struct_fc
.append_member('bar', bar_fc
)
1291 inner_struct_fc
.append_member('baz', baz_fc
)
1292 inner_struct_fc
.append_member('len', self
._len
_fc
)
1293 inner_struct_fc
.append_member('dyn_array', fc
)
1295 inner_struct_array_fc
= self
._tc
.create_static_array_field_class(
1299 outer_struct_fc
= self
._tc
.create_structure_field_class()
1300 outer_struct_fc
.append_member('foo', foo_fc
)
1301 outer_struct_fc
.append_member('inner_struct', inner_struct_array_fc
)
1303 # The path to the length field is resolved when the sequence is
1304 # actually used, for example in a packet context.
1305 self
._tc
.create_stream_class(
1306 packet_context_field_class
=outer_struct_fc
, supports_packets
=True
1311 def test_field_path_len(self
):
1312 fc
= self
._create
_field
_class
_for
_field
_path
_test
()
1313 self
.assertEqual(len(fc
.length_field_path
), 3)
1315 def test_field_path_iter(self
):
1316 fc
= self
._create
_field
_class
_for
_field
_path
_test
()
1317 path_items
= list(fc
.length_field_path
)
1319 self
.assertEqual(len(path_items
), 3)
1321 self
.assertIsInstance(path_items
[0], bt2
._IndexFieldPathItem
)
1322 self
.assertEqual(path_items
[0].index
, 1)
1324 self
.assertIsInstance(path_items
[1], bt2
._CurrentArrayElementFieldPathItem
)
1326 self
.assertIsInstance(path_items
[2], bt2
._IndexFieldPathItem
)
1327 self
.assertEqual(path_items
[2].index
, 2)
1329 def test_field_path_root_scope(self
):
1330 fc
= self
._create
_field
_class
_for
_field
_path
_test
()
1332 fc
.length_field_path
.root_scope
, bt2
.FieldPathScope
.PACKET_CONTEXT
1335 def test_create_invalid_field_class(self
):
1336 with self
.assertRaises(TypeError):
1337 self
._tc
.create_dynamic_array_field_class(object())
1339 def test_create_invalid_length_type(self
):
1340 with self
.assertRaises(TypeError):
1341 self
._tc
.create_dynamic_array_field_class(
1342 self
._tc
.create_string_field_class(), 17
1346 if __name__
== "__main__":