bt2: remove unused imports
[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 unittest
20 import bt2
21 from utils import get_default_trace_class
22
23
24 class _TestIntegerFieldClassProps:
25 def test_create_default(self):
26 fc = self._create_func()
27 self.assertEqual(fc.field_value_range, 64)
28 self.assertEqual(fc.preferred_display_base, bt2.IntegerDisplayBase.DECIMAL)
29
30 def test_create_range(self):
31 fc = self._create_func(field_value_range=35)
32 self.assertEqual(fc.field_value_range, 35)
33
34 fc = self._create_func(36)
35 self.assertEqual(fc.field_value_range, 36)
36
37 def test_create_invalid_range(self):
38 with self.assertRaises(TypeError):
39 self._create_func('yes')
40
41 with self.assertRaises(TypeError):
42 self._create_func(field_value_range='yes')
43
44 with self.assertRaises(ValueError):
45 self._create_func(field_value_range=-2)
46
47 with self.assertRaises(ValueError):
48 self._create_func(field_value_range=0)
49
50 def test_create_base(self):
51 fc = self._create_func(
52 preferred_display_base=bt2.IntegerDisplayBase.HEXADECIMAL
53 )
54 self.assertEqual(fc.preferred_display_base, bt2.IntegerDisplayBase.HEXADECIMAL)
55
56 def test_create_invalid_base_type(self):
57 with self.assertRaises(TypeError):
58 self._create_func(preferred_display_base='yes')
59
60 def test_create_invalid_base_value(self):
61 with self.assertRaises(ValueError):
62 self._create_func(preferred_display_base=444)
63
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)
68
69
70 class IntegerFieldClassTestCase(_TestIntegerFieldClassProps, unittest.TestCase):
71 def setUp(self):
72 self._tc = get_default_trace_class()
73 self._create_func = self._tc.create_signed_integer_field_class
74
75
76 class RealFieldClassTestCase(unittest.TestCase):
77 def setUp(self):
78 self._tc = get_default_trace_class()
79
80 def test_create_default(self):
81 fc = self._tc.create_real_field_class()
82 self.assertFalse(fc.is_single_precision)
83
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)
87
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')
91
92
93 # Converts an _EnumerationFieldClassMapping to a list of ranges:
94 #
95 # [(lower0, upper0), (lower1, upper1), ...]
96
97
98 def enum_mapping_to_set(mapping):
99 return {(x.lower, x.upper) for x in mapping.ranges}
100
101
102 class _EnumerationFieldClassTestCase(_TestIntegerFieldClassProps):
103 def setUp(self):
104 self._tc = get_default_trace_class()
105 self._spec_set_up()
106 self._fc = self._create_func()
107
108 def test_create_from_invalid_type(self):
109 with self.assertRaises(TypeError):
110 self._create_func('coucou')
111
112 def test_add_mapping_simple(self):
113 self._fc.add_mapping('hello', self._ranges1)
114 mapping = self._fc['hello']
115 self.assertEqual(mapping.label, 'hello')
116 self.assertEqual(mapping.ranges, self._ranges1)
117
118 def test_add_mapping_simple_kwargs(self):
119 self._fc.add_mapping(label='hello', ranges=self._ranges1)
120 mapping = self._fc['hello']
121 self.assertEqual(mapping.label, 'hello')
122 self.assertEqual(mapping.ranges, self._ranges1)
123
124 def test_add_mapping_invalid_name(self):
125 with self.assertRaises(TypeError):
126 self._fc.add_mapping(17, self._ranges1)
127
128 def test_add_mapping_invalid_range(self):
129 with self.assertRaises(TypeError):
130 self._fc.add_mapping('allo', 'meow')
131
132 def test_add_mapping_dup_label(self):
133 with self.assertRaises(ValueError):
134 self._fc.add_mapping('a', self._ranges1)
135 self._fc.add_mapping('a', self._ranges2)
136
137 def test_add_mapping_invalid_ranges_signedness(self):
138 with self.assertRaises(TypeError):
139 self._fc.add_mapping('allo', self._inval_ranges)
140
141 def test_iadd(self):
142 self._fc.add_mapping('c', self._ranges1)
143
144 self._fc += [('d', self._ranges2), ('e', self._ranges3)]
145
146 self.assertEqual(len(self._fc), 3)
147 self.assertEqual(self._fc['c'].label, 'c')
148 self.assertEqual(self._fc['c'].ranges, self._ranges1)
149 self.assertEqual(self._fc['d'].label, 'd')
150 self.assertEqual(self._fc['d'].ranges, self._ranges2)
151 self.assertEqual(self._fc['e'].label, 'e')
152 self.assertEqual(self._fc['e'].ranges, self._ranges3)
153
154 def test_bool_op(self):
155 self.assertFalse(self._fc)
156 self._fc.add_mapping('a', self._ranges1)
157 self.assertTrue(self._fc)
158
159 def test_len(self):
160 self._fc.add_mapping('a', self._ranges1)
161 self._fc.add_mapping('b', self._ranges2)
162 self._fc.add_mapping('c', self._ranges3)
163 self.assertEqual(len(self._fc), 3)
164
165 def test_getitem(self):
166 self._fc.add_mapping('a', self._ranges1)
167 self._fc.add_mapping('b', self._ranges2)
168 self._fc.add_mapping('c', self._ranges3)
169 mapping = self._fc['a']
170 self.assertEqual(mapping.label, 'a')
171 self.assertEqual(mapping.ranges, self._ranges1)
172
173 def test_getitem_nonexistent(self):
174 with self.assertRaises(KeyError):
175 self._fc['doesnotexist']
176
177 def test_iter(self):
178 self._fc.add_mapping('a', self._ranges1)
179 self._fc.add_mapping('b', self._ranges2)
180 self._fc.add_mapping('c', self._ranges3)
181
182 # This exercises iteration.
183 labels = sorted(self._fc)
184
185 self.assertEqual(labels, ['a', 'b', 'c'])
186
187 def test_find_by_value(self):
188 self._fc.add_mapping('a', self._ranges1)
189 self._fc.add_mapping('b', self._ranges2)
190 self._fc.add_mapping('c', self._ranges3)
191 mappings = self._fc.mappings_for_value(self._value_in_range_1_and_3)
192 labels = set([mapping.label for mapping in mappings])
193 expected_labels = set(['a', 'c'])
194 self.assertEqual(labels, expected_labels)
195
196
197 class UnsignedEnumerationFieldClassTestCase(
198 _EnumerationFieldClassTestCase, unittest.TestCase
199 ):
200 def _spec_set_up(self):
201 self._ranges1 = bt2.UnsignedIntegerRangeSet([(1, 4), (18, 47)])
202 self._ranges2 = bt2.UnsignedIntegerRangeSet([(5, 5)])
203 self._ranges3 = 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
207
208
209 class SignedEnumerationFieldClassTestCase(
210 _EnumerationFieldClassTestCase, unittest.TestCase
211 ):
212 def _spec_set_up(self):
213 self._ranges1 = bt2.SignedIntegerRangeSet([(-10, -4), (18, 47)])
214 self._ranges2 = bt2.SignedIntegerRangeSet([(-3, -3)])
215 self._ranges3 = bt2.SignedIntegerRangeSet([(-100, -1), (8, 16), (48, 99)])
216 self._inval_ranges = bt2.UnsignedIntegerRangeSet([(8, 16), (48, 99)])
217 self._value_in_range_1_and_3 = -7
218 self._create_func = self._tc.create_signed_enumeration_field_class
219
220
221 class StringFieldClassTestCase(unittest.TestCase):
222 def setUp(self):
223 tc = get_default_trace_class()
224 self._fc = tc.create_string_field_class()
225
226 def test_create_default(self):
227 self.assertIsNotNone(self._fc)
228
229
230 class _TestElementContainer:
231 def setUp(self):
232 self._tc = get_default_trace_class()
233 self._fc = self._create_default_fc()
234
235 def test_create_default(self):
236 self.assertIsNotNone(self._fc)
237
238 def test_append_element(self):
239 int_field_class = self._tc.create_signed_integer_field_class(32)
240 self._append_element_method(self._fc, 'int32', int_field_class)
241 field_class = self._fc['int32'].field_class
242 self.assertEqual(field_class.addr, int_field_class.addr)
243
244 def test_append_element_kwargs(self):
245 int_field_class = self._tc.create_signed_integer_field_class(32)
246 self._append_element_method(self._fc, name='int32', field_class=int_field_class)
247 field_class = self._fc['int32'].field_class
248 self.assertEqual(field_class.addr, int_field_class.addr)
249
250 def test_append_element_invalid_name(self):
251 sub_fc = self._tc.create_string_field_class()
252
253 with self.assertRaises(TypeError):
254 self._append_element_method(self._fc, 23, sub_fc)
255
256 def test_append_element_invalid_field_class(self):
257 with self.assertRaises(TypeError):
258 self._append_element_method(self._fc, 'yes', object())
259
260 def test_append_element_dup_name(self):
261 sub_fc1 = self._tc.create_string_field_class()
262 sub_fc2 = self._tc.create_string_field_class()
263
264 with self.assertRaises(ValueError):
265 self._append_element_method(self._fc, 'yes', sub_fc1)
266 self._append_element_method(self._fc, 'yes', sub_fc2)
267
268 def test_iadd(self):
269 other_fc = self._create_default_fc()
270 a_field_class = self._tc.create_real_field_class()
271 b_field_class = self._tc.create_signed_integer_field_class(17)
272 self._append_element_method(self._fc, 'a_float', a_field_class)
273 self._append_element_method(self._fc, 'b_int', b_field_class)
274 c_field_class = self._tc.create_string_field_class()
275 d_field_class = self._tc.create_signed_enumeration_field_class(
276 field_value_range=32
277 )
278 e_field_class = self._tc.create_structure_field_class()
279 self._fc += [
280 ('c_string', c_field_class),
281 ('d_enum', d_field_class),
282 ('e_struct', e_field_class),
283 ]
284 self.assertEqual(self._fc['a_float'].field_class.addr, a_field_class.addr)
285 self.assertEqual(self._fc['a_float'].name, 'a_float')
286 self.assertEqual(self._fc['b_int'].field_class.addr, b_field_class.addr)
287 self.assertEqual(self._fc['b_int'].name, 'b_int')
288 self.assertEqual(self._fc['c_string'].field_class.addr, c_field_class.addr)
289 self.assertEqual(self._fc['c_string'].name, 'c_string')
290 self.assertEqual(self._fc['d_enum'].field_class.addr, d_field_class.addr)
291 self.assertEqual(self._fc['d_enum'].name, 'd_enum')
292 self.assertEqual(self._fc['e_struct'].field_class.addr, e_field_class.addr)
293 self.assertEqual(self._fc['e_struct'].name, 'e_struct')
294
295 def test_bool_op(self):
296 self.assertFalse(self._fc)
297 self._append_element_method(self._fc, 'a', self._tc.create_string_field_class())
298 self.assertTrue(self._fc)
299
300 def test_len(self):
301 self._append_element_method(self._fc, 'a', self._tc.create_string_field_class())
302 self._append_element_method(self._fc, 'b', self._tc.create_string_field_class())
303 self._append_element_method(self._fc, 'c', self._tc.create_string_field_class())
304 self.assertEqual(len(self._fc), 3)
305
306 def test_getitem(self):
307 a_fc = self._tc.create_signed_integer_field_class(32)
308 b_fc = self._tc.create_string_field_class()
309 c_fc = self._tc.create_real_field_class()
310 self._append_element_method(self._fc, 'a', a_fc)
311 self._append_element_method(self._fc, 'b', b_fc)
312 self._append_element_method(self._fc, 'c', c_fc)
313 self.assertEqual(self._fc['b'].field_class.addr, b_fc.addr)
314 self.assertEqual(self._fc['b'].name, 'b')
315
316 def test_getitem_invalid_key_type(self):
317 with self.assertRaises(TypeError):
318 self._fc[0]
319
320 def test_getitem_invalid_key(self):
321 with self.assertRaises(KeyError):
322 self._fc['no way']
323
324 def test_contains(self):
325 self.assertFalse('a' in self._fc)
326 self._append_element_method(self._fc, 'a', self._tc.create_string_field_class())
327 self.assertTrue('a' in self._fc)
328
329 def test_iter(self):
330 a_fc = self._tc.create_signed_integer_field_class(32)
331 b_fc = self._tc.create_string_field_class()
332 c_fc = self._tc.create_real_field_class()
333 elements = (('a', a_fc), ('b', b_fc), ('c', c_fc))
334
335 for elem in elements:
336 self._append_element_method(self._fc, *elem)
337
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)
342
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')
353
354 def test_at_index_invalid(self):
355 self._append_element_method(
356 self._fc, 'c', self._tc.create_signed_integer_field_class(32)
357 )
358
359 with self.assertRaises(TypeError):
360 self._at_index_method(self._fc, 'yes')
361
362 def test_at_index_out_of_bounds_after(self):
363 self._append_element_method(
364 self._fc, 'c', self._tc.create_signed_integer_field_class(32)
365 )
366
367 with self.assertRaises(IndexError):
368 self._at_index_method(self._fc, len(self._fc))
369
370
371 class StructureFieldClassTestCase(_TestElementContainer, unittest.TestCase):
372 _append_element_method = staticmethod(bt2._StructureFieldClass.append_member)
373 _at_index_method = staticmethod(bt2._StructureFieldClass.member_at_index)
374
375 def _create_default_fc(self):
376 return self._tc.create_structure_field_class()
377
378
379 class VariantFieldClassWithoutSelectorTestCase(
380 _TestElementContainer, unittest.TestCase
381 ):
382 _append_element_method = staticmethod(
383 bt2._VariantFieldClassWithoutSelector.append_option
384 )
385 _at_index_method = staticmethod(
386 bt2._VariantFieldClassWithoutSelector.option_at_index
387 )
388
389 def _create_default_fc(self):
390 return self._tc.create_variant_field_class()
391
392
393 class _VariantFieldClassWithSelectorTestCase:
394 def setUp(self):
395 self._tc = get_default_trace_class()
396 self._spec_set_up()
397 self._fc = self._create_default_fc()
398
399 def _create_default_fc(self):
400 return self._tc.create_variant_field_class(self._selector_fc)
401
402 def test_create_default(self):
403 self.assertIsNotNone(self._fc)
404
405 def test_append_element(self):
406 str_field_class = self._tc.create_string_field_class()
407 self._fc.append_option('str', str_field_class, self._ranges1)
408 opt = self._fc['str']
409 self.assertEqual(opt.field_class.addr, str_field_class.addr)
410 self.assertEqual(opt.name, 'str')
411 self.assertEqual(opt.ranges.addr, self._ranges1.addr)
412
413 def test_append_element_kwargs(self):
414 int_field_class = self._tc.create_signed_integer_field_class(32)
415 self._fc.append_option(
416 name='int32', field_class=int_field_class, ranges=self._ranges1
417 )
418 opt = self._fc['int32']
419 self.assertEqual(opt.field_class.addr, int_field_class.addr)
420 self.assertEqual(opt.name, 'int32')
421 self.assertEqual(opt.ranges.addr, self._ranges1.addr)
422
423 def test_append_element_invalid_name(self):
424 sub_fc = self._tc.create_string_field_class()
425
426 with self.assertRaises(TypeError):
427 self._fc.append_option(self._fc, 23, sub_fc)
428
429 def test_append_element_invalid_field_class(self):
430 with self.assertRaises(TypeError):
431 self._fc.append_option(self._fc, 'yes', object())
432
433 def test_append_element_invalid_ranges(self):
434 sub_fc = self._tc.create_string_field_class()
435
436 with self.assertRaises(TypeError):
437 self._fc.append_option(self._fc, sub_fc, 'lel')
438
439 def test_append_element_dup_name(self):
440 sub_fc1 = self._tc.create_string_field_class()
441 sub_fc2 = self._tc.create_string_field_class()
442
443 with self.assertRaises(ValueError):
444 self._fc.append_option('yes', sub_fc1, self._ranges1)
445 self._fc.append_option('yes', sub_fc2, self._ranges2)
446
447 def test_append_element_invalid_ranges_signedness(self):
448 sub_fc = self._tc.create_string_field_class()
449
450 with self.assertRaises(TypeError):
451 self._fc.append_option(self._fc, sub_fc, self._inval_ranges)
452
453 def test_iadd(self):
454 other_fc = self._create_default_fc()
455 a_field_class = self._tc.create_real_field_class()
456 self._fc.append_option('a_float', a_field_class, self._ranges1)
457 c_field_class = self._tc.create_string_field_class()
458 d_field_class = self._tc.create_signed_enumeration_field_class(
459 field_value_range=32
460 )
461 self._fc += [
462 ('c_string', c_field_class, self._ranges2),
463 ('d_enum', d_field_class, self._ranges3),
464 ]
465 self.assertEqual(self._fc['a_float'].field_class.addr, a_field_class.addr)
466 self.assertEqual(self._fc['a_float'].name, 'a_float')
467 self.assertEqual(self._fc['a_float'].ranges, self._ranges1)
468 self.assertEqual(self._fc['c_string'].field_class.addr, c_field_class.addr)
469 self.assertEqual(self._fc['c_string'].name, 'c_string')
470 self.assertEqual(self._fc['c_string'].ranges, self._ranges2)
471 self.assertEqual(self._fc['d_enum'].field_class.addr, d_field_class.addr)
472 self.assertEqual(self._fc['d_enum'].name, 'd_enum')
473 self.assertEqual(self._fc['d_enum'].ranges, self._ranges3)
474
475 def test_bool_op(self):
476 self.assertFalse(self._fc)
477 self._fc.append_option('a', self._tc.create_string_field_class(), self._ranges1)
478 self.assertTrue(self._fc)
479
480 def test_len(self):
481 self._fc.append_option('a', self._tc.create_string_field_class(), self._ranges1)
482 self._fc.append_option('b', self._tc.create_string_field_class(), self._ranges2)
483 self._fc.append_option('c', self._tc.create_string_field_class(), self._ranges3)
484 self.assertEqual(len(self._fc), 3)
485
486 def test_getitem(self):
487 a_fc = self._tc.create_signed_integer_field_class(32)
488 b_fc = self._tc.create_string_field_class()
489 c_fc = self._tc.create_real_field_class()
490 self._fc.append_option('a', a_fc, self._ranges1)
491 self._fc.append_option('b', b_fc, self._ranges2)
492 self._fc.append_option('c', c_fc, self._ranges3)
493 self.assertEqual(self._fc['b'].field_class.addr, b_fc.addr)
494 self.assertEqual(self._fc['b'].name, 'b')
495 self.assertEqual(self._fc['b'].ranges.addr, self._ranges2.addr)
496
497 def test_getitem_invalid_key_type(self):
498 with self.assertRaises(TypeError):
499 self._fc[0]
500
501 def test_getitem_invalid_key(self):
502 with self.assertRaises(KeyError):
503 self._fc['no way']
504
505 def test_contains(self):
506 self.assertFalse('a' in self._fc)
507 self._fc.append_option('a', self._tc.create_string_field_class(), self._ranges1)
508 self.assertTrue('a' in self._fc)
509
510 def test_iter(self):
511 a_fc = self._tc.create_signed_integer_field_class(32)
512 b_fc = self._tc.create_string_field_class()
513 c_fc = self._tc.create_real_field_class()
514 opts = (
515 ('a', a_fc, self._ranges1),
516 ('b', b_fc, self._ranges2),
517 ('c', c_fc, self._ranges3),
518 )
519
520 for opt in opts:
521 self._fc.append_option(*opt)
522
523 for (name, opt), test_opt in zip(self._fc.items(), opts):
524 self.assertEqual(opt.name, test_opt[0])
525 self.assertEqual(name, opt.name)
526 self.assertEqual(opt.field_class.addr, test_opt[1].addr)
527 self.assertEqual(opt.ranges.addr, test_opt[2].addr)
528
529 def test_at_index(self):
530 a_fc = self._tc.create_signed_integer_field_class(32)
531 b_fc = self._tc.create_string_field_class()
532 c_fc = self._tc.create_real_field_class()
533 self._fc.append_option('c', c_fc, self._ranges1)
534 self._fc.append_option('a', a_fc, self._ranges2)
535 self._fc.append_option('b', b_fc, self._ranges3)
536 self.assertEqual(self._fc.option_at_index(1).field_class.addr, a_fc.addr)
537 self.assertEqual(self._fc.option_at_index(1).name, 'a')
538 self.assertEqual(self._fc.option_at_index(1).ranges.addr, self._ranges2.addr)
539
540 def test_at_index_invalid(self):
541 self._fc.append_option(
542 'c', self._tc.create_signed_integer_field_class(32), self._ranges3
543 )
544
545 with self.assertRaises(TypeError):
546 self._fc.option_at_index('yes')
547
548 def test_at_index_out_of_bounds_after(self):
549 self._fc.append_option(
550 'c', self._tc.create_signed_integer_field_class(32), self._ranges3
551 )
552
553 with self.assertRaises(IndexError):
554 self._fc.option_at_index(len(self._fc))
555
556 def _fill_default_fc_for_field_path_test(self):
557 # Create something equivalent to:
558 #
559 # struct outer_struct_fc {
560 # real foo;
561 # struct inner_struct_fc {
562 # [u]int64_t selector;
563 # string bar;
564 # string baz;
565 # variant <selector> {
566 # real a; // selected with self._ranges1
567 # int21_t b; // selected with self._ranges2
568 # uint34_t c; // selected with self._ranges3
569 # } variant;
570 # } inner_struct[2];
571 # };
572 self._fc.append_option('a', self._tc.create_real_field_class(), self._ranges1)
573 self._fc.append_option(
574 'b', self._tc.create_signed_integer_field_class(21), self._ranges2
575 )
576 self._fc.append_option(
577 'c', self._tc.create_unsigned_integer_field_class(34), self._ranges3
578 )
579
580 foo_fc = self._tc.create_real_field_class()
581 bar_fc = self._tc.create_string_field_class()
582 baz_fc = self._tc.create_string_field_class()
583
584 inner_struct_fc = self._tc.create_structure_field_class()
585 inner_struct_fc.append_member('selector', self._selector_fc)
586 inner_struct_fc.append_member('bar', bar_fc)
587 inner_struct_fc.append_member('baz', baz_fc)
588 inner_struct_fc.append_member('variant', self._fc)
589
590 inner_struct_array_fc = self._tc.create_static_array_field_class(
591 inner_struct_fc, 2
592 )
593
594 outer_struct_fc = self._tc.create_structure_field_class()
595 outer_struct_fc.append_member('foo', foo_fc)
596 outer_struct_fc.append_member('inner_struct', inner_struct_array_fc)
597
598 # The path to the selector field is resolved when the sequence is
599 # actually used, for example in a packet context.
600 self._tc.create_stream_class(
601 supports_packets=True, packet_context_field_class=outer_struct_fc
602 )
603
604 def test_selector_field_path_length(self):
605 self._fill_default_fc_for_field_path_test()
606 self.assertEqual(len(self._fc.selector_field_path), 3)
607
608 def test_selector_field_path_iter(self):
609 self._fill_default_fc_for_field_path_test()
610 path_items = list(self._fc.selector_field_path)
611
612 self.assertEqual(len(path_items), 3)
613
614 self.assertIsInstance(path_items[0], bt2._IndexFieldPathItem)
615 self.assertEqual(path_items[0].index, 1)
616
617 self.assertIsInstance(path_items[1], bt2._CurrentArrayElementFieldPathItem)
618
619 self.assertIsInstance(path_items[2], bt2._IndexFieldPathItem)
620 self.assertEqual(path_items[2].index, 0)
621
622 def test_selector_field_path_root_scope(self):
623 self._fill_default_fc_for_field_path_test()
624 self.assertEqual(
625 self._fc.selector_field_path.root_scope, bt2.FieldPathScope.PACKET_CONTEXT
626 )
627
628
629 class VariantFieldClassWithUnsignedSelectorTestCase(
630 _VariantFieldClassWithSelectorTestCase, unittest.TestCase
631 ):
632 def _spec_set_up(self):
633 self._ranges1 = bt2.UnsignedIntegerRangeSet([(1, 4), (18, 47)])
634 self._ranges2 = bt2.UnsignedIntegerRangeSet([(5, 5)])
635 self._ranges3 = bt2.UnsignedIntegerRangeSet([(8, 16), (48, 99)])
636 self._inval_ranges = bt2.SignedIntegerRangeSet([(-8, 16), (48, 99)])
637 self._selector_fc = self._tc.create_unsigned_integer_field_class()
638
639
640 class VariantFieldClassWithSignedSelectorTestCase(
641 _VariantFieldClassWithSelectorTestCase, unittest.TestCase
642 ):
643 def _spec_set_up(self):
644 self._ranges1 = bt2.SignedIntegerRangeSet([(-10, -4), (18, 47)])
645 self._ranges2 = bt2.SignedIntegerRangeSet([(-3, -3)])
646 self._ranges3 = bt2.SignedIntegerRangeSet([(8, 16), (48, 99)])
647 self._inval_ranges = bt2.UnsignedIntegerRangeSet([(8, 16), (48, 99)])
648 self._selector_fc = self._tc.create_signed_integer_field_class()
649
650
651 class StaticArrayFieldClassTestCase(unittest.TestCase):
652 def setUp(self):
653 self._tc = get_default_trace_class()
654 self._elem_fc = self._tc.create_signed_integer_field_class(23)
655
656 def test_create_default(self):
657 fc = self._tc.create_static_array_field_class(self._elem_fc, 45)
658 self.assertEqual(fc.element_field_class.addr, self._elem_fc.addr)
659 self.assertEqual(fc.length, 45)
660
661 def test_create_invalid_elem_field_class(self):
662 with self.assertRaises(TypeError):
663 self._tc.create_static_array_field_class(object(), 45)
664
665 def test_create_invalid_length(self):
666 with self.assertRaises(ValueError):
667 self._tc.create_static_array_field_class(
668 self._tc.create_string_field_class(), -17
669 )
670
671 def test_create_invalid_length_type(self):
672 with self.assertRaises(TypeError):
673 self._tc.create_static_array_field_class(
674 self._tc.create_string_field_class(), 'the length'
675 )
676
677
678 class DynamicArrayFieldClassTestCase(unittest.TestCase):
679 def setUp(self):
680 self._tc = get_default_trace_class()
681 self._elem_fc = self._tc.create_signed_integer_field_class(23)
682 self._len_fc = self._tc.create_unsigned_integer_field_class(12)
683
684 def test_create_default(self):
685 fc = self._tc.create_dynamic_array_field_class(self._elem_fc)
686 self.assertEqual(fc.element_field_class.addr, self._elem_fc.addr)
687 self.assertIsNone(fc.length_field_path, None)
688
689 def _create_field_class_for_field_path_test(self):
690 # Create something a field class that is equivalent to:
691 #
692 # struct outer_struct_fc {
693 # real foo;
694 # struct inner_struct_fc {
695 # string bar;
696 # string baz;
697 # uint12_t len;
698 # uint23_t dyn_array[len];
699 # } inner_struct[2];
700 # };
701
702 fc = self._tc.create_dynamic_array_field_class(self._elem_fc, self._len_fc)
703
704 foo_fc = self._tc.create_real_field_class()
705 bar_fc = self._tc.create_string_field_class()
706 baz_fc = self._tc.create_string_field_class()
707
708 inner_struct_fc = self._tc.create_structure_field_class()
709 inner_struct_fc.append_member('bar', bar_fc)
710 inner_struct_fc.append_member('baz', baz_fc)
711 inner_struct_fc.append_member('len', self._len_fc)
712 inner_struct_fc.append_member('dyn_array', fc)
713
714 inner_struct_array_fc = self._tc.create_static_array_field_class(
715 inner_struct_fc, 2
716 )
717
718 outer_struct_fc = self._tc.create_structure_field_class()
719 outer_struct_fc.append_member('foo', foo_fc)
720 outer_struct_fc.append_member('inner_struct', inner_struct_array_fc)
721
722 # The path to the length field is resolved when the sequence is
723 # actually used, for example in a packet context.
724 self._tc.create_stream_class(
725 packet_context_field_class=outer_struct_fc, supports_packets=True
726 )
727
728 return fc
729
730 def test_field_path_len(self):
731 fc = self._create_field_class_for_field_path_test()
732 self.assertEqual(len(fc.length_field_path), 3)
733
734 def test_field_path_iter(self):
735 fc = self._create_field_class_for_field_path_test()
736 path_items = list(fc.length_field_path)
737
738 self.assertEqual(len(path_items), 3)
739
740 self.assertIsInstance(path_items[0], bt2._IndexFieldPathItem)
741 self.assertEqual(path_items[0].index, 1)
742
743 self.assertIsInstance(path_items[1], bt2._CurrentArrayElementFieldPathItem)
744
745 self.assertIsInstance(path_items[2], bt2._IndexFieldPathItem)
746 self.assertEqual(path_items[2].index, 2)
747
748 def test_field_path_root_scope(self):
749 fc = self._create_field_class_for_field_path_test()
750 self.assertEqual(
751 fc.length_field_path.root_scope, bt2.FieldPathScope.PACKET_CONTEXT
752 )
753
754 def test_create_invalid_field_class(self):
755 with self.assertRaises(TypeError):
756 self._tc.create_dynamic_array_field_class(object())
757
758 def test_create_invalid_length_type(self):
759 with self.assertRaises(TypeError):
760 self._tc.create_dynamic_array_field_class(
761 self._tc.create_string_field_class(), 17
762 )
763
764
765 if __name__ == "__main__":
766 unittest.main()
This page took 0.055075 seconds and 5 git commands to generate.