+ with self.assertRaises(ValueError):
+ self._fc.append_option('yes', sub_fc1, self._ranges1)
+ self._fc.append_option('yes', sub_fc2, self._ranges2)
+
+ def test_append_element_invalid_ranges_signedness(self):
+ sub_fc = self._tc.create_string_field_class()
+
+ with self.assertRaises(TypeError):
+ self._fc.append_option(self._fc, sub_fc, self._inval_ranges)
+
+ def test_user_attributes(self):
+ self._fc.append_option(
+ 'c',
+ self._tc.create_string_field_class(),
+ self._ranges1,
+ user_attributes={'salut': 23},
+ )
+ self.assertEqual(self._fc['c'].user_attributes, {'salut': 23})
+ self.assertIs(type(self._fc.user_attributes), bt2_value.MapValue)
+
+ def test_const_user_attributes(self):
+ fc_const = self._create_default_const_field_class()
+ self.assertIs(type(fc_const.user_attributes), bt2_value._MapValueConst)
+
+ def test_invalid_user_attributes(self):
+ with self.assertRaises(TypeError):
+ self._fc.append_option(
+ 'c',
+ self._tc.create_string_field_class(),
+ self._ranges1,
+ user_attributes=object(),
+ )
+
+ def test_invalid_user_attributes_value_type(self):
+ with self.assertRaises(TypeError):
+ self._fc.append_option(
+ 'c',
+ self._tc.create_string_field_class(),
+ self._ranges1,
+ user_attributes=23,
+ )
+
+ def test_iadd(self):
+ a_field_class = self._tc.create_single_precision_real_field_class()
+ self._fc.append_option('a_float', a_field_class, self._ranges1)
+ c_field_class = self._tc.create_string_field_class()
+ d_field_class = self._tc.create_signed_enumeration_field_class(
+ field_value_range=32
+ )
+ self._fc += [
+ ('c_string', c_field_class, self._ranges2),
+ ('d_enum', d_field_class, self._ranges3),
+ ]
+ self.assertEqual(self._fc['a_float'].field_class.addr, a_field_class.addr)
+ self.assertEqual(self._fc['a_float'].name, 'a_float')
+ self.assertEqual(self._fc['a_float'].ranges, self._ranges1)
+ self.assertEqual(self._fc['c_string'].field_class.addr, c_field_class.addr)
+ self.assertEqual(self._fc['c_string'].name, 'c_string')
+ self.assertEqual(self._fc['c_string'].ranges, self._ranges2)
+ self.assertEqual(self._fc['d_enum'].field_class.addr, d_field_class.addr)
+ self.assertEqual(self._fc['d_enum'].name, 'd_enum')
+ self.assertEqual(self._fc['d_enum'].ranges, self._ranges3)
+
+ def test_const_iadd(self):
+ fc_const = self._create_default_const_field_class()
+ a_field_class = self._tc.create_single_precision_real_field_class()
+ with self.assertRaises(TypeError):
+ fc_const += [('a_float', a_field_class, self._ranges1)]
+
+ def test_bool_op(self):
+ self.assertFalse(self._fc)
+ self._fc.append_option('a', self._tc.create_string_field_class(), self._ranges1)
+ self.assertTrue(self._fc)
+
+ def test_len(self):
+ self._fc.append_option('a', self._tc.create_string_field_class(), self._ranges1)
+ self._fc.append_option('b', self._tc.create_string_field_class(), self._ranges2)
+ self._fc.append_option('c', self._tc.create_string_field_class(), self._ranges3)
+ self.assertEqual(len(self._fc), 3)
+
+ def test_getitem(self):
+ a_fc = self._tc.create_signed_integer_field_class(32)
+ b_fc = self._tc.create_string_field_class()
+ c_fc = self._tc.create_single_precision_real_field_class()
+ self._fc.append_option('a', a_fc, self._ranges1)
+ self._fc.append_option('b', b_fc, self._ranges2)
+ self._fc.append_option('c', c_fc, self._ranges3)
+ self.assertEqual(self._fc['b'].field_class.addr, b_fc.addr)
+ self.assertEqual(self._fc['b'].name, 'b')
+ self.assertEqual(self._fc['b'].ranges.addr, self._ranges2.addr)
+
+ def test_option_field_class(self):
+ a_fc = self._tc.create_signed_integer_field_class(32)
+ self._fc.append_option('a', a_fc, self._ranges1)
+ self.assertIs(
+ type(self._fc['a'].field_class), bt2_field_class._SignedIntegerFieldClass
+ )
+
+ def test_const_option_field_class(self):
+ fc_const = self._create_default_const_field_class()
+ self.assertIs(
+ type(fc_const['a'].field_class),
+ bt2_field_class._SignedIntegerFieldClassConst,
+ )
+
+ def test_getitem_invalid_key_type(self):
+ with self.assertRaises(TypeError):
+ self._fc[0]
+
+ def test_getitem_invalid_key(self):
+ with self.assertRaises(KeyError):
+ self._fc['no way']
+
+ def test_contains(self):
+ self.assertFalse('a' in self._fc)
+ self._fc.append_option('a', self._tc.create_string_field_class(), self._ranges1)
+ self.assertTrue('a' in self._fc)
+
+ def test_iter(self):
+ a_fc = self._tc.create_signed_integer_field_class(32)
+ b_fc = self._tc.create_string_field_class()
+ c_fc = self._tc.create_single_precision_real_field_class()
+ opts = (
+ ('a', a_fc, self._ranges1),
+ ('b', b_fc, self._ranges2),
+ ('c', c_fc, self._ranges3),
+ )
+
+ for opt in opts:
+ self._fc.append_option(*opt)
+
+ for (name, opt), test_opt in zip(self._fc.items(), opts):
+ self.assertEqual(opt.name, test_opt[0])
+ self.assertEqual(name, opt.name)
+ self.assertEqual(opt.field_class.addr, test_opt[1].addr)
+ self.assertEqual(opt.ranges.addr, test_opt[2].addr)
+
+ def test_at_index(self):
+ a_fc = self._tc.create_signed_integer_field_class(32)
+ b_fc = self._tc.create_string_field_class()
+ c_fc = self._tc.create_single_precision_real_field_class()
+ self._fc.append_option('c', c_fc, self._ranges1)
+ self._fc.append_option('a', a_fc, self._ranges2)
+ self._fc.append_option('b', b_fc, self._ranges3)
+ self.assertEqual(self._fc.option_at_index(1).field_class.addr, a_fc.addr)
+ self.assertEqual(self._fc.option_at_index(1).name, 'a')
+ self.assertEqual(self._fc.option_at_index(1).ranges.addr, self._ranges2.addr)
+
+ def test_at_index_invalid(self):
+ self._fc.append_option(
+ 'c', self._tc.create_signed_integer_field_class(32), self._ranges3
+ )
+
+ with self.assertRaises(TypeError):
+ self._fc.option_at_index('yes')
+
+ def test_at_index_out_of_bounds_after(self):
+ self._fc.append_option(
+ 'c', self._tc.create_signed_integer_field_class(32), self._ranges3
+ )
+
+ with self.assertRaises(IndexError):
+ self._fc.option_at_index(len(self._fc))
+
+ def _fill_default_fc_for_field_path_test(self):
+ # Create something equivalent to:
+ #
+ # struct outer_struct_fc {
+ # real foo;
+ # struct inner_struct_fc {
+ # [u]int64_t selector;
+ # string bar;
+ # string baz;
+ # variant <selector> {
+ # real a; // selected with self._ranges1
+ # int21_t b; // selected with self._ranges2
+ # uint34_t c; // selected with self._ranges3
+ # } variant;
+ # } inner_struct[2];
+ # };
+ self._fc.append_option(
+ 'a', self._tc.create_single_precision_real_field_class(), self._ranges1
+ )
+ self._fc.append_option(
+ 'b', self._tc.create_signed_integer_field_class(21), self._ranges2
+ )
+ self._fc.append_option(
+ 'c', self._tc.create_unsigned_integer_field_class(34), self._ranges3
+ )
+
+ foo_fc = self._tc.create_single_precision_real_field_class()
+ bar_fc = self._tc.create_string_field_class()
+ baz_fc = self._tc.create_string_field_class()
+
+ inner_struct_fc = self._tc.create_structure_field_class()
+ inner_struct_fc.append_member('selector', self._selector_fc)
+ inner_struct_fc.append_member('bar', bar_fc)
+ inner_struct_fc.append_member('baz', baz_fc)
+ inner_struct_fc.append_member('variant', self._fc)
+
+ inner_struct_array_fc = self._tc.create_static_array_field_class(
+ inner_struct_fc, 2
+ )
+
+ outer_struct_fc = self._tc.create_structure_field_class()
+ outer_struct_fc.append_member('foo', foo_fc)
+ outer_struct_fc.append_member('inner_struct', inner_struct_array_fc)
+
+ # The path to the selector field is resolved when the sequence is
+ # actually used, for example in a packet context.
+ self._tc.create_stream_class(
+ supports_packets=True, packet_context_field_class=outer_struct_fc
+ )
+
+ def test_selector_field_path_length(self):
+ self._fill_default_fc_for_field_path_test()
+ self.assertEqual(len(self._fc.selector_field_path), 3)
+
+ def test_selector_field_path_iter(self):
+ self._fill_default_fc_for_field_path_test()
+ path_items = list(self._fc.selector_field_path)
+
+ self.assertEqual(len(path_items), 3)
+
+ self.assertIsInstance(path_items[0], bt2._IndexFieldPathItem)
+ self.assertEqual(path_items[0].index, 1)
+
+ self.assertIsInstance(path_items[1], bt2._CurrentArrayElementFieldPathItem)
+
+ self.assertIsInstance(path_items[2], bt2._IndexFieldPathItem)
+ self.assertEqual(path_items[2].index, 0)
+
+ def test_selector_field_path_root_scope(self):
+ self._fill_default_fc_for_field_path_test()
+ self.assertEqual(
+ self._fc.selector_field_path.root_scope, bt2.FieldPathScope.PACKET_CONTEXT
+ )
+
+
+class VariantFieldClassWithUnsignedSelectorTestCase(
+ _VariantFieldClassWithIntegerSelectorTestCase, unittest.TestCase
+):
+ def _spec_set_up(self):
+ self._ranges1 = bt2.UnsignedIntegerRangeSet([(1, 4), (18, 47)])
+ self._ranges2 = bt2.UnsignedIntegerRangeSet([(5, 5)])
+ self._ranges3 = bt2.UnsignedIntegerRangeSet([(8, 16), (48, 99)])
+ self._inval_ranges = bt2.SignedIntegerRangeSet([(-8, 16), (48, 99)])
+ self._selector_fc = self._tc.create_unsigned_integer_field_class()
+
+
+class VariantFieldClassWithSignedSelectorTestCase(
+ _VariantFieldClassWithIntegerSelectorTestCase, unittest.TestCase
+):
+ def _spec_set_up(self):
+ self._ranges1 = bt2.SignedIntegerRangeSet([(-10, -4), (18, 47)])
+ self._ranges2 = bt2.SignedIntegerRangeSet([(-3, -3)])
+ self._ranges3 = bt2.SignedIntegerRangeSet([(8, 16), (48, 99)])
+ self._inval_ranges = bt2.UnsignedIntegerRangeSet([(8, 16), (48, 99)])
+ self._selector_fc = self._tc.create_signed_integer_field_class()
+
+
+class _ArrayFieldClassTestCase:
+ def test_attr_element_field_class(self):
+ fc = self._create_array()
+ self.assertIs(
+ type(fc.element_field_class), bt2_field_class._SignedIntegerFieldClass
+ )
+
+ def test_const_attr_element_field_class(self):
+ fc = self._create_const_array()
+ self.assertIs(
+ type(fc.element_field_class), bt2_field_class._SignedIntegerFieldClassConst
+ )
+
+
+class StaticArrayFieldClassTestCase(_ArrayFieldClassTestCase, unittest.TestCase):
+ @staticmethod
+ def _const_value_setter(field):
+ field.value = [9] * 45