+ def test_create_invalid_selector_type(self):
+ with self.assertRaises(TypeError):
+ self._tc.create_option_with_bool_selector_field_class(self._content_fc, 17)
+
+ def test_attr_selector_is_reversed(self):
+ fc = self._create_default_field_class(selector_is_reversed=True)
+ self.assertTrue(fc.selector_is_reversed)
+
+ def test_const_attr_selector_is_reversed(self):
+ fc = self._create_default_const_field_class(selector_is_reversed=True)
+ self.assertTrue(fc.selector_is_reversed)
+
+
+class _OptionWithIntegerSelectorFieldClassTestCase(
+ _OptionWithSelectorFieldClassTestCase
+):
+ def _create_default_field_class(self, *args, **kwargs):
+ return self._tc.create_option_with_integer_selector_field_class(
+ self._content_fc, self._tag_fc, self._ranges, *args, **kwargs
+ )
+
+ def test_create_default(self):
+ fc = self._create_default_field_class()
+ self.assertEqual(fc.field_class.addr, self._content_fc.addr)
+ self.assertEqual(fc.ranges, self._ranges)
+ self.assertEqual(len(fc.user_attributes), 0)
+
+ def test_create_ranges_wrong_type(self):
+ with self.assertRaises(TypeError):
+ self._tc.create_option_with_integer_selector_field_class(
+ self._content_fc, self._tag_fc, 23
+ )
+
+ def test_create_ranges_empty(self):
+ with self.assertRaises(ValueError):
+ self._tc.create_option_with_integer_selector_field_class(
+ self._content_fc, self._tag_fc, type(self._ranges)()
+ )
+
+ def test_create_invalid_selector_type(self):
+ with self.assertRaises(TypeError):
+ self._tc.create_option_with_bool_selector_field_class(self._content_fc, 17)
+
+ def test_attr_ranges(self):
+ fc = self._create_default_field_class()
+ print(type(fc.ranges), type(self._ranges))
+ self.assertEqual(fc.ranges, self._ranges)
+
+ def test_const_attr_ranges(self):
+ fc = self._create_default_const_field_class()
+ self.assertEqual(fc.ranges, self._ranges)
+
+
+class OptionWithUnsignedIntegerSelectorFieldClassTestCase(
+ _OptionWithIntegerSelectorFieldClassTestCase, unittest.TestCase
+):
+ def setUp(self):
+ self._ranges = bt2.UnsignedIntegerRangeSet([(1, 3), (18, 44)])
+ super().setUp()
+
+ def _create_tag_fc(self):
+ return self._tc.create_unsigned_integer_field_class()
+
+
+class VariantFieldClassWithoutSelectorTestCase(
+ _TestFieldClass, _TestElementContainer, unittest.TestCase
+):
+ _append_element_method = staticmethod(
+ bt2._VariantFieldClassWithoutSelector.append_option
+ )
+ _at_index_method = staticmethod(
+ bt2._VariantFieldClassWithoutSelector.option_at_index
+ )
+
+ @staticmethod
+ def _const_value_setter(variant_field):
+ variant_field.selected_option_index = 0
+ variant_field.value = 12
+
+ def _create_field_class(self, *args, **kwargs):
+ tc = get_default_trace_class()
+ return tc.create_variant_field_class(*args, **kwargs)
+
+ def _create_default_const_field_class(self, *args, **kwargs):
+ tc = get_default_trace_class()
+ fc = tc.create_variant_field_class(*args, **kwargs)
+ int_field_class = self._tc.create_signed_integer_field_class(32)
+ fc.append_option('int32', int_field_class)
+
+ return _create_const_field_class(tc, fc, self._const_value_setter)
+
+ _create_default_field_class = _create_field_class
+
+
+class _VariantFieldClassWithIntegerSelectorTestCase:
+ @staticmethod
+ def _const_value_setter(field):
+ field['variant'].selected_option_index = 0
+ field['variant'] = 12
+
+ def _create_default_field_class(self, *args, **kwargs):
+ return self._tc.create_variant_field_class(
+ *args, selector_fc=self._selector_fc, **kwargs
+ )
+
+ def _create_default_const_field_class(self, *args, **kwargs):
+ # Create a struct to contain the variant and its selector else we can't
+ # create the non-const field necessary to get the the const field_class
+ struct_fc = self._tc.create_structure_field_class()
+ struct_fc.append_member('selecteux', self._selector_fc)
+ variant_fc = self._tc.create_variant_field_class(
+ *args, selector_fc=self._selector_fc
+ )
+ variant_fc.append_option(
+ 'a', self._tc.create_signed_integer_field_class(32), self._ranges1
+ )
+ struct_fc.append_member('variant', variant_fc, **kwargs)
+
+ return _create_const_field_class(self._tc, struct_fc, self._const_value_setter)[
+ 'variant'
+ ].field_class
+
+ def setUp(self):
+ self._tc = get_default_trace_class()
+ self._spec_set_up()
+ self._fc = self._create_default_field_class()
+
+ def test_create_default(self):
+ self.assertIsNotNone(self._fc)
+ self.assertEqual(len(self._fc.user_attributes), 0)
+
+ def test_append_element(self):
+ str_field_class = self._tc.create_string_field_class()
+ self._fc.append_option('str', str_field_class, self._ranges1)
+ opt = self._fc['str']
+ self.assertEqual(opt.field_class.addr, str_field_class.addr)
+ self.assertEqual(opt.name, 'str')
+ self.assertEqual(opt.ranges.addr, self._ranges1.addr)
+
+ def test_const_append(self):
+ fc_const = self._create_default_const_field_class()
+ str_field_class = self._tc.create_string_field_class()
+ with self.assertRaises(AttributeError):
+ fc_const.append_option('str', str_field_class, self._ranges1)
+
+ def test_append_element_kwargs(self):
+ int_field_class = self._tc.create_signed_integer_field_class(32)
+ self._fc.append_option(
+ name='int32', field_class=int_field_class, ranges=self._ranges1
+ )
+ opt = self._fc['int32']
+ self.assertEqual(opt.field_class.addr, int_field_class.addr)
+ self.assertEqual(opt.name, 'int32')
+ self.assertEqual(opt.ranges.addr, self._ranges1.addr)
+
+ def test_append_element_invalid_name(self):
+ sub_fc = self._tc.create_string_field_class()
+
+ with self.assertRaises(TypeError):
+ self._fc.append_option(self._fc, 23, sub_fc)
+
+ def test_append_element_invalid_field_class(self):
+ with self.assertRaises(TypeError):
+ self._fc.append_option(self._fc, 'yes', object())
+
+ def test_append_element_invalid_ranges(self):
+ sub_fc = self._tc.create_string_field_class()
+
+ with self.assertRaises(TypeError):
+ self._fc.append_option(self._fc, sub_fc, 'lel')
+
+ def test_append_element_dup_name(self):
+ sub_fc1 = self._tc.create_string_field_class()
+ sub_fc2 = self._tc.create_string_field_class()
+
+ 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):