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
24 class _TestIntegerFieldClassProps
:
25 def test_create_default(self
):
26 fc
= self
._create
_func
()
27 self
.assertEqual(fc
.field_value_range
, 64)
28 self
.assertEqual(fc
.preferred_display_base
, bt2
.IntegerDisplayBase
.DECIMAL
)
30 def test_create_range(self
):
31 fc
= self
._create
_func
(field_value_range
=35)
32 self
.assertEqual(fc
.field_value_range
, 35)
34 fc
= self
._create
_func
(36)
35 self
.assertEqual(fc
.field_value_range
, 36)
37 def test_create_invalid_range(self
):
38 with self
.assertRaises(TypeError):
39 self
._create
_func
('yes')
41 with self
.assertRaises(TypeError):
42 self
._create
_func
(field_value_range
='yes')
44 with self
.assertRaises(ValueError):
45 self
._create
_func
(field_value_range
=-2)
47 with self
.assertRaises(ValueError):
48 self
._create
_func
(field_value_range
=0)
50 def test_create_base(self
):
51 fc
= self
._create
_func
(
52 preferred_display_base
=bt2
.IntegerDisplayBase
.HEXADECIMAL
54 self
.assertEqual(fc
.preferred_display_base
, bt2
.IntegerDisplayBase
.HEXADECIMAL
)
56 def test_create_invalid_base_type(self
):
57 with self
.assertRaises(TypeError):
58 self
._create
_func
(preferred_display_base
='yes')
60 def test_create_invalid_base_value(self
):
61 with self
.assertRaises(ValueError):
62 self
._create
_func
(preferred_display_base
=444)
64 def test_create_full(self
):
65 fc
= self
._create
_func
(24, preferred_display_base
=bt2
.IntegerDisplayBase
.OCTAL
)
66 self
.assertEqual(fc
.field_value_range
, 24)
67 self
.assertEqual(fc
.preferred_display_base
, bt2
.IntegerDisplayBase
.OCTAL
)
70 class IntegerFieldClassTestCase(_TestIntegerFieldClassProps
, unittest
.TestCase
):
72 self
._tc
= get_default_trace_class()
73 self
._create
_func
= self
._tc
.create_signed_integer_field_class
76 class RealFieldClassTestCase(unittest
.TestCase
):
78 self
._tc
= get_default_trace_class()
80 def test_create_default(self
):
81 fc
= self
._tc
.create_real_field_class()
82 self
.assertFalse(fc
.is_single_precision
)
84 def test_create_is_single_precision(self
):
85 fc
= self
._tc
.create_real_field_class(is_single_precision
=True)
86 self
.assertTrue(fc
.is_single_precision
)
88 def test_create_invalid_is_single_precision(self
):
89 with self
.assertRaises(TypeError):
90 self
._tc
.create_real_field_class(is_single_precision
='hohoho')
93 # Converts an _EnumerationFieldClassMapping to a list of ranges:
95 # [(lower0, upper0), (lower1, upper1), ...]
98 def enum_mapping_to_set(mapping
):
99 return {(x
.lower
, x
.upper
) for x
in mapping
.ranges
}
102 class _EnumerationFieldClassTestCase(_TestIntegerFieldClassProps
):
104 self
._tc
= get_default_trace_class()
106 self
._fc
= self
._create
_func
()
108 def test_create_from_invalid_type(self
):
109 with self
.assertRaises(TypeError):
110 self
._create
_func
('coucou')
112 def test_add_mapping_simple(self
):
113 self
._fc
.add_mapping('hello', self
._ranges
1)
114 mapping
= self
._fc
['hello']
115 self
.assertEqual(mapping
.label
, 'hello')
116 self
.assertEqual(mapping
.ranges
, self
._ranges
1)
118 def test_add_mapping_simple_kwargs(self
):
119 self
._fc
.add_mapping(label
='hello', ranges
=self
._ranges
1)
120 mapping
= self
._fc
['hello']
121 self
.assertEqual(mapping
.label
, 'hello')
122 self
.assertEqual(mapping
.ranges
, self
._ranges
1)
124 def test_add_mapping_invalid_name(self
):
125 with self
.assertRaises(TypeError):
126 self
._fc
.add_mapping(17, self
._ranges
1)
128 def test_add_mapping_invalid_range(self
):
129 with self
.assertRaises(TypeError):
130 self
._fc
.add_mapping('allo', 'meow')
132 def test_add_mapping_dup_label(self
):
133 with self
.assertRaises(ValueError):
134 self
._fc
.add_mapping('a', self
._ranges
1)
135 self
._fc
.add_mapping('a', self
._ranges
2)
137 def test_add_mapping_invalid_ranges_signedness(self
):
138 with self
.assertRaises(TypeError):
139 self
._fc
.add_mapping('allo', self
._inval
_ranges
)
142 self
._fc
.add_mapping('c', self
._ranges
1)
144 self
._fc
+= [('d', self
._ranges
2), ('e', self
._ranges
3)]
146 self
.assertEqual(len(self
._fc
), 3)
147 self
.assertEqual(self
._fc
['c'].label
, 'c')
148 self
.assertEqual(self
._fc
['c'].ranges
, self
._ranges
1)
149 self
.assertEqual(self
._fc
['d'].label
, 'd')
150 self
.assertEqual(self
._fc
['d'].ranges
, self
._ranges
2)
151 self
.assertEqual(self
._fc
['e'].label
, 'e')
152 self
.assertEqual(self
._fc
['e'].ranges
, self
._ranges
3)
154 def test_bool_op(self
):
155 self
.assertFalse(self
._fc
)
156 self
._fc
.add_mapping('a', self
._ranges
1)
157 self
.assertTrue(self
._fc
)
160 self
._fc
.add_mapping('a', self
._ranges
1)
161 self
._fc
.add_mapping('b', self
._ranges
2)
162 self
._fc
.add_mapping('c', self
._ranges
3)
163 self
.assertEqual(len(self
._fc
), 3)
165 def test_getitem(self
):
166 self
._fc
.add_mapping('a', self
._ranges
1)
167 self
._fc
.add_mapping('b', self
._ranges
2)
168 self
._fc
.add_mapping('c', self
._ranges
3)
169 mapping
= self
._fc
['a']
170 self
.assertEqual(mapping
.label
, 'a')
171 self
.assertEqual(mapping
.ranges
, self
._ranges
1)
173 def test_getitem_nonexistent(self
):
174 with self
.assertRaises(KeyError):
175 self
._fc
['doesnotexist']
178 self
._fc
.add_mapping('a', self
._ranges
1)
179 self
._fc
.add_mapping('b', self
._ranges
2)
180 self
._fc
.add_mapping('c', self
._ranges
3)
182 # This exercises iteration.
183 labels
= sorted(self
._fc
)
185 self
.assertEqual(labels
, ['a', 'b', 'c'])
187 def test_find_by_value(self
):
188 self
._fc
.add_mapping('a', self
._ranges
1)
189 self
._fc
.add_mapping('b', self
._ranges
2)
190 self
._fc
.add_mapping('c', self
._ranges
3)
191 mappings
= self
._fc
.mappings_for_value(self
._value
_in
_range
_1_and
_3)
192 labels
= set([mapping
.label
for mapping
in mappings
])
193 expected_labels
= set(['a', 'c'])
194 self
.assertEqual(labels
, expected_labels
)
197 class UnsignedEnumerationFieldClassTestCase(
198 _EnumerationFieldClassTestCase
, unittest
.TestCase
200 def _spec_set_up(self
):
201 self
._ranges
1 = bt2
.UnsignedIntegerRangeSet([(1, 4), (18, 47)])
202 self
._ranges
2 = bt2
.UnsignedIntegerRangeSet([(5, 5)])
203 self
._ranges
3 = bt2
.UnsignedIntegerRangeSet([(8, 22), (48, 99)])
204 self
._inval
_ranges
= bt2
.SignedIntegerRangeSet([(-8, -5), (48, 1928)])
205 self
._value
_in
_range
_1_and
_3 = 20
206 self
._create
_func
= self
._tc
.create_unsigned_enumeration_field_class
209 class SignedEnumerationFieldClassTestCase(
210 _EnumerationFieldClassTestCase
, unittest
.TestCase
212 def _spec_set_up(self
):
213 self
._ranges
1 = bt2
.SignedIntegerRangeSet([(-10, -4), (18, 47)])
214 self
._ranges
2 = bt2
.SignedIntegerRangeSet([(-3, -3)])
215 self
._ranges
3 = bt2
.SignedIntegerRangeSet([(-100, -1), (8, 16), (48, 99)])
216 self
._inval
_ranges
= bt2
.UnsignedIntegerRangeSet([(8, 16), (48, 99)])
217 self
._value
_in
_range
_1_and
_3 = -7
218 self
._create
_func
= self
._tc
.create_signed_enumeration_field_class
221 class StringFieldClassTestCase(unittest
.TestCase
):
223 tc
= get_default_trace_class()
224 self
._fc
= tc
.create_string_field_class()
226 def test_create_default(self
):
227 self
.assertIsNotNone(self
._fc
)
230 class _TestElementContainer
:
232 self
._tc
= get_default_trace_class()
233 self
._fc
= self
._create
_default
_fc
()
235 def test_create_default(self
):
236 self
.assertIsNotNone(self
._fc
)
238 def test_append_element(self
):
239 int_field_class
= self
._tc
.create_signed_integer_field_class(32)
240 self
._append
_element
_method
(self
._fc
, 'int32', int_field_class
)
241 field_class
= self
._fc
['int32'].field_class
242 self
.assertEqual(field_class
.addr
, int_field_class
.addr
)
244 def test_append_element_kwargs(self
):
245 int_field_class
= self
._tc
.create_signed_integer_field_class(32)
246 self
._append
_element
_method
(self
._fc
, name
='int32', field_class
=int_field_class
)
247 field_class
= self
._fc
['int32'].field_class
248 self
.assertEqual(field_class
.addr
, int_field_class
.addr
)
250 def test_append_element_invalid_name(self
):
251 sub_fc
= self
._tc
.create_string_field_class()
253 with self
.assertRaises(TypeError):
254 self
._append
_element
_method
(self
._fc
, 23, sub_fc
)
256 def test_append_element_invalid_field_class(self
):
257 with self
.assertRaises(TypeError):
258 self
._append
_element
_method
(self
._fc
, 'yes', object())
260 def test_append_element_dup_name(self
):
261 sub_fc1
= self
._tc
.create_string_field_class()
262 sub_fc2
= self
._tc
.create_string_field_class()
264 with self
.assertRaises(ValueError):
265 self
._append
_element
_method
(self
._fc
, 'yes', sub_fc1
)
266 self
._append
_element
_method
(self
._fc
, 'yes', sub_fc2
)
269 other_fc
= self
._create
_default
_fc
()
270 a_field_class
= self
._tc
.create_real_field_class()
271 b_field_class
= self
._tc
.create_signed_integer_field_class(17)
272 self
._append
_element
_method
(self
._fc
, 'a_float', a_field_class
)
273 self
._append
_element
_method
(self
._fc
, 'b_int', b_field_class
)
274 c_field_class
= self
._tc
.create_string_field_class()
275 d_field_class
= self
._tc
.create_signed_enumeration_field_class(
278 e_field_class
= self
._tc
.create_structure_field_class()
280 ('c_string', c_field_class
),
281 ('d_enum', d_field_class
),
282 ('e_struct', e_field_class
),
284 self
.assertEqual(self
._fc
['a_float'].field_class
.addr
, a_field_class
.addr
)
285 self
.assertEqual(self
._fc
['a_float'].name
, 'a_float')
286 self
.assertEqual(self
._fc
['b_int'].field_class
.addr
, b_field_class
.addr
)
287 self
.assertEqual(self
._fc
['b_int'].name
, 'b_int')
288 self
.assertEqual(self
._fc
['c_string'].field_class
.addr
, c_field_class
.addr
)
289 self
.assertEqual(self
._fc
['c_string'].name
, 'c_string')
290 self
.assertEqual(self
._fc
['d_enum'].field_class
.addr
, d_field_class
.addr
)
291 self
.assertEqual(self
._fc
['d_enum'].name
, 'd_enum')
292 self
.assertEqual(self
._fc
['e_struct'].field_class
.addr
, e_field_class
.addr
)
293 self
.assertEqual(self
._fc
['e_struct'].name
, 'e_struct')
295 def test_bool_op(self
):
296 self
.assertFalse(self
._fc
)
297 self
._append
_element
_method
(self
._fc
, 'a', self
._tc
.create_string_field_class())
298 self
.assertTrue(self
._fc
)
301 self
._append
_element
_method
(self
._fc
, 'a', self
._tc
.create_string_field_class())
302 self
._append
_element
_method
(self
._fc
, 'b', self
._tc
.create_string_field_class())
303 self
._append
_element
_method
(self
._fc
, 'c', self
._tc
.create_string_field_class())
304 self
.assertEqual(len(self
._fc
), 3)
306 def test_getitem(self
):
307 a_fc
= self
._tc
.create_signed_integer_field_class(32)
308 b_fc
= self
._tc
.create_string_field_class()
309 c_fc
= self
._tc
.create_real_field_class()
310 self
._append
_element
_method
(self
._fc
, 'a', a_fc
)
311 self
._append
_element
_method
(self
._fc
, 'b', b_fc
)
312 self
._append
_element
_method
(self
._fc
, 'c', c_fc
)
313 self
.assertEqual(self
._fc
['b'].field_class
.addr
, b_fc
.addr
)
314 self
.assertEqual(self
._fc
['b'].name
, 'b')
316 def test_getitem_invalid_key_type(self
):
317 with self
.assertRaises(TypeError):
320 def test_getitem_invalid_key(self
):
321 with self
.assertRaises(KeyError):
324 def test_contains(self
):
325 self
.assertFalse('a' in self
._fc
)
326 self
._append
_element
_method
(self
._fc
, 'a', self
._tc
.create_string_field_class())
327 self
.assertTrue('a' in self
._fc
)
330 a_fc
= self
._tc
.create_signed_integer_field_class(32)
331 b_fc
= self
._tc
.create_string_field_class()
332 c_fc
= self
._tc
.create_real_field_class()
333 elements
= (('a', a_fc
), ('b', b_fc
), ('c', c_fc
))
335 for elem
in elements
:
336 self
._append
_element
_method
(self
._fc
, *elem
)
338 for (name
, element
), test_elem
in zip(self
._fc
.items(), elements
):
339 self
.assertEqual(element
.name
, test_elem
[0])
340 self
.assertEqual(name
, element
.name
)
341 self
.assertEqual(element
.field_class
.addr
, test_elem
[1].addr
)
343 def test_at_index(self
):
344 a_fc
= self
._tc
.create_signed_integer_field_class(32)
345 b_fc
= self
._tc
.create_string_field_class()
346 c_fc
= self
._tc
.create_real_field_class()
347 self
._append
_element
_method
(self
._fc
, 'c', c_fc
)
348 self
._append
_element
_method
(self
._fc
, 'a', a_fc
)
349 self
._append
_element
_method
(self
._fc
, 'b', b_fc
)
350 elem
= self
._at
_index
_method
(self
._fc
, 1)
351 self
.assertEqual(elem
.field_class
.addr
, a_fc
.addr
)
352 self
.assertEqual(elem
.name
, 'a')
354 def test_at_index_invalid(self
):
355 self
._append
_element
_method
(
356 self
._fc
, 'c', self
._tc
.create_signed_integer_field_class(32)
359 with self
.assertRaises(TypeError):
360 self
._at
_index
_method
(self
._fc
, 'yes')
362 def test_at_index_out_of_bounds_after(self
):
363 self
._append
_element
_method
(
364 self
._fc
, 'c', self
._tc
.create_signed_integer_field_class(32)
367 with self
.assertRaises(IndexError):
368 self
._at
_index
_method
(self
._fc
, len(self
._fc
))
371 class StructureFieldClassTestCase(_TestElementContainer
, unittest
.TestCase
):
372 _append_element_method
= staticmethod(bt2
._StructureFieldClass
.append_member
)
373 _at_index_method
= staticmethod(bt2
._StructureFieldClass
.member_at_index
)
375 def _create_default_fc(self
):
376 return self
._tc
.create_structure_field_class()
379 class VariantFieldClassWithoutSelectorTestCase(
380 _TestElementContainer
, unittest
.TestCase
382 _append_element_method
= staticmethod(
383 bt2
._VariantFieldClassWithoutSelector
.append_option
385 _at_index_method
= staticmethod(
386 bt2
._VariantFieldClassWithoutSelector
.option_at_index
389 def _create_default_fc(self
):
390 return self
._tc
.create_variant_field_class()
393 class _VariantFieldClassWithSelectorTestCase
:
395 self
._tc
= get_default_trace_class()
397 self
._fc
= self
._create
_default
_fc
()
399 def _create_default_fc(self
):
400 return self
._tc
.create_variant_field_class(self
._selector
_fc
)
402 def test_create_default(self
):
403 self
.assertIsNotNone(self
._fc
)
405 def test_append_element(self
):
406 str_field_class
= self
._tc
.create_string_field_class()
407 self
._fc
.append_option('str', str_field_class
, self
._ranges
1)
408 opt
= self
._fc
['str']
409 self
.assertEqual(opt
.field_class
.addr
, str_field_class
.addr
)
410 self
.assertEqual(opt
.name
, 'str')
411 self
.assertEqual(opt
.ranges
.addr
, self
._ranges
1.addr
)
413 def test_append_element_kwargs(self
):
414 int_field_class
= self
._tc
.create_signed_integer_field_class(32)
415 self
._fc
.append_option(
416 name
='int32', field_class
=int_field_class
, ranges
=self
._ranges
1
418 opt
= self
._fc
['int32']
419 self
.assertEqual(opt
.field_class
.addr
, int_field_class
.addr
)
420 self
.assertEqual(opt
.name
, 'int32')
421 self
.assertEqual(opt
.ranges
.addr
, self
._ranges
1.addr
)
423 def test_append_element_invalid_name(self
):
424 sub_fc
= self
._tc
.create_string_field_class()
426 with self
.assertRaises(TypeError):
427 self
._fc
.append_option(self
._fc
, 23, sub_fc
)
429 def test_append_element_invalid_field_class(self
):
430 with self
.assertRaises(TypeError):
431 self
._fc
.append_option(self
._fc
, 'yes', object())
433 def test_append_element_invalid_ranges(self
):
434 sub_fc
= self
._tc
.create_string_field_class()
436 with self
.assertRaises(TypeError):
437 self
._fc
.append_option(self
._fc
, sub_fc
, 'lel')
439 def test_append_element_dup_name(self
):
440 sub_fc1
= self
._tc
.create_string_field_class()
441 sub_fc2
= self
._tc
.create_string_field_class()
443 with self
.assertRaises(ValueError):
444 self
._fc
.append_option('yes', sub_fc1
, self
._ranges
1)
445 self
._fc
.append_option('yes', sub_fc2
, self
._ranges
2)
447 def test_append_element_invalid_ranges_signedness(self
):
448 sub_fc
= self
._tc
.create_string_field_class()
450 with self
.assertRaises(TypeError):
451 self
._fc
.append_option(self
._fc
, sub_fc
, self
._inval
_ranges
)
454 other_fc
= self
._create
_default
_fc
()
455 a_field_class
= self
._tc
.create_real_field_class()
456 self
._fc
.append_option('a_float', a_field_class
, self
._ranges
1)
457 c_field_class
= self
._tc
.create_string_field_class()
458 d_field_class
= self
._tc
.create_signed_enumeration_field_class(
462 ('c_string', c_field_class
, self
._ranges
2),
463 ('d_enum', d_field_class
, self
._ranges
3),
465 self
.assertEqual(self
._fc
['a_float'].field_class
.addr
, a_field_class
.addr
)
466 self
.assertEqual(self
._fc
['a_float'].name
, 'a_float')
467 self
.assertEqual(self
._fc
['a_float'].ranges
, self
._ranges
1)
468 self
.assertEqual(self
._fc
['c_string'].field_class
.addr
, c_field_class
.addr
)
469 self
.assertEqual(self
._fc
['c_string'].name
, 'c_string')
470 self
.assertEqual(self
._fc
['c_string'].ranges
, self
._ranges
2)
471 self
.assertEqual(self
._fc
['d_enum'].field_class
.addr
, d_field_class
.addr
)
472 self
.assertEqual(self
._fc
['d_enum'].name
, 'd_enum')
473 self
.assertEqual(self
._fc
['d_enum'].ranges
, self
._ranges
3)
475 def test_bool_op(self
):
476 self
.assertFalse(self
._fc
)
477 self
._fc
.append_option('a', self
._tc
.create_string_field_class(), self
._ranges
1)
478 self
.assertTrue(self
._fc
)
481 self
._fc
.append_option('a', self
._tc
.create_string_field_class(), self
._ranges
1)
482 self
._fc
.append_option('b', self
._tc
.create_string_field_class(), self
._ranges
2)
483 self
._fc
.append_option('c', self
._tc
.create_string_field_class(), self
._ranges
3)
484 self
.assertEqual(len(self
._fc
), 3)
486 def test_getitem(self
):
487 a_fc
= self
._tc
.create_signed_integer_field_class(32)
488 b_fc
= self
._tc
.create_string_field_class()
489 c_fc
= self
._tc
.create_real_field_class()
490 self
._fc
.append_option('a', a_fc
, self
._ranges
1)
491 self
._fc
.append_option('b', b_fc
, self
._ranges
2)
492 self
._fc
.append_option('c', c_fc
, self
._ranges
3)
493 self
.assertEqual(self
._fc
['b'].field_class
.addr
, b_fc
.addr
)
494 self
.assertEqual(self
._fc
['b'].name
, 'b')
495 self
.assertEqual(self
._fc
['b'].ranges
.addr
, self
._ranges
2.addr
)
497 def test_getitem_invalid_key_type(self
):
498 with self
.assertRaises(TypeError):
501 def test_getitem_invalid_key(self
):
502 with self
.assertRaises(KeyError):
505 def test_contains(self
):
506 self
.assertFalse('a' in self
._fc
)
507 self
._fc
.append_option('a', self
._tc
.create_string_field_class(), self
._ranges
1)
508 self
.assertTrue('a' in self
._fc
)
511 a_fc
= self
._tc
.create_signed_integer_field_class(32)
512 b_fc
= self
._tc
.create_string_field_class()
513 c_fc
= self
._tc
.create_real_field_class()
515 ('a', a_fc
, self
._ranges
1),
516 ('b', b_fc
, self
._ranges
2),
517 ('c', c_fc
, self
._ranges
3),
521 self
._fc
.append_option(*opt
)
523 for (name
, opt
), test_opt
in zip(self
._fc
.items(), opts
):
524 self
.assertEqual(opt
.name
, test_opt
[0])
525 self
.assertEqual(name
, opt
.name
)
526 self
.assertEqual(opt
.field_class
.addr
, test_opt
[1].addr
)
527 self
.assertEqual(opt
.ranges
.addr
, test_opt
[2].addr
)
529 def test_at_index(self
):
530 a_fc
= self
._tc
.create_signed_integer_field_class(32)
531 b_fc
= self
._tc
.create_string_field_class()
532 c_fc
= self
._tc
.create_real_field_class()
533 self
._fc
.append_option('c', c_fc
, self
._ranges
1)
534 self
._fc
.append_option('a', a_fc
, self
._ranges
2)
535 self
._fc
.append_option('b', b_fc
, self
._ranges
3)
536 self
.assertEqual(self
._fc
.option_at_index(1).field_class
.addr
, a_fc
.addr
)
537 self
.assertEqual(self
._fc
.option_at_index(1).name
, 'a')
538 self
.assertEqual(self
._fc
.option_at_index(1).ranges
.addr
, self
._ranges
2.addr
)
540 def test_at_index_invalid(self
):
541 self
._fc
.append_option(
542 'c', self
._tc
.create_signed_integer_field_class(32), self
._ranges
3
545 with self
.assertRaises(TypeError):
546 self
._fc
.option_at_index('yes')
548 def test_at_index_out_of_bounds_after(self
):
549 self
._fc
.append_option(
550 'c', self
._tc
.create_signed_integer_field_class(32), self
._ranges
3
553 with self
.assertRaises(IndexError):
554 self
._fc
.option_at_index(len(self
._fc
))
556 def _fill_default_fc_for_field_path_test(self
):
557 # Create something equivalent to:
559 # struct outer_struct_fc {
561 # struct inner_struct_fc {
562 # [u]int64_t selector;
565 # variant <selector> {
566 # real a; // selected with self._ranges1
567 # int21_t b; // selected with self._ranges2
568 # uint34_t c; // selected with self._ranges3
572 self
._fc
.append_option('a', self
._tc
.create_real_field_class(), self
._ranges
1)
573 self
._fc
.append_option(
574 'b', self
._tc
.create_signed_integer_field_class(21), self
._ranges
2
576 self
._fc
.append_option(
577 'c', self
._tc
.create_unsigned_integer_field_class(34), self
._ranges
3
580 foo_fc
= self
._tc
.create_real_field_class()
581 bar_fc
= self
._tc
.create_string_field_class()
582 baz_fc
= self
._tc
.create_string_field_class()
584 inner_struct_fc
= self
._tc
.create_structure_field_class()
585 inner_struct_fc
.append_member('selector', self
._selector
_fc
)
586 inner_struct_fc
.append_member('bar', bar_fc
)
587 inner_struct_fc
.append_member('baz', baz_fc
)
588 inner_struct_fc
.append_member('variant', self
._fc
)
590 inner_struct_array_fc
= self
._tc
.create_static_array_field_class(
594 outer_struct_fc
= self
._tc
.create_structure_field_class()
595 outer_struct_fc
.append_member('foo', foo_fc
)
596 outer_struct_fc
.append_member('inner_struct', inner_struct_array_fc
)
598 # The path to the selector field is resolved when the sequence is
599 # actually used, for example in a packet context.
600 self
._tc
.create_stream_class(
601 supports_packets
=True, packet_context_field_class
=outer_struct_fc
604 def test_selector_field_path_length(self
):
605 self
._fill
_default
_fc
_for
_field
_path
_test
()
606 self
.assertEqual(len(self
._fc
.selector_field_path
), 3)
608 def test_selector_field_path_iter(self
):
609 self
._fill
_default
_fc
_for
_field
_path
_test
()
610 path_items
= list(self
._fc
.selector_field_path
)
612 self
.assertEqual(len(path_items
), 3)
614 self
.assertIsInstance(path_items
[0], bt2
._IndexFieldPathItem
)
615 self
.assertEqual(path_items
[0].index
, 1)
617 self
.assertIsInstance(path_items
[1], bt2
._CurrentArrayElementFieldPathItem
)
619 self
.assertIsInstance(path_items
[2], bt2
._IndexFieldPathItem
)
620 self
.assertEqual(path_items
[2].index
, 0)
622 def test_selector_field_path_root_scope(self
):
623 self
._fill
_default
_fc
_for
_field
_path
_test
()
625 self
._fc
.selector_field_path
.root_scope
, bt2
.FieldPathScope
.PACKET_CONTEXT
629 class VariantFieldClassWithUnsignedSelectorTestCase(
630 _VariantFieldClassWithSelectorTestCase
, unittest
.TestCase
632 def _spec_set_up(self
):
633 self
._ranges
1 = bt2
.UnsignedIntegerRangeSet([(1, 4), (18, 47)])
634 self
._ranges
2 = bt2
.UnsignedIntegerRangeSet([(5, 5)])
635 self
._ranges
3 = bt2
.UnsignedIntegerRangeSet([(8, 16), (48, 99)])
636 self
._inval
_ranges
= bt2
.SignedIntegerRangeSet([(-8, 16), (48, 99)])
637 self
._selector
_fc
= self
._tc
.create_unsigned_integer_field_class()
640 class VariantFieldClassWithSignedSelectorTestCase(
641 _VariantFieldClassWithSelectorTestCase
, unittest
.TestCase
643 def _spec_set_up(self
):
644 self
._ranges
1 = bt2
.SignedIntegerRangeSet([(-10, -4), (18, 47)])
645 self
._ranges
2 = bt2
.SignedIntegerRangeSet([(-3, -3)])
646 self
._ranges
3 = bt2
.SignedIntegerRangeSet([(8, 16), (48, 99)])
647 self
._inval
_ranges
= bt2
.UnsignedIntegerRangeSet([(8, 16), (48, 99)])
648 self
._selector
_fc
= self
._tc
.create_signed_integer_field_class()
651 class StaticArrayFieldClassTestCase(unittest
.TestCase
):
653 self
._tc
= get_default_trace_class()
654 self
._elem
_fc
= self
._tc
.create_signed_integer_field_class(23)
656 def test_create_default(self
):
657 fc
= self
._tc
.create_static_array_field_class(self
._elem
_fc
, 45)
658 self
.assertEqual(fc
.element_field_class
.addr
, self
._elem
_fc
.addr
)
659 self
.assertEqual(fc
.length
, 45)
661 def test_create_invalid_elem_field_class(self
):
662 with self
.assertRaises(TypeError):
663 self
._tc
.create_static_array_field_class(object(), 45)
665 def test_create_invalid_length(self
):
666 with self
.assertRaises(ValueError):
667 self
._tc
.create_static_array_field_class(
668 self
._tc
.create_string_field_class(), -17
671 def test_create_invalid_length_type(self
):
672 with self
.assertRaises(TypeError):
673 self
._tc
.create_static_array_field_class(
674 self
._tc
.create_string_field_class(), 'the length'
678 class DynamicArrayFieldClassTestCase(unittest
.TestCase
):
680 self
._tc
= get_default_trace_class()
681 self
._elem
_fc
= self
._tc
.create_signed_integer_field_class(23)
682 self
._len
_fc
= self
._tc
.create_unsigned_integer_field_class(12)
684 def test_create_default(self
):
685 fc
= self
._tc
.create_dynamic_array_field_class(self
._elem
_fc
)
686 self
.assertEqual(fc
.element_field_class
.addr
, self
._elem
_fc
.addr
)
687 self
.assertIsNone(fc
.length_field_path
, None)
689 def _create_field_class_for_field_path_test(self
):
690 # Create something a field class that is equivalent to:
692 # struct outer_struct_fc {
694 # struct inner_struct_fc {
698 # uint23_t dyn_array[len];
702 fc
= self
._tc
.create_dynamic_array_field_class(self
._elem
_fc
, self
._len
_fc
)
704 foo_fc
= self
._tc
.create_real_field_class()
705 bar_fc
= self
._tc
.create_string_field_class()
706 baz_fc
= self
._tc
.create_string_field_class()
708 inner_struct_fc
= self
._tc
.create_structure_field_class()
709 inner_struct_fc
.append_member('bar', bar_fc
)
710 inner_struct_fc
.append_member('baz', baz_fc
)
711 inner_struct_fc
.append_member('len', self
._len
_fc
)
712 inner_struct_fc
.append_member('dyn_array', fc
)
714 inner_struct_array_fc
= self
._tc
.create_static_array_field_class(
718 outer_struct_fc
= self
._tc
.create_structure_field_class()
719 outer_struct_fc
.append_member('foo', foo_fc
)
720 outer_struct_fc
.append_member('inner_struct', inner_struct_array_fc
)
722 # The path to the length field is resolved when the sequence is
723 # actually used, for example in a packet context.
724 self
._tc
.create_stream_class(
725 packet_context_field_class
=outer_struct_fc
, supports_packets
=True
730 def test_field_path_len(self
):
731 fc
= self
._create
_field
_class
_for
_field
_path
_test
()
732 self
.assertEqual(len(fc
.length_field_path
), 3)
734 def test_field_path_iter(self
):
735 fc
= self
._create
_field
_class
_for
_field
_path
_test
()
736 path_items
= list(fc
.length_field_path
)
738 self
.assertEqual(len(path_items
), 3)
740 self
.assertIsInstance(path_items
[0], bt2
._IndexFieldPathItem
)
741 self
.assertEqual(path_items
[0].index
, 1)
743 self
.assertIsInstance(path_items
[1], bt2
._CurrentArrayElementFieldPathItem
)
745 self
.assertIsInstance(path_items
[2], bt2
._IndexFieldPathItem
)
746 self
.assertEqual(path_items
[2].index
, 2)
748 def test_field_path_root_scope(self
):
749 fc
= self
._create
_field
_class
_for
_field
_path
_test
()
751 fc
.length_field_path
.root_scope
, bt2
.FieldPathScope
.PACKET_CONTEXT
754 def test_create_invalid_field_class(self
):
755 with self
.assertRaises(TypeError):
756 self
._tc
.create_dynamic_array_field_class(object())
758 def test_create_invalid_length_type(self
):
759 with self
.assertRaises(TypeError):
760 self
._tc
.create_dynamic_array_field_class(
761 self
._tc
.create_string_field_class(), 17
765 if __name__
== "__main__":