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)
378 def test_getitem_nonexistent(self
):
379 with self
.assertRaises(KeyError):
380 self
._fc
['doesnotexist']
383 self
._fc
.add_mapping('a', self
._ranges
1)
384 self
._fc
.add_mapping('b', self
._ranges
2)
385 self
._fc
.add_mapping('c', self
._ranges
3)
387 # This exercises iteration.
388 labels
= sorted(self
._fc
)
390 self
.assertEqual(labels
, ['a', 'b', 'c'])
392 def test_find_by_value(self
):
393 self
._fc
.add_mapping('a', self
._ranges
1)
394 self
._fc
.add_mapping('b', self
._ranges
2)
395 self
._fc
.add_mapping('c', self
._ranges
3)
396 mappings
= self
._fc
.mappings_for_value(self
._value
_in
_range
_1_and
_3)
397 labels
= set([mapping
.label
for mapping
in mappings
])
398 expected_labels
= set(['a', 'c'])
399 self
.assertEqual(labels
, expected_labels
)
402 class UnsignedEnumerationFieldClassTestCase(
403 _EnumerationFieldClassTestCase
, _TestFieldClass
, unittest
.TestCase
405 def _spec_set_up(self
):
406 self
._ranges
1 = bt2
.UnsignedIntegerRangeSet([(1, 4), (18, 47)])
407 self
._ranges
2 = bt2
.UnsignedIntegerRangeSet([(5, 5)])
408 self
._ranges
3 = bt2
.UnsignedIntegerRangeSet([(8, 22), (48, 99)])
409 self
._inval
_ranges
= bt2
.SignedIntegerRangeSet([(-8, -5), (48, 1928)])
410 self
._value
_in
_range
_1_and
_3 = 20
413 def _const_value_setter(field
):
416 def _create_field_class(self
, *args
, **kwargs
):
417 tc
= get_default_trace_class()
418 return tc
.create_unsigned_enumeration_field_class(*args
, **kwargs
)
420 def _create_default_const_field_class(self
, *args
, **kwargs
):
421 tc
= get_default_trace_class()
422 fc
= tc
.create_unsigned_enumeration_field_class(*args
, **kwargs
)
423 return _create_const_field_class(tc
, fc
, self
._const
_value
_setter
)
425 _create_default_field_class
= _create_field_class
428 class SignedEnumerationFieldClassTestCase(
429 _EnumerationFieldClassTestCase
, _TestFieldClass
, unittest
.TestCase
431 def _spec_set_up(self
):
432 self
._ranges
1 = bt2
.SignedIntegerRangeSet([(-10, -4), (18, 47)])
433 self
._ranges
2 = bt2
.SignedIntegerRangeSet([(-3, -3)])
434 self
._ranges
3 = bt2
.SignedIntegerRangeSet([(-100, -1), (8, 16), (48, 99)])
435 self
._inval
_ranges
= bt2
.UnsignedIntegerRangeSet([(8, 16), (48, 99)])
436 self
._value
_in
_range
_1_and
_3 = -7
439 def _const_value_setter(field
):
442 def _create_field_class(self
, *args
, **kwargs
):
443 tc
= get_default_trace_class()
444 return tc
.create_signed_enumeration_field_class(*args
, **kwargs
)
446 def _create_default_const_field_class(self
, *args
, **kwargs
):
447 tc
= get_default_trace_class()
448 fc
= tc
.create_signed_enumeration_field_class(*args
, **kwargs
)
449 return _create_const_field_class(tc
, fc
, self
._const
_value
_setter
)
451 _create_default_field_class
= _create_field_class
454 class StringFieldClassTestCase(_TestFieldClass
, unittest
.TestCase
):
456 def _const_value_setter(field
):
457 field
.value
= 'chaine'
459 def _create_field_class(self
, *args
, **kwargs
):
460 tc
= get_default_trace_class()
461 return tc
.create_string_field_class(*args
, **kwargs
)
463 def _create_default_const_field_class(self
, *args
, **kwargs
):
464 tc
= get_default_trace_class()
465 fc
= tc
.create_string_field_class(*args
, **kwargs
)
466 return _create_const_field_class(tc
, fc
, self
._const
_value
_setter
)
468 _create_default_field_class
= _create_field_class
471 self
._fc
= self
._create
_default
_field
_class
()
473 def test_create_default(self
):
474 self
.assertIsNotNone(self
._fc
)
475 self
.assertEqual(len(self
._fc
.user_attributes
), 0)
478 class _TestElementContainer
:
480 self
._tc
= get_default_trace_class()
481 self
._fc
= self
._create
_default
_field
_class
()
482 self
._fc
_const
= self
._create
_default
_const
_field
_class
()
484 def test_create_default(self
):
485 self
.assertIsNotNone(self
._fc
)
486 self
.assertEqual(len(self
._fc
.user_attributes
), 0)
488 def test_append_element(self
):
489 int_field_class
= self
._tc
.create_signed_integer_field_class(32)
490 self
._append
_element
_method
(self
._fc
, 'int32', int_field_class
)
491 field_class
= self
._fc
['int32'].field_class
492 self
.assertEqual(field_class
.addr
, int_field_class
.addr
)
494 def test_append_element_kwargs(self
):
495 int_field_class
= self
._tc
.create_signed_integer_field_class(32)
496 self
._append
_element
_method
(self
._fc
, name
='int32', field_class
=int_field_class
)
497 field_class
= self
._fc
['int32'].field_class
498 self
.assertEqual(field_class
.addr
, int_field_class
.addr
)
500 def test_append_element_invalid_name(self
):
501 sub_fc
= self
._tc
.create_string_field_class()
503 with self
.assertRaises(TypeError):
504 self
._append
_element
_method
(self
._fc
, 23, sub_fc
)
506 def test_append_element_invalid_field_class(self
):
507 with self
.assertRaises(TypeError):
508 self
._append
_element
_method
(self
._fc
, 'yes', object())
510 def test_append_element_dup_name(self
):
511 sub_fc1
= self
._tc
.create_string_field_class()
512 sub_fc2
= self
._tc
.create_string_field_class()
514 with self
.assertRaises(ValueError):
515 self
._append
_element
_method
(self
._fc
, 'yes', sub_fc1
)
516 self
._append
_element
_method
(self
._fc
, 'yes', sub_fc2
)
518 def test_attr_field_class(self
):
519 int_field_class
= self
._tc
.create_signed_integer_field_class(32)
520 self
._append
_element
_method
(self
._fc
, 'int32', int_field_class
)
521 field_class
= self
._fc
['int32'].field_class
523 self
.assertIs(type(field_class
), bt2_field_class
._SignedIntegerFieldClass
)
525 def test_const_attr_field_class(self
):
526 int_field_class
= self
._tc
.create_signed_integer_field_class(32)
527 self
._append
_element
_method
(self
._fc
, 'int32', int_field_class
)
528 field_class
= self
._fc
['int32'].field_class
529 const_fc
= _create_const_field_class(
530 self
._tc
, self
._fc
, self
._const
_value
_setter
532 field_class
= const_fc
['int32'].field_class
534 self
.assertIs(type(field_class
), bt2_field_class
._SignedIntegerFieldClassConst
)
537 a_field_class
= self
._tc
.create_single_precision_real_field_class()
538 b_field_class
= self
._tc
.create_signed_integer_field_class(17)
539 self
._append
_element
_method
(self
._fc
, 'a_float', a_field_class
)
540 self
._append
_element
_method
(self
._fc
, 'b_int', b_field_class
)
541 c_field_class
= self
._tc
.create_string_field_class()
542 d_field_class
= self
._tc
.create_signed_enumeration_field_class(
545 e_field_class
= self
._tc
.create_structure_field_class()
547 ('c_string', c_field_class
),
548 ('d_enum', d_field_class
),
549 ('e_struct', e_field_class
),
551 self
.assertEqual(self
._fc
['a_float'].field_class
.addr
, a_field_class
.addr
)
552 self
.assertEqual(self
._fc
['a_float'].name
, 'a_float')
553 self
.assertEqual(self
._fc
['b_int'].field_class
.addr
, b_field_class
.addr
)
554 self
.assertEqual(self
._fc
['b_int'].name
, 'b_int')
555 self
.assertEqual(self
._fc
['c_string'].field_class
.addr
, c_field_class
.addr
)
556 self
.assertEqual(self
._fc
['c_string'].name
, 'c_string')
557 self
.assertEqual(self
._fc
['d_enum'].field_class
.addr
, d_field_class
.addr
)
558 self
.assertEqual(self
._fc
['d_enum'].name
, 'd_enum')
559 self
.assertEqual(self
._fc
['e_struct'].field_class
.addr
, e_field_class
.addr
)
560 self
.assertEqual(self
._fc
['e_struct'].name
, 'e_struct')
562 def test_const_iadd(self
):
563 a_field_class
= self
._tc
.create_single_precision_real_field_class()
564 with self
.assertRaises(TypeError):
565 self
._fc
_const
+= a_field_class
567 def test_bool_op(self
):
568 self
.assertFalse(self
._fc
)
569 self
._append
_element
_method
(self
._fc
, 'a', self
._tc
.create_string_field_class())
570 self
.assertTrue(self
._fc
)
573 self
._append
_element
_method
(self
._fc
, 'a', self
._tc
.create_string_field_class())
574 self
._append
_element
_method
(self
._fc
, 'b', self
._tc
.create_string_field_class())
575 self
._append
_element
_method
(self
._fc
, 'c', self
._tc
.create_string_field_class())
576 self
.assertEqual(len(self
._fc
), 3)
578 def test_getitem(self
):
579 a_fc
= self
._tc
.create_signed_integer_field_class(32)
580 b_fc
= self
._tc
.create_string_field_class()
581 c_fc
= self
._tc
.create_single_precision_real_field_class()
582 self
._append
_element
_method
(self
._fc
, 'a', a_fc
)
583 self
._append
_element
_method
(self
._fc
, 'b', b_fc
)
584 self
._append
_element
_method
(self
._fc
, 'c', c_fc
)
585 self
.assertEqual(self
._fc
['b'].field_class
.addr
, b_fc
.addr
)
586 self
.assertEqual(self
._fc
['b'].name
, 'b')
588 def test_getitem_invalid_key_type(self
):
589 with self
.assertRaises(TypeError):
592 def test_getitem_invalid_key(self
):
593 with self
.assertRaises(KeyError):
596 def test_contains(self
):
597 self
.assertFalse('a' in self
._fc
)
598 self
._append
_element
_method
(self
._fc
, 'a', self
._tc
.create_string_field_class())
599 self
.assertTrue('a' in self
._fc
)
602 a_fc
= self
._tc
.create_signed_integer_field_class(32)
603 b_fc
= self
._tc
.create_string_field_class()
604 c_fc
= self
._tc
.create_single_precision_real_field_class()
605 elements
= (('a', a_fc
), ('b', b_fc
), ('c', c_fc
))
607 for elem
in elements
:
608 self
._append
_element
_method
(self
._fc
, *elem
)
610 for (name
, element
), test_elem
in zip(self
._fc
.items(), elements
):
611 self
.assertEqual(element
.name
, test_elem
[0])
612 self
.assertEqual(name
, element
.name
)
613 self
.assertEqual(element
.field_class
.addr
, test_elem
[1].addr
)
614 self
.assertEqual(len(element
.user_attributes
), 0)
616 def test_at_index(self
):
617 a_fc
= self
._tc
.create_signed_integer_field_class(32)
618 b_fc
= self
._tc
.create_string_field_class()
619 c_fc
= self
._tc
.create_single_precision_real_field_class()
620 self
._append
_element
_method
(self
._fc
, 'c', c_fc
)
621 self
._append
_element
_method
(self
._fc
, 'a', a_fc
)
622 self
._append
_element
_method
(self
._fc
, 'b', b_fc
)
623 elem
= self
._at
_index
_method
(self
._fc
, 1)
624 self
.assertEqual(elem
.field_class
.addr
, a_fc
.addr
)
625 self
.assertEqual(elem
.name
, 'a')
627 def test_at_index_invalid(self
):
628 self
._append
_element
_method
(
629 self
._fc
, 'c', self
._tc
.create_signed_integer_field_class(32)
632 with self
.assertRaises(TypeError):
633 self
._at
_index
_method
(self
._fc
, 'yes')
635 def test_at_index_out_of_bounds_after(self
):
636 self
._append
_element
_method
(
637 self
._fc
, 'c', self
._tc
.create_signed_integer_field_class(32)
640 with self
.assertRaises(IndexError):
641 self
._at
_index
_method
(self
._fc
, len(self
._fc
))
643 def test_user_attributes(self
):
644 self
._append
_element
_method
(
647 self
._tc
.create_string_field_class(),
648 user_attributes
={'salut': 23},
650 self
.assertEqual(self
._fc
['c'].user_attributes
, {'salut': 23})
651 self
.assertIs(type(self
._fc
.user_attributes
), bt2_value
.MapValue
)
652 self
.assertIs(type(self
._fc
['c'].user_attributes
), bt2_value
.MapValue
)
654 def test_invalid_user_attributes(self
):
655 with self
.assertRaises(TypeError):
656 self
._append
_element
_method
(
659 self
._tc
.create_string_field_class(),
660 user_attributes
=object(),
663 def test_invalid_user_attributes_value_type(self
):
664 with self
.assertRaises(TypeError):
665 self
._append
_element
_method
(
666 self
._fc
, 'c', self
._tc
.create_string_field_class(), user_attributes
=23
670 class StructureFieldClassTestCase(
671 _TestFieldClass
, _TestElementContainer
, unittest
.TestCase
673 _append_element_method
= staticmethod(bt2
._StructureFieldClass
.append_member
)
674 _at_index_method
= staticmethod(bt2
._StructureFieldClass
.member_at_index
)
677 def _const_value_setter(field
):
680 def _create_field_class(self
, *args
, **kwargs
):
681 tc
= get_default_trace_class()
682 return tc
.create_structure_field_class(*args
, **kwargs
)
684 def _create_default_const_field_class(self
, *args
, **kwargs
):
685 tc
= get_default_trace_class()
686 fc
= tc
.create_structure_field_class(*args
, **kwargs
)
687 return _create_const_field_class(tc
, fc
, self
._const
_value
_setter
)
689 _create_default_field_class
= _create_field_class
691 def test_const_member_field_class(self
):
692 def _real_value_setter(field
):
693 field
.value
= {'real': 0}
695 tc
= get_default_trace_class()
696 fc
= tc
.create_structure_field_class()
697 member_fc
= self
._tc
.create_single_precision_real_field_class()
698 fc
.append_member('real', member_fc
)
699 const_fc
= _create_const_field_class(tc
, fc
, _real_value_setter
)
702 type(const_fc
['real'].field_class
),
703 bt2_field_class
._SinglePrecisionRealFieldClassConst
,
706 def test_member_field_class(self
):
707 tc
= get_default_trace_class()
708 fc
= tc
.create_structure_field_class()
709 member_fc
= self
._tc
.create_single_precision_real_field_class()
710 fc
.append_member('real', member_fc
)
713 type(fc
['real'].field_class
), bt2_field_class
._SinglePrecisionRealFieldClass
717 class OptionFieldClassTestCase(_TestFieldClass
, unittest
.TestCase
):
719 def _const_value_setter(field
):
720 field
.has_field
= True
723 def _create_default_field_class(self
, *args
, **kwargs
):
724 return self
._tc
.create_option_field_class(self
._content
_fc
, **kwargs
)
726 def _create_default_const_field_class(self
, *args
, **kwargs
):
727 fc
= self
._tc
.create_option_field_class(self
._content
_fc
, **kwargs
)
728 return _create_const_field_class(self
._tc
, fc
, self
._const
_value
_setter
)
731 self
._tc
= get_default_trace_class()
732 self
._content
_fc
= self
._tc
.create_signed_integer_field_class(23)
733 self
._tag
_fc
= self
._tc
.create_bool_field_class()
735 def test_create_default(self
):
736 fc
= self
._create
_default
_field
_class
()
737 self
.assertEqual(fc
.field_class
.addr
, self
._content
_fc
.addr
)
738 self
.assertIsNone(fc
.selector_field_path
, None)
739 self
.assertEqual(len(fc
.user_attributes
), 0)
741 def test_attr_field_class(self
):
742 fc
= self
._create
_default
_field
_class
()
743 self
.assertIs(type(fc
.field_class
), bt2_field_class
._SignedIntegerFieldClass
)
745 def test_const_attr_field_class(self
):
746 fc
= self
._create
_default
_const
_field
_class
()
748 type(fc
.field_class
), bt2_field_class
._SignedIntegerFieldClassConst
751 def _create_field_class_for_field_path_test(self
):
752 fc
= self
._create
_default
_field
_class
(selector_fc
=self
._tag
_fc
)
754 foo_fc
= self
._tc
.create_single_precision_real_field_class()
755 bar_fc
= self
._tc
.create_string_field_class()
756 baz_fc
= self
._tc
.create_string_field_class()
758 inner_struct_fc
= self
._tc
.create_structure_field_class()
759 inner_struct_fc
.append_member('bar', bar_fc
)
760 inner_struct_fc
.append_member('baz', baz_fc
)
761 inner_struct_fc
.append_member('tag', self
._tag
_fc
)
762 inner_struct_fc
.append_member('opt', fc
)
764 opt_struct_array_fc
= self
._tc
.create_option_field_class(inner_struct_fc
)
766 outer_struct_fc
= self
._tc
.create_structure_field_class()
767 outer_struct_fc
.append_member('foo', foo_fc
)
768 outer_struct_fc
.append_member('inner_opt', opt_struct_array_fc
)
770 # The path to the selector field class is resolved when the
771 # option field class is actually used, for example in a packet
773 self
._tc
.create_stream_class(
774 packet_context_field_class
=outer_struct_fc
, supports_packets
=True
779 def test_field_path_len(self
):
780 fc
= self
._create
_field
_class
_for
_field
_path
_test
()
781 self
.assertEqual(len(fc
.selector_field_path
), 3)
783 def test_field_path_iter(self
):
784 fc
= self
._create
_field
_class
_for
_field
_path
_test
()
785 path_items
= list(fc
.selector_field_path
)
787 self
.assertEqual(len(path_items
), 3)
789 self
.assertIsInstance(path_items
[0], bt2
._IndexFieldPathItem
)
790 self
.assertEqual(path_items
[0].index
, 1)
792 self
.assertIsInstance(path_items
[1], bt2
._CurrentOptionContentFieldPathItem
)
794 self
.assertIsInstance(path_items
[2], bt2
._IndexFieldPathItem
)
795 self
.assertEqual(path_items
[2].index
, 2)
797 def test_field_path_root_scope(self
):
798 fc
= self
._create
_field
_class
_for
_field
_path
_test
()
800 fc
.selector_field_path
.root_scope
, bt2
.FieldPathScope
.PACKET_CONTEXT
803 def test_create_invalid_field_class(self
):
804 with self
.assertRaises(TypeError):
805 self
._tc
.create_option_field_class(object())
807 def test_create_invalid_selector_type(self
):
808 with self
.assertRaises(TypeError):
809 self
._tc
.create_option_field_class(self
._content
_fc
, 17)
812 class VariantFieldClassWithoutSelectorTestCase(
813 _TestFieldClass
, _TestElementContainer
, unittest
.TestCase
815 _append_element_method
= staticmethod(
816 bt2
._VariantFieldClassWithoutSelector
.append_option
818 _at_index_method
= staticmethod(
819 bt2
._VariantFieldClassWithoutSelector
.option_at_index
823 def _const_value_setter(variant_field
):
824 variant_field
.selected_option_index
= 0
825 variant_field
.value
= 12
827 def _create_field_class(self
, *args
, **kwargs
):
828 tc
= get_default_trace_class()
829 return tc
.create_variant_field_class(*args
, **kwargs
)
831 def _create_default_const_field_class(self
, *args
, **kwargs
):
832 tc
= get_default_trace_class()
833 fc
= tc
.create_variant_field_class(*args
, **kwargs
)
834 int_field_class
= self
._tc
.create_signed_integer_field_class(32)
835 fc
.append_option('int32', int_field_class
)
837 return _create_const_field_class(tc
, fc
, self
._const
_value
_setter
)
839 _create_default_field_class
= _create_field_class
842 class _VariantFieldClassWithSelectorTestCase
:
844 def _const_value_setter(field
):
845 field
['variant'].selected_option_index
= 0
846 field
['variant'] = 12
848 def _create_default_field_class(self
, *args
, **kwargs
):
849 return self
._tc
.create_variant_field_class(
850 *args
, selector_fc
=self
._selector
_fc
, **kwargs
853 def _create_default_const_field_class(self
, *args
, **kwargs
):
854 # Create a struct to contain the variant and its selector else we can't
855 # create the non-const field necessary to get the the const field_class
856 struct_fc
= self
._tc
.create_structure_field_class()
857 struct_fc
.append_member('selecteux', self
._selector
_fc
)
858 variant_fc
= self
._tc
.create_variant_field_class(
859 *args
, selector_fc
=self
._selector
_fc
861 variant_fc
.append_option(
862 'a', self
._tc
.create_signed_integer_field_class(32), self
._ranges
1
864 struct_fc
.append_member('variant', variant_fc
, **kwargs
)
866 return _create_const_field_class(self
._tc
, struct_fc
, self
._const
_value
_setter
)[
871 self
._tc
= get_default_trace_class()
873 self
._fc
= self
._create
_default
_field
_class
()
875 def test_create_default(self
):
876 self
.assertIsNotNone(self
._fc
)
877 self
.assertEqual(len(self
._fc
.user_attributes
), 0)
879 def test_append_element(self
):
880 str_field_class
= self
._tc
.create_string_field_class()
881 self
._fc
.append_option('str', str_field_class
, self
._ranges
1)
882 opt
= self
._fc
['str']
883 self
.assertEqual(opt
.field_class
.addr
, str_field_class
.addr
)
884 self
.assertEqual(opt
.name
, 'str')
885 self
.assertEqual(opt
.ranges
.addr
, self
._ranges
1.addr
)
887 def test_const_append(self
):
888 fc_const
= self
._create
_default
_const
_field
_class
()
889 str_field_class
= self
._tc
.create_string_field_class()
890 with self
.assertRaises(AttributeError):
891 fc_const
.append_option('str', str_field_class
, self
._ranges
1)
893 def test_append_element_kwargs(self
):
894 int_field_class
= self
._tc
.create_signed_integer_field_class(32)
895 self
._fc
.append_option(
896 name
='int32', field_class
=int_field_class
, ranges
=self
._ranges
1
898 opt
= self
._fc
['int32']
899 self
.assertEqual(opt
.field_class
.addr
, int_field_class
.addr
)
900 self
.assertEqual(opt
.name
, 'int32')
901 self
.assertEqual(opt
.ranges
.addr
, self
._ranges
1.addr
)
903 def test_append_element_invalid_name(self
):
904 sub_fc
= self
._tc
.create_string_field_class()
906 with self
.assertRaises(TypeError):
907 self
._fc
.append_option(self
._fc
, 23, sub_fc
)
909 def test_append_element_invalid_field_class(self
):
910 with self
.assertRaises(TypeError):
911 self
._fc
.append_option(self
._fc
, 'yes', object())
913 def test_append_element_invalid_ranges(self
):
914 sub_fc
= self
._tc
.create_string_field_class()
916 with self
.assertRaises(TypeError):
917 self
._fc
.append_option(self
._fc
, sub_fc
, 'lel')
919 def test_append_element_dup_name(self
):
920 sub_fc1
= self
._tc
.create_string_field_class()
921 sub_fc2
= self
._tc
.create_string_field_class()
923 with self
.assertRaises(ValueError):
924 self
._fc
.append_option('yes', sub_fc1
, self
._ranges
1)
925 self
._fc
.append_option('yes', sub_fc2
, self
._ranges
2)
927 def test_append_element_invalid_ranges_signedness(self
):
928 sub_fc
= self
._tc
.create_string_field_class()
930 with self
.assertRaises(TypeError):
931 self
._fc
.append_option(self
._fc
, sub_fc
, self
._inval
_ranges
)
933 def test_user_attributes(self
):
934 self
._fc
.append_option(
936 self
._tc
.create_string_field_class(),
938 user_attributes
={'salut': 23},
940 self
.assertEqual(self
._fc
['c'].user_attributes
, {'salut': 23})
941 self
.assertIs(type(self
._fc
.user_attributes
), bt2_value
.MapValue
)
943 def test_const_user_attributes(self
):
944 fc_const
= self
._create
_default
_const
_field
_class
()
945 self
.assertIs(type(fc_const
.user_attributes
), bt2_value
._MapValueConst
)
947 def test_invalid_user_attributes(self
):
948 with self
.assertRaises(TypeError):
949 self
._fc
.append_option(
951 self
._tc
.create_string_field_class(),
953 user_attributes
=object(),
956 def test_invalid_user_attributes_value_type(self
):
957 with self
.assertRaises(TypeError):
958 self
._fc
.append_option(
960 self
._tc
.create_string_field_class(),
966 a_field_class
= self
._tc
.create_single_precision_real_field_class()
967 self
._fc
.append_option('a_float', a_field_class
, self
._ranges
1)
968 c_field_class
= self
._tc
.create_string_field_class()
969 d_field_class
= self
._tc
.create_signed_enumeration_field_class(
973 ('c_string', c_field_class
, self
._ranges
2),
974 ('d_enum', d_field_class
, self
._ranges
3),
976 self
.assertEqual(self
._fc
['a_float'].field_class
.addr
, a_field_class
.addr
)
977 self
.assertEqual(self
._fc
['a_float'].name
, 'a_float')
978 self
.assertEqual(self
._fc
['a_float'].ranges
, self
._ranges
1)
979 self
.assertEqual(self
._fc
['c_string'].field_class
.addr
, c_field_class
.addr
)
980 self
.assertEqual(self
._fc
['c_string'].name
, 'c_string')
981 self
.assertEqual(self
._fc
['c_string'].ranges
, self
._ranges
2)
982 self
.assertEqual(self
._fc
['d_enum'].field_class
.addr
, d_field_class
.addr
)
983 self
.assertEqual(self
._fc
['d_enum'].name
, 'd_enum')
984 self
.assertEqual(self
._fc
['d_enum'].ranges
, self
._ranges
3)
986 def test_const_iadd(self
):
987 fc_const
= self
._create
_default
_const
_field
_class
()
988 a_field_class
= self
._tc
.create_single_precision_real_field_class()
989 with self
.assertRaises(TypeError):
990 fc_const
+= [('a_float', a_field_class
, self
._ranges
1)]
992 def test_bool_op(self
):
993 self
.assertFalse(self
._fc
)
994 self
._fc
.append_option('a', self
._tc
.create_string_field_class(), self
._ranges
1)
995 self
.assertTrue(self
._fc
)
998 self
._fc
.append_option('a', self
._tc
.create_string_field_class(), self
._ranges
1)
999 self
._fc
.append_option('b', self
._tc
.create_string_field_class(), self
._ranges
2)
1000 self
._fc
.append_option('c', self
._tc
.create_string_field_class(), self
._ranges
3)
1001 self
.assertEqual(len(self
._fc
), 3)
1003 def test_getitem(self
):
1004 a_fc
= self
._tc
.create_signed_integer_field_class(32)
1005 b_fc
= self
._tc
.create_string_field_class()
1006 c_fc
= self
._tc
.create_single_precision_real_field_class()
1007 self
._fc
.append_option('a', a_fc
, self
._ranges
1)
1008 self
._fc
.append_option('b', b_fc
, self
._ranges
2)
1009 self
._fc
.append_option('c', c_fc
, self
._ranges
3)
1010 self
.assertEqual(self
._fc
['b'].field_class
.addr
, b_fc
.addr
)
1011 self
.assertEqual(self
._fc
['b'].name
, 'b')
1012 self
.assertEqual(self
._fc
['b'].ranges
.addr
, self
._ranges
2.addr
)
1014 def test_option_field_class(self
):
1015 a_fc
= self
._tc
.create_signed_integer_field_class(32)
1016 self
._fc
.append_option('a', a_fc
, self
._ranges
1)
1018 type(self
._fc
['a'].field_class
), bt2_field_class
._SignedIntegerFieldClass
1021 def test_const_option_field_class(self
):
1022 fc_const
= self
._create
_default
_const
_field
_class
()
1024 type(fc_const
['a'].field_class
),
1025 bt2_field_class
._SignedIntegerFieldClassConst
,
1028 def test_getitem_invalid_key_type(self
):
1029 with self
.assertRaises(TypeError):
1032 def test_getitem_invalid_key(self
):
1033 with self
.assertRaises(KeyError):
1036 def test_contains(self
):
1037 self
.assertFalse('a' in self
._fc
)
1038 self
._fc
.append_option('a', self
._tc
.create_string_field_class(), self
._ranges
1)
1039 self
.assertTrue('a' in self
._fc
)
1041 def test_iter(self
):
1042 a_fc
= self
._tc
.create_signed_integer_field_class(32)
1043 b_fc
= self
._tc
.create_string_field_class()
1044 c_fc
= self
._tc
.create_single_precision_real_field_class()
1046 ('a', a_fc
, self
._ranges
1),
1047 ('b', b_fc
, self
._ranges
2),
1048 ('c', c_fc
, self
._ranges
3),
1052 self
._fc
.append_option(*opt
)
1054 for (name
, opt
), test_opt
in zip(self
._fc
.items(), opts
):
1055 self
.assertEqual(opt
.name
, test_opt
[0])
1056 self
.assertEqual(name
, opt
.name
)
1057 self
.assertEqual(opt
.field_class
.addr
, test_opt
[1].addr
)
1058 self
.assertEqual(opt
.ranges
.addr
, test_opt
[2].addr
)
1060 def test_at_index(self
):
1061 a_fc
= self
._tc
.create_signed_integer_field_class(32)
1062 b_fc
= self
._tc
.create_string_field_class()
1063 c_fc
= self
._tc
.create_single_precision_real_field_class()
1064 self
._fc
.append_option('c', c_fc
, self
._ranges
1)
1065 self
._fc
.append_option('a', a_fc
, self
._ranges
2)
1066 self
._fc
.append_option('b', b_fc
, self
._ranges
3)
1067 self
.assertEqual(self
._fc
.option_at_index(1).field_class
.addr
, a_fc
.addr
)
1068 self
.assertEqual(self
._fc
.option_at_index(1).name
, 'a')
1069 self
.assertEqual(self
._fc
.option_at_index(1).ranges
.addr
, self
._ranges
2.addr
)
1071 def test_at_index_invalid(self
):
1072 self
._fc
.append_option(
1073 'c', self
._tc
.create_signed_integer_field_class(32), self
._ranges
3
1076 with self
.assertRaises(TypeError):
1077 self
._fc
.option_at_index('yes')
1079 def test_at_index_out_of_bounds_after(self
):
1080 self
._fc
.append_option(
1081 'c', self
._tc
.create_signed_integer_field_class(32), self
._ranges
3
1084 with self
.assertRaises(IndexError):
1085 self
._fc
.option_at_index(len(self
._fc
))
1087 def _fill_default_fc_for_field_path_test(self
):
1088 # Create something equivalent to:
1090 # struct outer_struct_fc {
1092 # struct inner_struct_fc {
1093 # [u]int64_t selector;
1096 # variant <selector> {
1097 # real a; // selected with self._ranges1
1098 # int21_t b; // selected with self._ranges2
1099 # uint34_t c; // selected with self._ranges3
1101 # } inner_struct[2];
1103 self
._fc
.append_option(
1104 'a', self
._tc
.create_single_precision_real_field_class(), self
._ranges
1
1106 self
._fc
.append_option(
1107 'b', self
._tc
.create_signed_integer_field_class(21), self
._ranges
2
1109 self
._fc
.append_option(
1110 'c', self
._tc
.create_unsigned_integer_field_class(34), self
._ranges
3
1113 foo_fc
= self
._tc
.create_single_precision_real_field_class()
1114 bar_fc
= self
._tc
.create_string_field_class()
1115 baz_fc
= self
._tc
.create_string_field_class()
1117 inner_struct_fc
= self
._tc
.create_structure_field_class()
1118 inner_struct_fc
.append_member('selector', self
._selector
_fc
)
1119 inner_struct_fc
.append_member('bar', bar_fc
)
1120 inner_struct_fc
.append_member('baz', baz_fc
)
1121 inner_struct_fc
.append_member('variant', self
._fc
)
1123 inner_struct_array_fc
= self
._tc
.create_static_array_field_class(
1127 outer_struct_fc
= self
._tc
.create_structure_field_class()
1128 outer_struct_fc
.append_member('foo', foo_fc
)
1129 outer_struct_fc
.append_member('inner_struct', inner_struct_array_fc
)
1131 # The path to the selector field is resolved when the sequence is
1132 # actually used, for example in a packet context.
1133 self
._tc
.create_stream_class(
1134 supports_packets
=True, packet_context_field_class
=outer_struct_fc
1137 def test_selector_field_path_length(self
):
1138 self
._fill
_default
_fc
_for
_field
_path
_test
()
1139 self
.assertEqual(len(self
._fc
.selector_field_path
), 3)
1141 def test_selector_field_path_iter(self
):
1142 self
._fill
_default
_fc
_for
_field
_path
_test
()
1143 path_items
= list(self
._fc
.selector_field_path
)
1145 self
.assertEqual(len(path_items
), 3)
1147 self
.assertIsInstance(path_items
[0], bt2
._IndexFieldPathItem
)
1148 self
.assertEqual(path_items
[0].index
, 1)
1150 self
.assertIsInstance(path_items
[1], bt2
._CurrentArrayElementFieldPathItem
)
1152 self
.assertIsInstance(path_items
[2], bt2
._IndexFieldPathItem
)
1153 self
.assertEqual(path_items
[2].index
, 0)
1155 def test_selector_field_path_root_scope(self
):
1156 self
._fill
_default
_fc
_for
_field
_path
_test
()
1158 self
._fc
.selector_field_path
.root_scope
, bt2
.FieldPathScope
.PACKET_CONTEXT
1162 class VariantFieldClassWithUnsignedSelectorTestCase(
1163 _VariantFieldClassWithSelectorTestCase
, unittest
.TestCase
1165 def _spec_set_up(self
):
1166 self
._ranges
1 = bt2
.UnsignedIntegerRangeSet([(1, 4), (18, 47)])
1167 self
._ranges
2 = bt2
.UnsignedIntegerRangeSet([(5, 5)])
1168 self
._ranges
3 = bt2
.UnsignedIntegerRangeSet([(8, 16), (48, 99)])
1169 self
._inval
_ranges
= bt2
.SignedIntegerRangeSet([(-8, 16), (48, 99)])
1170 self
._selector
_fc
= self
._tc
.create_unsigned_integer_field_class()
1173 class VariantFieldClassWithSignedSelectorTestCase(
1174 _VariantFieldClassWithSelectorTestCase
, unittest
.TestCase
1176 def _spec_set_up(self
):
1177 self
._ranges
1 = bt2
.SignedIntegerRangeSet([(-10, -4), (18, 47)])
1178 self
._ranges
2 = bt2
.SignedIntegerRangeSet([(-3, -3)])
1179 self
._ranges
3 = bt2
.SignedIntegerRangeSet([(8, 16), (48, 99)])
1180 self
._inval
_ranges
= bt2
.UnsignedIntegerRangeSet([(8, 16), (48, 99)])
1181 self
._selector
_fc
= self
._tc
.create_signed_integer_field_class()
1184 class _ArrayFieldClassTestCase
:
1185 def test_attr_element_field_class(self
):
1186 fc
= self
._create
_array
()
1188 type(fc
.element_field_class
), bt2_field_class
._SignedIntegerFieldClass
1191 def test_const_attr_element_field_class(self
):
1192 fc
= self
._create
_const
_array
()
1194 type(fc
.element_field_class
), bt2_field_class
._SignedIntegerFieldClassConst
1198 class StaticArrayFieldClassTestCase(_ArrayFieldClassTestCase
, unittest
.TestCase
):
1200 def _const_value_setter(field
):
1201 field
.value
= [9] * 45
1203 def _create_array(self
):
1204 return self
._tc
.create_static_array_field_class(self
._elem
_fc
, 45)
1206 def _create_const_array(self
):
1207 fc
= self
._tc
.create_static_array_field_class(self
._elem
_fc
, 45)
1208 return _create_const_field_class(self
._tc
, fc
, self
._const
_value
_setter
)
1211 self
._tc
= get_default_trace_class()
1212 self
._elem
_fc
= self
._tc
.create_signed_integer_field_class(23)
1214 def test_create_default(self
):
1215 fc
= self
._tc
.create_static_array_field_class(self
._elem
_fc
, 45)
1216 self
.assertEqual(fc
.element_field_class
.addr
, self
._elem
_fc
.addr
)
1217 self
.assertEqual(fc
.length
, 45)
1218 self
.assertEqual(len(fc
.user_attributes
), 0)
1220 def test_create_invalid_elem_field_class(self
):
1221 with self
.assertRaises(TypeError):
1222 self
._tc
.create_static_array_field_class(object(), 45)
1224 def test_create_invalid_length(self
):
1225 with self
.assertRaises(ValueError):
1226 self
._tc
.create_static_array_field_class(
1227 self
._tc
.create_string_field_class(), -17
1230 def test_create_invalid_length_type(self
):
1231 with self
.assertRaises(TypeError):
1232 self
._tc
.create_static_array_field_class(
1233 self
._tc
.create_string_field_class(), 'the length'
1237 class DynamicArrayFieldClassTestCase(_ArrayFieldClassTestCase
, unittest
.TestCase
):
1239 def _const_value_setter(field
):
1242 def _create_array(self
):
1243 return self
._tc
.create_dynamic_array_field_class(self
._elem
_fc
)
1245 def _create_const_array(self
):
1246 fc
= self
._tc
.create_dynamic_array_field_class(self
._elem
_fc
)
1247 return _create_const_field_class(self
._tc
, fc
, self
._const
_value
_setter
)
1250 self
._tc
= get_default_trace_class()
1251 self
._elem
_fc
= self
._tc
.create_signed_integer_field_class(23)
1252 self
._len
_fc
= self
._tc
.create_unsigned_integer_field_class(12)
1254 def test_create_default(self
):
1255 fc
= self
._tc
.create_dynamic_array_field_class(self
._elem
_fc
)
1256 self
.assertEqual(fc
.element_field_class
.addr
, self
._elem
_fc
.addr
)
1257 self
.assertIsNone(fc
.length_field_path
, None)
1258 self
.assertEqual(len(fc
.user_attributes
), 0)
1260 def _create_field_class_for_field_path_test(self
):
1261 # Create something a field class that is equivalent to:
1263 # struct outer_struct_fc {
1265 # struct inner_struct_fc {
1269 # uint23_t dyn_array[len];
1270 # } inner_struct[2];
1273 fc
= self
._tc
.create_dynamic_array_field_class(self
._elem
_fc
, self
._len
_fc
)
1275 foo_fc
= self
._tc
.create_single_precision_real_field_class()
1276 bar_fc
= self
._tc
.create_string_field_class()
1277 baz_fc
= self
._tc
.create_string_field_class()
1279 inner_struct_fc
= self
._tc
.create_structure_field_class()
1280 inner_struct_fc
.append_member('bar', bar_fc
)
1281 inner_struct_fc
.append_member('baz', baz_fc
)
1282 inner_struct_fc
.append_member('len', self
._len
_fc
)
1283 inner_struct_fc
.append_member('dyn_array', fc
)
1285 inner_struct_array_fc
= self
._tc
.create_static_array_field_class(
1289 outer_struct_fc
= self
._tc
.create_structure_field_class()
1290 outer_struct_fc
.append_member('foo', foo_fc
)
1291 outer_struct_fc
.append_member('inner_struct', inner_struct_array_fc
)
1293 # The path to the length field is resolved when the sequence is
1294 # actually used, for example in a packet context.
1295 self
._tc
.create_stream_class(
1296 packet_context_field_class
=outer_struct_fc
, supports_packets
=True
1301 def test_field_path_len(self
):
1302 fc
= self
._create
_field
_class
_for
_field
_path
_test
()
1303 self
.assertEqual(len(fc
.length_field_path
), 3)
1305 def test_field_path_iter(self
):
1306 fc
= self
._create
_field
_class
_for
_field
_path
_test
()
1307 path_items
= list(fc
.length_field_path
)
1309 self
.assertEqual(len(path_items
), 3)
1311 self
.assertIsInstance(path_items
[0], bt2
._IndexFieldPathItem
)
1312 self
.assertEqual(path_items
[0].index
, 1)
1314 self
.assertIsInstance(path_items
[1], bt2
._CurrentArrayElementFieldPathItem
)
1316 self
.assertIsInstance(path_items
[2], bt2
._IndexFieldPathItem
)
1317 self
.assertEqual(path_items
[2].index
, 2)
1319 def test_field_path_root_scope(self
):
1320 fc
= self
._create
_field
_class
_for
_field
_path
_test
()
1322 fc
.length_field_path
.root_scope
, bt2
.FieldPathScope
.PACKET_CONTEXT
1325 def test_create_invalid_field_class(self
):
1326 with self
.assertRaises(TypeError):
1327 self
._tc
.create_dynamic_array_field_class(object())
1329 def test_create_invalid_length_type(self
):
1330 with self
.assertRaises(TypeError):
1331 self
._tc
.create_dynamic_array_field_class(
1332 self
._tc
.create_string_field_class(), 17
1336 if __name__
== "__main__":