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.
23 from utils
import get_default_trace_class
26 class _TestIntegerFieldClassProps
:
27 def test_create_default(self
):
28 fc
= self
._create
_func
()
29 self
.assertEqual(fc
.field_value_range
, 64)
30 self
.assertEqual(fc
.preferred_display_base
, bt2
.IntegerDisplayBase
.DECIMAL
)
32 def test_create_range(self
):
33 fc
= self
._create
_func
(field_value_range
=35)
34 self
.assertEqual(fc
.field_value_range
, 35)
36 fc
= self
._create
_func
(36)
37 self
.assertEqual(fc
.field_value_range
, 36)
39 def test_create_invalid_range(self
):
40 with self
.assertRaises(TypeError):
41 self
._create
_func
('yes')
43 with self
.assertRaises(TypeError):
44 self
._create
_func
(field_value_range
='yes')
46 with self
.assertRaises(ValueError):
47 self
._create
_func
(field_value_range
=-2)
49 with self
.assertRaises(ValueError):
50 self
._create
_func
(field_value_range
=0)
52 def test_create_base(self
):
53 fc
= self
._create
_func
(
54 preferred_display_base
=bt2
.IntegerDisplayBase
.HEXADECIMAL
56 self
.assertEqual(fc
.preferred_display_base
, bt2
.IntegerDisplayBase
.HEXADECIMAL
)
58 def test_create_invalid_base_type(self
):
59 with self
.assertRaises(TypeError):
60 self
._create
_func
(preferred_display_base
='yes')
62 def test_create_invalid_base_value(self
):
63 with self
.assertRaises(ValueError):
64 self
._create
_func
(preferred_display_base
=444)
66 def test_create_full(self
):
67 fc
= self
._create
_func
(24, preferred_display_base
=bt2
.IntegerDisplayBase
.OCTAL
)
68 self
.assertEqual(fc
.field_value_range
, 24)
69 self
.assertEqual(fc
.preferred_display_base
, bt2
.IntegerDisplayBase
.OCTAL
)
72 class IntegerFieldClassTestCase(_TestIntegerFieldClassProps
, unittest
.TestCase
):
74 self
._tc
= get_default_trace_class()
75 self
._create
_func
= self
._tc
.create_signed_integer_field_class
78 class RealFieldClassTestCase(unittest
.TestCase
):
80 self
._tc
= get_default_trace_class()
82 def test_create_default(self
):
83 fc
= self
._tc
.create_real_field_class()
84 self
.assertFalse(fc
.is_single_precision
)
86 def test_create_is_single_precision(self
):
87 fc
= self
._tc
.create_real_field_class(is_single_precision
=True)
88 self
.assertTrue(fc
.is_single_precision
)
90 def test_create_invalid_is_single_precision(self
):
91 with self
.assertRaises(TypeError):
92 self
._tc
.create_real_field_class(is_single_precision
='hohoho')
95 # Converts an _EnumerationFieldClassMapping to a list of ranges:
97 # [(lower0, upper0), (lower1, upper1), ...]
100 def enum_mapping_to_set(mapping
):
101 return {(x
.lower
, x
.upper
) for x
in mapping
.ranges
}
104 class _EnumerationFieldClassTestCase(_TestIntegerFieldClassProps
):
106 self
._tc
= get_default_trace_class()
108 self
._fc
= self
._create
_func
()
110 def test_create_from_invalid_type(self
):
111 with self
.assertRaises(TypeError):
112 self
._create
_func
('coucou')
114 def test_add_mapping_simple(self
):
115 self
._fc
.add_mapping('hello', self
._ranges
1)
116 mapping
= self
._fc
['hello']
117 self
.assertEqual(mapping
.label
, 'hello')
118 self
.assertEqual(mapping
.ranges
, self
._ranges
1)
120 def test_add_mapping_simple_kwargs(self
):
121 self
._fc
.add_mapping(label
='hello', ranges
=self
._ranges
1)
122 mapping
= self
._fc
['hello']
123 self
.assertEqual(mapping
.label
, 'hello')
124 self
.assertEqual(mapping
.ranges
, self
._ranges
1)
126 def test_add_mapping_invalid_name(self
):
127 with self
.assertRaises(TypeError):
128 self
._fc
.add_mapping(17, self
._ranges
1)
130 def test_add_mapping_invalid_range(self
):
131 with self
.assertRaises(TypeError):
132 self
._fc
.add_mapping('allo', 'meow')
134 def test_add_mapping_dup_label(self
):
135 with self
.assertRaises(ValueError):
136 self
._fc
.add_mapping('a', self
._ranges
1)
137 self
._fc
.add_mapping('a', self
._ranges
2)
139 def test_add_mapping_invalid_ranges_signedness(self
):
140 with self
.assertRaises(TypeError):
141 self
._fc
.add_mapping('allo', self
._inval
_ranges
)
144 self
._fc
.add_mapping('c', self
._ranges
1)
146 self
._fc
+= [('d', self
._ranges
2), ('e', self
._ranges
3)]
148 self
.assertEqual(len(self
._fc
), 3)
149 self
.assertEqual(self
._fc
['c'].label
, 'c')
150 self
.assertEqual(self
._fc
['c'].ranges
, self
._ranges
1)
151 self
.assertEqual(self
._fc
['d'].label
, 'd')
152 self
.assertEqual(self
._fc
['d'].ranges
, self
._ranges
2)
153 self
.assertEqual(self
._fc
['e'].label
, 'e')
154 self
.assertEqual(self
._fc
['e'].ranges
, self
._ranges
3)
156 def test_bool_op(self
):
157 self
.assertFalse(self
._fc
)
158 self
._fc
.add_mapping('a', self
._ranges
1)
159 self
.assertTrue(self
._fc
)
162 self
._fc
.add_mapping('a', self
._ranges
1)
163 self
._fc
.add_mapping('b', self
._ranges
2)
164 self
._fc
.add_mapping('c', self
._ranges
3)
165 self
.assertEqual(len(self
._fc
), 3)
167 def test_getitem(self
):
168 self
._fc
.add_mapping('a', self
._ranges
1)
169 self
._fc
.add_mapping('b', self
._ranges
2)
170 self
._fc
.add_mapping('c', self
._ranges
3)
171 mapping
= self
._fc
['a']
172 self
.assertEqual(mapping
.label
, 'a')
173 self
.assertEqual(mapping
.ranges
, self
._ranges
1)
175 def test_getitem_nonexistent(self
):
176 with self
.assertRaises(KeyError):
177 self
._fc
['doesnotexist']
180 self
._fc
.add_mapping('a', self
._ranges
1)
181 self
._fc
.add_mapping('b', self
._ranges
2)
182 self
._fc
.add_mapping('c', self
._ranges
3)
184 # This exercises iteration.
185 labels
= sorted(self
._fc
)
187 self
.assertEqual(labels
, ['a', 'b', 'c'])
189 def test_find_by_value(self
):
190 self
._fc
.add_mapping('a', self
._ranges
1)
191 self
._fc
.add_mapping('b', self
._ranges
2)
192 self
._fc
.add_mapping('c', self
._ranges
3)
193 mappings
= self
._fc
.mappings_for_value(self
._value
_in
_range
_1_and
_3)
194 labels
= set([mapping
.label
for mapping
in mappings
])
195 expected_labels
= set(['a', 'c'])
196 self
.assertEqual(labels
, expected_labels
)
199 class UnsignedEnumerationFieldClassTestCase(
200 _EnumerationFieldClassTestCase
, unittest
.TestCase
202 def _spec_set_up(self
):
203 self
._ranges
1 = bt2
.UnsignedIntegerRangeSet([(1, 4), (18, 47)])
204 self
._ranges
2 = bt2
.UnsignedIntegerRangeSet([(5, 5)])
205 self
._ranges
3 = bt2
.UnsignedIntegerRangeSet([(8, 22), (48, 99)])
206 self
._inval
_ranges
= bt2
.SignedIntegerRangeSet([(-8, -5), (48, 1928)])
207 self
._value
_in
_range
_1_and
_3 = 20
208 self
._create
_func
= self
._tc
.create_unsigned_enumeration_field_class
211 class SignedEnumerationFieldClassTestCase(
212 _EnumerationFieldClassTestCase
, unittest
.TestCase
214 def _spec_set_up(self
):
215 self
._ranges
1 = bt2
.SignedIntegerRangeSet([(-10, -4), (18, 47)])
216 self
._ranges
2 = bt2
.SignedIntegerRangeSet([(-3, -3)])
217 self
._ranges
3 = bt2
.SignedIntegerRangeSet([(-100, -1), (8, 16), (48, 99)])
218 self
._inval
_ranges
= bt2
.UnsignedIntegerRangeSet([(8, 16), (48, 99)])
219 self
._value
_in
_range
_1_and
_3 = -7
220 self
._create
_func
= self
._tc
.create_signed_enumeration_field_class
223 class StringFieldClassTestCase(unittest
.TestCase
):
225 tc
= get_default_trace_class()
226 self
._fc
= tc
.create_string_field_class()
228 def test_create_default(self
):
229 self
.assertIsNotNone(self
._fc
)
232 class _TestElementContainer
:
234 self
._tc
= get_default_trace_class()
235 self
._fc
= self
._create
_default
_fc
()
237 def test_create_default(self
):
238 self
.assertIsNotNone(self
._fc
)
240 def test_append_element(self
):
241 int_field_class
= self
._tc
.create_signed_integer_field_class(32)
242 self
._append
_element
_method
(self
._fc
, 'int32', int_field_class
)
243 field_class
= self
._fc
['int32'].field_class
244 self
.assertEqual(field_class
.addr
, int_field_class
.addr
)
246 def test_append_element_kwargs(self
):
247 int_field_class
= self
._tc
.create_signed_integer_field_class(32)
248 self
._append
_element
_method
(self
._fc
, name
='int32', field_class
=int_field_class
)
249 field_class
= self
._fc
['int32'].field_class
250 self
.assertEqual(field_class
.addr
, int_field_class
.addr
)
252 def test_append_element_invalid_name(self
):
253 sub_fc
= self
._tc
.create_string_field_class()
255 with self
.assertRaises(TypeError):
256 self
._append
_element
_method
(self
._fc
, 23, sub_fc
)
258 def test_append_element_invalid_field_class(self
):
259 with self
.assertRaises(TypeError):
260 self
._append
_element
_method
(self
._fc
, 'yes', object())
262 def test_append_element_dup_name(self
):
263 sub_fc1
= self
._tc
.create_string_field_class()
264 sub_fc2
= self
._tc
.create_string_field_class()
266 with self
.assertRaises(ValueError):
267 self
._append
_element
_method
(self
._fc
, 'yes', sub_fc1
)
268 self
._append
_element
_method
(self
._fc
, 'yes', sub_fc2
)
271 other_fc
= self
._create
_default
_fc
()
272 a_field_class
= self
._tc
.create_real_field_class()
273 b_field_class
= self
._tc
.create_signed_integer_field_class(17)
274 self
._append
_element
_method
(self
._fc
, 'a_float', a_field_class
)
275 self
._append
_element
_method
(self
._fc
, 'b_int', b_field_class
)
276 c_field_class
= self
._tc
.create_string_field_class()
277 d_field_class
= self
._tc
.create_signed_enumeration_field_class(
280 e_field_class
= self
._tc
.create_structure_field_class()
282 ('c_string', c_field_class
),
283 ('d_enum', d_field_class
),
284 ('e_struct', e_field_class
),
286 self
.assertEqual(self
._fc
['a_float'].field_class
.addr
, a_field_class
.addr
)
287 self
.assertEqual(self
._fc
['a_float'].name
, 'a_float')
288 self
.assertEqual(self
._fc
['b_int'].field_class
.addr
, b_field_class
.addr
)
289 self
.assertEqual(self
._fc
['b_int'].name
, 'b_int')
290 self
.assertEqual(self
._fc
['c_string'].field_class
.addr
, c_field_class
.addr
)
291 self
.assertEqual(self
._fc
['c_string'].name
, 'c_string')
292 self
.assertEqual(self
._fc
['d_enum'].field_class
.addr
, d_field_class
.addr
)
293 self
.assertEqual(self
._fc
['d_enum'].name
, 'd_enum')
294 self
.assertEqual(self
._fc
['e_struct'].field_class
.addr
, e_field_class
.addr
)
295 self
.assertEqual(self
._fc
['e_struct'].name
, 'e_struct')
297 def test_bool_op(self
):
298 self
.assertFalse(self
._fc
)
299 self
._append
_element
_method
(self
._fc
, 'a', self
._tc
.create_string_field_class())
300 self
.assertTrue(self
._fc
)
303 self
._append
_element
_method
(self
._fc
, 'a', self
._tc
.create_string_field_class())
304 self
._append
_element
_method
(self
._fc
, 'b', self
._tc
.create_string_field_class())
305 self
._append
_element
_method
(self
._fc
, 'c', self
._tc
.create_string_field_class())
306 self
.assertEqual(len(self
._fc
), 3)
308 def test_getitem(self
):
309 a_fc
= self
._tc
.create_signed_integer_field_class(32)
310 b_fc
= self
._tc
.create_string_field_class()
311 c_fc
= self
._tc
.create_real_field_class()
312 self
._append
_element
_method
(self
._fc
, 'a', a_fc
)
313 self
._append
_element
_method
(self
._fc
, 'b', b_fc
)
314 self
._append
_element
_method
(self
._fc
, 'c', c_fc
)
315 self
.assertEqual(self
._fc
['b'].field_class
.addr
, b_fc
.addr
)
316 self
.assertEqual(self
._fc
['b'].name
, 'b')
318 def test_getitem_invalid_key_type(self
):
319 with self
.assertRaises(TypeError):
322 def test_getitem_invalid_key(self
):
323 with self
.assertRaises(KeyError):
326 def test_contains(self
):
327 self
.assertFalse('a' in self
._fc
)
328 self
._append
_element
_method
(self
._fc
, 'a', self
._tc
.create_string_field_class())
329 self
.assertTrue('a' in self
._fc
)
332 a_fc
= self
._tc
.create_signed_integer_field_class(32)
333 b_fc
= self
._tc
.create_string_field_class()
334 c_fc
= self
._tc
.create_real_field_class()
335 elements
= (('a', a_fc
), ('b', b_fc
), ('c', c_fc
))
337 for elem
in elements
:
338 self
._append
_element
_method
(self
._fc
, *elem
)
340 for (name
, element
), test_elem
in zip(self
._fc
.items(), elements
):
341 self
.assertEqual(element
.name
, test_elem
[0])
342 self
.assertEqual(name
, element
.name
)
343 self
.assertEqual(element
.field_class
.addr
, test_elem
[1].addr
)
345 def test_at_index(self
):
346 a_fc
= self
._tc
.create_signed_integer_field_class(32)
347 b_fc
= self
._tc
.create_string_field_class()
348 c_fc
= self
._tc
.create_real_field_class()
349 self
._append
_element
_method
(self
._fc
, 'c', c_fc
)
350 self
._append
_element
_method
(self
._fc
, 'a', a_fc
)
351 self
._append
_element
_method
(self
._fc
, 'b', b_fc
)
352 elem
= self
._at
_index
_method
(self
._fc
, 1)
353 self
.assertEqual(elem
.field_class
.addr
, a_fc
.addr
)
354 self
.assertEqual(elem
.name
, 'a')
356 def test_at_index_invalid(self
):
357 self
._append
_element
_method
(
358 self
._fc
, 'c', self
._tc
.create_signed_integer_field_class(32)
361 with self
.assertRaises(TypeError):
362 self
._at
_index
_method
(self
._fc
, 'yes')
364 def test_at_index_out_of_bounds_after(self
):
365 self
._append
_element
_method
(
366 self
._fc
, 'c', self
._tc
.create_signed_integer_field_class(32)
369 with self
.assertRaises(IndexError):
370 self
._at
_index
_method
(self
._fc
, len(self
._fc
))
373 class StructureFieldClassTestCase(_TestElementContainer
, unittest
.TestCase
):
374 _append_element_method
= staticmethod(
375 bt2
.field_class
._StructureFieldClass
.append_member
377 _at_index_method
= staticmethod(
378 bt2
.field_class
._StructureFieldClass
.member_at_index
381 def _create_default_fc(self
):
382 return self
._tc
.create_structure_field_class()
385 class VariantFieldClassWithoutSelectorTestCase(
386 _TestElementContainer
, unittest
.TestCase
388 _append_element_method
= staticmethod(
389 bt2
.field_class
._VariantFieldClassWithoutSelector
.append_option
391 _at_index_method
= staticmethod(
392 bt2
.field_class
._VariantFieldClassWithoutSelector
.option_at_index
395 def _create_default_fc(self
):
396 return self
._tc
.create_variant_field_class()
399 class _VariantFieldClassWithSelectorTestCase
:
401 self
._tc
= get_default_trace_class()
403 self
._fc
= self
._create
_default
_fc
()
405 def _create_default_fc(self
):
406 return self
._tc
.create_variant_field_class(self
._selector
_fc
)
408 def test_create_default(self
):
409 self
.assertIsNotNone(self
._fc
)
411 def test_append_element(self
):
412 str_field_class
= self
._tc
.create_string_field_class()
413 self
._fc
.append_option('str', str_field_class
, self
._ranges
1)
414 opt
= self
._fc
['str']
415 self
.assertEqual(opt
.field_class
.addr
, str_field_class
.addr
)
416 self
.assertEqual(opt
.name
, 'str')
417 self
.assertEqual(opt
.ranges
.addr
, self
._ranges
1.addr
)
419 def test_append_element_kwargs(self
):
420 int_field_class
= self
._tc
.create_signed_integer_field_class(32)
421 self
._fc
.append_option(
422 name
='int32', field_class
=int_field_class
, ranges
=self
._ranges
1
424 opt
= self
._fc
['int32']
425 self
.assertEqual(opt
.field_class
.addr
, int_field_class
.addr
)
426 self
.assertEqual(opt
.name
, 'int32')
427 self
.assertEqual(opt
.ranges
.addr
, self
._ranges
1.addr
)
429 def test_append_element_invalid_name(self
):
430 sub_fc
= self
._tc
.create_string_field_class()
432 with self
.assertRaises(TypeError):
433 self
._fc
.append_option(self
._fc
, 23, sub_fc
)
435 def test_append_element_invalid_field_class(self
):
436 with self
.assertRaises(TypeError):
437 self
._fc
.append_option(self
._fc
, 'yes', object())
439 def test_append_element_invalid_ranges(self
):
440 sub_fc
= self
._tc
.create_string_field_class()
442 with self
.assertRaises(TypeError):
443 self
._fc
.append_option(self
._fc
, sub_fc
, 'lel')
445 def test_append_element_dup_name(self
):
446 sub_fc1
= self
._tc
.create_string_field_class()
447 sub_fc2
= self
._tc
.create_string_field_class()
449 with self
.assertRaises(ValueError):
450 self
._fc
.append_option('yes', sub_fc1
, self
._ranges
1)
451 self
._fc
.append_option('yes', sub_fc2
, self
._ranges
2)
453 def test_append_element_invalid_ranges_signedness(self
):
454 sub_fc
= self
._tc
.create_string_field_class()
456 with self
.assertRaises(TypeError):
457 self
._fc
.append_option(self
._fc
, sub_fc
, self
._inval
_ranges
)
460 other_fc
= self
._create
_default
_fc
()
461 a_field_class
= self
._tc
.create_real_field_class()
462 self
._fc
.append_option('a_float', a_field_class
, self
._ranges
1)
463 c_field_class
= self
._tc
.create_string_field_class()
464 d_field_class
= self
._tc
.create_signed_enumeration_field_class(
468 ('c_string', c_field_class
, self
._ranges
2),
469 ('d_enum', d_field_class
, self
._ranges
3),
471 self
.assertEqual(self
._fc
['a_float'].field_class
.addr
, a_field_class
.addr
)
472 self
.assertEqual(self
._fc
['a_float'].name
, 'a_float')
473 self
.assertEqual(self
._fc
['a_float'].ranges
, self
._ranges
1)
474 self
.assertEqual(self
._fc
['c_string'].field_class
.addr
, c_field_class
.addr
)
475 self
.assertEqual(self
._fc
['c_string'].name
, 'c_string')
476 self
.assertEqual(self
._fc
['c_string'].ranges
, self
._ranges
2)
477 self
.assertEqual(self
._fc
['d_enum'].field_class
.addr
, d_field_class
.addr
)
478 self
.assertEqual(self
._fc
['d_enum'].name
, 'd_enum')
479 self
.assertEqual(self
._fc
['d_enum'].ranges
, self
._ranges
3)
481 def test_bool_op(self
):
482 self
.assertFalse(self
._fc
)
483 self
._fc
.append_option('a', self
._tc
.create_string_field_class(), self
._ranges
1)
484 self
.assertTrue(self
._fc
)
487 self
._fc
.append_option('a', self
._tc
.create_string_field_class(), self
._ranges
1)
488 self
._fc
.append_option('b', self
._tc
.create_string_field_class(), self
._ranges
2)
489 self
._fc
.append_option('c', self
._tc
.create_string_field_class(), self
._ranges
3)
490 self
.assertEqual(len(self
._fc
), 3)
492 def test_getitem(self
):
493 a_fc
= self
._tc
.create_signed_integer_field_class(32)
494 b_fc
= self
._tc
.create_string_field_class()
495 c_fc
= self
._tc
.create_real_field_class()
496 self
._fc
.append_option('a', a_fc
, self
._ranges
1)
497 self
._fc
.append_option('b', b_fc
, self
._ranges
2)
498 self
._fc
.append_option('c', c_fc
, self
._ranges
3)
499 self
.assertEqual(self
._fc
['b'].field_class
.addr
, b_fc
.addr
)
500 self
.assertEqual(self
._fc
['b'].name
, 'b')
501 self
.assertEqual(self
._fc
['b'].ranges
.addr
, self
._ranges
2.addr
)
503 def test_getitem_invalid_key_type(self
):
504 with self
.assertRaises(TypeError):
507 def test_getitem_invalid_key(self
):
508 with self
.assertRaises(KeyError):
511 def test_contains(self
):
512 self
.assertFalse('a' in self
._fc
)
513 self
._fc
.append_option('a', self
._tc
.create_string_field_class(), self
._ranges
1)
514 self
.assertTrue('a' in self
._fc
)
517 a_fc
= self
._tc
.create_signed_integer_field_class(32)
518 b_fc
= self
._tc
.create_string_field_class()
519 c_fc
= self
._tc
.create_real_field_class()
521 ('a', a_fc
, self
._ranges
1),
522 ('b', b_fc
, self
._ranges
2),
523 ('c', c_fc
, self
._ranges
3),
527 self
._fc
.append_option(*opt
)
529 for (name
, opt
), test_opt
in zip(self
._fc
.items(), opts
):
530 self
.assertEqual(opt
.name
, test_opt
[0])
531 self
.assertEqual(name
, opt
.name
)
532 self
.assertEqual(opt
.field_class
.addr
, test_opt
[1].addr
)
533 self
.assertEqual(opt
.ranges
.addr
, test_opt
[2].addr
)
535 def test_at_index(self
):
536 a_fc
= self
._tc
.create_signed_integer_field_class(32)
537 b_fc
= self
._tc
.create_string_field_class()
538 c_fc
= self
._tc
.create_real_field_class()
539 self
._fc
.append_option('c', c_fc
, self
._ranges
1)
540 self
._fc
.append_option('a', a_fc
, self
._ranges
2)
541 self
._fc
.append_option('b', b_fc
, self
._ranges
3)
542 self
.assertEqual(self
._fc
.option_at_index(1).field_class
.addr
, a_fc
.addr
)
543 self
.assertEqual(self
._fc
.option_at_index(1).name
, 'a')
544 self
.assertEqual(self
._fc
.option_at_index(1).ranges
.addr
, self
._ranges
2.addr
)
546 def test_at_index_invalid(self
):
547 self
._fc
.append_option(
548 'c', self
._tc
.create_signed_integer_field_class(32), self
._ranges
3
551 with self
.assertRaises(TypeError):
552 self
._fc
.option_at_index('yes')
554 def test_at_index_out_of_bounds_after(self
):
555 self
._fc
.append_option(
556 'c', self
._tc
.create_signed_integer_field_class(32), self
._ranges
3
559 with self
.assertRaises(IndexError):
560 self
._fc
.option_at_index(len(self
._fc
))
562 def _fill_default_fc_for_field_path_test(self
):
563 # Create something equivalent to:
565 # struct outer_struct_fc {
567 # struct inner_struct_fc {
568 # [u]int64_t selector;
571 # variant <selector> {
572 # real a; // selected with self._ranges1
573 # int21_t b; // selected with self._ranges2
574 # uint34_t c; // selected with self._ranges3
578 self
._fc
.append_option('a', self
._tc
.create_real_field_class(), self
._ranges
1)
579 self
._fc
.append_option(
580 'b', self
._tc
.create_signed_integer_field_class(21), self
._ranges
2
582 self
._fc
.append_option(
583 'c', self
._tc
.create_unsigned_integer_field_class(34), self
._ranges
3
586 foo_fc
= self
._tc
.create_real_field_class()
587 bar_fc
= self
._tc
.create_string_field_class()
588 baz_fc
= self
._tc
.create_string_field_class()
590 inner_struct_fc
= self
._tc
.create_structure_field_class()
591 inner_struct_fc
.append_member('selector', self
._selector
_fc
)
592 inner_struct_fc
.append_member('bar', bar_fc
)
593 inner_struct_fc
.append_member('baz', baz_fc
)
594 inner_struct_fc
.append_member('variant', self
._fc
)
596 inner_struct_array_fc
= self
._tc
.create_static_array_field_class(
600 outer_struct_fc
= self
._tc
.create_structure_field_class()
601 outer_struct_fc
.append_member('foo', foo_fc
)
602 outer_struct_fc
.append_member('inner_struct', inner_struct_array_fc
)
604 # The path to the selector field is resolved when the sequence is
605 # actually used, for example in a packet context.
606 self
._tc
.create_stream_class(
607 supports_packets
=True, packet_context_field_class
=outer_struct_fc
610 def test_selector_field_path_length(self
):
611 self
._fill
_default
_fc
_for
_field
_path
_test
()
612 self
.assertEqual(len(self
._fc
.selector_field_path
), 3)
614 def test_selector_field_path_iter(self
):
615 self
._fill
_default
_fc
_for
_field
_path
_test
()
616 path_items
= list(self
._fc
.selector_field_path
)
618 self
.assertEqual(len(path_items
), 3)
620 self
.assertIsInstance(path_items
[0], bt2
.field_path
._IndexFieldPathItem
)
621 self
.assertEqual(path_items
[0].index
, 1)
623 self
.assertIsInstance(
624 path_items
[1], bt2
.field_path
._CurrentArrayElementFieldPathItem
627 self
.assertIsInstance(path_items
[2], bt2
.field_path
._IndexFieldPathItem
)
628 self
.assertEqual(path_items
[2].index
, 0)
630 def test_selector_field_path_root_scope(self
):
631 self
._fill
_default
_fc
_for
_field
_path
_test
()
633 self
._fc
.selector_field_path
.root_scope
, bt2
.field_path
.Scope
.PACKET_CONTEXT
637 class VariantFieldClassWithUnsignedSelectorTestCase(
638 _VariantFieldClassWithSelectorTestCase
, unittest
.TestCase
640 def _spec_set_up(self
):
641 self
._ranges
1 = bt2
.UnsignedIntegerRangeSet([(1, 4), (18, 47)])
642 self
._ranges
2 = bt2
.UnsignedIntegerRangeSet([(5, 5)])
643 self
._ranges
3 = bt2
.UnsignedIntegerRangeSet([(8, 16), (48, 99)])
644 self
._inval
_ranges
= bt2
.SignedIntegerRangeSet([(-8, 16), (48, 99)])
645 self
._selector
_fc
= self
._tc
.create_unsigned_integer_field_class()
648 class VariantFieldClassWithSignedSelectorTestCase(
649 _VariantFieldClassWithSelectorTestCase
, unittest
.TestCase
651 def _spec_set_up(self
):
652 self
._ranges
1 = bt2
.SignedIntegerRangeSet([(-10, -4), (18, 47)])
653 self
._ranges
2 = bt2
.SignedIntegerRangeSet([(-3, -3)])
654 self
._ranges
3 = bt2
.SignedIntegerRangeSet([(8, 16), (48, 99)])
655 self
._inval
_ranges
= bt2
.UnsignedIntegerRangeSet([(8, 16), (48, 99)])
656 self
._selector
_fc
= self
._tc
.create_signed_integer_field_class()
659 class StaticArrayFieldClassTestCase(unittest
.TestCase
):
661 self
._tc
= get_default_trace_class()
662 self
._elem
_fc
= self
._tc
.create_signed_integer_field_class(23)
664 def test_create_default(self
):
665 fc
= self
._tc
.create_static_array_field_class(self
._elem
_fc
, 45)
666 self
.assertEqual(fc
.element_field_class
.addr
, self
._elem
_fc
.addr
)
667 self
.assertEqual(fc
.length
, 45)
669 def test_create_invalid_elem_field_class(self
):
670 with self
.assertRaises(TypeError):
671 self
._tc
.create_static_array_field_class(object(), 45)
673 def test_create_invalid_length(self
):
674 with self
.assertRaises(ValueError):
675 self
._tc
.create_static_array_field_class(
676 self
._tc
.create_string_field_class(), -17
679 def test_create_invalid_length_type(self
):
680 with self
.assertRaises(TypeError):
681 self
._tc
.create_static_array_field_class(
682 self
._tc
.create_string_field_class(), 'the length'
686 class DynamicArrayFieldClassTestCase(unittest
.TestCase
):
688 self
._tc
= get_default_trace_class()
689 self
._elem
_fc
= self
._tc
.create_signed_integer_field_class(23)
690 self
._len
_fc
= self
._tc
.create_unsigned_integer_field_class(12)
692 def test_create_default(self
):
693 fc
= self
._tc
.create_dynamic_array_field_class(self
._elem
_fc
)
694 self
.assertEqual(fc
.element_field_class
.addr
, self
._elem
_fc
.addr
)
695 self
.assertIsNone(fc
.length_field_path
, None)
697 def _create_field_class_for_field_path_test(self
):
698 # Create something a field class that is equivalent to:
700 # struct outer_struct_fc {
702 # struct inner_struct_fc {
706 # uint23_t dyn_array[len];
710 fc
= self
._tc
.create_dynamic_array_field_class(self
._elem
_fc
, self
._len
_fc
)
712 foo_fc
= self
._tc
.create_real_field_class()
713 bar_fc
= self
._tc
.create_string_field_class()
714 baz_fc
= self
._tc
.create_string_field_class()
716 inner_struct_fc
= self
._tc
.create_structure_field_class()
717 inner_struct_fc
.append_member('bar', bar_fc
)
718 inner_struct_fc
.append_member('baz', baz_fc
)
719 inner_struct_fc
.append_member('len', self
._len
_fc
)
720 inner_struct_fc
.append_member('dyn_array', fc
)
722 inner_struct_array_fc
= self
._tc
.create_static_array_field_class(
726 outer_struct_fc
= self
._tc
.create_structure_field_class()
727 outer_struct_fc
.append_member('foo', foo_fc
)
728 outer_struct_fc
.append_member('inner_struct', inner_struct_array_fc
)
730 # The path to the length field is resolved when the sequence is
731 # actually used, for example in a packet context.
732 self
._tc
.create_stream_class(
733 packet_context_field_class
=outer_struct_fc
, supports_packets
=True
738 def test_field_path_len(self
):
739 fc
= self
._create
_field
_class
_for
_field
_path
_test
()
740 self
.assertEqual(len(fc
.length_field_path
), 3)
742 def test_field_path_iter(self
):
743 fc
= self
._create
_field
_class
_for
_field
_path
_test
()
744 path_items
= list(fc
.length_field_path
)
746 self
.assertEqual(len(path_items
), 3)
748 self
.assertIsInstance(path_items
[0], bt2
.field_path
._IndexFieldPathItem
)
749 self
.assertEqual(path_items
[0].index
, 1)
751 self
.assertIsInstance(
752 path_items
[1], bt2
.field_path
._CurrentArrayElementFieldPathItem
755 self
.assertIsInstance(path_items
[2], bt2
.field_path
._IndexFieldPathItem
)
756 self
.assertEqual(path_items
[2].index
, 2)
758 def test_field_path_root_scope(self
):
759 fc
= self
._create
_field
_class
_for
_field
_path
_test
()
761 fc
.length_field_path
.root_scope
, bt2
.field_path
.Scope
.PACKET_CONTEXT
764 def test_create_invalid_field_class(self
):
765 with self
.assertRaises(TypeError):
766 self
._tc
.create_dynamic_array_field_class(object())
768 def test_create_invalid_length_type(self
):
769 with self
.assertRaises(TypeError):
770 self
._tc
.create_dynamic_array_field_class(
771 self
._tc
.create_string_field_class(), 17
775 if __name__
== "__main__":