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
(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), ...]
97 def enum_mapping_to_set(mapping
):
98 return {(x
.lower
, x
.upper
) for x
in mapping
.ranges
}
101 class _EnumerationFieldClassTestCase(_TestIntegerFieldClassProps
):
103 self
._tc
= get_default_trace_class()
105 self
._fc
= self
._create
_func
()
107 def test_create_from_invalid_type(self
):
108 with self
.assertRaises(TypeError):
109 self
._create
_func
('coucou')
111 def test_add_mapping_simple(self
):
112 self
._fc
.add_mapping('hello', self
._ranges
1)
113 mapping
= self
._fc
['hello']
114 self
.assertEqual(mapping
.label
, 'hello')
115 self
.assertEqual(mapping
.ranges
, self
._ranges
1)
117 def test_add_mapping_simple_kwargs(self
):
118 self
._fc
.add_mapping(label
='hello', ranges
=self
._ranges
1)
119 mapping
= self
._fc
['hello']
120 self
.assertEqual(mapping
.label
, 'hello')
121 self
.assertEqual(mapping
.ranges
, self
._ranges
1)
123 def test_add_mapping_invalid_name(self
):
124 with self
.assertRaises(TypeError):
125 self
._fc
.add_mapping(17, self
._ranges
1)
127 def test_add_mapping_invalid_range(self
):
128 with self
.assertRaises(TypeError):
129 self
._fc
.add_mapping('allo', 'meow')
131 def test_add_mapping_dup_label(self
):
132 with self
.assertRaises(bt2
.Error
):
133 self
._fc
.add_mapping('a', self
._ranges
1)
134 self
._fc
.add_mapping('a', self
._ranges
2)
136 def test_add_mapping_invalid_ranges_signedness(self
):
137 with self
.assertRaises(TypeError):
138 self
._fc
.add_mapping('allo', self
._inval
_ranges
)
141 self
._fc
.add_mapping('c', self
._ranges
1)
144 ('d', self
._ranges
2),
145 ('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(_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(_EnumerationFieldClassTestCase
, unittest
.TestCase
):
210 def _spec_set_up(self
):
211 self
._ranges
1 = bt2
.SignedIntegerRangeSet([(-10, -4), (18, 47)])
212 self
._ranges
2 = bt2
.SignedIntegerRangeSet([(-3, -3)])
213 self
._ranges
3 = bt2
.SignedIntegerRangeSet([(-100, -1), (8, 16), (48, 99)])
214 self
._inval
_ranges
= bt2
.UnsignedIntegerRangeSet([(8, 16), (48, 99)])
215 self
._value
_in
_range
_1_and
_3 = -7
216 self
._create
_func
= self
._tc
.create_signed_enumeration_field_class
219 class StringFieldClassTestCase(unittest
.TestCase
):
221 tc
= get_default_trace_class()
222 self
._fc
= tc
.create_string_field_class()
224 def test_create_default(self
):
225 self
.assertIsNotNone(self
._fc
)
228 class _TestElementContainer():
230 self
._tc
= get_default_trace_class()
231 self
._fc
= self
._create
_default
_fc
()
233 def test_create_default(self
):
234 self
.assertIsNotNone(self
._fc
)
236 def test_append_element(self
):
237 int_field_class
= self
._tc
.create_signed_integer_field_class(32)
238 self
._append
_element
_method
(self
._fc
, 'int32', int_field_class
)
239 field_class
= self
._fc
['int32'].field_class
240 self
.assertEqual(field_class
.addr
, int_field_class
.addr
)
242 def test_append_element_kwargs(self
):
243 int_field_class
= self
._tc
.create_signed_integer_field_class(32)
244 self
._append
_element
_method
(self
._fc
, name
='int32', field_class
=int_field_class
)
245 field_class
= self
._fc
['int32'].field_class
246 self
.assertEqual(field_class
.addr
, int_field_class
.addr
)
248 def test_append_element_invalid_name(self
):
249 sub_fc
= self
._tc
.create_string_field_class()
251 with self
.assertRaises(TypeError):
252 self
._append
_element
_method
(self
._fc
, 23, sub_fc
)
254 def test_append_element_invalid_field_class(self
):
255 with self
.assertRaises(TypeError):
256 self
._append
_element
_method
(self
._fc
, 'yes', object())
258 def test_append_element_dup_name(self
):
259 sub_fc1
= self
._tc
.create_string_field_class()
260 sub_fc2
= self
._tc
.create_string_field_class()
262 with self
.assertRaises(bt2
.Error
):
263 self
._append
_element
_method
(self
._fc
, 'yes', sub_fc1
)
264 self
._append
_element
_method
(self
._fc
, 'yes', sub_fc2
)
267 other_fc
= self
._create
_default
_fc
()
268 a_field_class
= self
._tc
.create_real_field_class()
269 b_field_class
= self
._tc
.create_signed_integer_field_class(17)
270 self
._append
_element
_method
(self
._fc
, 'a_float', a_field_class
)
271 self
._append
_element
_method
(self
._fc
, 'b_int', b_field_class
)
272 c_field_class
= self
._tc
.create_string_field_class()
273 d_field_class
= self
._tc
.create_signed_enumeration_field_class(field_value_range
=32)
274 e_field_class
= self
._tc
.create_structure_field_class()
276 ('c_string', c_field_class
),
277 ('d_enum', d_field_class
),
278 ('e_struct', e_field_class
),
280 self
.assertEqual(self
._fc
['a_float'].field_class
.addr
, a_field_class
.addr
)
281 self
.assertEqual(self
._fc
['a_float'].name
, 'a_float')
282 self
.assertEqual(self
._fc
['b_int'].field_class
.addr
, b_field_class
.addr
)
283 self
.assertEqual(self
._fc
['b_int'].name
, 'b_int')
284 self
.assertEqual(self
._fc
['c_string'].field_class
.addr
, c_field_class
.addr
)
285 self
.assertEqual(self
._fc
['c_string'].name
, 'c_string')
286 self
.assertEqual(self
._fc
['d_enum'].field_class
.addr
, d_field_class
.addr
)
287 self
.assertEqual(self
._fc
['d_enum'].name
, 'd_enum')
288 self
.assertEqual(self
._fc
['e_struct'].field_class
.addr
, e_field_class
.addr
)
289 self
.assertEqual(self
._fc
['e_struct'].name
, 'e_struct')
291 def test_bool_op(self
):
292 self
.assertFalse(self
._fc
)
293 self
._append
_element
_method
(self
._fc
, 'a', self
._tc
.create_string_field_class())
294 self
.assertTrue(self
._fc
)
297 self
._append
_element
_method
(self
._fc
, 'a', self
._tc
.create_string_field_class())
298 self
._append
_element
_method
(self
._fc
, 'b', self
._tc
.create_string_field_class())
299 self
._append
_element
_method
(self
._fc
, 'c', self
._tc
.create_string_field_class())
300 self
.assertEqual(len(self
._fc
), 3)
302 def test_getitem(self
):
303 a_fc
= self
._tc
.create_signed_integer_field_class(32)
304 b_fc
= self
._tc
.create_string_field_class()
305 c_fc
= self
._tc
.create_real_field_class()
306 self
._append
_element
_method
(self
._fc
, 'a', a_fc
)
307 self
._append
_element
_method
(self
._fc
, 'b', b_fc
)
308 self
._append
_element
_method
(self
._fc
, 'c', c_fc
)
309 self
.assertEqual(self
._fc
['b'].field_class
.addr
, b_fc
.addr
)
310 self
.assertEqual(self
._fc
['b'].name
, 'b')
312 def test_getitem_invalid_key_type(self
):
313 with self
.assertRaises(TypeError):
316 def test_getitem_invalid_key(self
):
317 with self
.assertRaises(KeyError):
320 def test_contains(self
):
321 self
.assertFalse('a' in self
._fc
)
322 self
._append
_element
_method
(self
._fc
, 'a', self
._tc
.create_string_field_class())
323 self
.assertTrue('a' in self
._fc
)
326 a_fc
= self
._tc
.create_signed_integer_field_class(32)
327 b_fc
= self
._tc
.create_string_field_class()
328 c_fc
= self
._tc
.create_real_field_class()
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
(self
._fc
, 'c', self
._tc
.create_signed_integer_field_class(32))
357 with self
.assertRaises(TypeError):
358 self
._at
_index
_method
(self
._fc
, 'yes')
360 def test_at_index_out_of_bounds_after(self
):
361 self
._append
_element
_method
(self
._fc
, 'c', self
._tc
.create_signed_integer_field_class(32))
363 with self
.assertRaises(IndexError):
364 self
._at
_index
_method
(self
._fc
, len(self
._fc
))
367 class StructureFieldClassTestCase(_TestElementContainer
, unittest
.TestCase
):
368 _append_element_method
= staticmethod(bt2
.field_class
._StructureFieldClass
.append_member
)
369 _at_index_method
= staticmethod(bt2
.field_class
._StructureFieldClass
.member_at_index
)
371 def _create_default_fc(self
):
372 return self
._tc
.create_structure_field_class()
375 class VariantFieldClassWithoutSelectorTestCase(_TestElementContainer
, unittest
.TestCase
):
376 _append_element_method
= staticmethod(bt2
.field_class
._VariantFieldClassWithoutSelector
.append_option
)
377 _at_index_method
= staticmethod(bt2
.field_class
._VariantFieldClassWithoutSelector
.option_at_index
)
379 def _create_default_fc(self
):
380 return self
._tc
.create_variant_field_class()
383 class _VariantFieldClassWithSelectorTestCase
:
385 self
._tc
= get_default_trace_class()
387 self
._fc
= self
._create
_default
_fc
()
389 def _create_default_fc(self
):
390 return self
._tc
.create_variant_field_class(self
._selector
_fc
)
392 def test_create_default(self
):
393 self
.assertIsNotNone(self
._fc
)
395 def test_append_element(self
):
396 str_field_class
= self
._tc
.create_string_field_class()
397 self
._fc
.append_option('str', str_field_class
, self
._ranges
1)
398 opt
= self
._fc
['str']
399 self
.assertEqual(opt
.field_class
.addr
, str_field_class
.addr
)
400 self
.assertEqual(opt
.name
, 'str')
401 self
.assertEqual(opt
.ranges
.addr
, self
._ranges
1.addr
)
403 def test_append_element_kwargs(self
):
404 int_field_class
= self
._tc
.create_signed_integer_field_class(32)
405 self
._fc
.append_option(name
='int32', field_class
=int_field_class
,
406 ranges
=self
._ranges
1)
407 opt
= self
._fc
['int32']
408 self
.assertEqual(opt
.field_class
.addr
, int_field_class
.addr
)
409 self
.assertEqual(opt
.name
, 'int32')
410 self
.assertEqual(opt
.ranges
.addr
, self
._ranges
1.addr
)
412 def test_append_element_invalid_name(self
):
413 sub_fc
= self
._tc
.create_string_field_class()
415 with self
.assertRaises(TypeError):
416 self
._fc
.append_option(self
._fc
, 23, sub_fc
)
418 def test_append_element_invalid_field_class(self
):
419 with self
.assertRaises(TypeError):
420 self
._fc
.append_option(self
._fc
, 'yes', object())
422 def test_append_element_invalid_ranges(self
):
423 sub_fc
= self
._tc
.create_string_field_class()
425 with self
.assertRaises(TypeError):
426 self
._fc
.append_option(self
._fc
, sub_fc
, 'lel')
428 def test_append_element_dup_name(self
):
429 sub_fc1
= self
._tc
.create_string_field_class()
430 sub_fc2
= self
._tc
.create_string_field_class()
432 with self
.assertRaises(bt2
.Error
):
433 self
._fc
.append_option('yes', sub_fc1
, self
._ranges
1)
434 self
._fc
.append_option('yes', sub_fc2
, self
._ranges
2)
436 def test_append_element_invalid_ranges_signedness(self
):
437 sub_fc
= self
._tc
.create_string_field_class()
439 with self
.assertRaises(TypeError):
440 self
._fc
.append_option(self
._fc
, sub_fc
, self
._inval
_ranges
)
443 other_fc
= self
._create
_default
_fc
()
444 a_field_class
= self
._tc
.create_real_field_class()
445 self
._fc
.append_option('a_float', a_field_class
, self
._ranges
1)
446 c_field_class
= self
._tc
.create_string_field_class()
447 d_field_class
= self
._tc
.create_signed_enumeration_field_class(field_value_range
=32)
449 ('c_string', c_field_class
, self
._ranges
2),
450 ('d_enum', d_field_class
, self
._ranges
3),
452 self
.assertEqual(self
._fc
['a_float'].field_class
.addr
, a_field_class
.addr
)
453 self
.assertEqual(self
._fc
['a_float'].name
, 'a_float')
454 self
.assertEqual(self
._fc
['a_float'].ranges
, self
._ranges
1)
455 self
.assertEqual(self
._fc
['c_string'].field_class
.addr
, c_field_class
.addr
)
456 self
.assertEqual(self
._fc
['c_string'].name
, 'c_string')
457 self
.assertEqual(self
._fc
['c_string'].ranges
, self
._ranges
2)
458 self
.assertEqual(self
._fc
['d_enum'].field_class
.addr
, d_field_class
.addr
)
459 self
.assertEqual(self
._fc
['d_enum'].name
, 'd_enum')
460 self
.assertEqual(self
._fc
['d_enum'].ranges
, self
._ranges
3)
462 def test_bool_op(self
):
463 self
.assertFalse(self
._fc
)
464 self
._fc
.append_option('a', self
._tc
.create_string_field_class(), self
._ranges
1)
465 self
.assertTrue(self
._fc
)
468 self
._fc
.append_option('a', self
._tc
.create_string_field_class(), self
._ranges
1)
469 self
._fc
.append_option('b', self
._tc
.create_string_field_class(), self
._ranges
2)
470 self
._fc
.append_option('c', self
._tc
.create_string_field_class(), self
._ranges
3)
471 self
.assertEqual(len(self
._fc
), 3)
473 def test_getitem(self
):
474 a_fc
= self
._tc
.create_signed_integer_field_class(32)
475 b_fc
= self
._tc
.create_string_field_class()
476 c_fc
= self
._tc
.create_real_field_class()
477 self
._fc
.append_option('a', a_fc
, self
._ranges
1)
478 self
._fc
.append_option('b', b_fc
, self
._ranges
2)
479 self
._fc
.append_option('c', c_fc
, self
._ranges
3)
480 self
.assertEqual(self
._fc
['b'].field_class
.addr
, b_fc
.addr
)
481 self
.assertEqual(self
._fc
['b'].name
, 'b')
482 self
.assertEqual(self
._fc
['b'].ranges
.addr
, self
._ranges
2.addr
)
484 def test_getitem_invalid_key_type(self
):
485 with self
.assertRaises(TypeError):
488 def test_getitem_invalid_key(self
):
489 with self
.assertRaises(KeyError):
492 def test_contains(self
):
493 self
.assertFalse('a' in self
._fc
)
494 self
._fc
.append_option('a', self
._tc
.create_string_field_class(), self
._ranges
1)
495 self
.assertTrue('a' in self
._fc
)
498 a_fc
= self
._tc
.create_signed_integer_field_class(32)
499 b_fc
= self
._tc
.create_string_field_class()
500 c_fc
= self
._tc
.create_real_field_class()
502 ('a', a_fc
, self
._ranges
1),
503 ('b', b_fc
, self
._ranges
2),
504 ('c', c_fc
, self
._ranges
3),
508 self
._fc
.append_option(*opt
)
510 for (name
, opt
), test_opt
in zip(self
._fc
.items(), opts
):
511 self
.assertEqual(opt
.name
, test_opt
[0])
512 self
.assertEqual(name
, opt
.name
)
513 self
.assertEqual(opt
.field_class
.addr
, test_opt
[1].addr
)
514 self
.assertEqual(opt
.ranges
.addr
, test_opt
[2].addr
)
516 def test_at_index(self
):
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()
520 self
._fc
.append_option('c', c_fc
, self
._ranges
1)
521 self
._fc
.append_option('a', a_fc
, self
._ranges
2)
522 self
._fc
.append_option('b', b_fc
, self
._ranges
3)
523 self
.assertEqual(self
._fc
.option_at_index(1).field_class
.addr
, a_fc
.addr
)
524 self
.assertEqual(self
._fc
.option_at_index(1).name
, 'a')
525 self
.assertEqual(self
._fc
.option_at_index(1).ranges
.addr
, self
._ranges
2.addr
)
527 def test_at_index_invalid(self
):
528 self
._fc
.append_option('c', self
._tc
.create_signed_integer_field_class(32), self
._ranges
3)
530 with self
.assertRaises(TypeError):
531 self
._fc
.option_at_index('yes')
533 def test_at_index_out_of_bounds_after(self
):
534 self
._fc
.append_option('c', self
._tc
.create_signed_integer_field_class(32), self
._ranges
3)
536 with self
.assertRaises(IndexError):
537 self
._fc
.option_at_index(len(self
._fc
))
539 def _fill_default_fc_for_field_path_test(self
):
540 # Create something equivalent to:
542 # struct outer_struct_fc {
544 # struct inner_struct_fc {
545 # [u]int64_t selector;
548 # variant <selector> {
549 # real a; // selected with self._ranges1
550 # int21_t b; // selected with self._ranges2
551 # uint34_t c; // selected with self._ranges3
555 self
._fc
.append_option('a', self
._tc
.create_real_field_class(), self
._ranges
1)
556 self
._fc
.append_option('b', self
._tc
.create_signed_integer_field_class(21), self
._ranges
2)
557 self
._fc
.append_option('c', self
._tc
.create_unsigned_integer_field_class(34), self
._ranges
3)
559 foo_fc
= self
._tc
.create_real_field_class()
560 bar_fc
= self
._tc
.create_string_field_class()
561 baz_fc
= self
._tc
.create_string_field_class()
563 inner_struct_fc
= self
._tc
.create_structure_field_class()
564 inner_struct_fc
.append_member('selector', self
._selector
_fc
)
565 inner_struct_fc
.append_member('bar', bar_fc
)
566 inner_struct_fc
.append_member('baz', baz_fc
)
567 inner_struct_fc
.append_member('variant', self
._fc
)
569 inner_struct_array_fc
= self
._tc
.create_static_array_field_class(inner_struct_fc
, 2)
571 outer_struct_fc
= self
._tc
.create_structure_field_class()
572 outer_struct_fc
.append_member('foo', foo_fc
)
573 outer_struct_fc
.append_member('inner_struct', inner_struct_array_fc
)
575 # The path to the selector field is resolved when the sequence is
576 # actually used, for example in a packet context.
577 self
._tc
.create_stream_class(supports_packets
=True,
578 packet_context_field_class
=outer_struct_fc
)
580 def test_selector_field_path_length(self
):
581 self
._fill
_default
_fc
_for
_field
_path
_test
()
582 self
.assertEqual(len(self
._fc
.selector_field_path
), 3)
584 def test_selector_field_path_iter(self
):
585 self
._fill
_default
_fc
_for
_field
_path
_test
()
586 path_items
= list(self
._fc
.selector_field_path
)
588 self
.assertEqual(len(path_items
), 3)
590 self
.assertIsInstance(path_items
[0], bt2
.field_path
._IndexFieldPathItem
)
591 self
.assertEqual(path_items
[0].index
, 1)
593 self
.assertIsInstance(path_items
[1], bt2
.field_path
._CurrentArrayElementFieldPathItem
)
595 self
.assertIsInstance(path_items
[2], bt2
.field_path
._IndexFieldPathItem
)
596 self
.assertEqual(path_items
[2].index
, 0)
598 def test_selector_field_path_root_scope(self
):
599 self
._fill
_default
_fc
_for
_field
_path
_test
()
600 self
.assertEqual(self
._fc
.selector_field_path
.root_scope
, bt2
.field_path
.Scope
.PACKET_CONTEXT
)
603 class VariantFieldClassWithUnsignedSelectorTestCase(_VariantFieldClassWithSelectorTestCase
, unittest
.TestCase
):
604 def _spec_set_up(self
):
605 self
._ranges
1 = bt2
.UnsignedIntegerRangeSet([(1, 4), (18, 47)])
606 self
._ranges
2 = bt2
.UnsignedIntegerRangeSet([(5, 5)])
607 self
._ranges
3 = bt2
.UnsignedIntegerRangeSet([(8, 16), (48, 99)])
608 self
._inval
_ranges
= bt2
.SignedIntegerRangeSet([(-8, 16), (48, 99)])
609 self
._selector
_fc
= self
._tc
.create_unsigned_integer_field_class()
612 class VariantFieldClassWithSignedSelectorTestCase(_VariantFieldClassWithSelectorTestCase
, unittest
.TestCase
):
613 def _spec_set_up(self
):
614 self
._ranges
1 = bt2
.SignedIntegerRangeSet([(-10, -4), (18, 47)])
615 self
._ranges
2 = bt2
.SignedIntegerRangeSet([(-3, -3)])
616 self
._ranges
3 = bt2
.SignedIntegerRangeSet([(8, 16), (48, 99)])
617 self
._inval
_ranges
= bt2
.UnsignedIntegerRangeSet([(8, 16), (48, 99)])
618 self
._selector
_fc
= self
._tc
.create_signed_integer_field_class()
621 class StaticArrayFieldClassTestCase(unittest
.TestCase
):
623 self
._tc
= get_default_trace_class()
624 self
._elem
_fc
= self
._tc
.create_signed_integer_field_class(23)
626 def test_create_default(self
):
627 fc
= self
._tc
.create_static_array_field_class(self
._elem
_fc
, 45)
628 self
.assertEqual(fc
.element_field_class
.addr
, self
._elem
_fc
.addr
)
629 self
.assertEqual(fc
.length
, 45)
631 def test_create_invalid_elem_field_class(self
):
632 with self
.assertRaises(TypeError):
633 self
._tc
.create_static_array_field_class(object(), 45)
635 def test_create_invalid_length(self
):
636 with self
.assertRaises(ValueError):
637 self
._tc
.create_static_array_field_class(self
._tc
.create_string_field_class(), -17)
639 def test_create_invalid_length_type(self
):
640 with self
.assertRaises(TypeError):
641 self
._tc
.create_static_array_field_class(self
._tc
.create_string_field_class(), 'the length')
644 class DynamicArrayFieldClassTestCase(unittest
.TestCase
):
646 self
._tc
= get_default_trace_class()
647 self
._elem
_fc
= self
._tc
.create_signed_integer_field_class(23)
648 self
._len
_fc
= self
._tc
.create_unsigned_integer_field_class(12)
650 def test_create_default(self
):
651 fc
= self
._tc
.create_dynamic_array_field_class(self
._elem
_fc
)
652 self
.assertEqual(fc
.element_field_class
.addr
, self
._elem
_fc
.addr
)
653 self
.assertIsNone(fc
.length_field_path
, None)
655 def _create_field_class_for_field_path_test(self
):
656 # Create something a field class that is equivalent to:
658 # struct outer_struct_fc {
660 # struct inner_struct_fc {
664 # uint23_t dyn_array[len];
668 fc
= self
._tc
.create_dynamic_array_field_class(self
._elem
_fc
, self
._len
_fc
)
670 foo_fc
= self
._tc
.create_real_field_class()
671 bar_fc
= self
._tc
.create_string_field_class()
672 baz_fc
= self
._tc
.create_string_field_class()
674 inner_struct_fc
= self
._tc
.create_structure_field_class()
675 inner_struct_fc
.append_member('bar', bar_fc
)
676 inner_struct_fc
.append_member('baz', baz_fc
)
677 inner_struct_fc
.append_member('len', self
._len
_fc
)
678 inner_struct_fc
.append_member('dyn_array', fc
)
680 inner_struct_array_fc
= self
._tc
.create_static_array_field_class(inner_struct_fc
, 2)
682 outer_struct_fc
= self
._tc
.create_structure_field_class()
683 outer_struct_fc
.append_member('foo', foo_fc
)
684 outer_struct_fc
.append_member('inner_struct', inner_struct_array_fc
)
686 # The path to the length field is resolved when the sequence is
687 # actually used, for example in a packet context.
688 self
._tc
.create_stream_class(packet_context_field_class
=outer_struct_fc
,
689 supports_packets
=True)
693 def test_field_path_len(self
):
694 fc
= self
._create
_field
_class
_for
_field
_path
_test
()
695 self
.assertEqual(len(fc
.length_field_path
), 3)
697 def test_field_path_iter(self
):
698 fc
= self
._create
_field
_class
_for
_field
_path
_test
()
699 path_items
= list(fc
.length_field_path
)
701 self
.assertEqual(len(path_items
), 3)
703 self
.assertIsInstance(path_items
[0], bt2
.field_path
._IndexFieldPathItem
)
704 self
.assertEqual(path_items
[0].index
, 1)
706 self
.assertIsInstance(path_items
[1], bt2
.field_path
._CurrentArrayElementFieldPathItem
)
708 self
.assertIsInstance(path_items
[2], bt2
.field_path
._IndexFieldPathItem
)
709 self
.assertEqual(path_items
[2].index
, 2)
711 def test_field_path_root_scope(self
):
712 fc
= self
._create
_field
_class
_for
_field
_path
_test
()
713 self
.assertEqual(fc
.length_field_path
.root_scope
, bt2
.field_path
.Scope
.PACKET_CONTEXT
)
715 def test_create_invalid_field_class(self
):
716 with self
.assertRaises(TypeError):
717 self
._tc
.create_dynamic_array_field_class(object())
719 def test_create_invalid_length_type(self
):
720 with self
.assertRaises(TypeError):
721 self
._tc
.create_dynamic_array_field_class(self
._tc
.create_string_field_class(), 17)
724 if __name__
== "__main__":