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