sink.ctf.fs: write bit array field classes and fields
[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
d47b87ac
SM
33class _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)
b4f45851 38
d47b87ac
SM
39 def test_create_range(self):
40 fc = self._create_func(field_value_range=35)
41 self.assertEqual(fc.field_value_range, 35)
b4f45851 42
d47b87ac
SM
43 fc = self._create_func(36)
44 self.assertEqual(fc.field_value_range, 36)
b4f45851 45
d47b87ac
SM
46 def test_create_invalid_range(self):
47 with self.assertRaises(TypeError):
48 self._create_func('yes')
b4f45851 49
d47b87ac
SM
50 with self.assertRaises(TypeError):
51 self._create_func(field_value_range='yes')
b4f45851 52
b4f45851 53 with self.assertRaises(ValueError):
d47b87ac 54 self._create_func(field_value_range=-2)
b4f45851 55
d47b87ac
SM
56 with self.assertRaises(ValueError):
57 self._create_func(field_value_range=0)
b4f45851 58
d47b87ac 59 def test_create_base(self):
cfbd7cf3
FD
60 fc = self._create_func(
61 preferred_display_base=bt2.IntegerDisplayBase.HEXADECIMAL
62 )
d47b87ac 63 self.assertEqual(fc.preferred_display_base, bt2.IntegerDisplayBase.HEXADECIMAL)
b4f45851 64
d47b87ac 65 def test_create_invalid_base_type(self):
b4f45851 66 with self.assertRaises(TypeError):
d47b87ac 67 self._create_func(preferred_display_base='yes')
b4f45851 68
d47b87ac
SM
69 def test_create_invalid_base_value(self):
70 with self.assertRaises(ValueError):
71 self._create_func(preferred_display_base=444)
b4f45851 72
d47b87ac
SM
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)
b4f45851
SM
77
78
d47b87ac
SM
79class 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
b4f45851 83
b4f45851 84
d47b87ac
SM
85class RealFieldClassTestCase(unittest.TestCase):
86 def setUp(self):
87 self._tc = get_default_trace_class()
b4f45851 88
d47b87ac
SM
89 def test_create_default(self):
90 fc = self._tc.create_real_field_class()
91 self.assertFalse(fc.is_single_precision)
b4f45851 92
d47b87ac
SM
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):
b4f45851 98 with self.assertRaises(TypeError):
d47b87ac 99 self._tc.create_real_field_class(is_single_precision='hohoho')
b4f45851 100
b4f45851 101
d47b87ac
SM
102# Converts an _EnumerationFieldClassMapping to a list of ranges:
103#
104# [(lower0, upper0), (lower1, upper1), ...]
105
cfbd7cf3 106
45c51519
PP
107def enum_mapping_to_set(mapping):
108 return {(x.lower, x.upper) for x in mapping.ranges}
b4f45851 109
b4f45851 110
45c51519 111class _EnumerationFieldClassTestCase(_TestIntegerFieldClassProps):
d47b87ac
SM
112 def setUp(self):
113 self._tc = get_default_trace_class()
45c51519
PP
114 self._spec_set_up()
115 self._fc = self._create_func()
d47b87ac
SM
116
117 def test_create_from_invalid_type(self):
b4f45851 118 with self.assertRaises(TypeError):
d47b87ac 119 self._create_func('coucou')
b4f45851 120
d47b87ac 121 def test_add_mapping_simple(self):
45c51519 122 self._fc.add_mapping('hello', self._ranges1)
d47b87ac
SM
123 mapping = self._fc['hello']
124 self.assertEqual(mapping.label, 'hello')
45c51519 125 self.assertEqual(mapping.ranges, self._ranges1)
b4f45851 126
d47b87ac 127 def test_add_mapping_simple_kwargs(self):
45c51519 128 self._fc.add_mapping(label='hello', ranges=self._ranges1)
d47b87ac
SM
129 mapping = self._fc['hello']
130 self.assertEqual(mapping.label, 'hello')
45c51519 131 self.assertEqual(mapping.ranges, self._ranges1)
b4f45851 132
45c51519
PP
133 def test_add_mapping_invalid_name(self):
134 with self.assertRaises(TypeError):
135 self._fc.add_mapping(17, self._ranges1)
b4f45851 136
45c51519
PP
137 def test_add_mapping_invalid_range(self):
138 with self.assertRaises(TypeError):
139 self._fc.add_mapping('allo', 'meow')
d47b87ac 140
45c51519 141 def test_add_mapping_dup_label(self):
ce4923b0 142 with self.assertRaises(ValueError):
45c51519
PP
143 self._fc.add_mapping('a', self._ranges1)
144 self._fc.add_mapping('a', self._ranges2)
d47b87ac 145
45c51519 146 def test_add_mapping_invalid_ranges_signedness(self):
b4f45851 147 with self.assertRaises(TypeError):
45c51519 148 self._fc.add_mapping('allo', self._inval_ranges)
b4f45851 149
d47b87ac 150 def test_iadd(self):
45c51519 151 self._fc.add_mapping('c', self._ranges1)
b4f45851 152
cfbd7cf3 153 self._fc += [('d', self._ranges2), ('e', self._ranges3)]
b4f45851 154
45c51519 155 self.assertEqual(len(self._fc), 3)
d47b87ac 156 self.assertEqual(self._fc['c'].label, 'c')
45c51519 157 self.assertEqual(self._fc['c'].ranges, self._ranges1)
d47b87ac 158 self.assertEqual(self._fc['d'].label, 'd')
45c51519 159 self.assertEqual(self._fc['d'].ranges, self._ranges2)
d47b87ac 160 self.assertEqual(self._fc['e'].label, 'e')
45c51519 161 self.assertEqual(self._fc['e'].ranges, self._ranges3)
b4f45851 162
d47b87ac
SM
163 def test_bool_op(self):
164 self.assertFalse(self._fc)
45c51519 165 self._fc.add_mapping('a', self._ranges1)
d47b87ac 166 self.assertTrue(self._fc)
b4f45851 167
d47b87ac 168 def test_len(self):
45c51519
PP
169 self._fc.add_mapping('a', self._ranges1)
170 self._fc.add_mapping('b', self._ranges2)
171 self._fc.add_mapping('c', self._ranges3)
d47b87ac 172 self.assertEqual(len(self._fc), 3)
b4f45851 173
d47b87ac 174 def test_getitem(self):
45c51519
PP
175 self._fc.add_mapping('a', self._ranges1)
176 self._fc.add_mapping('b', self._ranges2)
177 self._fc.add_mapping('c', self._ranges3)
d47b87ac 178 mapping = self._fc['a']
d47b87ac 179 self.assertEqual(mapping.label, 'a')
45c51519 180 self.assertEqual(mapping.ranges, self._ranges1)
b4f45851 181
45c51519 182 def test_getitem_nonexistent(self):
d47b87ac
SM
183 with self.assertRaises(KeyError):
184 self._fc['doesnotexist']
b4f45851 185
d47b87ac 186 def test_iter(self):
45c51519
PP
187 self._fc.add_mapping('a', self._ranges1)
188 self._fc.add_mapping('b', self._ranges2)
189 self._fc.add_mapping('c', self._ranges3)
b4f45851 190
d47b87ac
SM
191 # This exercises iteration.
192 labels = sorted(self._fc)
b4f45851 193
45c51519 194 self.assertEqual(labels, ['a', 'b', 'c'])
b4f45851 195
d47b87ac 196 def test_find_by_value(self):
45c51519
PP
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
cfbd7cf3
FD
206class UnsignedEnumerationFieldClassTestCase(
207 _EnumerationFieldClassTestCase, unittest.TestCase
208):
45c51519
PP
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
d47b87ac 215 self._create_func = self._tc.create_unsigned_enumeration_field_class
b4f45851 216
b4f45851 217
cfbd7cf3
FD
218class SignedEnumerationFieldClassTestCase(
219 _EnumerationFieldClassTestCase, unittest.TestCase
220):
45c51519
PP
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
d47b87ac 227 self._create_func = self._tc.create_signed_enumeration_field_class
b4f45851 228
b4f45851 229
d47b87ac 230class StringFieldClassTestCase(unittest.TestCase):
b4f45851 231 def setUp(self):
d47b87ac
SM
232 tc = get_default_trace_class()
233 self._fc = tc.create_string_field_class()
b4f45851
SM
234
235 def test_create_default(self):
d47b87ac 236 self.assertIsNotNone(self._fc)
b4f45851 237
b4f45851 238
cfbd7cf3 239class _TestElementContainer:
45c51519
PP
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
d47b87ac
SM
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)
45c51519 250 field_class = self._fc['int32'].field_class
d47b87ac 251 self.assertEqual(field_class.addr, int_field_class.addr)
b4f45851 252
45c51519 253 def test_append_element_kwargs(self):
d47b87ac
SM
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)
45c51519 256 field_class = self._fc['int32'].field_class
d47b87ac
SM
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()
b4f45851 261
b4f45851 262 with self.assertRaises(TypeError):
d47b87ac 263 self._append_element_method(self._fc, 23, sub_fc)
b4f45851 264
d47b87ac 265 def test_append_element_invalid_field_class(self):
b4f45851 266 with self.assertRaises(TypeError):
d47b87ac 267 self._append_element_method(self._fc, 'yes', object())
b4f45851 268
45c51519
PP
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
ce4923b0 273 with self.assertRaises(ValueError):
45c51519
PP
274 self._append_element_method(self._fc, 'yes', sub_fc1)
275 self._append_element_method(self._fc, 'yes', sub_fc2)
276
b4f45851 277 def test_iadd(self):
45c51519 278 other_fc = self._create_default_fc()
d47b87ac
SM
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)
45c51519 283 c_field_class = self._tc.create_string_field_class()
cfbd7cf3
FD
284 d_field_class = self._tc.create_signed_enumeration_field_class(
285 field_value_range=32
286 )
45c51519
PP
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')
b4f45851
SM
303
304 def test_bool_op(self):
305 self.assertFalse(self._fc)
d47b87ac 306 self._append_element_method(self._fc, 'a', self._tc.create_string_field_class())
b4f45851
SM
307 self.assertTrue(self._fc)
308
309 def test_len(self):
45c51519
PP
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())
b4f45851
SM
313 self.assertEqual(len(self._fc), 3)
314
315 def test_getitem(self):
d47b87ac
SM
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)
45c51519
PP
322 self.assertEqual(self._fc['b'].field_class.addr, b_fc.addr)
323 self.assertEqual(self._fc['b'].name, 'b')
b4f45851
SM
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)
d47b87ac 335 self._append_element_method(self._fc, 'a', self._tc.create_string_field_class())
b4f45851
SM
336 self.assertTrue('a' in self._fc)
337
338 def test_iter(self):
d47b87ac
SM
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()
cfbd7cf3 342 elements = (('a', a_fc), ('b', b_fc), ('c', c_fc))
b4f45851 343
45c51519
PP
344 for elem in elements:
345 self._append_element_method(self._fc, *elem)
b4f45851 346
45c51519
PP
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)
b4f45851
SM
351
352 def test_at_index(self):
d47b87ac
SM
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)
45c51519
PP
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')
b4f45851
SM
362
363 def test_at_index_invalid(self):
cfbd7cf3
FD
364 self._append_element_method(
365 self._fc, 'c', self._tc.create_signed_integer_field_class(32)
366 )
b4f45851
SM
367
368 with self.assertRaises(TypeError):
d47b87ac 369 self._at_index_method(self._fc, 'yes')
b4f45851
SM
370
371 def test_at_index_out_of_bounds_after(self):
cfbd7cf3
FD
372 self._append_element_method(
373 self._fc, 'c', self._tc.create_signed_integer_field_class(32)
374 )
b4f45851
SM
375
376 with self.assertRaises(IndexError):
d47b87ac 377 self._at_index_method(self._fc, len(self._fc))
b4f45851
SM
378
379
45c51519 380class StructureFieldClassTestCase(_TestElementContainer, unittest.TestCase):
3fb99a22
PP
381 _append_element_method = staticmethod(bt2._StructureFieldClass.append_member)
382 _at_index_method = staticmethod(bt2._StructureFieldClass.member_at_index)
45c51519
PP
383
384 def _create_default_fc(self):
385 return self._tc.create_structure_field_class()
386
387
cec0261d
PP
388class 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
cfbd7cf3
FD
460class VariantFieldClassWithoutSelectorTestCase(
461 _TestElementContainer, unittest.TestCase
462):
463 _append_element_method = staticmethod(
3fb99a22 464 bt2._VariantFieldClassWithoutSelector.append_option
cfbd7cf3
FD
465 )
466 _at_index_method = staticmethod(
3fb99a22 467 bt2._VariantFieldClassWithoutSelector.option_at_index
cfbd7cf3 468 )
45c51519
PP
469
470 def _create_default_fc(self):
471 return self._tc.create_variant_field_class()
472
473
474class _VariantFieldClassWithSelectorTestCase:
b4f45851 475 def setUp(self):
d47b87ac 476 self._tc = get_default_trace_class()
45c51519
PP
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)
d47b87ac
SM
482
483 def test_create_default(self):
484 self.assertIsNotNone(self._fc)
b4f45851 485
45c51519
PP
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)
cfbd7cf3
FD
496 self._fc.append_option(
497 name='int32', field_class=int_field_class, ranges=self._ranges1
498 )
45c51519
PP
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)
d47b87ac 503
45c51519
PP
504 def test_append_element_invalid_name(self):
505 sub_fc = self._tc.create_string_field_class()
b4f45851 506
45c51519
PP
507 with self.assertRaises(TypeError):
508 self._fc.append_option(self._fc, 23, sub_fc)
b4f45851 509
45c51519
PP
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
ce4923b0 524 with self.assertRaises(ValueError):
45c51519
PP
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()
cfbd7cf3
FD
539 d_field_class = self._tc.create_signed_enumeration_field_class(
540 field_value_range=32
541 )
45c51519
PP
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)
b4f45851 603
45c51519
PP
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):
cfbd7cf3
FD
622 self._fc.append_option(
623 'c', self._tc.create_signed_integer_field_class(32), self._ranges3
624 )
45c51519
PP
625
626 with self.assertRaises(TypeError):
627 self._fc.option_at_index('yes')
628
629 def test_at_index_out_of_bounds_after(self):
cfbd7cf3
FD
630 self._fc.append_option(
631 'c', self._tc.create_signed_integer_field_class(32), self._ranges3
632 )
45c51519
PP
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):
d47b87ac
SM
638 # Create something equivalent to:
639 #
640 # struct outer_struct_fc {
641 # real foo;
642 # struct inner_struct_fc {
45c51519 643 # [u]int64_t selector;
d47b87ac
SM
644 # string bar;
645 # string baz;
45c51519
PP
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
d47b87ac
SM
650 # } variant;
651 # } inner_struct[2];
652 # };
45c51519 653 self._fc.append_option('a', self._tc.create_real_field_class(), self._ranges1)
cfbd7cf3
FD
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 )
b4f45851 660
d47b87ac
SM
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()
b4f45851 664
d47b87ac 665 inner_struct_fc = self._tc.create_structure_field_class()
45c51519 666 inner_struct_fc.append_member('selector', self._selector_fc)
d47b87ac
SM
667 inner_struct_fc.append_member('bar', bar_fc)
668 inner_struct_fc.append_member('baz', baz_fc)
45c51519 669 inner_struct_fc.append_member('variant', self._fc)
b4f45851 670
cfbd7cf3
FD
671 inner_struct_array_fc = self._tc.create_static_array_field_class(
672 inner_struct_fc, 2
673 )
b4f45851 674
d47b87ac
SM
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)
b4f45851 678
d47b87ac
SM
679 # The path to the selector field is resolved when the sequence is
680 # actually used, for example in a packet context.
cfbd7cf3
FD
681 self._tc.create_stream_class(
682 supports_packets=True, packet_context_field_class=outer_struct_fc
683 )
b4f45851 684
d47b87ac 685 def test_selector_field_path_length(self):
45c51519
PP
686 self._fill_default_fc_for_field_path_test()
687 self.assertEqual(len(self._fc.selector_field_path), 3)
b4f45851 688
d47b87ac 689 def test_selector_field_path_iter(self):
45c51519
PP
690 self._fill_default_fc_for_field_path_test()
691 path_items = list(self._fc.selector_field_path)
b4f45851 692
d47b87ac 693 self.assertEqual(len(path_items), 3)
b4f45851 694
3fb99a22 695 self.assertIsInstance(path_items[0], bt2._IndexFieldPathItem)
d47b87ac 696 self.assertEqual(path_items[0].index, 1)
b4f45851 697
3fb99a22 698 self.assertIsInstance(path_items[1], bt2._CurrentArrayElementFieldPathItem)
b4f45851 699
3fb99a22 700 self.assertIsInstance(path_items[2], bt2._IndexFieldPathItem)
d47b87ac 701 self.assertEqual(path_items[2].index, 0)
b4f45851 702
d47b87ac 703 def test_selector_field_path_root_scope(self):
45c51519 704 self._fill_default_fc_for_field_path_test()
cfbd7cf3 705 self.assertEqual(
e7ceb9df 706 self._fc.selector_field_path.root_scope, bt2.FieldPathScope.PACKET_CONTEXT
cfbd7cf3 707 )
45c51519
PP
708
709
cfbd7cf3
FD
710class VariantFieldClassWithUnsignedSelectorTestCase(
711 _VariantFieldClassWithSelectorTestCase, unittest.TestCase
712):
45c51519
PP
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
cfbd7cf3
FD
721class VariantFieldClassWithSignedSelectorTestCase(
722 _VariantFieldClassWithSelectorTestCase, unittest.TestCase
723):
45c51519
PP
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()
b4f45851 730
d47b87ac
SM
731
732class 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)
b4f45851
SM
736
737 def test_create_default(self):
d47b87ac
SM
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)
b4f45851 741
d47b87ac 742 def test_create_invalid_elem_field_class(self):
b4f45851 743 with self.assertRaises(TypeError):
d47b87ac 744 self._tc.create_static_array_field_class(object(), 45)
b4f45851
SM
745
746 def test_create_invalid_length(self):
747 with self.assertRaises(ValueError):
cfbd7cf3
FD
748 self._tc.create_static_array_field_class(
749 self._tc.create_string_field_class(), -17
750 )
b4f45851
SM
751
752 def test_create_invalid_length_type(self):
753 with self.assertRaises(TypeError):
cfbd7cf3
FD
754 self._tc.create_static_array_field_class(
755 self._tc.create_string_field_class(), 'the length'
756 )
d47b87ac
SM
757
758
759class 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)
b4f45851 764
d47b87ac
SM
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)
b4f45851 769
d47b87ac
SM
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 # };
b4f45851 782
d47b87ac 783 fc = self._tc.create_dynamic_array_field_class(self._elem_fc, self._len_fc)
b4f45851 784
d47b87ac
SM
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()
b4f45851 788
d47b87ac
SM
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)
b4f45851 794
cfbd7cf3
FD
795 inner_struct_array_fc = self._tc.create_static_array_field_class(
796 inner_struct_fc, 2
797 )
d47b87ac
SM
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.
cfbd7cf3
FD
805 self._tc.create_stream_class(
806 packet_context_field_class=outer_struct_fc, supports_packets=True
807 )
d47b87ac
SM
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
3fb99a22 821 self.assertIsInstance(path_items[0], bt2._IndexFieldPathItem)
d47b87ac
SM
822 self.assertEqual(path_items[0].index, 1)
823
3fb99a22 824 self.assertIsInstance(path_items[1], bt2._CurrentArrayElementFieldPathItem)
d47b87ac 825
3fb99a22 826 self.assertIsInstance(path_items[2], bt2._IndexFieldPathItem)
d47b87ac
SM
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()
e7ceb9df
PP
831 self.assertEqual(
832 fc.length_field_path.root_scope, bt2.FieldPathScope.PACKET_CONTEXT
833 )
b4f45851
SM
834
835 def test_create_invalid_field_class(self):
836 with self.assertRaises(TypeError):
d47b87ac 837 self._tc.create_dynamic_array_field_class(object())
b4f45851
SM
838
839 def test_create_invalid_length_type(self):
840 with self.assertRaises(TypeError):
cfbd7cf3
FD
841 self._tc.create_dynamic_array_field_class(
842 self._tc.create_string_field_class(), 17
843 )
b4f45851 844
b4f45851 845
d47b87ac
SM
846if __name__ == "__main__":
847 unittest.main()
This page took 0.070503 seconds and 4 git commands to generate.