from bt2.error import _Error
from bt2.event_class import EventClassLogLevel
from bt2.field import _BoolField
+from bt2.field import _BitArrayField
from bt2.field import _IntegerField
from bt2.field import _UnsignedIntegerField
from bt2.field import _SignedIntegerField
from bt2.field import _DynamicArrayField
from bt2.field_class import IntegerDisplayBase
from bt2.field_class import _BoolFieldClass
+from bt2.field_class import _BitArrayFieldClass
from bt2.field_class import _IntegerFieldClass
from bt2.field_class import _UnsignedIntegerFieldClass
from bt2.field_class import _SignedIntegerFieldClass
return self._repr()
+class _BitArrayField(_Field):
+ _NAME = 'Bit array'
+
+ @property
+ def value_as_integer(self):
+ return native_bt.field_bit_array_get_value_as_integer(self._ptr)
+
+ @value_as_integer.setter
+ def value_as_integer(self, value):
+ utils._check_uint64(value)
+ native_bt.field_bit_array_set_value_as_integer(self._ptr, value)
+
+ def _spec_eq(self, other):
+ if type(other) is not type(self):
+ return False
+
+ return self.value_as_integer == other.value_as_integer
+
+ def _repr(self):
+ return repr(self.value_as_integer)
+
+ def __str__(self):
+ return str(self.value_as_integer)
+
+ def __len__(self):
+ return self.field_class.length
+
+
@functools.total_ordering
class _NumericField(_Field):
@staticmethod
_TYPE_ID_TO_OBJ = {
native_bt.FIELD_CLASS_TYPE_BOOL: _BoolField,
+ native_bt.FIELD_CLASS_TYPE_BIT_ARRAY: _BitArrayField,
native_bt.FIELD_CLASS_TYPE_UNSIGNED_INTEGER: _UnsignedIntegerField,
native_bt.FIELD_CLASS_TYPE_SIGNED_INTEGER: _SignedIntegerField,
native_bt.FIELD_CLASS_TYPE_REAL: _RealField,
_NAME = 'Boolean'
+class _BitArrayFieldClass(_FieldClass):
+ _NAME = 'Bit array'
+
+ @property
+ def length(self):
+ length = native_bt.field_class_bit_array_get_length(self._ptr)
+ assert length >= 1
+ return length
+
+
class _IntegerFieldClass(_FieldClass):
@property
def field_value_range(self):
_FIELD_CLASS_TYPE_TO_OBJ = {
native_bt.FIELD_CLASS_TYPE_BOOL: _BoolFieldClass,
+ native_bt.FIELD_CLASS_TYPE_BIT_ARRAY: _BitArrayFieldClass,
native_bt.FIELD_CLASS_TYPE_UNSIGNED_INTEGER: _UnsignedIntegerFieldClass,
native_bt.FIELD_CLASS_TYPE_SIGNED_INTEGER: _SignedIntegerFieldClass,
native_bt.FIELD_CLASS_TYPE_REAL: _RealFieldClass,
return bt2_field_class._BoolFieldClass._create_from_ptr(field_class_ptr)
+ def create_bit_array_field_class(self, length):
+ utils._check_uint64(length)
+
+ if length < 1 or length > 64:
+ raise ValueError(
+ 'invalid length {}: expecting a value in the [1, 64] range'.format(
+ length
+ )
+ )
+
+ field_class_ptr = native_bt.field_class_bit_array_create(self._ptr, length)
+ self._check_field_class_create_status(field_class_ptr, 'bit array')
+
+ return bt2_field_class._BitArrayFieldClass._create_from_ptr(field_class_ptr)
+
def _create_integer_field_class(
self, create_func, py_cls, type_name, field_value_range, preferred_display_base
):
return packet.context_field[field_name]
+class BitArrayFieldTestCase(unittest.TestCase):
+ def _create_field(self):
+ return _create_field(self._tc, self._tc.create_bit_array_field_class(24))
+
+ def setUp(self):
+ self._tc = get_default_trace_class()
+ self._def_value = 15497
+ self._def = self._create_field()
+ self._def.value_as_integer = self._def_value
+ self._def_new_value = 101542
+
+ def test_assign_invalid_type(self):
+ with self.assertRaises(TypeError):
+ self._def.value_as_integer = 'onze'
+
+ def test_assign(self):
+ self._def.value_as_integer = 199
+ self.assertEqual(self._def.value_as_integer, 199)
+
+ def test_assign_masked(self):
+ self._def.value_as_integer = 0xE1549BB
+ self.assertEqual(self._def.value_as_integer, 0xE1549BB & ((1 << 24) - 1))
+
+ def test_eq(self):
+ other = self._create_field()
+ other.value_as_integer = self._def_value
+ self.assertEqual(self._def, other)
+
+ def test_ne_same_type(self):
+ other = self._create_field()
+ other.value_as_integer = self._def_value - 1
+ self.assertNotEqual(self._def, other)
+
+ def test_ne_diff_type(self):
+ self.assertNotEqual(self._def, self._def_value)
+
+ def test_len(self):
+ self.assertEqual(len(self._def), 24)
+
+ def test_str(self):
+ self.assertEqual(str(self._def), str(self._def_value))
+
+ def test_repr(self):
+ self.assertEqual(repr(self._def), repr(self._def_value))
+
+
# Base class for numeric field test cases.
#
# To be compatible with this base class, a derived class must, in its
self.assertIsNotNone(self._fc)
+class BitArrayFieldClassTestCase(unittest.TestCase):
+ def setUp(self):
+ self._tc = get_default_trace_class()
+ self._fc = self._tc.create_bit_array_field_class(17)
+
+ def test_create_default(self):
+ self.assertIsNotNone(self._fc)
+
+ def test_create_length_out_of_range(self):
+ with self.assertRaises(ValueError):
+ self._tc.create_bit_array_field_class(65)
+
+ def test_create_length_zero(self):
+ with self.assertRaises(ValueError):
+ self._tc.create_bit_array_field_class(0)
+
+ def test_create_length_invalid_type(self):
+ with self.assertRaises(TypeError):
+ self._tc.create_bit_array_field_class('lel')
+
+ def test_length_prop(self):
+ self.assertEqual(self._fc.length, 17)
+
+
class _TestIntegerFieldClassProps:
def test_create_default(self):
fc = self._create_func()
def test_has__BoolField(self):
self._assert_in_bt2('_BoolField')
+ def test_has__BitArrayField(self):
+ self._assert_in_bt2('_BitArrayField')
+
def test_has__IntegerField(self):
self._assert_in_bt2('_IntegerField')
def test_has__BoolFieldClass(self):
self._assert_in_bt2('_BoolFieldClass')
+ def test_has__BitArrayFieldClass(self):
+ self._assert_in_bt2('_BitArrayFieldClass')
+
def test_has__IntegerFieldClass(self):
self._assert_in_bt2('_IntegerFieldClass')