X-Git-Url: http://git.efficios.com/?a=blobdiff_plain;f=tests%2Fbindings%2Fpython%2Fbt2%2Ftest_field_class.py;h=4ca01d866b98fa4a2c797b2f3f19563ec9e23a9f;hb=d47b87ac209ea8f1b98b7e6cf209dfd4bf9cc9c0;hp=ca524bb0ac53131d25b8cdc17c6d36238441def5;hpb=5f25509b06eb8dd0a76d3068894d9f8fefd63178;p=babeltrace.git diff --git a/tests/bindings/python/bt2/test_field_class.py b/tests/bindings/python/bt2/test_field_class.py index ca524bb0..4ca01d86 100644 --- a/tests/bindings/python/bt2/test_field_class.py +++ b/tests/bindings/python/bt2/test_field_class.py @@ -1,527 +1,319 @@ import bt2.field import unittest -import copy import bt2 +from utils import get_default_trace_class -class _TestCopySimple: - def _test_copy(self, cpy): - self.assertIsNot(cpy, self._fc) - self.assertNotEqual(cpy.addr, self._fc.addr) - self.assertEqual(cpy, self._fc) +class _TestIntegerFieldClassProps: + def test_create_default(self): + fc = self._create_func() + self.assertEqual(fc.field_value_range, 64) + self.assertEqual(fc.preferred_display_base, bt2.IntegerDisplayBase.DECIMAL) - def test_copy(self): - cpy = copy.copy(self._fc) - self._test_copy(cpy) + def test_create_range(self): + fc = self._create_func(field_value_range=35) + self.assertEqual(fc.field_value_range, 35) - def test_deepcopy(self): - cpy = copy.deepcopy(self._fc) - self._test_copy(cpy) + fc = self._create_func(36) + self.assertEqual(fc.field_value_range, 36) + def test_create_invalid_range(self): + with self.assertRaises(TypeError): + self._create_func('yes') -class _TestAlignmentProp: - def test_assign_alignment(self): - self._fc.alignment = 32 - self.assertEqual(self._fc.alignment, 32) + with self.assertRaises(TypeError): + self._create_func(field_value_range='yes') - def test_assign_invalid_alignment(self): with self.assertRaises(ValueError): - self._fc.alignment = 23 + self._create_func(field_value_range=-2) + with self.assertRaises(ValueError): + self._create_func(field_value_range=0) -class _TestByteOrderProp: - def test_assign_byte_order(self): - self._fc.byte_order = bt2.ByteOrder.LITTLE_ENDIAN - self.assertEqual(self._fc.byte_order, bt2.ByteOrder.LITTLE_ENDIAN) + def test_create_base(self): + fc = self._create_func(preferred_display_base=bt2.IntegerDisplayBase.HEXADECIMAL) + self.assertEqual(fc.preferred_display_base, bt2.IntegerDisplayBase.HEXADECIMAL) - def test_assign_invalid_byte_order(self): + def test_create_invalid_base_type(self): with self.assertRaises(TypeError): - self._fc.byte_order = 'hey' + self._create_func(preferred_display_base='yes') + def test_create_invalid_base_value(self): + with self.assertRaises(ValueError): + self._create_func(preferred_display_base=444) -class _TestInvalidEq: - def test_eq_invalid(self): - self.assertFalse(self._fc == 23) + def test_create_full(self): + fc = self._create_func(24, preferred_display_base=bt2.IntegerDisplayBase.OCTAL) + self.assertEqual(fc.field_value_range, 24) + self.assertEqual(fc.preferred_display_base, bt2.IntegerDisplayBase.OCTAL) -class _TestIntegerFieldClassProps: - def test_size_prop(self): - self.assertEqual(self._fc.size, 35) +class IntegerFieldClassTestCase(_TestIntegerFieldClassProps, unittest.TestCase): + def setUp(self): + self._tc = get_default_trace_class() + self._create_func = self._tc.create_signed_integer_field_class - def test_assign_signed(self): - self._fc.is_signed = True - self.assertTrue(self._fc.is_signed) - def test_assign_invalid_signed(self): - with self.assertRaises(TypeError): - self._fc.is_signed = 23 +class RealFieldClassTestCase(unittest.TestCase): + def setUp(self): + self._tc = get_default_trace_class() - def test_assign_base(self): - self._fc.base = bt2.Base.HEXADECIMAL - self.assertEqual(self._fc.base, bt2.Base.HEXADECIMAL) + def test_create_default(self): + fc = self._tc.create_real_field_class() + self.assertFalse(fc.is_single_precision) - def test_assign_invalid_base(self): + def test_create_is_single_precision(self): + fc = self._tc.create_real_field_class(is_single_precision=True) + self.assertTrue(fc.is_single_precision) + + def test_create_invalid_is_single_precision(self): with self.assertRaises(TypeError): - self._fc.base = 'hey' + self._tc.create_real_field_class(is_single_precision='hohoho') - def test_assign_encoding(self): - self._fc.encoding = bt2.Encoding.UTF8 - self.assertEqual(self._fc.encoding, bt2.Encoding.UTF8) - def test_assign_invalid_encoding(self): - with self.assertRaises(TypeError): - self._fc.encoding = 'hey' +# Converts an _EnumerationFieldClassMapping to a list of ranges: +# +# [(lower0, upper0), (lower1, upper1), ...] + +def enum_mapping_to_list(mapping): + return sorted([(x.lower, x.upper) for x in mapping]) - def test_assign_mapped_clock_class(self): - cc = bt2.ClockClass('name', 1000) - self._fc.mapped_clock_class = cc - self.assertEqual(self._fc.mapped_clock_class, cc) - def test_assign_invalid_mapped_clock_class(self): +class EnumerationFieldClassTestCase(_TestIntegerFieldClassProps): + def setUp(self): + self._tc = get_default_trace_class() + + def test_create_from_invalid_type(self): with self.assertRaises(TypeError): - self._fc.mapped_clock_class = object() + self._create_func('coucou') + def test_add_mapping_simple(self): + self._fc.map_range('hello', 24) + mapping = self._fc['hello'] + self.assertEqual(mapping.label, 'hello') -@unittest.skip("this is broken") -class IntegerFieldClassTestCase(_TestIntegerFieldClassProps, _TestCopySimple, - _TestAlignmentProp, _TestByteOrderProp, - _TestInvalidEq, unittest.TestCase): - def setUp(self): - self._fc = bt2.IntegerFieldClass(35) + ranges = enum_mapping_to_list(mapping) + self.assertEqual(ranges, [(24, 24)]) - def tearDown(self): - del self._fc + def test_add_mapping_simple_kwargs(self): + self._fc.map_range(label='hello', lower=17, upper=23) + mapping = self._fc['hello'] + self.assertEqual(mapping.label, 'hello') - def test_create_default(self): - self.assertEqual(self._fc.size, 35) - self.assertIsNone(self._fc.mapped_clock_class) + ranges = enum_mapping_to_list(mapping) + self.assertEqual(ranges, [(17, 23)]) - def test_create_invalid_size(self): + def test_add_mapping_range(self): + self._fc.map_range('hello', 21, 199) + mapping = self._fc['hello'] + self.assertEqual(mapping.label, 'hello') + + ranges = enum_mapping_to_list(mapping) + self.assertEqual(ranges, [(21, 199)]) + + def test_add_mapping_invalid_name(self): with self.assertRaises(TypeError): - fc = bt2.IntegerFieldClass('yes') + self._fc.map_range(17, 21, 199) - def test_create_neg_size(self): - with self.assertRaises(ValueError): - fc = bt2.IntegerFieldClass(-2) + def test_iadd(self): + enum_fc = self._tc.create_signed_enumeration_field_class(field_value_range=16) + enum_fc.map_range('c', 4, 5) + enum_fc.map_range('d', 6, 18) + enum_fc.map_range('e', 20, 27) + self._fc.map_range('a', 0, 2) + self._fc.map_range('b', 3) + self._fc += enum_fc - def test_create_neg_zero(self): - with self.assertRaises(ValueError): - fc = bt2.IntegerFieldClass(0) + self.assertEqual(self._fc['a'].label, 'a') + self.assertEqual(enum_mapping_to_list(self._fc['a']), [(0, 2)]) - def test_create_full(self): - cc = bt2.ClockClass('name', 1000) - fc = bt2.IntegerFieldClass(24, alignment=16, - byte_order=bt2.ByteOrder.BIG_ENDIAN, - is_signed=True, base=bt2.Base.OCTAL, - encoding=bt2.Encoding.NONE, - mapped_clock_class=cc) - self.assertEqual(fc.size, 24) - self.assertEqual(fc.alignment, 16) - self.assertEqual(fc.byte_order, bt2.ByteOrder.BIG_ENDIAN) - self.assertTrue(fc.is_signed) - self.assertEqual(fc.base, bt2.Base.OCTAL) - self.assertEqual(fc.encoding, bt2.Encoding.NONE) - self.assertEqual(fc.mapped_clock_class, cc) - - def test_create_field(self): - field = self._fc() - self.assertIsInstance(field, bt2.field._IntegerField) - - def test_create_field_init(self): - field = self._fc(23) - self.assertEqual(field, 23) - - -@unittest.skip("this is broken") -class FloatingPointNumberFieldClassTestCase(_TestCopySimple, _TestAlignmentProp, - _TestByteOrderProp, _TestInvalidEq, - unittest.TestCase): - def setUp(self): - self._fc = bt2.FloatingPointNumberFieldClass() + self.assertEqual(self._fc['b'].label, 'b') + self.assertEqual(enum_mapping_to_list(self._fc['b']), [(3, 3)]) - def tearDown(self): - del self._fc + self.assertEqual(self._fc['c'].label, 'c') + self.assertEqual(enum_mapping_to_list(self._fc['c']), [(4, 5)]) - def test_create_default(self): - pass + self.assertEqual(self._fc['d'].label, 'd') + self.assertEqual(enum_mapping_to_list(self._fc['d']), [(6, 18)]) - def test_create_full(self): - fc = bt2.FloatingPointNumberFieldClass(alignment=16, - byte_order=bt2.ByteOrder.BIG_ENDIAN, - exponent_size=11, - mantissa_size=53) - self.assertEqual(fc.alignment, 16) - self.assertEqual(fc.byte_order, bt2.ByteOrder.BIG_ENDIAN) - self.assertEqual(fc.exponent_size, 11) - self.assertEqual(fc.mantissa_size, 53) - - def test_assign_exponent_size(self): - self._fc.exponent_size = 8 - self.assertEqual(self._fc.exponent_size, 8) - - def test_assign_invalid_exponent_size(self): - with self.assertRaises(TypeError): - self._fc.exponent_size = 'yes' + self.assertEqual(self._fc['e'].label, 'e') + self.assertEqual(enum_mapping_to_list(self._fc['e']), [(20, 27)]) - def test_assign_mantissa_size(self): - self._fc.mantissa_size = 24 - self.assertEqual(self._fc.mantissa_size, 24) + def test_bool_op(self): + self.assertFalse(self._fc) + self._fc.map_range('a', 0) + self.assertTrue(self._fc) - def test_assign_invalid_mantissa_size(self): - with self.assertRaises(TypeError): - self._fc.mantissa_size = 'no' + def test_len(self): + self._fc.map_range('a', 0) + self._fc.map_range('b', 1) + self._fc.map_range('c', 2) + self.assertEqual(len(self._fc), 3) - def test_create_field(self): - field = self._fc() - self.assertIsInstance(field, bt2.field._FloatingPointNumberField) + def test_getitem(self): + self._fc.map_range('a', 0) + self._fc.map_range('b', 1, 3) + self._fc.map_range('a', 5) + self._fc.map_range('a', 17, 123) + self._fc.map_range('C', 5) + mapping = self._fc['a'] - def test_create_field_init(self): - field = self._fc(17.5) - self.assertEqual(field, 17.5) + self.assertEqual(mapping.label, 'a') + ranges = enum_mapping_to_list(mapping) + self.assertEqual(ranges, [(0, 0), (5, 5), (17, 123)]) + with self.assertRaises(KeyError): + self._fc['doesnotexist'] -@unittest.skip("this is broken") -class EnumerationFieldClassTestCase(_TestIntegerFieldClassProps, _TestInvalidEq, - _TestCopySimple, _TestAlignmentProp, - _TestByteOrderProp, unittest.TestCase): - def setUp(self): - self._fc = bt2.EnumerationFieldClass(size=35) + def test_contains(self): + self._fc.map_range('a', 0) + self._fc.map_range('a', 2, 23) + self._fc.map_range('b', 2) + self._fc.map_range('c', 5) - def tearDown(self): - del self._fc + a_mapping = self._fc['a'] + b_mapping = self._fc['b'] + first_range = next(iter(a_mapping)) - def test_create_from_int_fc(self): - int_fc = bt2.IntegerFieldClass(23) - self._fc = bt2.EnumerationFieldClass(int_fc) + self.assertIn(first_range, a_mapping) + self.assertNotIn(first_range, b_mapping) - def test_create_from_invalid_type(self): - with self.assertRaises(TypeError): - self._fc = bt2.EnumerationFieldClass('coucou') + def test_iter(self): + self._fc.map_range('a', 1, 5) + self._fc.map_range('b', 10, 17) + self._fc.map_range('c', 20, 1504) - def test_create_from_invalid_fc(self): - with self.assertRaises(TypeError): - fc = bt2.FloatingPointNumberFieldClass() - self._fc = bt2.EnumerationFieldClass(fc) + self._fc.map_range('d', 22510, 99999) - def test_create_full(self): - fc = bt2.EnumerationFieldClass(size=24, alignment=16, - byte_order=bt2.ByteOrder.BIG_ENDIAN, - is_signed=True, base=bt2.Base.OCTAL, - encoding=bt2.Encoding.NONE, - mapped_clock_class=None) - self.assertEqual(fc.size, 24) - self.assertEqual(fc.alignment, 16) - self.assertEqual(fc.byte_order, bt2.ByteOrder.BIG_ENDIAN) - self.assertTrue(fc.is_signed) - self.assertEqual(fc.base, bt2.Base.OCTAL) - self.assertEqual(fc.encoding, bt2.Encoding.NONE) - #self.assertIsNone(fc.mapped_clock_class) - - def test_integer_field_class_prop(self): - int_fc = bt2.IntegerFieldClass(23) - enum_fc = bt2.EnumerationFieldClass(int_fc) - self.assertEqual(enum_fc.integer_field_class.addr, int_fc.addr) + # This exercises iteration. + labels = sorted(self._fc) - def test_add_mapping_simple(self): - self._fc.add_mapping('hello', 24) - mapping = self._fc[0] - self.assertEqual(mapping.name, 'hello') - self.assertEqual(mapping.lower, 24) - self.assertEqual(mapping.upper, 24) + self.assertEqual(labels, ['a', 'b', 'c', 'd']) - def test_add_mapping_simple_kwargs(self): - self._fc.add_mapping(name='hello', lower=17, upper=23) - mapping = self._fc[0] - self.assertEqual(mapping.name, 'hello') - self.assertEqual(mapping.lower, 17) - self.assertEqual(mapping.upper, 23) + def test_find_by_value(self): + self._fc.map_range('a', 0) + self._fc.map_range('b', 1, 3) + self._fc.map_range('c', 5, 19) + self._fc.map_range('d', 8, 15) + self._fc.map_range('e', 10, 21) + self._fc.map_range('f', 0) + self._fc.map_range('g', 14) - def test_add_mapping_range(self): - self._fc.add_mapping('hello', 21, 199) - mapping = self._fc[0] - self.assertEqual(mapping.name, 'hello') - self.assertEqual(mapping.lower, 21) - self.assertEqual(mapping.upper, 199) + labels = self._fc.labels_by_value(14) - def test_add_mapping_invalid_name(self): - with self.assertRaises(TypeError): - self._fc.add_mapping(17, 21, 199) + expected_labels = ['c', 'd', 'e', 'g'] + + self.assertTrue(all(label in labels for label in expected_labels)) + + +class UnsignedEnumerationFieldClassTestCase(EnumerationFieldClassTestCase, unittest.TestCase): + def setUp(self): + super().setUp() + self._create_func = self._tc.create_unsigned_enumeration_field_class + self._fc = self._tc.create_unsigned_enumeration_field_class() def test_add_mapping_invalid_signedness_lower(self): with self.assertRaises(ValueError): - self._fc.add_mapping('hello', -21, 199) + self._fc.map_range('hello', -21, 199) def test_add_mapping_invalid_signedness_upper(self): with self.assertRaises(ValueError): - self._fc.add_mapping('hello', 21, -199) + self._fc.map_range('hello', 21, -199) - def test_add_mapping_simple_signed(self): - self._fc.is_signed = True - self._fc.add_mapping('hello', -24) - mapping = self._fc[0] - self.assertEqual(mapping.name, 'hello') - self.assertEqual(mapping.lower, -24) - self.assertEqual(mapping.upper, -24) - def test_add_mapping_range_signed(self): - self._fc.is_signed = True - self._fc.add_mapping('hello', -21, 199) - mapping = self._fc[0] - self.assertEqual(mapping.name, 'hello') - self.assertEqual(mapping.lower, -21) - self.assertEqual(mapping.upper, 199) +class SignedEnumerationFieldClassTestCase(EnumerationFieldClassTestCase, unittest.TestCase): + def setUp(self): + super().setUp() + self._create_func = self._tc.create_signed_enumeration_field_class + self._fc = self._tc.create_signed_enumeration_field_class() - def test_iadd(self): - enum_fc = bt2.EnumerationFieldClass(size=16) - enum_fc.add_mapping('c', 4, 5) - enum_fc.add_mapping('d', 6, 18) - enum_fc.add_mapping('e', 20, 27) - self._fc.add_mapping('a', 0, 2) - self._fc.add_mapping('b', 3) - self._fc += enum_fc - self.assertEqual(self._fc[0].name, 'a') - self.assertEqual(self._fc[0].lower, 0) - self.assertEqual(self._fc[0].upper, 2) - self.assertEqual(self._fc[1].name, 'b') - self.assertEqual(self._fc[1].lower, 3) - self.assertEqual(self._fc[1].upper, 3) - self.assertEqual(self._fc[2].name, 'c') - self.assertEqual(self._fc[2].lower, 4) - self.assertEqual(self._fc[2].upper, 5) - self.assertEqual(self._fc[3].name, 'd') - self.assertEqual(self._fc[3].lower, 6) - self.assertEqual(self._fc[3].upper, 18) - self.assertEqual(self._fc[4].name, 'e') - self.assertEqual(self._fc[4].lower, 20) - self.assertEqual(self._fc[4].upper, 27) + def test_add_mapping_simple_signed(self): + self._fc.map_range('hello', -24) + mapping = self._fc['hello'] + self.assertEqual(mapping.label, 'hello') - def test_bool_op(self): - self.assertFalse(self._fc) - self._fc.add_mapping('a', 0) - self.assertTrue(self._fc) + ranges = enum_mapping_to_list(mapping) + self.assertEqual(ranges, [(-24, -24)]) - def test_len(self): - self._fc.add_mapping('a', 0) - self._fc.add_mapping('b', 1) - self._fc.add_mapping('c', 2) - self.assertEqual(len(self._fc), 3) + def test_add_mapping_range_signed(self): + self._fc.map_range('hello', -21, 199) + mapping = self._fc['hello'] + self.assertEqual(mapping.label, 'hello') + ranges = enum_mapping_to_list(mapping) + self.assertEqual(ranges, [(-21, 199)]) - def test_getitem(self): - self._fc.add_mapping('a', 0) - self._fc.add_mapping('b', 1, 3) - self._fc.add_mapping('c', 5) - mapping = self._fc[1] - self.assertEqual(mapping.name, 'b') - self.assertEqual(mapping.lower, 1) - self.assertEqual(mapping.upper, 3) - def test_iter(self): - mappings = ( - ('a', 1, 5), - ('b', 10, 17), - ('c', 20, 1504), - ('d', 22510, 99999), - ) - - for mapping in mappings: - self._fc.add_mapping(*mapping) - - for fc_mapping, mapping in zip(self._fc, mappings): - self.assertEqual(fc_mapping.name, mapping[0]) - self.assertEqual(fc_mapping.lower, mapping[1]) - self.assertEqual(fc_mapping.upper, mapping[2]) - - def test_mapping_eq(self): - enum1 = bt2.EnumerationFieldClass(size=32) - enum2 = bt2.EnumerationFieldClass(size=16) - enum1.add_mapping('b', 1, 3) - enum2.add_mapping('b', 1, 3) - self.assertEqual(enum1[0], enum2[0]) - - def test_mapping_eq_invalid(self): - enum1 = bt2.EnumerationFieldClass(size=32) - enum1.add_mapping('b', 1, 3) - self.assertNotEqual(enum1[0], 23) - - def _test_find_by_name(self, fc): - fc.add_mapping('a', 0) - fc.add_mapping('b', 1, 3) - fc.add_mapping('a', 5) - fc.add_mapping('a', 17, 144) - fc.add_mapping('C', 5) - mapping_iter = fc.mappings_by_name('a') - mappings = list(mapping_iter) - a0 = False - a5 = False - a17_144 = False - i = 0 - - for mapping in mappings: - i += 1 - self.assertEqual(mapping.name, 'a') - - if mapping.lower == 0 and mapping.upper == 0: - a0 = True - elif mapping.lower == 5 and mapping.upper == 5: - a5 = True - elif mapping.lower == 17 and mapping.upper == 144: - a17_144 = True - - self.assertEqual(i, 3) - self.assertTrue(a0) - self.assertTrue(a5) - self.assertTrue(a17_144) - - def test_find_by_name_signed(self): - self._test_find_by_name(bt2.EnumerationFieldClass(size=8, is_signed=True)) - - def test_find_by_name_unsigned(self): - self._test_find_by_name(bt2.EnumerationFieldClass(size=8)) - - def _test_find_by_value(self, fc): - fc.add_mapping('a', 0) - fc.add_mapping('b', 1, 3) - fc.add_mapping('c', 5, 19) - fc.add_mapping('d', 8, 15) - fc.add_mapping('e', 10, 21) - fc.add_mapping('f', 0) - fc.add_mapping('g', 14) - mapping_iter = fc.mappings_by_value(14) - mappings = list(mapping_iter) - c = False - d = False - e = False - g = False - i = 0 - - for mapping in mappings: - i += 1 - - if mapping.name == 'c': - c = True - elif mapping.name == 'd': - d = True - elif mapping.name == 'e': - e = True - elif mapping.name == 'g': - g = True - - self.assertEqual(i, 4) - self.assertTrue(c) - self.assertTrue(d) - self.assertTrue(e) - self.assertTrue(g) - - def test_find_by_value_signed(self): - self._test_find_by_value(bt2.EnumerationFieldClass(size=8, is_signed=True)) - - def test_find_by_value_unsigned(self): - self._test_find_by_value(bt2.EnumerationFieldClass(size=8)) - - def test_create_field(self): - self._fc.add_mapping('c', 4, 5) - field = self._fc() - self.assertIsInstance(field, bt2.field._EnumerationField) - - def test_create_field_init(self): - self._fc.add_mapping('c', 4, 5) - field = self._fc(4) - self.assertEqual(field, 4) - - -@unittest.skip("this is broken") -class StringFieldClassTestCase(_TestCopySimple, _TestInvalidEq, - unittest.TestCase): +class StringFieldClassTestCase(unittest.TestCase): def setUp(self): - self._fc = bt2.StringFieldClass() - - def tearDown(self): - del self._fc + tc = get_default_trace_class() + self._fc = tc.create_string_field_class() def test_create_default(self): - pass - - def test_create_full(self): - fc = bt2.StringFieldClass(encoding=bt2.Encoding.UTF8) - self.assertEqual(fc.encoding, bt2.Encoding.UTF8) - - def test_assign_encoding(self): - self._fc.encoding = bt2.Encoding.UTF8 - self.assertEqual(self._fc.encoding, bt2.Encoding.UTF8) - - def test_assign_invalid_encoding(self): - with self.assertRaises(TypeError): - self._fc.encoding = 'yes' - - def test_create_field(self): - field = self._fc() - self.assertIsInstance(field, bt2.field._StringField) + self.assertIsNotNone(self._fc) - def test_create_field_init(self): - field = self._fc('hola') - self.assertEqual(field, 'hola') - -class _TestFieldContainer(_TestInvalidEq, _TestCopySimple): - def test_append_field(self): - int_field_class = bt2.IntegerFieldClass(32) - self._fc.append_field('int32', int_field_class) +class _TestFieldContainer(): + def test_append_element(self): + int_field_class = self._tc.create_signed_integer_field_class(32) + self._append_element_method(self._fc, 'int32', int_field_class) field_class = self._fc['int32'] - self.assertEqual(field_class, int_field_class) + self.assertEqual(field_class.addr, int_field_class.addr) - def test_append_field_kwargs(self): - int_field_class = bt2.IntegerFieldClass(32) - self._fc.append_field(name='int32', field_class=int_field_class) + def test_append_elemenbt_kwargs(self): + int_field_class = self._tc.create_signed_integer_field_class(32) + self._append_element_method(self._fc, name='int32', field_class=int_field_class) field_class = self._fc['int32'] - self.assertEqual(field_class, int_field_class) + self.assertEqual(field_class.addr, int_field_class.addr) + + def test_append_element_invalid_name(self): + sub_fc = self._tc.create_string_field_class() - def test_append_field_invalid_name(self): with self.assertRaises(TypeError): - self._fc.append_field(23, bt2.StringFieldClass()) + self._append_element_method(self._fc, 23, sub_fc) - def test_append_field_invalid_field_class(self): + def test_append_element_invalid_field_class(self): with self.assertRaises(TypeError): - self._fc.append_field('yes', object()) + self._append_element_method(self._fc, 'yes', object()) def test_iadd(self): - struct_fc = bt2.StructureFieldClass() - c_field_class = bt2.StringFieldClass() - d_field_class = bt2.EnumerationFieldClass(size=32) - e_field_class = bt2.StructureFieldClass() - struct_fc.append_field('c_string', c_field_class) - struct_fc.append_field('d_enum', d_field_class) - struct_fc.append_field('e_struct', e_field_class) - a_field_class = bt2.FloatingPointNumberFieldClass() - b_field_class = bt2.IntegerFieldClass(17) - self._fc.append_field('a_float', a_field_class) - self._fc.append_field('b_int', b_field_class) + struct_fc = self._tc.create_structure_field_class() + c_field_class = self._tc.create_string_field_class() + d_field_class = self._tc.create_signed_enumeration_field_class(field_value_range=32) + e_field_class = self._tc.create_structure_field_class() + self._append_element_method(struct_fc, 'c_string', c_field_class) + self._append_element_method(struct_fc, 'd_enum', d_field_class) + self._append_element_method(struct_fc, 'e_struct', e_field_class) + a_field_class = self._tc.create_real_field_class() + b_field_class = self._tc.create_signed_integer_field_class(17) + self._append_element_method(self._fc, 'a_float', a_field_class) + self._append_element_method(self._fc, 'b_int', b_field_class) self._fc += struct_fc - self.assertEqual(self._fc['a_float'], a_field_class) - self.assertEqual(self._fc['b_int'], b_field_class) - self.assertEqual(self._fc['c_string'], c_field_class) - self.assertEqual(self._fc['d_enum'], d_field_class) - self.assertEqual(self._fc['e_struct'], e_field_class) + self.assertEqual(self._fc['a_float'].addr, a_field_class.addr) + self.assertEqual(self._fc['b_int'].addr, b_field_class.addr) + self.assertEqual(self._fc['c_string'].addr, c_field_class.addr) + self.assertEqual(self._fc['d_enum'].addr, d_field_class.addr) + self.assertEqual(self._fc['e_struct'].addr, e_field_class.addr) def test_bool_op(self): self.assertFalse(self._fc) - self._fc.append_field('a', bt2.StringFieldClass()) + self._append_element_method(self._fc, 'a', self._tc.create_string_field_class()) self.assertTrue(self._fc) def test_len(self): - fc = bt2.StringFieldClass() - self._fc.append_field('a', fc) - self._fc.append_field('b', fc) - self._fc.append_field('c', fc) + fc = self._tc.create_string_field_class() + self._append_element_method(self._fc, 'a', fc) + self._append_element_method(self._fc, 'b', fc) + self._append_element_method(self._fc, 'c', fc) self.assertEqual(len(self._fc), 3) def test_getitem(self): - a_fc = bt2.IntegerFieldClass(32) - b_fc = bt2.StringFieldClass() - c_fc = bt2.FloatingPointNumberFieldClass() - self._fc.append_field('a', a_fc) - self._fc.append_field('b', b_fc) - self._fc.append_field('c', c_fc) - self.assertEqual(self._fc['b'], b_fc) + a_fc = self._tc.create_signed_integer_field_class(32) + b_fc = self._tc.create_string_field_class() + c_fc = self._tc.create_real_field_class() + self._append_element_method(self._fc, 'a', a_fc) + self._append_element_method(self._fc, 'b', b_fc) + self._append_element_method(self._fc, 'c', c_fc) + self.assertEqual(self._fc['b'].addr, b_fc.addr) def test_getitem_invalid_key_type(self): with self.assertRaises(TypeError): @@ -533,13 +325,13 @@ class _TestFieldContainer(_TestInvalidEq, _TestCopySimple): def test_contains(self): self.assertFalse('a' in self._fc) - self._fc.append_field('a', bt2.StringFieldClass()) + self._append_element_method(self._fc, 'a', self._tc.create_string_field_class()) self.assertTrue('a' in self._fc) def test_iter(self): - a_fc = bt2.IntegerFieldClass(32) - b_fc = bt2.StringFieldClass() - c_fc = bt2.FloatingPointNumberFieldClass() + a_fc = self._tc.create_signed_integer_field_class(32) + b_fc = self._tc.create_string_field_class() + c_fc = self._tc.create_real_field_class() fields = ( ('a', a_fc), ('b', b_fc), @@ -547,161 +339,228 @@ class _TestFieldContainer(_TestInvalidEq, _TestCopySimple): ) for field in fields: - self._fc.append_field(*field) + self._append_element_method(self._fc, *field) for (name, fc_field_class), field in zip(self._fc.items(), fields): self.assertEqual(name, field[0]) - self.assertEqual(fc_field_class, field[1]) + self.assertEqual(fc_field_class.addr, field[1].addr) def test_at_index(self): - a_fc = bt2.IntegerFieldClass(32) - b_fc = bt2.StringFieldClass() - c_fc = bt2.FloatingPointNumberFieldClass() - self._fc.append_field('c', c_fc) - self._fc.append_field('a', a_fc) - self._fc.append_field('b', b_fc) - self.assertEqual(self._fc.at_index(1), a_fc) + a_fc = self._tc.create_signed_integer_field_class(32) + b_fc = self._tc.create_string_field_class() + c_fc = self._tc.create_real_field_class() + self._append_element_method(self._fc, 'c', c_fc) + self._append_element_method(self._fc, 'a', a_fc) + self._append_element_method(self._fc, 'b', b_fc) + self.assertEqual(self._at_index_method(self._fc, 1).addr, a_fc.addr) def test_at_index_invalid(self): - self._fc.append_field('c', bt2.IntegerFieldClass(32)) + self._append_element_method(self._fc, 'c', self._tc.create_signed_integer_field_class(32)) with self.assertRaises(TypeError): - self._fc.at_index('yes') + self._at_index_method(self._fc, 'yes') def test_at_index_out_of_bounds_after(self): - self._fc.append_field('c', bt2.IntegerFieldClass(32)) + self._append_element_method(self._fc, 'c', self._tc.create_signed_integer_field_class(32)) with self.assertRaises(IndexError): - self._fc.at_index(len(self._fc)) + self._at_index_method(self._fc, len(self._fc)) -@unittest.skip("this is broken") class StructureFieldClassTestCase(_TestFieldContainer, unittest.TestCase): def setUp(self): - self._fc = bt2.StructureFieldClass() + self._append_element_method = bt2.field_class._StructureFieldClass.append_member + self._at_index_method = bt2.field_class._StructureFieldClass.member_at_index + self._tc = get_default_trace_class() + self._fc = self._tc.create_structure_field_class() + + def test_create_default(self): + self.assertIsNotNone(self._fc) - def tearDown(self): - del self._fc + +class VariantFieldClassTestCase(_TestFieldContainer, unittest.TestCase): + def setUp(self): + self._append_element_method = bt2.field_class._VariantFieldClass.append_option + self._at_index_method = bt2.field_class._VariantFieldClass.option_at_index + self._tc = get_default_trace_class() + self._fc = self._tc.create_variant_field_class() def test_create_default(self): - self.assertEqual(self._fc.alignment, 1) + fc = self._tc.create_variant_field_class() - def test_create_with_min_alignment(self): - fc = bt2.StructureFieldClass(8) - self.assertEqual(fc.alignment, 8) + self.assertIsNone(fc.selector_field_path) - def test_assign_alignment(self): - with self.assertRaises(AttributeError): - self._fc.alignment = 32 + def _create_field_class_for_field_path_test(self): + # Create something equivalent to: + # + # struct outer_struct_fc { + # real foo; + # struct inner_struct_fc { + # enum { first = 1, second = 2..434 } selector; + # string bar; + # string baz; + # variant { + # real a; + # int21_t b; + # uint34_t c; + # } variant; + # } inner_struct[2]; + # }; + selector_fc = self._tc.create_unsigned_enumeration_field_class(field_value_range=42) + selector_fc.map_range('first', 1) + selector_fc.map_range('second', 2, 434) - def test_assign_min_alignment(self): - self._fc.min_alignment = 64 - self.assertTrue(self._fc.alignment >= 64) + fc = self._tc.create_variant_field_class(selector_fc) + fc.append_option('a', self._tc.create_real_field_class()) + fc.append_option('b', self._tc.create_signed_integer_field_class(21)) + fc.append_option('c', self._tc.create_unsigned_integer_field_class(34)) - def test_assign_invalid_min_alignment(self): - with self.assertRaises(ValueError): - self._fc.min_alignment = 23 + foo_fc = self._tc.create_real_field_class() + bar_fc = self._tc.create_string_field_class() + baz_fc = self._tc.create_string_field_class() - def test_assign_get_min_alignment(self): - with self.assertRaises(AttributeError): - self._fc.min_alignment + inner_struct_fc = self._tc.create_structure_field_class() + inner_struct_fc.append_member('selector', selector_fc) + inner_struct_fc.append_member('bar', bar_fc) + inner_struct_fc.append_member('baz', baz_fc) + inner_struct_fc.append_member('variant', fc) - def test_create_field(self): - field = self._fc() - self.assertIsInstance(field, bt2.field._StructureField) + inner_struct_array_fc = self._tc.create_static_array_field_class(inner_struct_fc, 2) - def test_create_field_init_invalid(self): - with self.assertRaises(bt2.Error): - field = self._fc(23) + 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(packet_context_field_class=outer_struct_fc) -@unittest.skip("this is broken") -class VariantFieldClassTestCase(_TestFieldContainer, unittest.TestCase): - def setUp(self): - self._fc = bt2.VariantFieldClass('path.to.tag') + return fc - def tearDown(self): - del self._fc + def test_selector_field_path_length(self): + fc = self._create_field_class_for_field_path_test() + self.assertEqual(len(fc.selector_field_path), 3) - def test_create_default(self): - self.assertEqual(self._fc.tag_name, 'path.to.tag') + def test_selector_field_path_iter(self): + fc = self._create_field_class_for_field_path_test() + path_items = list(fc.selector_field_path) - def test_create_invalid_tag_name(self): - with self.assertRaises(TypeError): - self._fc = bt2.VariantFieldClass(23) + self.assertEqual(len(path_items), 3) - def test_assign_tag_name(self): - self._fc.tag_name = 'a.different.tag' - self.assertEqual(self._fc.tag_name, 'a.different.tag') + self.assertIsInstance(path_items[0], bt2.field_path._IndexFieldPathItem) + self.assertEqual(path_items[0].index, 1) - def test_assign_invalid_tag_name(self): - with self.assertRaises(TypeError): - self._fc.tag_name = -17 + self.assertIsInstance(path_items[1], bt2.field_path._CurrentArrayElementFieldPathItem) + self.assertIsInstance(path_items[2], bt2.field_path._IndexFieldPathItem) + self.assertEqual(path_items[2].index, 0) -@unittest.skip("this is broken") -class ArrayFieldClassTestCase(_TestInvalidEq, _TestCopySimple, - unittest.TestCase): - def setUp(self): - self._elem_fc = bt2.IntegerFieldClass(23) - self._fc = bt2.ArrayFieldClass(self._elem_fc, 45) + def test_selector_field_path_root_scope(self): + fc = self._create_field_class_for_field_path_test() + self.assertEqual(fc.selector_field_path.root_scope, bt2.field_path.Scope.PACKET_CONTEXT) - def tearDown(self): - del self._fc - del self._elem_fc + +class StaticArrayFieldClassTestCase(unittest.TestCase): + def setUp(self): + self._tc = get_default_trace_class() + self._elem_fc = self._tc.create_signed_integer_field_class(23) def test_create_default(self): - self.assertEqual(self._fc.element_field_class, self._elem_fc) - self.assertEqual(self._fc.length, 45) + fc = self._tc.create_static_array_field_class(self._elem_fc, 45) + self.assertEqual(fc.element_field_class.addr, self._elem_fc.addr) + self.assertEqual(fc.length, 45) - def test_create_invalid_field_class(self): + def test_create_invalid_elem_field_class(self): with self.assertRaises(TypeError): - self._fc = bt2.ArrayFieldClass(object(), 45) + self._tc.create_static_array_field_class(object(), 45) def test_create_invalid_length(self): with self.assertRaises(ValueError): - self._fc = bt2.ArrayFieldClass(bt2.StringFieldClass(), -17) + self._tc.create_static_array_field_class(self._tc.create_string_field_class(), -17) def test_create_invalid_length_type(self): with self.assertRaises(TypeError): - self._fc = bt2.ArrayFieldClass(bt2.StringFieldClass(), 'the length') + self._tc.create_static_array_field_class(self._tc.create_string_field_class(), 'the length') + + +class DynamicArrayFieldClassTestCase(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_field(self): - field = self._fc() - self.assertIsInstance(field, bt2.field._ArrayField) + 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.assertIsNone(fc.length_field_path, None) - def test_create_field_init_invalid(self): - with self.assertRaises(bt2.Error): - field = self._fc(23) + def _create_field_class_for_field_path_test(self): + # Create something a field class that is equivalent to: + # + # struct outer_struct_fc { + # real foo; + # struct inner_struct_fc { + # string bar; + # string baz; + # uint12_t len; + # uint23_t dyn_array[len]; + # } inner_struct[2]; + # }; + fc = self._tc.create_dynamic_array_field_class(self._elem_fc, self._len_fc) -@unittest.skip("this is broken") -class SequenceFieldClassTestCase(_TestInvalidEq, _TestCopySimple, - unittest.TestCase): - def setUp(self): - self._elem_fc = bt2.IntegerFieldClass(23) - self._fc = bt2.SequenceFieldClass(self._elem_fc, 'the.length') + foo_fc = self._tc.create_real_field_class() + bar_fc = self._tc.create_string_field_class() + baz_fc = self._tc.create_string_field_class() - def tearDown(self): - del self._fc - del self._elem_fc + inner_struct_fc = self._tc.create_structure_field_class() + inner_struct_fc.append_member('bar', bar_fc) + inner_struct_fc.append_member('baz', baz_fc) + inner_struct_fc.append_member('len', self._len_fc) + inner_struct_fc.append_member('dyn_array', fc) - def test_create_default(self): - self.assertEqual(self._fc.element_field_class, self._elem_fc) - self.assertEqual(self._fc.length_name, 'the.length') + 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 length field is resolved when the sequence is + # actually used, for example in a packet context. + self._tc.create_stream_class(packet_context_field_class=outer_struct_fc) + + return fc + + def test_field_path_len(self): + fc = self._create_field_class_for_field_path_test() + self.assertEqual(len(fc.length_field_path), 3) + + def test_field_path_iter(self): + fc = self._create_field_class_for_field_path_test() + path_items = list(fc.length_field_path) + + self.assertEqual(len(path_items), 3) + + self.assertIsInstance(path_items[0], bt2.field_path._IndexFieldPathItem) + self.assertEqual(path_items[0].index, 1) + + self.assertIsInstance(path_items[1], bt2.field_path._CurrentArrayElementFieldPathItem) + + self.assertIsInstance(path_items[2], bt2.field_path._IndexFieldPathItem) + self.assertEqual(path_items[2].index, 2) + + def test_field_path_root_scope(self): + fc = self._create_field_class_for_field_path_test() + self.assertEqual(fc.length_field_path.root_scope, bt2.field_path.Scope.PACKET_CONTEXT) def test_create_invalid_field_class(self): with self.assertRaises(TypeError): - self._fc = bt2.ArrayFieldClass(object(), 'the.length') + self._tc.create_dynamic_array_field_class(object()) def test_create_invalid_length_type(self): with self.assertRaises(TypeError): - self._fc = bt2.SequenceFieldClass(bt2.StringFieldClass(), 17) + self._tc.create_dynamic_array_field_class(self._tc.create_string_field_class(), 17) - def test_create_field(self): - field = self._fc() - self.assertIsInstance(field, bt2.field._SequenceField) - def test_create_field_init_invalid(self): - with self.assertRaises(bt2.Error): - field = self._fc(23) +if __name__ == "__main__": + unittest.main()