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