X-Git-Url: http://git.efficios.com/?a=blobdiff_plain;f=tests%2Fbindings%2Fpython%2Fbt2%2Ftest_field_class.py;h=cc6a54bc8c01531c8caaa1e25c286f524e061acd;hb=ffecc00e2725dea0af6a7e0cfb10a95b8f56ee68;hp=b59cc72f48424c2c8248b0d5132ad68f14d4faf3;hpb=fe4df857056b4a03898f1031f136359ce733b0f5;p=babeltrace.git diff --git a/tests/bindings/python/bt2/test_field_class.py b/tests/bindings/python/bt2/test_field_class.py index b59cc72f..cc6a54bc 100644 --- a/tests/bindings/python/bt2/test_field_class.py +++ b/tests/bindings/python/bt2/test_field_class.py @@ -1,20 +1,7 @@ +# SPDX-License-Identifier: GPL-2.0-only # # Copyright (C) 2019 EfficiOS Inc. # -# This program is free software; you can redistribute it and/or -# modify it under the terms of the GNU General Public License -# as published by the Free Software Foundation; only version 2 -# of the License. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program; if not, write to the Free Software -# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. -# import unittest import bt2 @@ -374,6 +361,8 @@ class _EnumerationFieldClassTestCase(_TestIntegerFieldClassProps): mapping = self._fc['a'] self.assertEqual(mapping.label, 'a') self.assertEqual(mapping.ranges, self._ranges1) + self.assertIs(type(mapping), self._MAPPING_CLASS) + self.assertIs(type(mapping.ranges), self._CONST_RANGE_SET_CLASS) def test_getitem_nonexistent(self): with self.assertRaises(KeyError): @@ -402,6 +391,10 @@ class _EnumerationFieldClassTestCase(_TestIntegerFieldClassProps): class UnsignedEnumerationFieldClassTestCase( _EnumerationFieldClassTestCase, _TestFieldClass, unittest.TestCase ): + _MAPPING_CLASS = bt2_field_class._UnsignedEnumerationFieldClassMappingConst + _RANGE_SET_CLASS = bt2.UnsignedIntegerRangeSet + _CONST_RANGE_SET_CLASS = bt2._UnsignedIntegerRangeSetConst + def _spec_set_up(self): self._ranges1 = bt2.UnsignedIntegerRangeSet([(1, 4), (18, 47)]) self._ranges2 = bt2.UnsignedIntegerRangeSet([(5, 5)]) @@ -428,6 +421,10 @@ class UnsignedEnumerationFieldClassTestCase( class SignedEnumerationFieldClassTestCase( _EnumerationFieldClassTestCase, _TestFieldClass, unittest.TestCase ): + _MAPPING_CLASS = bt2_field_class._SignedEnumerationFieldClassMappingConst + _RANGE_SET_CLASS = bt2.SignedIntegerRangeSet + _CONST_RANGE_SET_CLASS = bt2._SignedIntegerRangeSetConst + def _spec_set_up(self): self._ranges1 = bt2.SignedIntegerRangeSet([(-10, -4), (18, 47)]) self._ranges2 = bt2.SignedIntegerRangeSet([(-3, -3)]) @@ -714,30 +711,36 @@ class StructureFieldClassTestCase( ) -class OptionFieldClassTestCase(_TestFieldClass, unittest.TestCase): +class OptionWithoutSelectorFieldClassTestCase(_TestFieldClass, unittest.TestCase): @staticmethod def _const_value_setter(field): field.has_field = True field.value = 12 def _create_default_field_class(self, *args, **kwargs): - return self._tc.create_option_field_class(self._content_fc, **kwargs) + return self._tc.create_option_without_selector_field_class( + self._content_fc, *args, **kwargs + ) def _create_default_const_field_class(self, *args, **kwargs): - fc = self._tc.create_option_field_class(self._content_fc, **kwargs) + fc = self._tc.create_option_without_selector_field_class( + self._content_fc, *args, **kwargs + ) return _create_const_field_class(self._tc, fc, self._const_value_setter) def setUp(self): self._tc = get_default_trace_class() self._content_fc = self._tc.create_signed_integer_field_class(23) - self._tag_fc = self._tc.create_bool_field_class() def test_create_default(self): fc = self._create_default_field_class() self.assertEqual(fc.field_class.addr, self._content_fc.addr) - self.assertIsNone(fc.selector_field_path, None) self.assertEqual(len(fc.user_attributes), 0) + def test_create_invalid_field_class(self): + with self.assertRaises(TypeError): + self._tc.create_option_without_selector_field_class(object()) + def test_attr_field_class(self): fc = self._create_default_field_class() self.assertIs(type(fc.field_class), bt2_field_class._SignedIntegerFieldClass) @@ -748,8 +751,32 @@ class OptionFieldClassTestCase(_TestFieldClass, unittest.TestCase): type(fc.field_class), bt2_field_class._SignedIntegerFieldClassConst ) + +class _OptionWithSelectorFieldClassTestCase(_TestFieldClass): + @staticmethod + def _const_value_setter(field): + field['opt'].has_field = True + field['opt'].value = 12 + + def _create_default_const_field_class(self, *args, **kwargs): + # Create a struct to contain the option 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._tag_fc) + opt_fc = self._create_default_field_class(*args, **kwargs) + struct_fc.append_member('opt', opt_fc) + + return _create_const_field_class(self._tc, struct_fc, self._const_value_setter)[ + 'opt' + ].field_class + + def setUp(self): + self._tc = get_default_trace_class() + self._content_fc = self._tc.create_signed_integer_field_class(23) + self._tag_fc = self._create_tag_fc() + def _create_field_class_for_field_path_test(self): - fc = self._create_default_field_class(selector_fc=self._tag_fc) + fc = self._create_default_field_class() foo_fc = self._tc.create_single_precision_real_field_class() bar_fc = self._tc.create_string_field_class() @@ -761,7 +788,9 @@ class OptionFieldClassTestCase(_TestFieldClass, unittest.TestCase): inner_struct_fc.append_member('tag', self._tag_fc) inner_struct_fc.append_member('opt', fc) - opt_struct_array_fc = self._tc.create_option_field_class(inner_struct_fc) + opt_struct_array_fc = self._tc.create_option_without_selector_field_class( + inner_struct_fc + ) outer_struct_fc = self._tc.create_structure_field_class() outer_struct_fc.append_member('foo', foo_fc) @@ -800,13 +829,89 @@ class OptionFieldClassTestCase(_TestFieldClass, unittest.TestCase): fc.selector_field_path.root_scope, bt2.FieldPathScope.PACKET_CONTEXT ) - def test_create_invalid_field_class(self): + +class OptionWithBoolSelectorFieldClassTestCase( + _OptionWithSelectorFieldClassTestCase, unittest.TestCase +): + def _create_default_field_class(self, *args, **kwargs): + return self._tc.create_option_with_bool_selector_field_class( + self._content_fc, self._tag_fc, *args, **kwargs + ) + + def _create_tag_fc(self): + return self._tc.create_bool_field_class() + + def test_create_default(self): + fc = self._create_default_field_class() + self.assertEqual(fc.field_class.addr, self._content_fc.addr) + self.assertFalse(fc.selector_is_reversed) + self.assertEqual(len(fc.user_attributes), 0) + + def test_create_selector_is_reversed_wrong_type(self): + with self.assertRaises(TypeError): + self._create_default_field_class(selector_is_reversed=23) + + 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_field_class(object()) + 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_field_class(self._content_fc, 17) + self._tc.create_option_with_bool_selector_field_class(self._content_fc, 17) + + def test_attr_ranges(self): + fc = self._create_default_field_class() + 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( @@ -839,7 +944,7 @@ class VariantFieldClassWithoutSelectorTestCase( _create_default_field_class = _create_field_class -class _VariantFieldClassWithSelectorTestCase: +class _VariantFieldClassWithIntegerSelectorTestCase: @staticmethod def _const_value_setter(field): field['variant'].selected_option_index = 0 @@ -1160,7 +1265,7 @@ class _VariantFieldClassWithSelectorTestCase: class VariantFieldClassWithUnsignedSelectorTestCase( - _VariantFieldClassWithSelectorTestCase, unittest.TestCase + _VariantFieldClassWithIntegerSelectorTestCase, unittest.TestCase ): def _spec_set_up(self): self._ranges1 = bt2.UnsignedIntegerRangeSet([(1, 4), (18, 47)]) @@ -1171,7 +1276,7 @@ class VariantFieldClassWithUnsignedSelectorTestCase( class VariantFieldClassWithSignedSelectorTestCase( - _VariantFieldClassWithSelectorTestCase, unittest.TestCase + _VariantFieldClassWithIntegerSelectorTestCase, unittest.TestCase ): def _spec_set_up(self): self._ranges1 = bt2.SignedIntegerRangeSet([(-10, -4), (18, 47)]) @@ -1188,6 +1293,8 @@ class _ArrayFieldClassTestCase: type(fc.element_field_class), bt2_field_class._SignedIntegerFieldClass ) + +class _ArrayFieldClassConstTestCase: def test_const_attr_element_field_class(self): fc = self._create_const_array() self.assertIs( @@ -1195,7 +1302,9 @@ class _ArrayFieldClassTestCase: ) -class StaticArrayFieldClassTestCase(_ArrayFieldClassTestCase, unittest.TestCase): +class StaticArrayFieldClassTestCase( + _ArrayFieldClassTestCase, _ArrayFieldClassConstTestCase, unittest.TestCase +): @staticmethod def _const_value_setter(field): field.value = [9] * 45 @@ -1234,7 +1343,9 @@ class StaticArrayFieldClassTestCase(_ArrayFieldClassTestCase, unittest.TestCase) ) -class DynamicArrayFieldClassTestCase(_ArrayFieldClassTestCase, unittest.TestCase): +class DynamicArrayFieldClassTestCase( + _ArrayFieldClassTestCase, _ArrayFieldClassConstTestCase, unittest.TestCase +): @staticmethod def _const_value_setter(field): field.value = [] @@ -1249,11 +1360,35 @@ class DynamicArrayFieldClassTestCase(_ArrayFieldClassTestCase, unittest.TestCase def setUp(self): self._tc = get_default_trace_class() self._elem_fc = self._tc.create_signed_integer_field_class(23) - self._len_fc = self._tc.create_unsigned_integer_field_class(12) def test_create_default(self): fc = self._tc.create_dynamic_array_field_class(self._elem_fc) self.assertEqual(fc.element_field_class.addr, self._elem_fc.addr) + self.assertEqual(len(fc.user_attributes), 0) + + def test_create_invalid_field_class(self): + with self.assertRaises(TypeError): + self._tc.create_dynamic_array_field_class(object()) + + +class DynamicArrayWithLengthFieldFieldClassTestCase( + _ArrayFieldClassTestCase, unittest.TestCase +): + @staticmethod + def _const_value_setter(field): + field.value = [] + + def _create_array(self): + return self._tc.create_dynamic_array_field_class(self._elem_fc, self._len_fc) + + def setUp(self): + self._tc = get_default_trace_class() + self._elem_fc = self._tc.create_signed_integer_field_class(23) + self._len_fc = self._tc.create_unsigned_integer_field_class(12) + + def test_create_default(self): + fc = self._create_array() + self.assertEqual(fc.element_field_class.addr, self._elem_fc.addr) self.assertIsNone(fc.length_field_path, None) self.assertEqual(len(fc.user_attributes), 0) @@ -1270,7 +1405,7 @@ class DynamicArrayFieldClassTestCase(_ArrayFieldClassTestCase, unittest.TestCase # } inner_struct[2]; # }; - fc = self._tc.create_dynamic_array_field_class(self._elem_fc, self._len_fc) + fc = self._create_array() foo_fc = self._tc.create_single_precision_real_field_class() bar_fc = self._tc.create_string_field_class() @@ -1322,10 +1457,6 @@ class DynamicArrayFieldClassTestCase(_ArrayFieldClassTestCase, unittest.TestCase fc.length_field_path.root_scope, bt2.FieldPathScope.PACKET_CONTEXT ) - def test_create_invalid_field_class(self): - with self.assertRaises(TypeError): - self._tc.create_dynamic_array_field_class(object()) - def test_create_invalid_length_type(self): with self.assertRaises(TypeError): self._tc.create_dynamic_array_field_class(