From 1eccc498917f855490876fc8b3e41f90f89f1c6d Mon Sep 17 00:00:00 2001 From: Simon Marchi Date: Tue, 21 May 2019 17:01:59 -0400 Subject: [PATCH] bt2: Adapt test_field.py and make it pass Update test_field.py and field.py to match the current API of Babeltrace. In the test, most changes are related to the fact that field classes and fields are created ultimately from a trace class. A bunch of helpers are added at the top, which allow to easily create a field of a certain type without repeating the boilerplate. Anything related to copy and deepcopy is removed. However, the test _TestNumericField._test_binop_lhs_value_same relies on copy support, so I am not too sure what to do with it. I have currently marked it as skipped, but we should either adapt it or remove it. A concept that disappeared (or rather, is not exposed by the public API) is whether a field is set or not, and the ability to reset a field's value. All tests and code related to that are removed. Change-Id: I27f1ee6a3a2152232556d9d9c301de8411189a2c Signed-off-by: Simon Marchi Signed-off-by: Francis Deslauriers Reviewed-on: https://review.lttng.org/c/babeltrace/+/1323 Tested-by: jenkins Reviewed-by: Philippe Proulx --- bindings/python/bt2/bt2/__init__.py.in | 9 - bindings/python/bt2/bt2/field.py | 300 +++----- bindings/python/bt2/bt2/native_bt_field.i | 11 +- tests/bindings/python/bt2/test_field.py | 839 ++++++++++------------ 4 files changed, 488 insertions(+), 671 deletions(-) diff --git a/bindings/python/bt2/bt2/__init__.py.in b/bindings/python/bt2/bt2/__init__.py.in index 08fc0cac..3dac124e 100644 --- a/bindings/python/bt2/bt2/__init__.py.in +++ b/bindings/python/bt2/bt2/__init__.py.in @@ -44,15 +44,6 @@ from bt2.event_class import * from bt2.field_class import * from bt2.field_path import * from bt2.field import * -from bt2.field import _ArrayField -from bt2.field import _EnumerationField -from bt2.field import _Field -from bt2.field import _RealField -from bt2.field import _IntegerField -from bt2.field import _SequenceField -from bt2.field import _StringField -from bt2.field import _StructureField -from bt2.field import _VariantField from bt2.graph import * from bt2.logging import * from bt2.message import * diff --git a/bindings/python/bt2/bt2/field.py b/bindings/python/bt2/bt2/field.py index df3f967c..62dac5ff 100644 --- a/bindings/python/bt2/bt2/field.py +++ b/bindings/python/bt2/bt2/field.py @@ -26,17 +26,9 @@ import collections.abc import functools import numbers import math -import abc import bt2 -def _get_leaf_field(obj): - if type(obj) is not _VariantField: - return obj - - return _get_leaf_field(obj.selected_field) - - def _create_field_from_ptr(ptr, owner_ptr, owner_get_ref, owner_put_ref): field_class_ptr = native_bt.field_borrow_class_const(ptr) utils._handle_ptr(field_class_ptr, "cannot get field object's class") @@ -46,25 +38,19 @@ def _create_field_from_ptr(ptr, owner_ptr, owner_get_ref, owner_put_ref): return field -class _Field(object._UniqueObject, metaclass=abc.ABCMeta): - def __copy__(self): - ptr = native_bt.field_copy(self._ptr) - utils._handle_ptr(ptr, 'cannot copy {} field object'.format(self._NAME.lower())) - return _create_from_ptr(ptr) +# Get the "effective" field of `field`. If `field` is a variant, return the +# currently selected field. If `field` is of any other type, return `field` +# directly. - def __deepcopy__(self, memo): - cpy = self.__copy__() - memo[id(self)] = cpy - return cpy +def _get_leaf_field(field): + if not isinstance(field, _VariantField): + return field + + return _get_leaf_field(field.selected_option) - def __eq__(self, other): - # special case: two unset fields with the same field class are equal - if isinstance(other, _Field): - if not self.is_set or not other.is_set: - if not self.is_set and not other.is_set and self.field_class == other.field_class: - return True - return False +class _Field(object._UniqueObject): + def __eq__(self, other): other = _get_leaf_field(other) return self._spec_eq(other) @@ -74,20 +60,11 @@ class _Field(object._UniqueObject, metaclass=abc.ABCMeta): assert field_class_ptr is not None return bt2.field_class._create_field_class_from_ptr_and_get_ref(field_class_ptr) - @property - def is_set(self): - is_set = native_bt.field_is_set(self._ptr) - return is_set > 0 - - def reset(self): - ret = native_bt.field_reset(self._ptr) - utils._handle_ret(ret, "cannot reset field object's value") - def _repr(self): raise NotImplementedError def __repr__(self): - return self._repr() if self.is_set else 'Unset' + return self._repr() @functools.total_ordering @@ -133,7 +110,7 @@ class _NumericField(_Field): def _spec_eq(self, other): if not isinstance(other, numbers.Number): - return False + return NotImplemented return self._value == complex(other) @@ -287,6 +264,7 @@ class _IntegerField(_IntegralField, _Field): class _UnsignedIntegerField(_IntegerField, _Field): _NAME = 'Unsigned integer' + def _value_to_int(self, value): if not isinstance(value, numbers.Real): raise TypeError('expecting a real number object') @@ -309,6 +287,7 @@ class _UnsignedIntegerField(_IntegerField, _Field): class _SignedIntegerField(_IntegerField, _Field): _NAME = 'Signed integer' + def _value_to_int(self, value): if not isinstance(value, numbers.Real): raise TypeError('expecting a real number object') @@ -350,37 +329,30 @@ class _RealField(_NumericField, numbers.Real): class _EnumerationField(_IntegerField): - _NAME = 'Enumeration' + def _repr(self): + return '{} ({})'.format(self._value, ', '.join(self.labels)) @property - def integer_field(self): - int_field_ptr = native_bt.field_enumeration_get_container(self._ptr) - assert(int_field_ptr) - return _create_from_ptr(int_field_ptr) + def labels(self): + ret, labels = self._get_mapping_labels(self._ptr) + utils._handle_ret(ret, "cannot get label for enumeration field") - def _set_value(self, value): - self.integer_field.value = value + assert labels is not None + return labels - def _repr(self): - labels = [repr(v.name) for v in self.mappings] - return '{} ({})'.format(self._value, ', '.join(labels)) - value = property(fset=_set_value) +class _UnsignedEnumerationField(_EnumerationField, _UnsignedIntegerField): + _NAME = 'Unsigned Enumeration' + _get_mapping_labels = staticmethod(native_bt.field_unsigned_enumeration_get_mapping_labels) - @property - def _value(self): - return self.integer_field._value - @property - def mappings(self): - iter_ptr = native_bt.field_enumeration_get_mappings(self._ptr) - assert(iter_ptr) - return bt2.field_class._EnumerationFieldClassMappingIterator(iter_ptr, - self.field_class.is_signed) +class _SignedEnumerationField(_EnumerationField, _SignedIntegerField): + _NAME = 'Signed Enumeration' + _get_mapping_labels = staticmethod(native_bt.field_signed_enumeration_get_mapping_labels) @functools.total_ordering -class _StringField(_Field, collections.abc.Sequence): +class _StringField(_Field): _NAME = 'String' def _value_to_str(self, value): @@ -394,20 +366,18 @@ class _StringField(_Field, collections.abc.Sequence): @property def _value(self): - value = native_bt.field_string_get_value(self._ptr) - return value + return native_bt.field_string_get_value(self._ptr) def _set_value(self, value): value = self._value_to_str(value) - ret = native_bt.field_string_set_value(self._ptr, value) - utils._handle_ret(ret, "cannot set string field object's value") + native_bt.field_string_set_value(self._ptr, value) value = property(fset=_set_value) def _spec_eq(self, other): try: other = self._value_to_str(other) - except: + except Exception: return False return self._value == other @@ -425,13 +395,13 @@ class _StringField(_Field, collections.abc.Sequence): return repr(self._value) def __str__(self): - return self._value if self.is_set else repr(self) + return str(self._value) def __getitem__(self, index): return self._value[index] def __len__(self): - return len(self._value) + return native_bt.field_string_get_length(self._ptr) def __iadd__(self, value): value = self._value_to_str(value) @@ -446,7 +416,7 @@ class _ContainerField(_Field): def __len__(self): count = self._count() - assert(count >= 0) + assert count >= 0 return count def __delitem__(self, index): @@ -459,17 +429,6 @@ class _StructureField(_ContainerField, collections.abc.MutableMapping): def _count(self): return len(self.field_class) - def __getitem__(self, key): - utils._check_str(key) - field_ptr = native_bt.field_structure_borrow_member_field_by_name(self._ptr, key) - - if field_ptr is None: - raise KeyError(key) - - return _create_field_from_ptr(field_ptr, self._owner_ptr, - self._owner_get_ref, - self._owner_put_ref) - def __setitem__(self, key, value): # raises if key is somehow invalid field = self[key] @@ -478,16 +437,6 @@ class _StructureField(_ContainerField, collections.abc.MutableMapping): # the appropriate exception field.value = value - def at_index(self, index): - utils._check_uint64(index) - - if index >= len(self): - raise IndexError - - field_ptr = native_bt.field_structure_get_field_by_index(self._ptr, index) - assert(field_ptr) - return _create_from_ptr(field_ptr) - def __iter__(self): # same name iterator return iter(self.field_class) @@ -507,21 +456,14 @@ class _StructureField(_ContainerField, collections.abc.MutableMapping): return False return True - except: + except Exception: return False - @property - def _value(self): - return {key: value._value for key, value in self.items()} - def _set_value(self, values): - original_values = self._value - try: for key, value in values.items(): self[key].value = value - except: - self.value = original_values + except Exception: raise value = property(fset=_set_value) @@ -530,60 +472,76 @@ class _StructureField(_ContainerField, collections.abc.MutableMapping): items = ['{}: {}'.format(repr(k), repr(v)) for k, v in self.items()] return '{{{}}}'.format(', '.join(items)) + def __getitem__(self, key): + utils._check_str(key) + field_ptr = native_bt.field_structure_borrow_member_field_by_name(self._ptr, key) -class _VariantField(_Field): - _NAME = 'Variant' + if field_ptr is None: + raise KeyError(key) - @property - def tag_field(self): - field_ptr = native_bt.field_variant_get_tag(self._ptr) + return _create_field_from_ptr(field_ptr, self._owner_ptr, + self._owner_get_ref, + self._owner_put_ref) - if field_ptr is None: - return + def member_at_index(self, index): + utils._check_uint64(index) - return _create_from_ptr(field_ptr) + if index >= len(self): + raise IndexError + + field_ptr = native_bt.field_structure_borrow_member_field_by_index(self._ptr, index) + assert field_ptr is not None + return _create_field_from_ptr(field_ptr, self._owner_ptr, + self._owner_get_ref, + self._owner_put_ref) + + +class _VariantField(_ContainerField, _Field): + _NAME = 'Variant' @property - def selected_field(self): - return self.field() + def selected_option_index(self): + return native_bt.field_variant_get_selected_option_field_index(self._ptr) - def field(self, tag_field=None): - if tag_field is None: - field_ptr = native_bt.field_variant_get_current_field(self._ptr) + @selected_option_index.setter + def selected_option_index(self, index): + native_bt.field_variant_select_option_field(self._ptr, index) - if field_ptr is None: - return - else: - utils._check_type(tag_field, _EnumerationField) - field_ptr = native_bt.field_variant_get_field(self._ptr, tag_field._ptr) - utils._handle_ptr(field_ptr, "cannot select variant field object's field") + @property + def selected_option(self): + field_ptr = native_bt.field_variant_borrow_selected_option_field(self._ptr) + utils._handle_ptr(field_ptr, "cannot get variant field's selected option") - return _create_from_ptr(field_ptr) + return _create_field_from_ptr(field_ptr, self._owner_ptr, + self._owner_get_ref, + self._owner_put_ref) def _spec_eq(self, other): - return _get_leaf_field(self) == other + new_self = _get_leaf_field(self) + return new_self == other def __bool__(self): - return bool(self.selected_field) + raise NotImplementedError def __str__(self): - return str(self.selected_field) if self.is_set else repr(self) + return str(self.selected_option) def _repr(self): - return repr(self.selected_field) - - @property - def _value(self): - if self.selected_field is not None: - return self.selected_field._value + return repr(self.selected_option) def _set_value(self, value): - self.selected_field.value = value + self.selected_option.value = value value = property(fset=_set_value) -class _ArraySequenceField(_ContainerField, collections.abc.MutableSequence): +class _ArrayField(_ContainerField, _Field): + + def _get_length(self): + return native_bt.field_array_get_length(self._ptr) + + length = property(fget=_get_length) + def __getitem__(self, index): if not isinstance(index, numbers.Integral): raise TypeError("'{}' is not an integral number object: invalid index".format(index.__class__.__name__)) @@ -593,9 +551,11 @@ class _ArraySequenceField(_ContainerField, collections.abc.MutableSequence): if index < 0 or index >= len(self): raise IndexError('{} field object index is out of range'.format(self._NAME)) - field_ptr = self._get_field_ptr_at_index(index) + field_ptr = native_bt.field_array_borrow_element_field_by_index(self._ptr, index) assert(field_ptr) - return _create_from_ptr(field_ptr) + return _create_field_from_ptr(field_ptr, self._owner_ptr, + self._owner_get_ref, + self._owner_put_ref) def __setitem__(self, index, value): # we can only set numbers and strings @@ -625,92 +585,51 @@ class _ArraySequenceField(_ContainerField, collections.abc.MutableSequence): return False return True - except: + except Exception: return False - @property - def _value(self): - return [field._value for field in self] - def _repr(self): return '[{}]'.format(', '.join([repr(v) for v in self])) -class _ArrayField(_ArraySequenceField): - _NAME = 'Array' +class _StaticArrayField(_ArrayField, _Field): + _NAME = 'Static array' def _count(self): - return self.field_class.length - - def _get_field_ptr_at_index(self, index): - return native_bt.field_array_get_field(self._ptr, index) + return native_bt.field_array_get_length(self._ptr) def _set_value(self, values): if len(self) != len(values): raise ValueError( 'expected length of value and array field to match') - original_values = self._value - try: - for index, value in enumerate(values): - if value is not None: - self[index].value = value - else: - self[index].reset() - except: - self.value = original_values - raise + for index, value in enumerate(values): + if value is not None: + self[index].value = value value = property(fset=_set_value) -class _SequenceField(_ArraySequenceField): - _NAME = 'Sequence' +class _DynamicArrayField(_ArrayField, _Field): + _NAME = 'Dynamic array' def _count(self): - return int(self.length_field) + return self.length - @property - def length_field(self): - field_ptr = native_bt.field_sequence_get_length(self._ptr) - if field_ptr is None: - return - return _create_from_ptr(field_ptr) - - @length_field.setter - def length_field(self, length_field): - utils._check_type(length_field, _IntegerField) - ret = native_bt.field_sequence_set_length(self._ptr, length_field._ptr) - utils._handle_ret(ret, "cannot set sequence field object's length field") + def _set_length(self, length): + utils._check_uint64(length) + ret = native_bt.field_dynamic_array_set_length(self._ptr, length) + utils._handle_ret(ret, "cannot set dynamic array length") - def _get_field_ptr_at_index(self, index): - return native_bt.field_sequence_get_field(self._ptr, index) + length = property(fget=_ArrayField._get_length, fset=_set_length) def _set_value(self, values): - original_length_field = self.length_field - if original_length_field is not None: - original_values = self._value - - if len(values) != self.length_field: - if self.length_field is not None: - length_fc = self.length_field.field_class - else: - length_fc = bt2.IntegerFieldClass(size=64, is_signed=False) - self.length_field = length_fc(len(values)) + if len(values) != self.length: + self.length = len(values) - try: - for index, value in enumerate(values): - if value is not None: - self[index].value = value - else: - self[index].reset() - except: - if original_length_field is not None: - self.length_field = original_length_field - self.value = original_values - else: - self.reset() - raise + for index, value in enumerate(values): + if value is not None: + self[index].value = value value = property(fset=_set_value) @@ -719,6 +638,11 @@ _TYPE_ID_TO_OBJ = { native_bt.FIELD_CLASS_TYPE_UNSIGNED_INTEGER: _UnsignedIntegerField, native_bt.FIELD_CLASS_TYPE_SIGNED_INTEGER: _SignedIntegerField, native_bt.FIELD_CLASS_TYPE_REAL: _RealField, + native_bt.FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION: _UnsignedEnumerationField, + native_bt.FIELD_CLASS_TYPE_SIGNED_ENUMERATION: _SignedEnumerationField, native_bt.FIELD_CLASS_TYPE_STRING: _StringField, native_bt.FIELD_CLASS_TYPE_STRUCTURE: _StructureField, + native_bt.FIELD_CLASS_TYPE_STATIC_ARRAY: _StaticArrayField, + native_bt.FIELD_CLASS_TYPE_DYNAMIC_ARRAY: _DynamicArrayField, + native_bt.FIELD_CLASS_TYPE_VARIANT: _VariantField, } diff --git a/bindings/python/bt2/bt2/native_bt_field.i b/bindings/python/bt2/bt2/native_bt_field.i index 1a7c2cde..f4511f45 100644 --- a/bindings/python/bt2/bt2/native_bt_field.i +++ b/bindings/python/bt2/bt2/native_bt_field.i @@ -22,6 +22,9 @@ * THE SOFTWARE. */ +/* For label type mappings. */ +%include "native_bt_field_class.i" + /* From field-const.h */ typedef enum bt_field_status { @@ -44,13 +47,13 @@ extern double bt_field_real_get_value(const bt_field *field); extern bt_field_status bt_field_unsigned_enumeration_get_mapping_labels( const bt_field *field, - bt_field_class_enumeration_mapping_label_array *label_array, - uint64_t *count); + bt_field_class_enumeration_mapping_label_array *LABELARRAY, + uint64_t *LABELCOUNT); extern bt_field_status bt_field_signed_enumeration_get_mapping_labels( const bt_field *field, - bt_field_class_enumeration_mapping_label_array *label_array, - uint64_t *count); + bt_field_class_enumeration_mapping_label_array *LABELARRAY, + uint64_t *LABELCOUNT); extern const char *bt_field_string_get_value(const bt_field *field); diff --git a/tests/bindings/python/bt2/test_field.py b/tests/bindings/python/bt2/test_field.py index 97d5a4f3..3f5c466f 100644 --- a/tests/bindings/python/bt2/test_field.py +++ b/tests/bindings/python/bt2/test_field.py @@ -1,25 +1,11 @@ from functools import partial, partialmethod import operator import unittest -import numbers import math import copy import itertools import bt2 - - -class _TestCopySimple: - def test_copy(self): - cpy = copy.copy(self._def) - self.assertIsNot(cpy, self._def) - self.assertNotEqual(cpy.addr, self._def.addr) - self.assertEqual(cpy, self._def) - - def test_deepcopy(self): - cpy = copy.deepcopy(self._def) - self.assertIsNot(cpy, self._def) - self.assertNotEqual(cpy.addr, self._def.addr) - self.assertEqual(cpy, self._def) +from utils import get_default_trace_class _COMP_BINOPS = ( @@ -28,15 +14,100 @@ _COMP_BINOPS = ( ) -class _TestNumericField(_TestCopySimple): +# Create and return a stream with the field classes part of its stream packet +# context. +# +# The stream is part of a dummy trace created from trace class `tc`. + +def _create_stream(tc, ctx_field_classes): + packet_context_fc = tc.create_structure_field_class() + for name, fc in ctx_field_classes: + packet_context_fc.append_member(name, fc) + + trace = tc() + stream_class = tc.create_stream_class(packet_context_field_class=packet_context_fc) + + stream = trace.create_stream(stream_class) + return stream + + +# Create a field of the given field class. +# +# The field is part of a dummy stream, itself part of a dummy trace created +# from trace class `tc`. + +def _create_field(tc, field_class): + field_name = 'field' + stream = _create_stream(tc, [(field_name, field_class)]) + packet = stream.create_packet() + return packet.context_field[field_name] + + +# Create a field of type string. +# +# The field is part of a dummy stream, itself part of a dummy trace created +# from trace class `tc`. It is made out of a dummy string field class. + +def _create_string_field(tc): + field_name = 'string_field' + stream = _create_stream(tc, [(field_name, tc.create_string_field_class())]) + packet = stream.create_packet() + return packet.context_field[field_name] + + +# Create a field of type static array of ints. +# +# The field is part of a dummy stream, itself part of a dummy trace created +# from trace class `tc`. It is made out of a dummy static array field class, +# with a dummy integer field class as element class. + +def _create_int_array_field(tc, length): + elem_fc = tc.create_signed_integer_field_class(32) + fc = tc.create_static_array_field_class(elem_fc, length) + field_name = 'int_array' + stream = _create_stream(tc, [(field_name, fc)]) + packet = stream.create_packet() + return packet.context_field[field_name] + + +# Create a field of type dynamic array of ints. +# +# The field is part of a dummy stream, itself part of a dummy trace created +# from trace class `tc`. It is made out of a dummy static array field class, +# with a dummy integer field class as element and length classes. + +def _create_dynamic_array(tc): + elem_fc = tc.create_signed_integer_field_class(32) + len_fc = tc.create_signed_integer_field_class(32) + fc = tc.create_dynamic_array_field_class(elem_fc) + field_name = 'int_dyn_array' + stream = _create_stream(tc, [('thelength', len_fc), (field_name, fc)]) + packet = stream.create_packet() + packet.context_field[field_name].length = 3 + return packet.context_field[field_name] + + +# Create a field of type array of (empty) structures. +# +# The field is part of a dummy stream, itself part of a dummy trace created +# from trace class `tc`. It is made out of a dummy static array field class, +# with a dummy struct field class as element class. + +def _create_struct_array_field(tc, length): + elem_fc = tc.create_structure_field_class() + fc = tc.create_static_array_field_class(elem_fc, length) + field_name = 'struct_array' + stream = _create_stream(tc, [(field_name, fc)]) + packet = stream.create_packet() + return packet.context_field[field_name] + + +class _TestNumericField: def _binop(self, op, rhs): rexc = None rvexc = None comp_value = rhs - if isinstance(rhs, (bt2.field._IntegerField, bt2.field._FloatingPointNumberField)): - comp_value = copy.copy(rhs) - try: r = op(self._def, rhs) except Exception as e: @@ -132,6 +203,7 @@ class _TestNumericField(_TestCopySimple): r, rv = self._binop(op, rhs) self.assertEqual(self._def.addr, addr_before) + @unittest.skip('copy is not implemented') def _test_binop_lhs_value_same(self, op, rhs): value_before = copy.copy(self._def) r, rv = self._binop(op, rhs) @@ -512,27 +584,16 @@ class _TestNumericField(_TestCopySimple): self.assertEqual(str(self._def), str(self._def_value)) def test_eq_none(self): - self.assertFalse(self._def == None) + # Ignore this lint error: + # E711 comparison to None should be 'if cond is None:' + # since this is what we want to test (even though not good practice). + self.assertFalse(self._def == None) # noqa: E711 def test_ne_none(self): - self.assertTrue(self._def != None) - - def test_is_set(self): - raw = self._def_value - field = self._fc() - self.assertFalse(field.is_set) - field.value = raw - self.assertTrue(field.is_set) - - def test_reset(self): - raw = self._def_value - field = self._fc() - field.value = raw - self.assertTrue(field.is_set) - field.reset() - self.assertFalse(field.is_set) - other = self._fc() - self.assertEqual(other, field) + # Ignore this lint error: + # E711 comparison to None should be 'if cond is not None:' + # since this is what we want to test (even though not good practice). + self.assertTrue(self._def != None) # noqa: E711 _BINOPS = ( @@ -736,7 +797,7 @@ class _TestIntegerFieldCommon(_TestNumericField): def test_assign_int_field(self): raw = 999 - field = self._fc() + field = _create_field(self._tc, self._create_fc(self._tc)) field.value = raw self._def.value = field self.assertEqual(self._def, raw) @@ -751,15 +812,15 @@ class _TestIntegerFieldCommon(_TestNumericField): self._def.value = 'yes' def test_assign_uint(self): - fc = bt2.IntegerFieldClass(size=32, is_signed=False) - field = fc() + uint_fc = self._tc.create_unsigned_integer_field_class(32) + field = _create_field(self._tc, uint_fc) raw = 1777 field.value = 1777 self.assertEqual(field, raw) def test_assign_uint_invalid_neg(self): - fc = bt2.IntegerFieldClass(size=32, is_signed=False) - field = fc() + uint_fc = self._tc.create_unsigned_integer_field_class(32) + field = _create_field(self._tc, uint_fc) with self.assertRaises(ValueError): field.value = -23 @@ -767,66 +828,42 @@ class _TestIntegerFieldCommon(_TestNumericField): def test_str_op(self): self.assertEqual(str(self._def), str(self._def_value)) - def test_str_op_unset(self): - self.assertEqual(str(self._fc()), 'Unset') - _inject_numeric_testing_methods(_TestIntegerFieldCommon) -@unittest.skip("this is broken") -class IntegerFieldTestCase(_TestIntegerFieldCommon, unittest.TestCase): +class SignedIntegerFieldTestCase(_TestIntegerFieldCommon, unittest.TestCase): + def _create_fc(self, tc): + return tc.create_signed_integer_field_class(25) + def setUp(self): - self._fc = bt2.IntegerFieldClass(25, is_signed=True) - self._field = self._fc() - self._def = self._fc() + self._tc = get_default_trace_class() + self._field = _create_field(self._tc, self._create_fc(self._tc)) + self._field.value = 17 + self._def = _create_field(self._tc, self._create_fc(self._tc)) self._def.value = 17 self._def_value = 17 self._def_new_value = -101 - def tearDown(self): - del self._fc - del self._field - del self._def +class SignedEnumerationFieldTestCase(_TestIntegerFieldCommon, unittest.TestCase): + def _create_fc(self, tc): + fc = tc.create_signed_enumeration_field_class(32) + fc.map_range('something', 17) + fc.map_range('speaker', 12, 16) + fc.map_range('can', 18, 2540) + fc.map_range('whole range', -(2 ** 31), (2 ** 31) - 1) + fc.map_range('zip', -45, 1001) + return fc -@unittest.skip("this is broken") -class EnumerationFieldTestCase(_TestIntegerFieldCommon, unittest.TestCase): def setUp(self): - self._fc = bt2.EnumerationFieldClass(size=32, is_signed=True) - self._fc.add_mapping('whole range', -(2 ** 31), (2 ** 31) - 1) - self._fc.add_mapping('something', 17) - self._fc.add_mapping('speaker', 12, 16) - self._fc.add_mapping('can', 18, 2540) - self._fc.add_mapping('zip', -45, 1001) - self._def = self._fc() + self._tc = get_default_trace_class() + self._field = _create_field(self._tc, self._create_fc(self._tc)) + self._def = _create_field(self._tc, self._create_fc(self._tc)) self._def.value = 17 self._def_value = 17 self._def_new_value = -101 - def tearDown(self): - del self._fc - del self._def - - def test_mappings(self): - mappings = ( - ('whole range', -(2 ** 31), (2 ** 31) - 1), - ('something', 17, 17), - ('zip', -45, 1001), - ) - - total = 0 - index_set = set() - - for fm in self._def.mappings: - total += 1 - for index, mapping in enumerate(mappings): - if fm.name == mapping[0] and fm.lower == mapping[1] and fm.upper == mapping[2]: - index_set.add(index) - - self.assertEqual(total, 3) - self.assertTrue(0 in index_set and 1 in index_set and 2 in index_set) - def test_str_op(self): expected_string_found = False s = str(self._def) @@ -834,8 +871,8 @@ class EnumerationFieldTestCase(_TestIntegerFieldCommon, unittest.TestCase): # Establish all permutations of the three expected matches since # the order in which mappings are enumerated is not explicitly part of # the API. - for p in itertools.permutations(["'whole range'", "'something'", - "'zip'"]): + for p in itertools.permutations(['whole range', 'something', + 'zip']): candidate = '{} ({})'.format(self._def_value, ', '.join(p)) if candidate == s: expected_string_found = True @@ -843,25 +880,24 @@ class EnumerationFieldTestCase(_TestIntegerFieldCommon, unittest.TestCase): self.assertTrue(expected_string_found) - def test_str_op_unset(self): - self.assertEqual(str(self._fc()), 'Unset') + def test_labels(self): + self._field.value = 17 + labels = sorted(self._field.labels) + self.assertEqual(labels, ['something', 'whole range', 'zip']) + +class RealFieldTestCase(_TestNumericField, unittest.TestCase): + def _create_fc(self, tc): + return tc.create_real_field_class() -@unittest.skip("this is broken") -class FloatingPointNumberFieldTestCase(_TestNumericField, unittest.TestCase): def setUp(self): - self._fc = bt2.FloatingPointNumberFieldClass() - self._field = self._fc() - self._def = self._fc() + self._tc = get_default_trace_class() + self._field = _create_field(self._tc, self._create_fc(self._tc)) + self._def = _create_field(self._tc, self._create_fc(self._tc)) self._def.value = 52.7 self._def_value = 52.7 self._def_new_value = -17.164857 - def tearDown(self): - del self._fc - del self._field - del self._def - def _test_invalid_op(self, cb): with self.assertRaises(TypeError): cb() @@ -885,11 +921,11 @@ class FloatingPointNumberFieldTestCase(_TestNumericField, unittest.TestCase): self.assertEqual(self._def, float(raw)) def test_assign_int_field(self): - fc = bt2.IntegerFieldClass(32) - field = fc() + int_fc = self._tc.create_signed_integer_field_class(32) + int_field = _create_field(self._tc, int_fc) raw = 999 - field.value = raw - self._def.value = field + int_field.value = raw + self._def.value = int_field self.assertEqual(self._def, float(raw)) def test_assign_float(self): @@ -898,8 +934,7 @@ class FloatingPointNumberFieldTestCase(_TestNumericField, unittest.TestCase): self.assertEqual(self._def, raw) def test_assign_float_field(self): - fc = bt2.FloatingPointNumberFieldClass(32) - field = fc() + field = _create_field(self._tc, self._create_fc(self._tc)) raw = 101.32 field.value = raw self._def.value = field @@ -930,32 +965,24 @@ class FloatingPointNumberFieldTestCase(_TestNumericField, unittest.TestCase): def test_str_op(self): self.assertEqual(str(self._def), str(self._def_value)) - def test_str_op_unset(self): - self.assertEqual(str(self._fc()), 'Unset') -_inject_numeric_testing_methods(FloatingPointNumberFieldTestCase) +_inject_numeric_testing_methods(RealFieldTestCase) -@unittest.skip("this is broken") -class StringFieldTestCase(_TestCopySimple, unittest.TestCase): +class StringFieldTestCase(unittest.TestCase): def setUp(self): - self._fc = bt2.StringFieldClass() + self._tc = get_default_trace_class() self._def_value = 'Hello, World!' - self._def = self._fc() + self._def = _create_string_field(self._tc) self._def.value = self._def_value self._def_new_value = 'Yes!' - def tearDown(self): - del self._fc - del self._def - def test_assign_int(self): with self.assertRaises(TypeError): self._def.value = 283 def test_assign_string_field(self): - fc = bt2.StringFieldClass() - field = fc() + field = _create_string_field(self._tc) raw = 'zorg' field.value = raw self.assertEqual(field, raw) @@ -963,54 +990,54 @@ class StringFieldTestCase(_TestCopySimple, unittest.TestCase): def test_eq(self): self.assertEqual(self._def, self._def_value) - def test_eq(self): + def test_not_eq(self): self.assertNotEqual(self._def, 23) def test_lt_vstring(self): - s1 = self._fc() + s1 = _create_string_field(self._tc) s1.value = 'allo' - s2 = self._fc() + s2 = _create_string_field(self._tc) s2.value = 'bateau' self.assertLess(s1, s2) def test_lt_string(self): - s1 = self._fc() + s1 = _create_string_field(self._tc) s1.value = 'allo' self.assertLess(s1, 'bateau') def test_le_vstring(self): - s1 = self._fc() + s1 = _create_string_field(self._tc) s1.value = 'allo' - s2 = self._fc() + s2 = _create_string_field(self._tc) s2.value = 'bateau' self.assertLessEqual(s1, s2) def test_le_string(self): - s1 = self._fc() + s1 = _create_string_field(self._tc) s1.value = 'allo' self.assertLessEqual(s1, 'bateau') def test_gt_vstring(self): - s1 = self._fc() + s1 = _create_string_field(self._tc) s1.value = 'allo' - s2 = self._fc() + s2 = _create_string_field(self._tc) s2.value = 'bateau' self.assertGreater(s2, s1) def test_gt_string(self): - s1 = self._fc() + s1 = _create_string_field(self._tc) s1.value = 'allo' self.assertGreater('bateau', s1) def test_ge_vstring(self): - s1 = self._fc() + s1 = _create_string_field(self._tc) s1.value = 'allo' - s2 = self._fc() + s2 = _create_string_field(self._tc) s2.value = 'bateau' self.assertGreaterEqual(s2, s1) def test_ge_string(self): - s1 = self._fc() + s1 = _create_string_field(self._tc) s1.value = 'allo' self.assertGreaterEqual('bateau', s1) @@ -1020,9 +1047,6 @@ class StringFieldTestCase(_TestCopySimple, unittest.TestCase): def test_str_op(self): self.assertEqual(str(self._def), str(self._def_value)) - def test_str_op_unset(self): - self.assertEqual(str(self._fc()), 'Unset') - def test_len(self): self.assertEqual(len(self._def), len(self._def_value)) @@ -1036,33 +1060,15 @@ class StringFieldTestCase(_TestCopySimple, unittest.TestCase): self.assertEqual(self._def, self._def_value) def test_append_string_field(self): - fc = bt2.StringFieldClass() - field = fc() + field = _create_string_field(self._tc) to_append = 'meow meow meow' field.value = to_append self._def += field self._def_value += to_append self.assertEqual(self._def, self._def_value) - def test_is_set(self): - raw = self._def_value - field = self._fc() - self.assertFalse(field.is_set) - field.value = raw - self.assertTrue(field.is_set) - def test_reset(self): - raw = self._def_value - field = self._fc() - field.value = raw - self.assertTrue(field.is_set) - field.reset() - self.assertFalse(field.is_set) - other = self._fc() - self.assertEqual(other, field) - - -class _TestArraySequenceFieldCommon(_TestCopySimple): +class _TestArrayFieldCommon: def _modify_def(self): self._def[2] = 23 @@ -1072,14 +1078,16 @@ class _TestArraySequenceFieldCommon(_TestCopySimple): def test_len(self): self.assertEqual(len(self._def), 3) + def test_length(self): + self.assertEqual(self._def.length, 3) + def test_getitem(self): field = self._def[1] - self.assertIs(type(field), bt2.field._IntegerField) + self.assertIs(type(field), bt2.field._SignedIntegerField) self.assertEqual(field, 1847) def test_eq(self): - fc = bt2.ArrayFieldClass(self._elem_fc, 3) - field = fc() + field = _create_int_array_field(self._tc, 3) field[0] = 45 field[1] = 1847 field[2] = 1948754 @@ -1089,15 +1097,13 @@ class _TestArraySequenceFieldCommon(_TestCopySimple): self.assertNotEqual(self._def, 23) def test_eq_diff_len(self): - fc = bt2.ArrayFieldClass(self._elem_fc, 2) - field = fc() + field = _create_int_array_field(self._tc, 2) field[0] = 45 field[1] = 1847 self.assertNotEqual(self._def, field) def test_eq_diff_content_same_len(self): - fc = bt2.ArrayFieldClass(self._elem_fc, 3) - field = fc() + field = _create_int_array_field(self._tc, 3) field[0] = 45 field[1] = 1846 field[2] = 1948754 @@ -1108,17 +1114,14 @@ class _TestArraySequenceFieldCommon(_TestCopySimple): self.assertEqual(self._def[2], 24) def test_setitem_int_field(self): - int_field = self._elem_fc() + int_fc = self._tc.create_signed_integer_field_class(32) + int_field = _create_field(self._tc, int_fc) int_field.value = 19487 self._def[1] = int_field self.assertEqual(self._def[1], 19487) def test_setitem_non_basic_field(self): - elem_fc = bt2.StructureFieldClass() - array_fc = bt2.ArrayFieldClass(elem_fc, 3) - elem_field = elem_fc() - array_field = array_fc() - + array_field = _create_struct_array_field(self._tc, 2) with self.assertRaises(TypeError): array_field[1] = 23 @@ -1147,18 +1150,6 @@ class _TestArraySequenceFieldCommon(_TestCopySimple): self._def.value = values self.assertEqual(values, self._def) - def test_value_unset(self): - values = [45646, None, 12145] - self._def.value = values - self.assertFalse(self._def[1].is_set) - - def test_value_rollback(self): - values = [45, 1847, 1948754] - # value is out of range, should not affect those we set previously - with self.assertRaises(bt2.Error): - self._def[2].value = 2**60 - self.assertEqual(values, self._def) - def test_value_check_sequence(self): values = 42 with self.assertRaises(TypeError): @@ -1170,13 +1161,15 @@ class _TestArraySequenceFieldCommon(_TestCopySimple): self._def.value = values def test_value_complex_type(self): - struct_fc = bt2.StructureFieldClass() - int_fc = bt2.IntegerFieldClass(32) - str_fc = bt2.StringFieldClass() - struct_fc.append_field(field_class=int_fc, name='an_int') - struct_fc.append_field(field_class=str_fc, name='a_string') - struct_fc.append_field(field_class=int_fc, name='another_int') - array_fc = bt2.ArrayFieldClass(struct_fc, 3) + struct_fc = self._tc.create_structure_field_class() + int_fc = self._tc.create_signed_integer_field_class(32) + another_int_fc = self._tc.create_signed_integer_field_class(32) + str_fc = self._tc.create_string_field_class() + struct_fc.append_member(field_class=int_fc, name='an_int') + struct_fc.append_member(field_class=str_fc, name='a_string') + struct_fc.append_member(field_class=another_int_fc, name='another_int') + array_fc = self._tc.create_static_array_field_class(struct_fc, 3) + stream = _create_stream(self._tc, [('array_field', array_fc)]) values = [ { 'an_int': 42, @@ -1195,129 +1188,99 @@ class _TestArraySequenceFieldCommon(_TestCopySimple): }, ] - array = array_fc() + array = stream.create_packet().context_field['array_field'] array.value = values self.assertEqual(values, array) values[0]['an_int'] = 'a string' with self.assertRaises(TypeError): array.value = values - def test_is_set(self): - raw = self._def_value - field = self._fc() - self.assertFalse(field.is_set) - field.value = raw - self.assertTrue(field.is_set) - - def test_reset(self): - raw = self._def_value - field = self._fc() - field.value = raw - self.assertTrue(field.is_set) - field.reset() - self.assertFalse(field.is_set) - other = self._fc() - self.assertEqual(other, field) - def test_str_op(self): s = str(self._def) expected_string = '[{}]'.format(', '.join( [repr(v) for v in self._def_value])) self.assertEqual(expected_string, s) - def test_str_op_unset(self): - self.assertEqual(str(self._fc()), 'Unset') - -@unittest.skip("this is broken") -class ArrayFieldTestCase(_TestArraySequenceFieldCommon, unittest.TestCase): +class StaticArrayFieldTestCase(_TestArrayFieldCommon, unittest.TestCase): def setUp(self): - self._elem_fc = bt2.IntegerFieldClass(32) - self._fc = bt2.ArrayFieldClass(self._elem_fc, 3) - self._def = self._fc() + self._tc = get_default_trace_class() + self._def = _create_int_array_field(self._tc, 3) self._def[0] = 45 self._def[1] = 1847 self._def[2] = 1948754 self._def_value = [45, 1847, 1948754] - def tearDown(self): - del self._elem_fc - del self._fc - del self._def - def test_value_wrong_len(self): values = [45, 1847] with self.assertRaises(ValueError): self._def.value = values -@unittest.skip("this is broken") -class SequenceFieldTestCase(_TestArraySequenceFieldCommon, unittest.TestCase): +class DynamicArrayFieldTestCase(_TestArrayFieldCommon, unittest.TestCase): def setUp(self): - self._elem_fc = bt2.IntegerFieldClass(32) - self._fc = bt2.SequenceFieldClass(self._elem_fc, 'the.length') - self._def = self._fc() - self._length_field = self._elem_fc(3) - self._def.length_field = self._length_field + self._tc = get_default_trace_class() + self._def = _create_dynamic_array(self._tc) self._def[0] = 45 self._def[1] = 1847 self._def[2] = 1948754 self._def_value = [45, 1847, 1948754] - def tearDown(self): - del self._elem_fc - del self._fc - del self._def - del self._length_field - def test_value_resize(self): new_values = [1, 2, 3, 4] self._def.value = new_values self.assertCountEqual(self._def, new_values) - def test_value_resize_rollback(self): - with self.assertRaises(TypeError): - self._def.value = [1, 2, 3, 'unexpected string'] - self.assertEqual(self._def, self._def_value) + def test_set_length(self): + self._def.length = 4 + self._def[3] = 0 + self.assertEqual(len(self._def), 4) - self._def.reset() + def test_set_invalid_length(self): with self.assertRaises(TypeError): - self._def.value = [1, 2, 3, 'unexpected string'] - self.assertFalse(self._def.is_set) + self._def.length = 'cheval' + + +class StructureFieldTestCase(unittest.TestCase): + def _create_fc(self, tc): + fc = tc.create_structure_field_class() + fc.append_member('A', self._fc0_fn()) + fc.append_member('B', self._fc1_fn()) + fc.append_member('C', self._fc2_fn()) + fc.append_member('D', self._fc3_fn()) + fc.append_member('E', self._fc4_fn()) + fc5 = self._fc5_fn() + fc5.append_member('F_1', self._fc5_inner_fn()) + fc.append_member('F', fc5) + return fc - -@unittest.skip("this is broken") -class StructureFieldTestCase(_TestCopySimple, unittest.TestCase): def setUp(self): - self._fc0 = bt2.IntegerFieldClass(32, is_signed=True) - self._fc1 = bt2.StringFieldClass() - self._fc2 = bt2.FloatingPointNumberFieldClass() - self._fc3 = bt2.IntegerFieldClass(17) - self._fc = bt2.StructureFieldClass() - self._fc.append_field('A', self._fc0) - self._fc.append_field('B', self._fc1) - self._fc.append_field('C', self._fc2) - self._fc.append_field('D', self._fc3) - self._def = self._fc() + self._tc = get_default_trace_class() + self._fc0_fn = self._tc.create_signed_integer_field_class + self._fc1_fn = self._tc.create_string_field_class + self._fc2_fn = self._tc.create_real_field_class + self._fc3_fn = self._tc.create_signed_integer_field_class + self._fc4_fn = self._tc.create_structure_field_class + self._fc5_fn = self._tc.create_structure_field_class + self._fc5_inner_fn = self._tc.create_signed_integer_field_class + + self._fc = self._create_fc(self._tc) + self._def = _create_field(self._tc, self._fc) self._def['A'] = -1872 self._def['B'] = 'salut' self._def['C'] = 17.5 self._def['D'] = 16497 + self._def['E'] = {} + self._def['F'] = {'F_1': 52} self._def_value = { 'A': -1872, 'B': 'salut', 'C': 17.5, - 'D': 16497 + 'D': 16497, + 'E': {}, + 'F': {'F_1': 52} } - def tearDown(self): - del self._fc0 - del self._fc1 - del self._fc2 - del self._fc3 - del self._fc - del self._def - def _modify_def(self): self._def['B'] = 'hola' @@ -1325,73 +1288,88 @@ class StructureFieldTestCase(_TestCopySimple, unittest.TestCase): self.assertTrue(self._def) def test_bool_op_false(self): - fc = bt2.StructureFieldClass() - field = fc() + field = self._def['E'] self.assertFalse(field) def test_len(self): - self.assertEqual(len(self._def), 4) + self.assertEqual(len(self._def), len(self._def_value)) def test_getitem(self): field = self._def['A'] - self.assertIs(type(field), bt2.field._IntegerField) + self.assertIs(type(field), bt2.field._SignedIntegerField) self.assertEqual(field, -1872) - def test_at_index_out_of_bounds_after(self): + def test_member_at_index_out_of_bounds_after(self): with self.assertRaises(IndexError): - self._def.at_index(len(self._fc)) + self._def.member_at_index(len(self._def_value)) def test_eq(self): - fc = bt2.StructureFieldClass() - fc.append_field('A', self._fc0) - fc.append_field('B', self._fc1) - fc.append_field('C', self._fc2) - fc.append_field('D', self._fc3) - field = fc() + field = _create_field(self._tc, self._create_fc(self._tc, )) field['A'] = -1872 field['B'] = 'salut' field['C'] = 17.5 field['D'] = 16497 + field['E'] = {} + field['F'] = {'F_1': 52} self.assertEqual(self._def, field) def test_eq_invalid_type(self): self.assertNotEqual(self._def, 23) def test_eq_diff_len(self): - fc = bt2.StructureFieldClass() - fc.append_field('A', self._fc0) - fc.append_field('B', self._fc1) - fc.append_field('C', self._fc2) - field = fc() + fc = self._tc.create_structure_field_class() + fc.append_member('A', self._fc0_fn()) + fc.append_member('B', self._fc1_fn()) + fc.append_member('C', self._fc2_fn()) + + field = _create_field(self._tc, fc) field['A'] = -1872 field['B'] = 'salut' field['C'] = 17.5 self.assertNotEqual(self._def, field) + def test_eq_diff_keys(self): + fc = self._tc.create_structure_field_class() + fc.append_member('U', self._fc0_fn()) + fc.append_member('V', self._fc1_fn()) + fc.append_member('W', self._fc2_fn()) + fc.append_member('X', self._fc3_fn()) + fc.append_member('Y', self._fc4_fn()) + fc.append_member('Z', self._fc5_fn()) + field = _create_field(self._tc, fc) + field['U'] = -1871 + field['V'] = "gerry" + field['W'] = 18.19 + field['X'] = 16497 + field['Y'] = {} + field['Z'] = {} + self.assertNotEqual(self._def, field) + def test_eq_diff_content_same_len(self): - fc = bt2.StructureFieldClass() - fc.append_field('A', self._fc0) - fc.append_field('B', self._fc1) - fc.append_field('C', self._fc2) - fc.append_field('D', self._fc3) - field = fc() + field = _create_field(self._tc, self._create_fc(self._tc)) field['A'] = -1872 field['B'] = 'salut' field['C'] = 17.4 field['D'] = 16497 + field['E'] = {} + field['F'] = {'F_1': 0} self.assertNotEqual(self._def, field) def test_eq_same_content_diff_keys(self): - fc = bt2.StructureFieldClass() - fc.append_field('A', self._fc0) - fc.append_field('B', self._fc1) - fc.append_field('E', self._fc2) - fc.append_field('D', self._fc3) - field = fc() + fc = self._tc.create_structure_field_class() + fc.append_member('A', self._fc0_fn()) + fc.append_member('B', self._fc1_fn()) + fc.append_member('E', self._fc2_fn()) + fc.append_member('D', self._fc3_fn()) + fc.append_member('C', self._fc4_fn()) + fc.append_member('F', self._fc5_fn()) + field = _create_field(self._tc, fc) field['A'] = -1872 field['B'] = 'salut' - field['E'] = 17.4 + field['E'] = 17.5 field['D'] = 16497 + field['C'] = {} + field['F'] = {} self.assertNotEqual(self._def, field) def test_setitem(self): @@ -1399,18 +1377,17 @@ class StructureFieldTestCase(_TestCopySimple, unittest.TestCase): self.assertEqual(self._def['C'], -18.47) def test_setitem_int_field(self): - int_fc = bt2.IntegerFieldClass(16) - int_field = int_fc() + int_fc = self._tc.create_signed_integer_field_class(32) + int_field = _create_field(self._tc, int_fc) int_field.value = 19487 self._def['D'] = int_field self.assertEqual(self._def['D'], 19487) def test_setitem_non_basic_field(self): - elem_fc = bt2.StructureFieldClass() - elem_field = elem_fc() - struct_fc = bt2.StructureFieldClass() - struct_fc.append_field('A', elem_fc) - struct_field = struct_fc() + elem_fc = self._tc.create_structure_field_class() + struct_fc = self._tc.create_structure_field_class() + struct_fc.append_member('A', elem_fc) + struct_field = _create_field(self._tc, struct_fc) # Will fail on access to .items() of the value with self.assertRaises(AttributeError): @@ -1428,8 +1405,8 @@ class StructureFieldTestCase(_TestCopySimple, unittest.TestCase): with self.assertRaises(KeyError): self._def['hi'] = 134679 - def test_at_index(self): - self.assertEqual(self._def.at_index(1), 'salut') + def test_member_at_index(self): + self.assertEqual(self._def.member_at_index(1), 'salut') def test_iter(self): orig_values = { @@ -1437,6 +1414,8 @@ class StructureFieldTestCase(_TestCopySimple, unittest.TestCase): 'B': 'salut', 'C': 17.5, 'D': 16497, + 'E': {}, + 'F': {'F_1': 52} } for vkey, vval in self._def.items(): @@ -1449,23 +1428,26 @@ class StructureFieldTestCase(_TestCopySimple, unittest.TestCase): 'B': 'salut', 'C': 17.5, 'D': 16497, + 'E': {}, + 'F': {'F_1': 52} } self.assertEqual(self._def, orig_values) def test_set_value(self): - int_fc = bt2.IntegerFieldClass(32) - str_fc = bt2.StringFieldClass() - struct_fc = bt2.StructureFieldClass() - struct_fc.append_field(field_class=int_fc, name='an_int') - struct_fc.append_field(field_class=str_fc, name='a_string') - struct_fc.append_field(field_class=int_fc, name='another_int') + int_fc = self._tc.create_signed_integer_field_class(32) + another_int_fc = self._tc.create_signed_integer_field_class(32) + str_fc = self._tc.create_string_field_class() + struct_fc = self._tc.create_structure_field_class() + struct_fc.append_member(field_class=int_fc, name='an_int') + struct_fc.append_member(field_class=str_fc, name='a_string') + struct_fc.append_member(field_class=another_int_fc, name='another_int') values = { 'an_int': 42, 'a_string': 'hello', 'another_int': 66 } - struct = struct_fc() + struct = _create_field(self._tc, struct_fc) struct.value = values self.assertEqual(values, struct) @@ -1479,62 +1461,6 @@ class StructureFieldTestCase(_TestCopySimple, unittest.TestCase): with self.assertRaises(KeyError): struct.value = unknown_key_values - def test_value_rollback(self): - int_fc = bt2.IntegerFieldClass(32) - str_fc = bt2.StringFieldClass() - struct_fc = bt2.StructureFieldClass() - struct_fc.append_field(field_class=int_fc, name='an_int') - struct_fc.append_field(field_class=str_fc, name='a_string') - struct_fc.append_field(field_class=int_fc, name='another_int') - values = { - 'an_int': 42, - 'a_string': 'hello', - 'another_int': 66 - } - - def test_is_set(self): - values = { - 'an_int': 42, - 'a_string': 'hello', - 'another_int': 66 - } - - int_fc = bt2.IntegerFieldClass(32) - str_fc = bt2.StringFieldClass() - struct_fc = bt2.StructureFieldClass() - struct_fc.append_field(field_class=int_fc, name='an_int') - struct_fc.append_field(field_class=str_fc, name='a_string') - struct_fc.append_field(field_class=int_fc, name='another_int') - - struct = struct_fc() - self.assertFalse(struct.is_set) - struct.value = values - self.assertTrue(struct.is_set) - - struct = struct_fc() - struct['an_int'].value = 42 - self.assertFalse(struct.is_set) - - def test_reset(self): - values = { - 'an_int': 42, - 'a_string': 'hello', - 'another_int': 66 - } - - int_fc = bt2.IntegerFieldClass(32) - str_fc = bt2.StringFieldClass() - struct_fc = bt2.StructureFieldClass() - struct_fc.append_field(field_class=int_fc, name='an_int') - struct_fc.append_field(field_class=str_fc, name='a_string') - struct_fc.append_field(field_class=int_fc, name='another_int') - - struct = struct_fc() - struct.value = values - self.assertTrue(struct.is_set) - struct.reset() - self.assertEqual(struct_fc(), struct) - def test_str_op(self): expected_string_found = False s = str(self._def) @@ -1550,126 +1476,99 @@ class StructureFieldTestCase(_TestCopySimple, unittest.TestCase): self.assertTrue(expected_string_found) - def test_str_op_unset(self): - self.assertEqual(str(self._fc()), 'Unset') +class VariantFieldTestCase(unittest.TestCase): + def _create_fc(self, tc): + selector_fc = tc.create_signed_enumeration_field_class(field_value_range=32) + selector_fc.map_range('corner', 23) + selector_fc.map_range('zoom', 17, 20) + selector_fc.map_range('mellotron', 1001) + selector_fc.map_range('giorgio', 2000, 3000) + + ft0 = tc.create_signed_integer_field_class(32) + ft1 = tc.create_string_field_class() + ft2 = tc.create_real_field_class() + ft3 = tc.create_signed_integer_field_class(17) + + fc = tc.create_variant_field_class() + fc.append_option('corner', ft0) + fc.append_option('zoom', ft1) + fc.append_option('mellotron', ft2) + fc.append_option('giorgio', ft3) + fc.selector_field_class = selector_fc + + top_fc = tc.create_structure_field_class() + top_fc.append_member('selector_field', selector_fc) + top_fc.append_member('variant_field', fc) + return top_fc -@unittest.skip("this is broken") -class VariantFieldTestCase(_TestCopySimple, unittest.TestCase): def setUp(self): - self._tag_fc = bt2.EnumerationFieldClass(size=32) - self._tag_fc.add_mapping('corner', 23) - self._tag_fc.add_mapping('zoom', 17, 20) - self._tag_fc.add_mapping('mellotron', 1001) - self._tag_fc.add_mapping('giorgio', 2000, 3000) - self._fc0 = bt2.IntegerFieldClass(32, is_signed=True) - self._fc1 = bt2.StringFieldClass() - self._fc2 = bt2.FloatingPointNumberFieldClass() - self._fc3 = bt2.IntegerFieldClass(17) - self._fc = bt2.VariantFieldClass('salut', self._tag_fc) - self._fc.append_field('corner', self._fc0) - self._fc.append_field('zoom', self._fc1) - self._fc.append_field('mellotron', self._fc2) - self._fc.append_field('giorgio', self._fc3) - self._def = self._fc() - - def tearDown(self): - del self._tag_fc - del self._fc0 - del self._fc1 - del self._fc2 - del self._fc3 - del self._fc - del self._def + self._tc = get_default_trace_class() + fld = _create_field(self._tc, self._create_fc(self._tc)) + self._def = fld['variant_field'] - def test_bool_op_true(self): - tag_field = self._tag_fc(1001) - self._def.field(tag_field).value = -17.34 - self.assertTrue(self._def) + def test_bool_op(self): + self._def.selected_option_index = 2 + self._def.value = -17.34 + with self.assertRaises(NotImplementedError): + bool(self._def) - def test_bool_op_false(self): - self.assertFalse(self._def) + def test_selected_option_index(self): + self._def.selected_option_index = 2 + self.assertEqual(self._def.selected_option_index, 2) - def test_tag_field_none(self): - self.assertIsNone(self._def.tag_field) - - def test_tag_field(self): - tag_field = self._tag_fc(2800) - self._def.field(tag_field).value = 1847 - self.assertEqual(self._def.tag_field, tag_field) - self.assertEqual(self._def.tag_field.addr, tag_field.addr) - - def test_selected_field_none(self): - self.assertIsNone(self._def.selected_field) - - def test_selected_field(self): - var_field1 = self._fc() - tag_field1 = self._tag_fc(1001) - var_field1.field(tag_field1).value = -17.34 - self.assertEqual(var_field1.field(), -17.34) - self.assertEqual(var_field1.selected_field, -17.34) - var_field2 = self._fc() - tag_field2 = self._tag_fc(2500) - var_field2.field(tag_field2).value = 1921 - self.assertEqual(var_field2.field(), 1921) - self.assertEqual(var_field2.selected_field, 1921) + def test_selected_option(self): + self._def.selected_option_index = 2 + self._def.value = -17.34 + self.assertEqual(self._def.selected_option, -17.34) + + self._def.selected_option_index = 3 + self._def.value = 1921 + self.assertEqual(self._def.selected_option, 1921) def test_eq(self): - tag_fc = bt2.EnumerationFieldClass(size=32) - tag_fc.add_mapping('corner', 23) - tag_fc.add_mapping('zoom', 17, 20) - tag_fc.add_mapping('mellotron', 1001) - tag_fc.add_mapping('giorgio', 2000, 3000) - fc0 = bt2.IntegerFieldClass(32, is_signed=True) - fc1 = bt2.StringFieldClass() - fc2 = bt2.FloatingPointNumberFieldClass() - fc3 = bt2.IntegerFieldClass(17) - fc = bt2.VariantFieldClass('salut', tag_fc) - fc.append_field('corner', fc0) - fc.append_field('zoom', fc1) - fc.append_field('mellotron', fc2) - fc.append_field('giorgio', fc3) - field = fc() - field_tag = tag_fc(23) - def_tag = self._tag_fc(23) - field.field(field_tag).value = 1774 - self._def.field(def_tag).value = 1774 + field = _create_field(self._tc, self._create_fc(self._tc)) + field = field['variant_field'] + field.selected_option_index = 0 + field.value = 1774 + self._def.selected_option_index = 0 + self._def.value = 1774 self.assertEqual(self._def, field) def test_eq_invalid_type(self): + self._def.selected_option_index = 1 + self._def.value = 'gerry' self.assertNotEqual(self._def, 23) - def test_is_set(self): - self.assertFalse(self._def.is_set) - tag_field = self._tag_fc(2800) - self._def.field(tag_field).value = 684 - self.assertTrue(self._def.is_set) - - def test_reset(self): - tag_field = self._tag_fc(2800) - self._def.field(tag_field).value = 684 - self._def.reset() - self.assertFalse(self._def.is_set) - self.assertIsNone(self._def.selected_field) - self.assertIsNone(self._def.tag_field) - def test_str_op_int(self): - v = self._fc() - v.field(self._tag_fc(23)).value = 42 - f = self._fc0(42) - self.assertEqual(str(f), str(v)) + field = _create_field(self._tc, self._create_fc(self._tc)) + field = field['variant_field'] + field.selected_option_index = 0 + field.value = 1774 + other_field = _create_field(self._tc, self._create_fc(self._tc)) + other_field = other_field['variant_field'] + other_field.selected_option_index = 0 + other_field.value = 1774 + self.assertEqual(str(field), str(other_field)) def test_str_op_str(self): - v = self._fc() - v.field(self._tag_fc(18)).value = 'some test string' - f = self._fc1('some test string') - self.assertEqual(str(f), str(v)) - - def test_str_op_flt(self): - v = self._fc() - v.field(self._tag_fc(1001)).value = 14.4245 - f = self._fc2(14.4245) - self.assertEqual(str(f), str(v)) - - def test_str_op_unset(self): - self.assertEqual(str(self._fc()), 'Unset') + field = _create_field(self._tc, self._create_fc(self._tc)) + field = field['variant_field'] + field.selected_option_index = 1 + field.value = 'un beau grand bateau' + other_field = _create_field(self._tc, self._create_fc(self._tc)) + other_field = other_field['variant_field'] + other_field.selected_option_index = 1 + other_field.value = 'un beau grand bateau' + self.assertEqual(str(field), str(other_field)) + + def test_str_op_float(self): + field = _create_field(self._tc, self._create_fc(self._tc)) + field = field['variant_field'] + field.selected_option_index = 2 + field.value = 14.4245 + other_field = _create_field(self._tc, self._create_fc(self._tc)) + other_field = other_field['variant_field'] + other_field.selected_option_index = 2 + other_field.value = 14.4245 + self.assertEqual(str(field), str(other_field)) -- 2.34.1