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 BoolFieldClassTestCase(unittest
.TestCase
):
26 tc
= get_default_trace_class()
27 self
._fc
= tc
.create_bool_field_class()
29 def test_create_default(self
):
30 self
.assertIsNotNone(self
._fc
)
33 class _TestIntegerFieldClassProps
:
34 def test_create_default(self
):
35 fc
= self
._create
_func
()
36 self
.assertEqual(fc
.field_value_range
, 64)
37 self
.assertEqual(fc
.preferred_display_base
, bt2
.IntegerDisplayBase
.DECIMAL
)
39 def test_create_range(self
):
40 fc
= self
._create
_func
(field_value_range
=35)
41 self
.assertEqual(fc
.field_value_range
, 35)
43 fc
= self
._create
_func
(36)
44 self
.assertEqual(fc
.field_value_range
, 36)
46 def test_create_invalid_range(self
):
47 with self
.assertRaises(TypeError):
48 self
._create
_func
('yes')
50 with self
.assertRaises(TypeError):
51 self
._create
_func
(field_value_range
='yes')
53 with self
.assertRaises(ValueError):
54 self
._create
_func
(field_value_range
=-2)
56 with self
.assertRaises(ValueError):
57 self
._create
_func
(field_value_range
=0)
59 def test_create_base(self
):
60 fc
= self
._create
_func
(
61 preferred_display_base
=bt2
.IntegerDisplayBase
.HEXADECIMAL
63 self
.assertEqual(fc
.preferred_display_base
, bt2
.IntegerDisplayBase
.HEXADECIMAL
)
65 def test_create_invalid_base_type(self
):
66 with self
.assertRaises(TypeError):
67 self
._create
_func
(preferred_display_base
='yes')
69 def test_create_invalid_base_value(self
):
70 with self
.assertRaises(ValueError):
71 self
._create
_func
(preferred_display_base
=444)
73 def test_create_full(self
):
74 fc
= self
._create
_func
(24, preferred_display_base
=bt2
.IntegerDisplayBase
.OCTAL
)
75 self
.assertEqual(fc
.field_value_range
, 24)
76 self
.assertEqual(fc
.preferred_display_base
, bt2
.IntegerDisplayBase
.OCTAL
)
79 class IntegerFieldClassTestCase(_TestIntegerFieldClassProps
, unittest
.TestCase
):
81 self
._tc
= get_default_trace_class()
82 self
._create
_func
= self
._tc
.create_signed_integer_field_class
85 class RealFieldClassTestCase(unittest
.TestCase
):
87 self
._tc
= get_default_trace_class()
89 def test_create_default(self
):
90 fc
= self
._tc
.create_real_field_class()
91 self
.assertFalse(fc
.is_single_precision
)
93 def test_create_is_single_precision(self
):
94 fc
= self
._tc
.create_real_field_class(is_single_precision
=True)
95 self
.assertTrue(fc
.is_single_precision
)
97 def test_create_invalid_is_single_precision(self
):
98 with self
.assertRaises(TypeError):
99 self
._tc
.create_real_field_class(is_single_precision
='hohoho')
102 # Converts an _EnumerationFieldClassMapping to a list of ranges:
104 # [(lower0, upper0), (lower1, upper1), ...]
107 def enum_mapping_to_set(mapping
):
108 return {(x
.lower
, x
.upper
) for x
in mapping
.ranges
}
111 class _EnumerationFieldClassTestCase(_TestIntegerFieldClassProps
):
113 self
._tc
= get_default_trace_class()
115 self
._fc
= self
._create
_func
()
117 def test_create_from_invalid_type(self
):
118 with self
.assertRaises(TypeError):
119 self
._create
_func
('coucou')
121 def test_add_mapping_simple(self
):
122 self
._fc
.add_mapping('hello', self
._ranges
1)
123 mapping
= self
._fc
['hello']
124 self
.assertEqual(mapping
.label
, 'hello')
125 self
.assertEqual(mapping
.ranges
, self
._ranges
1)
127 def test_add_mapping_simple_kwargs(self
):
128 self
._fc
.add_mapping(label
='hello', ranges
=self
._ranges
1)
129 mapping
= self
._fc
['hello']
130 self
.assertEqual(mapping
.label
, 'hello')
131 self
.assertEqual(mapping
.ranges
, self
._ranges
1)
133 def test_add_mapping_invalid_name(self
):
134 with self
.assertRaises(TypeError):
135 self
._fc
.add_mapping(17, self
._ranges
1)
137 def test_add_mapping_invalid_range(self
):
138 with self
.assertRaises(TypeError):
139 self
._fc
.add_mapping('allo', 'meow')
141 def test_add_mapping_dup_label(self
):
142 with self
.assertRaises(ValueError):
143 self
._fc
.add_mapping('a', self
._ranges
1)
144 self
._fc
.add_mapping('a', self
._ranges
2)
146 def test_add_mapping_invalid_ranges_signedness(self
):
147 with self
.assertRaises(TypeError):
148 self
._fc
.add_mapping('allo', self
._inval
_ranges
)
151 self
._fc
.add_mapping('c', self
._ranges
1)
153 self
._fc
+= [('d', self
._ranges
2), ('e', self
._ranges
3)]
155 self
.assertEqual(len(self
._fc
), 3)
156 self
.assertEqual(self
._fc
['c'].label
, 'c')
157 self
.assertEqual(self
._fc
['c'].ranges
, self
._ranges
1)
158 self
.assertEqual(self
._fc
['d'].label
, 'd')
159 self
.assertEqual(self
._fc
['d'].ranges
, self
._ranges
2)
160 self
.assertEqual(self
._fc
['e'].label
, 'e')
161 self
.assertEqual(self
._fc
['e'].ranges
, self
._ranges
3)
163 def test_bool_op(self
):
164 self
.assertFalse(self
._fc
)
165 self
._fc
.add_mapping('a', self
._ranges
1)
166 self
.assertTrue(self
._fc
)
169 self
._fc
.add_mapping('a', self
._ranges
1)
170 self
._fc
.add_mapping('b', self
._ranges
2)
171 self
._fc
.add_mapping('c', self
._ranges
3)
172 self
.assertEqual(len(self
._fc
), 3)
174 def test_getitem(self
):
175 self
._fc
.add_mapping('a', self
._ranges
1)
176 self
._fc
.add_mapping('b', self
._ranges
2)
177 self
._fc
.add_mapping('c', self
._ranges
3)
178 mapping
= self
._fc
['a']
179 self
.assertEqual(mapping
.label
, 'a')
180 self
.assertEqual(mapping
.ranges
, self
._ranges
1)
182 def test_getitem_nonexistent(self
):
183 with self
.assertRaises(KeyError):
184 self
._fc
['doesnotexist']
187 self
._fc
.add_mapping('a', self
._ranges
1)
188 self
._fc
.add_mapping('b', self
._ranges
2)
189 self
._fc
.add_mapping('c', self
._ranges
3)
191 # This exercises iteration.
192 labels
= sorted(self
._fc
)
194 self
.assertEqual(labels
, ['a', 'b', 'c'])
196 def test_find_by_value(self
):
197 self
._fc
.add_mapping('a', self
._ranges
1)
198 self
._fc
.add_mapping('b', self
._ranges
2)
199 self
._fc
.add_mapping('c', self
._ranges
3)
200 mappings
= self
._fc
.mappings_for_value(self
._value
_in
_range
_1_and
_3)
201 labels
= set([mapping
.label
for mapping
in mappings
])
202 expected_labels
= set(['a', 'c'])
203 self
.assertEqual(labels
, expected_labels
)
206 class UnsignedEnumerationFieldClassTestCase(
207 _EnumerationFieldClassTestCase
, unittest
.TestCase
209 def _spec_set_up(self
):
210 self
._ranges
1 = bt2
.UnsignedIntegerRangeSet([(1, 4), (18, 47)])
211 self
._ranges
2 = bt2
.UnsignedIntegerRangeSet([(5, 5)])
212 self
._ranges
3 = bt2
.UnsignedIntegerRangeSet([(8, 22), (48, 99)])
213 self
._inval
_ranges
= bt2
.SignedIntegerRangeSet([(-8, -5), (48, 1928)])
214 self
._value
_in
_range
_1_and
_3 = 20
215 self
._create
_func
= self
._tc
.create_unsigned_enumeration_field_class
218 class SignedEnumerationFieldClassTestCase(
219 _EnumerationFieldClassTestCase
, unittest
.TestCase
221 def _spec_set_up(self
):
222 self
._ranges
1 = bt2
.SignedIntegerRangeSet([(-10, -4), (18, 47)])
223 self
._ranges
2 = bt2
.SignedIntegerRangeSet([(-3, -3)])
224 self
._ranges
3 = bt2
.SignedIntegerRangeSet([(-100, -1), (8, 16), (48, 99)])
225 self
._inval
_ranges
= bt2
.UnsignedIntegerRangeSet([(8, 16), (48, 99)])
226 self
._value
_in
_range
_1_and
_3 = -7
227 self
._create
_func
= self
._tc
.create_signed_enumeration_field_class
230 class StringFieldClassTestCase(unittest
.TestCase
):
232 tc
= get_default_trace_class()
233 self
._fc
= tc
.create_string_field_class()
235 def test_create_default(self
):
236 self
.assertIsNotNone(self
._fc
)
239 class _TestElementContainer
:
241 self
._tc
= get_default_trace_class()
242 self
._fc
= self
._create
_default
_fc
()
244 def test_create_default(self
):
245 self
.assertIsNotNone(self
._fc
)
247 def test_append_element(self
):
248 int_field_class
= self
._tc
.create_signed_integer_field_class(32)
249 self
._append
_element
_method
(self
._fc
, 'int32', int_field_class
)
250 field_class
= self
._fc
['int32'].field_class
251 self
.assertEqual(field_class
.addr
, int_field_class
.addr
)
253 def test_append_element_kwargs(self
):
254 int_field_class
= self
._tc
.create_signed_integer_field_class(32)
255 self
._append
_element
_method
(self
._fc
, name
='int32', field_class
=int_field_class
)
256 field_class
= self
._fc
['int32'].field_class
257 self
.assertEqual(field_class
.addr
, int_field_class
.addr
)
259 def test_append_element_invalid_name(self
):
260 sub_fc
= self
._tc
.create_string_field_class()
262 with self
.assertRaises(TypeError):
263 self
._append
_element
_method
(self
._fc
, 23, sub_fc
)
265 def test_append_element_invalid_field_class(self
):
266 with self
.assertRaises(TypeError):
267 self
._append
_element
_method
(self
._fc
, 'yes', object())
269 def test_append_element_dup_name(self
):
270 sub_fc1
= self
._tc
.create_string_field_class()
271 sub_fc2
= self
._tc
.create_string_field_class()
273 with self
.assertRaises(ValueError):
274 self
._append
_element
_method
(self
._fc
, 'yes', sub_fc1
)
275 self
._append
_element
_method
(self
._fc
, 'yes', sub_fc2
)
278 other_fc
= self
._create
_default
_fc
()
279 a_field_class
= self
._tc
.create_real_field_class()
280 b_field_class
= self
._tc
.create_signed_integer_field_class(17)
281 self
._append
_element
_method
(self
._fc
, 'a_float', a_field_class
)
282 self
._append
_element
_method
(self
._fc
, 'b_int', b_field_class
)
283 c_field_class
= self
._tc
.create_string_field_class()
284 d_field_class
= self
._tc
.create_signed_enumeration_field_class(
287 e_field_class
= self
._tc
.create_structure_field_class()
289 ('c_string', c_field_class
),
290 ('d_enum', d_field_class
),
291 ('e_struct', e_field_class
),
293 self
.assertEqual(self
._fc
['a_float'].field_class
.addr
, a_field_class
.addr
)
294 self
.assertEqual(self
._fc
['a_float'].name
, 'a_float')
295 self
.assertEqual(self
._fc
['b_int'].field_class
.addr
, b_field_class
.addr
)
296 self
.assertEqual(self
._fc
['b_int'].name
, 'b_int')
297 self
.assertEqual(self
._fc
['c_string'].field_class
.addr
, c_field_class
.addr
)
298 self
.assertEqual(self
._fc
['c_string'].name
, 'c_string')
299 self
.assertEqual(self
._fc
['d_enum'].field_class
.addr
, d_field_class
.addr
)
300 self
.assertEqual(self
._fc
['d_enum'].name
, 'd_enum')
301 self
.assertEqual(self
._fc
['e_struct'].field_class
.addr
, e_field_class
.addr
)
302 self
.assertEqual(self
._fc
['e_struct'].name
, 'e_struct')
304 def test_bool_op(self
):
305 self
.assertFalse(self
._fc
)
306 self
._append
_element
_method
(self
._fc
, 'a', self
._tc
.create_string_field_class())
307 self
.assertTrue(self
._fc
)
310 self
._append
_element
_method
(self
._fc
, 'a', self
._tc
.create_string_field_class())
311 self
._append
_element
_method
(self
._fc
, 'b', self
._tc
.create_string_field_class())
312 self
._append
_element
_method
(self
._fc
, 'c', self
._tc
.create_string_field_class())
313 self
.assertEqual(len(self
._fc
), 3)
315 def test_getitem(self
):
316 a_fc
= self
._tc
.create_signed_integer_field_class(32)
317 b_fc
= self
._tc
.create_string_field_class()
318 c_fc
= self
._tc
.create_real_field_class()
319 self
._append
_element
_method
(self
._fc
, 'a', a_fc
)
320 self
._append
_element
_method
(self
._fc
, 'b', b_fc
)
321 self
._append
_element
_method
(self
._fc
, 'c', c_fc
)
322 self
.assertEqual(self
._fc
['b'].field_class
.addr
, b_fc
.addr
)
323 self
.assertEqual(self
._fc
['b'].name
, 'b')
325 def test_getitem_invalid_key_type(self
):
326 with self
.assertRaises(TypeError):
329 def test_getitem_invalid_key(self
):
330 with self
.assertRaises(KeyError):
333 def test_contains(self
):
334 self
.assertFalse('a' in self
._fc
)
335 self
._append
_element
_method
(self
._fc
, 'a', self
._tc
.create_string_field_class())
336 self
.assertTrue('a' in self
._fc
)
339 a_fc
= self
._tc
.create_signed_integer_field_class(32)
340 b_fc
= self
._tc
.create_string_field_class()
341 c_fc
= self
._tc
.create_real_field_class()
342 elements
= (('a', a_fc
), ('b', b_fc
), ('c', c_fc
))
344 for elem
in elements
:
345 self
._append
_element
_method
(self
._fc
, *elem
)
347 for (name
, element
), test_elem
in zip(self
._fc
.items(), elements
):
348 self
.assertEqual(element
.name
, test_elem
[0])
349 self
.assertEqual(name
, element
.name
)
350 self
.assertEqual(element
.field_class
.addr
, test_elem
[1].addr
)
352 def test_at_index(self
):
353 a_fc
= self
._tc
.create_signed_integer_field_class(32)
354 b_fc
= self
._tc
.create_string_field_class()
355 c_fc
= self
._tc
.create_real_field_class()
356 self
._append
_element
_method
(self
._fc
, 'c', c_fc
)
357 self
._append
_element
_method
(self
._fc
, 'a', a_fc
)
358 self
._append
_element
_method
(self
._fc
, 'b', b_fc
)
359 elem
= self
._at
_index
_method
(self
._fc
, 1)
360 self
.assertEqual(elem
.field_class
.addr
, a_fc
.addr
)
361 self
.assertEqual(elem
.name
, 'a')
363 def test_at_index_invalid(self
):
364 self
._append
_element
_method
(
365 self
._fc
, 'c', self
._tc
.create_signed_integer_field_class(32)
368 with self
.assertRaises(TypeError):
369 self
._at
_index
_method
(self
._fc
, 'yes')
371 def test_at_index_out_of_bounds_after(self
):
372 self
._append
_element
_method
(
373 self
._fc
, 'c', self
._tc
.create_signed_integer_field_class(32)
376 with self
.assertRaises(IndexError):
377 self
._at
_index
_method
(self
._fc
, len(self
._fc
))
380 class StructureFieldClassTestCase(_TestElementContainer
, unittest
.TestCase
):
381 _append_element_method
= staticmethod(bt2
._StructureFieldClass
.append_member
)
382 _at_index_method
= staticmethod(bt2
._StructureFieldClass
.member_at_index
)
384 def _create_default_fc(self
):
385 return self
._tc
.create_structure_field_class()
388 class VariantFieldClassWithoutSelectorTestCase(
389 _TestElementContainer
, unittest
.TestCase
391 _append_element_method
= staticmethod(
392 bt2
._VariantFieldClassWithoutSelector
.append_option
394 _at_index_method
= staticmethod(
395 bt2
._VariantFieldClassWithoutSelector
.option_at_index
398 def _create_default_fc(self
):
399 return self
._tc
.create_variant_field_class()
402 class _VariantFieldClassWithSelectorTestCase
:
404 self
._tc
= get_default_trace_class()
406 self
._fc
= self
._create
_default
_fc
()
408 def _create_default_fc(self
):
409 return self
._tc
.create_variant_field_class(self
._selector
_fc
)
411 def test_create_default(self
):
412 self
.assertIsNotNone(self
._fc
)
414 def test_append_element(self
):
415 str_field_class
= self
._tc
.create_string_field_class()
416 self
._fc
.append_option('str', str_field_class
, self
._ranges
1)
417 opt
= self
._fc
['str']
418 self
.assertEqual(opt
.field_class
.addr
, str_field_class
.addr
)
419 self
.assertEqual(opt
.name
, 'str')
420 self
.assertEqual(opt
.ranges
.addr
, self
._ranges
1.addr
)
422 def test_append_element_kwargs(self
):
423 int_field_class
= self
._tc
.create_signed_integer_field_class(32)
424 self
._fc
.append_option(
425 name
='int32', field_class
=int_field_class
, ranges
=self
._ranges
1
427 opt
= self
._fc
['int32']
428 self
.assertEqual(opt
.field_class
.addr
, int_field_class
.addr
)
429 self
.assertEqual(opt
.name
, 'int32')
430 self
.assertEqual(opt
.ranges
.addr
, self
._ranges
1.addr
)
432 def test_append_element_invalid_name(self
):
433 sub_fc
= self
._tc
.create_string_field_class()
435 with self
.assertRaises(TypeError):
436 self
._fc
.append_option(self
._fc
, 23, sub_fc
)
438 def test_append_element_invalid_field_class(self
):
439 with self
.assertRaises(TypeError):
440 self
._fc
.append_option(self
._fc
, 'yes', object())
442 def test_append_element_invalid_ranges(self
):
443 sub_fc
= self
._tc
.create_string_field_class()
445 with self
.assertRaises(TypeError):
446 self
._fc
.append_option(self
._fc
, sub_fc
, 'lel')
448 def test_append_element_dup_name(self
):
449 sub_fc1
= self
._tc
.create_string_field_class()
450 sub_fc2
= self
._tc
.create_string_field_class()
452 with self
.assertRaises(ValueError):
453 self
._fc
.append_option('yes', sub_fc1
, self
._ranges
1)
454 self
._fc
.append_option('yes', sub_fc2
, self
._ranges
2)
456 def test_append_element_invalid_ranges_signedness(self
):
457 sub_fc
= self
._tc
.create_string_field_class()
459 with self
.assertRaises(TypeError):
460 self
._fc
.append_option(self
._fc
, sub_fc
, self
._inval
_ranges
)
463 other_fc
= self
._create
_default
_fc
()
464 a_field_class
= self
._tc
.create_real_field_class()
465 self
._fc
.append_option('a_float', a_field_class
, self
._ranges
1)
466 c_field_class
= self
._tc
.create_string_field_class()
467 d_field_class
= self
._tc
.create_signed_enumeration_field_class(
471 ('c_string', c_field_class
, self
._ranges
2),
472 ('d_enum', d_field_class
, self
._ranges
3),
474 self
.assertEqual(self
._fc
['a_float'].field_class
.addr
, a_field_class
.addr
)
475 self
.assertEqual(self
._fc
['a_float'].name
, 'a_float')
476 self
.assertEqual(self
._fc
['a_float'].ranges
, self
._ranges
1)
477 self
.assertEqual(self
._fc
['c_string'].field_class
.addr
, c_field_class
.addr
)
478 self
.assertEqual(self
._fc
['c_string'].name
, 'c_string')
479 self
.assertEqual(self
._fc
['c_string'].ranges
, self
._ranges
2)
480 self
.assertEqual(self
._fc
['d_enum'].field_class
.addr
, d_field_class
.addr
)
481 self
.assertEqual(self
._fc
['d_enum'].name
, 'd_enum')
482 self
.assertEqual(self
._fc
['d_enum'].ranges
, self
._ranges
3)
484 def test_bool_op(self
):
485 self
.assertFalse(self
._fc
)
486 self
._fc
.append_option('a', self
._tc
.create_string_field_class(), self
._ranges
1)
487 self
.assertTrue(self
._fc
)
490 self
._fc
.append_option('a', self
._tc
.create_string_field_class(), self
._ranges
1)
491 self
._fc
.append_option('b', self
._tc
.create_string_field_class(), self
._ranges
2)
492 self
._fc
.append_option('c', self
._tc
.create_string_field_class(), self
._ranges
3)
493 self
.assertEqual(len(self
._fc
), 3)
495 def test_getitem(self
):
496 a_fc
= self
._tc
.create_signed_integer_field_class(32)
497 b_fc
= self
._tc
.create_string_field_class()
498 c_fc
= self
._tc
.create_real_field_class()
499 self
._fc
.append_option('a', a_fc
, self
._ranges
1)
500 self
._fc
.append_option('b', b_fc
, self
._ranges
2)
501 self
._fc
.append_option('c', c_fc
, self
._ranges
3)
502 self
.assertEqual(self
._fc
['b'].field_class
.addr
, b_fc
.addr
)
503 self
.assertEqual(self
._fc
['b'].name
, 'b')
504 self
.assertEqual(self
._fc
['b'].ranges
.addr
, self
._ranges
2.addr
)
506 def test_getitem_invalid_key_type(self
):
507 with self
.assertRaises(TypeError):
510 def test_getitem_invalid_key(self
):
511 with self
.assertRaises(KeyError):
514 def test_contains(self
):
515 self
.assertFalse('a' in self
._fc
)
516 self
._fc
.append_option('a', self
._tc
.create_string_field_class(), self
._ranges
1)
517 self
.assertTrue('a' in self
._fc
)
520 a_fc
= self
._tc
.create_signed_integer_field_class(32)
521 b_fc
= self
._tc
.create_string_field_class()
522 c_fc
= self
._tc
.create_real_field_class()
524 ('a', a_fc
, self
._ranges
1),
525 ('b', b_fc
, self
._ranges
2),
526 ('c', c_fc
, self
._ranges
3),
530 self
._fc
.append_option(*opt
)
532 for (name
, opt
), test_opt
in zip(self
._fc
.items(), opts
):
533 self
.assertEqual(opt
.name
, test_opt
[0])
534 self
.assertEqual(name
, opt
.name
)
535 self
.assertEqual(opt
.field_class
.addr
, test_opt
[1].addr
)
536 self
.assertEqual(opt
.ranges
.addr
, test_opt
[2].addr
)
538 def test_at_index(self
):
539 a_fc
= self
._tc
.create_signed_integer_field_class(32)
540 b_fc
= self
._tc
.create_string_field_class()
541 c_fc
= self
._tc
.create_real_field_class()
542 self
._fc
.append_option('c', c_fc
, self
._ranges
1)
543 self
._fc
.append_option('a', a_fc
, self
._ranges
2)
544 self
._fc
.append_option('b', b_fc
, self
._ranges
3)
545 self
.assertEqual(self
._fc
.option_at_index(1).field_class
.addr
, a_fc
.addr
)
546 self
.assertEqual(self
._fc
.option_at_index(1).name
, 'a')
547 self
.assertEqual(self
._fc
.option_at_index(1).ranges
.addr
, self
._ranges
2.addr
)
549 def test_at_index_invalid(self
):
550 self
._fc
.append_option(
551 'c', self
._tc
.create_signed_integer_field_class(32), self
._ranges
3
554 with self
.assertRaises(TypeError):
555 self
._fc
.option_at_index('yes')
557 def test_at_index_out_of_bounds_after(self
):
558 self
._fc
.append_option(
559 'c', self
._tc
.create_signed_integer_field_class(32), self
._ranges
3
562 with self
.assertRaises(IndexError):
563 self
._fc
.option_at_index(len(self
._fc
))
565 def _fill_default_fc_for_field_path_test(self
):
566 # Create something equivalent to:
568 # struct outer_struct_fc {
570 # struct inner_struct_fc {
571 # [u]int64_t selector;
574 # variant <selector> {
575 # real a; // selected with self._ranges1
576 # int21_t b; // selected with self._ranges2
577 # uint34_t c; // selected with self._ranges3
581 self
._fc
.append_option('a', self
._tc
.create_real_field_class(), self
._ranges
1)
582 self
._fc
.append_option(
583 'b', self
._tc
.create_signed_integer_field_class(21), self
._ranges
2
585 self
._fc
.append_option(
586 'c', self
._tc
.create_unsigned_integer_field_class(34), self
._ranges
3
589 foo_fc
= self
._tc
.create_real_field_class()
590 bar_fc
= self
._tc
.create_string_field_class()
591 baz_fc
= self
._tc
.create_string_field_class()
593 inner_struct_fc
= self
._tc
.create_structure_field_class()
594 inner_struct_fc
.append_member('selector', self
._selector
_fc
)
595 inner_struct_fc
.append_member('bar', bar_fc
)
596 inner_struct_fc
.append_member('baz', baz_fc
)
597 inner_struct_fc
.append_member('variant', self
._fc
)
599 inner_struct_array_fc
= self
._tc
.create_static_array_field_class(
603 outer_struct_fc
= self
._tc
.create_structure_field_class()
604 outer_struct_fc
.append_member('foo', foo_fc
)
605 outer_struct_fc
.append_member('inner_struct', inner_struct_array_fc
)
607 # The path to the selector field is resolved when the sequence is
608 # actually used, for example in a packet context.
609 self
._tc
.create_stream_class(
610 supports_packets
=True, packet_context_field_class
=outer_struct_fc
613 def test_selector_field_path_length(self
):
614 self
._fill
_default
_fc
_for
_field
_path
_test
()
615 self
.assertEqual(len(self
._fc
.selector_field_path
), 3)
617 def test_selector_field_path_iter(self
):
618 self
._fill
_default
_fc
_for
_field
_path
_test
()
619 path_items
= list(self
._fc
.selector_field_path
)
621 self
.assertEqual(len(path_items
), 3)
623 self
.assertIsInstance(path_items
[0], bt2
._IndexFieldPathItem
)
624 self
.assertEqual(path_items
[0].index
, 1)
626 self
.assertIsInstance(path_items
[1], bt2
._CurrentArrayElementFieldPathItem
)
628 self
.assertIsInstance(path_items
[2], bt2
._IndexFieldPathItem
)
629 self
.assertEqual(path_items
[2].index
, 0)
631 def test_selector_field_path_root_scope(self
):
632 self
._fill
_default
_fc
_for
_field
_path
_test
()
634 self
._fc
.selector_field_path
.root_scope
, bt2
.FieldPathScope
.PACKET_CONTEXT
638 class VariantFieldClassWithUnsignedSelectorTestCase(
639 _VariantFieldClassWithSelectorTestCase
, unittest
.TestCase
641 def _spec_set_up(self
):
642 self
._ranges
1 = bt2
.UnsignedIntegerRangeSet([(1, 4), (18, 47)])
643 self
._ranges
2 = bt2
.UnsignedIntegerRangeSet([(5, 5)])
644 self
._ranges
3 = bt2
.UnsignedIntegerRangeSet([(8, 16), (48, 99)])
645 self
._inval
_ranges
= bt2
.SignedIntegerRangeSet([(-8, 16), (48, 99)])
646 self
._selector
_fc
= self
._tc
.create_unsigned_integer_field_class()
649 class VariantFieldClassWithSignedSelectorTestCase(
650 _VariantFieldClassWithSelectorTestCase
, unittest
.TestCase
652 def _spec_set_up(self
):
653 self
._ranges
1 = bt2
.SignedIntegerRangeSet([(-10, -4), (18, 47)])
654 self
._ranges
2 = bt2
.SignedIntegerRangeSet([(-3, -3)])
655 self
._ranges
3 = bt2
.SignedIntegerRangeSet([(8, 16), (48, 99)])
656 self
._inval
_ranges
= bt2
.UnsignedIntegerRangeSet([(8, 16), (48, 99)])
657 self
._selector
_fc
= self
._tc
.create_signed_integer_field_class()
660 class StaticArrayFieldClassTestCase(unittest
.TestCase
):
662 self
._tc
= get_default_trace_class()
663 self
._elem
_fc
= self
._tc
.create_signed_integer_field_class(23)
665 def test_create_default(self
):
666 fc
= self
._tc
.create_static_array_field_class(self
._elem
_fc
, 45)
667 self
.assertEqual(fc
.element_field_class
.addr
, self
._elem
_fc
.addr
)
668 self
.assertEqual(fc
.length
, 45)
670 def test_create_invalid_elem_field_class(self
):
671 with self
.assertRaises(TypeError):
672 self
._tc
.create_static_array_field_class(object(), 45)
674 def test_create_invalid_length(self
):
675 with self
.assertRaises(ValueError):
676 self
._tc
.create_static_array_field_class(
677 self
._tc
.create_string_field_class(), -17
680 def test_create_invalid_length_type(self
):
681 with self
.assertRaises(TypeError):
682 self
._tc
.create_static_array_field_class(
683 self
._tc
.create_string_field_class(), 'the length'
687 class DynamicArrayFieldClassTestCase(unittest
.TestCase
):
689 self
._tc
= get_default_trace_class()
690 self
._elem
_fc
= self
._tc
.create_signed_integer_field_class(23)
691 self
._len
_fc
= self
._tc
.create_unsigned_integer_field_class(12)
693 def test_create_default(self
):
694 fc
= self
._tc
.create_dynamic_array_field_class(self
._elem
_fc
)
695 self
.assertEqual(fc
.element_field_class
.addr
, self
._elem
_fc
.addr
)
696 self
.assertIsNone(fc
.length_field_path
, None)
698 def _create_field_class_for_field_path_test(self
):
699 # Create something a field class that is equivalent to:
701 # struct outer_struct_fc {
703 # struct inner_struct_fc {
707 # uint23_t dyn_array[len];
711 fc
= self
._tc
.create_dynamic_array_field_class(self
._elem
_fc
, self
._len
_fc
)
713 foo_fc
= self
._tc
.create_real_field_class()
714 bar_fc
= self
._tc
.create_string_field_class()
715 baz_fc
= self
._tc
.create_string_field_class()
717 inner_struct_fc
= self
._tc
.create_structure_field_class()
718 inner_struct_fc
.append_member('bar', bar_fc
)
719 inner_struct_fc
.append_member('baz', baz_fc
)
720 inner_struct_fc
.append_member('len', self
._len
_fc
)
721 inner_struct_fc
.append_member('dyn_array', fc
)
723 inner_struct_array_fc
= self
._tc
.create_static_array_field_class(
727 outer_struct_fc
= self
._tc
.create_structure_field_class()
728 outer_struct_fc
.append_member('foo', foo_fc
)
729 outer_struct_fc
.append_member('inner_struct', inner_struct_array_fc
)
731 # The path to the length field is resolved when the sequence is
732 # actually used, for example in a packet context.
733 self
._tc
.create_stream_class(
734 packet_context_field_class
=outer_struct_fc
, supports_packets
=True
739 def test_field_path_len(self
):
740 fc
= self
._create
_field
_class
_for
_field
_path
_test
()
741 self
.assertEqual(len(fc
.length_field_path
), 3)
743 def test_field_path_iter(self
):
744 fc
= self
._create
_field
_class
_for
_field
_path
_test
()
745 path_items
= list(fc
.length_field_path
)
747 self
.assertEqual(len(path_items
), 3)
749 self
.assertIsInstance(path_items
[0], bt2
._IndexFieldPathItem
)
750 self
.assertEqual(path_items
[0].index
, 1)
752 self
.assertIsInstance(path_items
[1], bt2
._CurrentArrayElementFieldPathItem
)
754 self
.assertIsInstance(path_items
[2], bt2
._IndexFieldPathItem
)
755 self
.assertEqual(path_items
[2].index
, 2)
757 def test_field_path_root_scope(self
):
758 fc
= self
._create
_field
_class
_for
_field
_path
_test
()
760 fc
.length_field_path
.root_scope
, bt2
.FieldPathScope
.PACKET_CONTEXT
763 def test_create_invalid_field_class(self
):
764 with self
.assertRaises(TypeError):
765 self
._tc
.create_dynamic_array_field_class(object())
767 def test_create_invalid_length_type(self
):
768 with self
.assertRaises(TypeError):
769 self
._tc
.create_dynamic_array_field_class(
770 self
._tc
.create_string_field_class(), 17
774 if __name__
== "__main__":