bt2: make bt2.Error wrap current thread's error
[babeltrace.git] / tests / bindings / python / bt2 / test_field_class.py
1 #
2 # Copyright (C) 2019 EfficiOS Inc.
3 #
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
7 # of the License.
8 #
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.
13 #
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.
17 #
18
19 import bt2.field
20 import unittest
21 import bt2
22 import collections
23 from utils import get_default_trace_class
24
25
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)
31
32 def test_create_range(self):
33 fc = self._create_func(field_value_range=35)
34 self.assertEqual(fc.field_value_range, 35)
35
36 fc = self._create_func(36)
37 self.assertEqual(fc.field_value_range, 36)
38
39 def test_create_invalid_range(self):
40 with self.assertRaises(TypeError):
41 self._create_func('yes')
42
43 with self.assertRaises(TypeError):
44 self._create_func(field_value_range='yes')
45
46 with self.assertRaises(ValueError):
47 self._create_func(field_value_range=-2)
48
49 with self.assertRaises(ValueError):
50 self._create_func(field_value_range=0)
51
52 def test_create_base(self):
53 fc = self._create_func(
54 preferred_display_base=bt2.IntegerDisplayBase.HEXADECIMAL
55 )
56 self.assertEqual(fc.preferred_display_base, bt2.IntegerDisplayBase.HEXADECIMAL)
57
58 def test_create_invalid_base_type(self):
59 with self.assertRaises(TypeError):
60 self._create_func(preferred_display_base='yes')
61
62 def test_create_invalid_base_value(self):
63 with self.assertRaises(ValueError):
64 self._create_func(preferred_display_base=444)
65
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)
70
71
72 class IntegerFieldClassTestCase(_TestIntegerFieldClassProps, unittest.TestCase):
73 def setUp(self):
74 self._tc = get_default_trace_class()
75 self._create_func = self._tc.create_signed_integer_field_class
76
77
78 class RealFieldClassTestCase(unittest.TestCase):
79 def setUp(self):
80 self._tc = get_default_trace_class()
81
82 def test_create_default(self):
83 fc = self._tc.create_real_field_class()
84 self.assertFalse(fc.is_single_precision)
85
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)
89
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')
93
94
95 # Converts an _EnumerationFieldClassMapping to a list of ranges:
96 #
97 # [(lower0, upper0), (lower1, upper1), ...]
98
99
100 def enum_mapping_to_set(mapping):
101 return {(x.lower, x.upper) for x in mapping.ranges}
102
103
104 class _EnumerationFieldClassTestCase(_TestIntegerFieldClassProps):
105 def setUp(self):
106 self._tc = get_default_trace_class()
107 self._spec_set_up()
108 self._fc = self._create_func()
109
110 def test_create_from_invalid_type(self):
111 with self.assertRaises(TypeError):
112 self._create_func('coucou')
113
114 def test_add_mapping_simple(self):
115 self._fc.add_mapping('hello', self._ranges1)
116 mapping = self._fc['hello']
117 self.assertEqual(mapping.label, 'hello')
118 self.assertEqual(mapping.ranges, self._ranges1)
119
120 def test_add_mapping_simple_kwargs(self):
121 self._fc.add_mapping(label='hello', ranges=self._ranges1)
122 mapping = self._fc['hello']
123 self.assertEqual(mapping.label, 'hello')
124 self.assertEqual(mapping.ranges, self._ranges1)
125
126 def test_add_mapping_invalid_name(self):
127 with self.assertRaises(TypeError):
128 self._fc.add_mapping(17, self._ranges1)
129
130 def test_add_mapping_invalid_range(self):
131 with self.assertRaises(TypeError):
132 self._fc.add_mapping('allo', 'meow')
133
134 def test_add_mapping_dup_label(self):
135 with self.assertRaises(ValueError):
136 self._fc.add_mapping('a', self._ranges1)
137 self._fc.add_mapping('a', self._ranges2)
138
139 def test_add_mapping_invalid_ranges_signedness(self):
140 with self.assertRaises(TypeError):
141 self._fc.add_mapping('allo', self._inval_ranges)
142
143 def test_iadd(self):
144 self._fc.add_mapping('c', self._ranges1)
145
146 self._fc += [('d', self._ranges2), ('e', self._ranges3)]
147
148 self.assertEqual(len(self._fc), 3)
149 self.assertEqual(self._fc['c'].label, 'c')
150 self.assertEqual(self._fc['c'].ranges, self._ranges1)
151 self.assertEqual(self._fc['d'].label, 'd')
152 self.assertEqual(self._fc['d'].ranges, self._ranges2)
153 self.assertEqual(self._fc['e'].label, 'e')
154 self.assertEqual(self._fc['e'].ranges, self._ranges3)
155
156 def test_bool_op(self):
157 self.assertFalse(self._fc)
158 self._fc.add_mapping('a', self._ranges1)
159 self.assertTrue(self._fc)
160
161 def test_len(self):
162 self._fc.add_mapping('a', self._ranges1)
163 self._fc.add_mapping('b', self._ranges2)
164 self._fc.add_mapping('c', self._ranges3)
165 self.assertEqual(len(self._fc), 3)
166
167 def test_getitem(self):
168 self._fc.add_mapping('a', self._ranges1)
169 self._fc.add_mapping('b', self._ranges2)
170 self._fc.add_mapping('c', self._ranges3)
171 mapping = self._fc['a']
172 self.assertEqual(mapping.label, 'a')
173 self.assertEqual(mapping.ranges, self._ranges1)
174
175 def test_getitem_nonexistent(self):
176 with self.assertRaises(KeyError):
177 self._fc['doesnotexist']
178
179 def test_iter(self):
180 self._fc.add_mapping('a', self._ranges1)
181 self._fc.add_mapping('b', self._ranges2)
182 self._fc.add_mapping('c', self._ranges3)
183
184 # This exercises iteration.
185 labels = sorted(self._fc)
186
187 self.assertEqual(labels, ['a', 'b', 'c'])
188
189 def test_find_by_value(self):
190 self._fc.add_mapping('a', self._ranges1)
191 self._fc.add_mapping('b', self._ranges2)
192 self._fc.add_mapping('c', self._ranges3)
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)
197
198
199 class UnsignedEnumerationFieldClassTestCase(
200 _EnumerationFieldClassTestCase, unittest.TestCase
201 ):
202 def _spec_set_up(self):
203 self._ranges1 = bt2.UnsignedIntegerRangeSet([(1, 4), (18, 47)])
204 self._ranges2 = bt2.UnsignedIntegerRangeSet([(5, 5)])
205 self._ranges3 = 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
209
210
211 class SignedEnumerationFieldClassTestCase(
212 _EnumerationFieldClassTestCase, unittest.TestCase
213 ):
214 def _spec_set_up(self):
215 self._ranges1 = bt2.SignedIntegerRangeSet([(-10, -4), (18, 47)])
216 self._ranges2 = bt2.SignedIntegerRangeSet([(-3, -3)])
217 self._ranges3 = 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
221
222
223 class StringFieldClassTestCase(unittest.TestCase):
224 def setUp(self):
225 tc = get_default_trace_class()
226 self._fc = tc.create_string_field_class()
227
228 def test_create_default(self):
229 self.assertIsNotNone(self._fc)
230
231
232 class _TestElementContainer:
233 def setUp(self):
234 self._tc = get_default_trace_class()
235 self._fc = self._create_default_fc()
236
237 def test_create_default(self):
238 self.assertIsNotNone(self._fc)
239
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)
245
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)
251
252 def test_append_element_invalid_name(self):
253 sub_fc = self._tc.create_string_field_class()
254
255 with self.assertRaises(TypeError):
256 self._append_element_method(self._fc, 23, sub_fc)
257
258 def test_append_element_invalid_field_class(self):
259 with self.assertRaises(TypeError):
260 self._append_element_method(self._fc, 'yes', object())
261
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()
265
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)
269
270 def test_iadd(self):
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(
278 field_value_range=32
279 )
280 e_field_class = self._tc.create_structure_field_class()
281 self._fc += [
282 ('c_string', c_field_class),
283 ('d_enum', d_field_class),
284 ('e_struct', e_field_class),
285 ]
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')
296
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)
301
302 def test_len(self):
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)
307
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')
317
318 def test_getitem_invalid_key_type(self):
319 with self.assertRaises(TypeError):
320 self._fc[0]
321
322 def test_getitem_invalid_key(self):
323 with self.assertRaises(KeyError):
324 self._fc['no way']
325
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)
330
331 def test_iter(self):
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))
336
337 for elem in elements:
338 self._append_element_method(self._fc, *elem)
339
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)
344
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')
355
356 def test_at_index_invalid(self):
357 self._append_element_method(
358 self._fc, 'c', self._tc.create_signed_integer_field_class(32)
359 )
360
361 with self.assertRaises(TypeError):
362 self._at_index_method(self._fc, 'yes')
363
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)
367 )
368
369 with self.assertRaises(IndexError):
370 self._at_index_method(self._fc, len(self._fc))
371
372
373 class StructureFieldClassTestCase(_TestElementContainer, unittest.TestCase):
374 _append_element_method = staticmethod(
375 bt2.field_class._StructureFieldClass.append_member
376 )
377 _at_index_method = staticmethod(
378 bt2.field_class._StructureFieldClass.member_at_index
379 )
380
381 def _create_default_fc(self):
382 return self._tc.create_structure_field_class()
383
384
385 class VariantFieldClassWithoutSelectorTestCase(
386 _TestElementContainer, unittest.TestCase
387 ):
388 _append_element_method = staticmethod(
389 bt2.field_class._VariantFieldClassWithoutSelector.append_option
390 )
391 _at_index_method = staticmethod(
392 bt2.field_class._VariantFieldClassWithoutSelector.option_at_index
393 )
394
395 def _create_default_fc(self):
396 return self._tc.create_variant_field_class()
397
398
399 class _VariantFieldClassWithSelectorTestCase:
400 def setUp(self):
401 self._tc = get_default_trace_class()
402 self._spec_set_up()
403 self._fc = self._create_default_fc()
404
405 def _create_default_fc(self):
406 return self._tc.create_variant_field_class(self._selector_fc)
407
408 def test_create_default(self):
409 self.assertIsNotNone(self._fc)
410
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._ranges1)
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._ranges1.addr)
418
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._ranges1
423 )
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._ranges1.addr)
428
429 def test_append_element_invalid_name(self):
430 sub_fc = self._tc.create_string_field_class()
431
432 with self.assertRaises(TypeError):
433 self._fc.append_option(self._fc, 23, sub_fc)
434
435 def test_append_element_invalid_field_class(self):
436 with self.assertRaises(TypeError):
437 self._fc.append_option(self._fc, 'yes', object())
438
439 def test_append_element_invalid_ranges(self):
440 sub_fc = self._tc.create_string_field_class()
441
442 with self.assertRaises(TypeError):
443 self._fc.append_option(self._fc, sub_fc, 'lel')
444
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()
448
449 with self.assertRaises(ValueError):
450 self._fc.append_option('yes', sub_fc1, self._ranges1)
451 self._fc.append_option('yes', sub_fc2, self._ranges2)
452
453 def test_append_element_invalid_ranges_signedness(self):
454 sub_fc = self._tc.create_string_field_class()
455
456 with self.assertRaises(TypeError):
457 self._fc.append_option(self._fc, sub_fc, self._inval_ranges)
458
459 def test_iadd(self):
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._ranges1)
463 c_field_class = self._tc.create_string_field_class()
464 d_field_class = self._tc.create_signed_enumeration_field_class(
465 field_value_range=32
466 )
467 self._fc += [
468 ('c_string', c_field_class, self._ranges2),
469 ('d_enum', d_field_class, self._ranges3),
470 ]
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._ranges1)
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._ranges2)
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._ranges3)
480
481 def test_bool_op(self):
482 self.assertFalse(self._fc)
483 self._fc.append_option('a', self._tc.create_string_field_class(), self._ranges1)
484 self.assertTrue(self._fc)
485
486 def test_len(self):
487 self._fc.append_option('a', self._tc.create_string_field_class(), self._ranges1)
488 self._fc.append_option('b', self._tc.create_string_field_class(), self._ranges2)
489 self._fc.append_option('c', self._tc.create_string_field_class(), self._ranges3)
490 self.assertEqual(len(self._fc), 3)
491
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._ranges1)
497 self._fc.append_option('b', b_fc, self._ranges2)
498 self._fc.append_option('c', c_fc, self._ranges3)
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._ranges2.addr)
502
503 def test_getitem_invalid_key_type(self):
504 with self.assertRaises(TypeError):
505 self._fc[0]
506
507 def test_getitem_invalid_key(self):
508 with self.assertRaises(KeyError):
509 self._fc['no way']
510
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._ranges1)
514 self.assertTrue('a' in self._fc)
515
516 def test_iter(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 opts = (
521 ('a', a_fc, self._ranges1),
522 ('b', b_fc, self._ranges2),
523 ('c', c_fc, self._ranges3),
524 )
525
526 for opt in opts:
527 self._fc.append_option(*opt)
528
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)
534
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._ranges1)
540 self._fc.append_option('a', a_fc, self._ranges2)
541 self._fc.append_option('b', b_fc, self._ranges3)
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._ranges2.addr)
545
546 def test_at_index_invalid(self):
547 self._fc.append_option(
548 'c', self._tc.create_signed_integer_field_class(32), self._ranges3
549 )
550
551 with self.assertRaises(TypeError):
552 self._fc.option_at_index('yes')
553
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._ranges3
557 )
558
559 with self.assertRaises(IndexError):
560 self._fc.option_at_index(len(self._fc))
561
562 def _fill_default_fc_for_field_path_test(self):
563 # Create something equivalent to:
564 #
565 # struct outer_struct_fc {
566 # real foo;
567 # struct inner_struct_fc {
568 # [u]int64_t selector;
569 # string bar;
570 # string baz;
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
575 # } variant;
576 # } inner_struct[2];
577 # };
578 self._fc.append_option('a', self._tc.create_real_field_class(), self._ranges1)
579 self._fc.append_option(
580 'b', self._tc.create_signed_integer_field_class(21), self._ranges2
581 )
582 self._fc.append_option(
583 'c', self._tc.create_unsigned_integer_field_class(34), self._ranges3
584 )
585
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()
589
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)
595
596 inner_struct_array_fc = self._tc.create_static_array_field_class(
597 inner_struct_fc, 2
598 )
599
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)
603
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
608 )
609
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)
613
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)
617
618 self.assertEqual(len(path_items), 3)
619
620 self.assertIsInstance(path_items[0], bt2.field_path._IndexFieldPathItem)
621 self.assertEqual(path_items[0].index, 1)
622
623 self.assertIsInstance(
624 path_items[1], bt2.field_path._CurrentArrayElementFieldPathItem
625 )
626
627 self.assertIsInstance(path_items[2], bt2.field_path._IndexFieldPathItem)
628 self.assertEqual(path_items[2].index, 0)
629
630 def test_selector_field_path_root_scope(self):
631 self._fill_default_fc_for_field_path_test()
632 self.assertEqual(
633 self._fc.selector_field_path.root_scope, bt2.field_path.Scope.PACKET_CONTEXT
634 )
635
636
637 class VariantFieldClassWithUnsignedSelectorTestCase(
638 _VariantFieldClassWithSelectorTestCase, unittest.TestCase
639 ):
640 def _spec_set_up(self):
641 self._ranges1 = bt2.UnsignedIntegerRangeSet([(1, 4), (18, 47)])
642 self._ranges2 = bt2.UnsignedIntegerRangeSet([(5, 5)])
643 self._ranges3 = 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()
646
647
648 class VariantFieldClassWithSignedSelectorTestCase(
649 _VariantFieldClassWithSelectorTestCase, unittest.TestCase
650 ):
651 def _spec_set_up(self):
652 self._ranges1 = bt2.SignedIntegerRangeSet([(-10, -4), (18, 47)])
653 self._ranges2 = bt2.SignedIntegerRangeSet([(-3, -3)])
654 self._ranges3 = 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()
657
658
659 class StaticArrayFieldClassTestCase(unittest.TestCase):
660 def setUp(self):
661 self._tc = get_default_trace_class()
662 self._elem_fc = self._tc.create_signed_integer_field_class(23)
663
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)
668
669 def test_create_invalid_elem_field_class(self):
670 with self.assertRaises(TypeError):
671 self._tc.create_static_array_field_class(object(), 45)
672
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
677 )
678
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'
683 )
684
685
686 class DynamicArrayFieldClassTestCase(unittest.TestCase):
687 def setUp(self):
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)
691
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)
696
697 def _create_field_class_for_field_path_test(self):
698 # Create something a field class that is equivalent to:
699 #
700 # struct outer_struct_fc {
701 # real foo;
702 # struct inner_struct_fc {
703 # string bar;
704 # string baz;
705 # uint12_t len;
706 # uint23_t dyn_array[len];
707 # } inner_struct[2];
708 # };
709
710 fc = self._tc.create_dynamic_array_field_class(self._elem_fc, self._len_fc)
711
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()
715
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)
721
722 inner_struct_array_fc = self._tc.create_static_array_field_class(
723 inner_struct_fc, 2
724 )
725
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)
729
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
734 )
735
736 return fc
737
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)
741
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)
745
746 self.assertEqual(len(path_items), 3)
747
748 self.assertIsInstance(path_items[0], bt2.field_path._IndexFieldPathItem)
749 self.assertEqual(path_items[0].index, 1)
750
751 self.assertIsInstance(
752 path_items[1], bt2.field_path._CurrentArrayElementFieldPathItem
753 )
754
755 self.assertIsInstance(path_items[2], bt2.field_path._IndexFieldPathItem)
756 self.assertEqual(path_items[2].index, 2)
757
758 def test_field_path_root_scope(self):
759 fc = self._create_field_class_for_field_path_test()
760 self.assertEqual(
761 fc.length_field_path.root_scope, bt2.field_path.Scope.PACKET_CONTEXT
762 )
763
764 def test_create_invalid_field_class(self):
765 with self.assertRaises(TypeError):
766 self._tc.create_dynamic_array_field_class(object())
767
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
772 )
773
774
775 if __name__ == "__main__":
776 unittest.main()
This page took 0.065788 seconds and 4 git commands to generate.