import math
import copy
import itertools
+import collections
import bt2
from utils import get_default_trace_class
-_COMP_BINOPS = (
- operator.eq,
- operator.ne,
-)
+_COMP_BINOPS = (operator.eq, operator.ne)
# Create and return a stream with the field classes part of its stream packet
#
# 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_class = tc.create_stream_class(
+ packet_context_field_class=packet_context_fc, supports_packets=True
+ )
stream = trace.create_stream(stream_class)
return stream
# 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)])
# 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())])
# 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)
# 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)
# 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)
return packet.context_field[field_name]
+# Base class for numeric field test cases.
+#
+# To be compatible with this base class, a derived class must, in its
+# setUp() method:
+#
+# * Set `self._def` to a field object with an arbitrary value.
+# * Set `self._def_value` to the equivalent value of `self._def`.
class _TestNumericField:
+ # Tries the binary operation `op`:
+ #
+ # 1. Between `self._def`, which is a field object, and `rhs`.
+ # 2. Between `self._def_value`, which is the raw value of
+ # `self._def`, and `rhs`.
+ #
+ # Returns the results of 1. and 2.
+ #
+ # If there's an exception while performing 1. or 2., asserts that
+ # both operations raised exceptions, that both exceptions have the
+ # same type, and returns `None` for both results.
def _binop(self, op, rhs):
- rexc = None
- rvexc = None
+ type_rexc = None
+ type_rvexc = None
comp_value = rhs
+ # try with field object
try:
r = op(self._def, rhs)
except Exception as e:
- rexc = e
+ type_rexc = type(e)
+ # try with value
try:
rv = op(self._def_value, comp_value)
except Exception as e:
- rvexc = e
+ type_rvexc = type(e)
- if rexc is not None or rvexc is not None:
+ if type_rexc is not None or type_rvexc is not None:
# at least one of the operations raised an exception: in
# this case both operations should have raised the same
# type of exception (division by zero, bit shift with a
# floating point number operand, etc.)
- self.assertIs(type(rexc), type(rvexc))
+ self.assertIs(type_rexc, type_rvexc)
return None, None
return r, rv
+ # Tries the unary operation `op`:
+ #
+ # 1. On `self._def`, which is a field object.
+ # 2. On `self._def_value`, which is the value of `self._def`.
+ #
+ # Returns the results of 1. and 2.
+ #
+ # If there's an exception while performing 1. or 2., asserts that
+ # both operations raised exceptions, that both exceptions have the
+ # same type, and returns `None` for both results.
def _unaryop(self, op):
- rexc = None
- rvexc = None
+ type_rexc = None
+ type_rvexc = None
+ # try with field object
try:
r = op(self._def)
except Exception as e:
- rexc = e
+ type_rexc = type(e)
+ # try with value
try:
rv = op(self._def_value)
except Exception as e:
- rvexc = e
+ type_rvexc = type(e)
- if rexc is not None or rvexc is not None:
+ if type_rexc is not None or type_rvexc is not None:
# at least one of the operations raised an exception: in
# this case both operations should have raised the same
# type of exception (division by zero, bit shift with a
# floating point number operand, etc.)
- self.assertIs(type(rexc), type(rvexc))
+ self.assertIs(type_rexc, type_rvexc)
return None, None
return r, rv
+ # Tests that the unary operation `op` gives results with the same
+ # type for both `self._def` and `self._def_value`.
def _test_unaryop_type(self, op):
r, rv = self._unaryop(op)
self.assertIsInstance(r, type(rv))
+ # Tests that the unary operation `op` gives results with the same
+ # value for both `self._def` and `self._def_value`. This uses the
+ # __eq__() operator of `self._def`.
def _test_unaryop_value(self, op):
r, rv = self._unaryop(op)
self.assertEqual(r, rv)
+ # Tests that the unary operation `op`, when applied to `self._def`,
+ # does not change its underlying BT object address.
def _test_unaryop_addr_same(self, op):
addr_before = self._def.addr
self._unaryop(op)
self.assertEqual(self._def.addr, addr_before)
+ # Tests that the unary operation `op`, when applied to `self._def`,
+ # does not change its value.
def _test_unaryop_value_same(self, op):
value_before = copy.copy(self._def_value)
self._unaryop(op)
self.assertEqual(self._def, value_before)
+ # Tests that the binary operation `op` gives results with the same
+ # type for both `self._def` and `self._def_value`.
def _test_binop_type(self, op, rhs):
r, rv = self._binop(op, rhs)
else:
self.assertIsInstance(r, type(rv))
+ # Tests that the binary operation `op` gives results with the same
+ # value for both `self._def` and `self._def_value`. This uses the
+ # __eq__() operator of `self._def`.
def _test_binop_value(self, op, rhs):
r, rv = self._binop(op, rhs)
self.assertEqual(r, rv)
+ # Tests that the binary operation `op`, when applied to `self._def`,
+ # does not change its underlying BT object address.
def _test_binop_lhs_addr_same(self, op, rhs):
addr_before = self._def.addr
r, rv = self._binop(op, rhs)
self.assertEqual(self._def.addr, addr_before)
+ # Tests that the binary operation `op`, when applied to `self._def`,
+ # does not change its value.
@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)
self.assertEqual(self._def, value_before)
+ # The methods below which take the `test_cb` and/or `op` parameters
+ # are meant to be used with one of the _test_binop_*() functions
+ # above as `test_cb` and a binary operator function as `op`.
+ #
+ # For example:
+ #
+ # self._test_binop_rhs_pos_int(self._test_binop_value,
+ # operator.add)
+ #
+ # This tests that a numeric field object added to a positive integer
+ # value gives a result with the expected value.
+ #
+ # `vint` and `vfloat` mean a signed integer value object and a real
+ # value object.
+
def _test_binop_invalid_unknown(self, op):
if op in _COMP_BINOPS:
self.skipTest('not testing')
def _test_binop_rhs_zero_vfloat(self, test_cb, op):
test_cb(op, bt2.create_value(0.0))
+ def _test_binop_rhs_complex(self, test_cb, op):
+ test_cb(op, -23 + 19j)
+
+ def _test_binop_rhs_zero_complex(self, test_cb, op):
+ test_cb(op, 0j)
+
def _test_binop_type_false(self, op):
self._test_binop_rhs_false(self._test_binop_type, op)
def _test_binop_type_zero_vfloat(self, op):
self._test_binop_rhs_zero_vfloat(self._test_binop_type, op)
+ def _test_binop_type_complex(self, op):
+ self._test_binop_rhs_complex(self._test_binop_type, op)
+
+ def _test_binop_type_zero_complex(self, op):
+ self._test_binop_rhs_zero_complex(self._test_binop_type, op)
+
def _test_binop_value_false(self, op):
self._test_binop_rhs_false(self._test_binop_value, op)
def _test_binop_value_zero_vfloat(self, op):
self._test_binop_rhs_zero_vfloat(self._test_binop_value, op)
+ def _test_binop_value_complex(self, op):
+ self._test_binop_rhs_complex(self._test_binop_value, op)
+
+ def _test_binop_value_zero_complex(self, op):
+ self._test_binop_rhs_zero_complex(self._test_binop_value, op)
+
def _test_binop_lhs_addr_same_false(self, op):
self._test_binop_rhs_false(self._test_binop_lhs_addr_same, op)
def _test_binop_lhs_addr_same_zero_vfloat(self, op):
self._test_binop_rhs_zero_vfloat(self._test_binop_lhs_addr_same, op)
+ def _test_binop_lhs_addr_same_complex(self, op):
+ self._test_binop_rhs_complex(self._test_binop_lhs_addr_same, op)
+
+ def _test_binop_lhs_addr_same_zero_complex(self, op):
+ self._test_binop_rhs_zero_complex(self._test_binop_lhs_addr_same, op)
+
def _test_binop_lhs_value_same_false(self, op):
self._test_binop_rhs_false(self._test_binop_lhs_value_same, op)
def _test_binop_lhs_value_same_zero_vfloat(self, op):
self._test_binop_rhs_zero_vfloat(self._test_binop_lhs_value_same, op)
+ def _test_binop_lhs_value_same_complex(self, op):
+ self._test_binop_rhs_complex(self._test_binop_lhs_value_same, op)
+
+ def _test_binop_lhs_value_same_zero_complex(self, op):
+ self._test_binop_rhs_zero_complex(self._test_binop_lhs_value_same, op)
+
def test_bool_op(self):
self.assertEqual(bool(self._def), bool(self._def_value))
# 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
+ self.assertFalse(self._def == None) # noqa: E711
def test_ne_none(self):
# 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
+ self.assertTrue(self._def != None) # noqa: E711
+# This is a list of binary operators used for
+# _inject_numeric_testing_methods().
+#
+# Each entry is a pair of binary operator name (used as part of the
+# created testing method's name) and operator function.
_BINOPS = (
('lt', operator.lt),
('le', operator.le),
)
+# This is a list of unary operators used for
+# _inject_numeric_testing_methods().
+#
+# Each entry is a pair of unary operator name (used as part of the
+# created testing method's name) and operator function.
_UNARYOPS = (
('neg', operator.neg),
('pos', operator.pos),
)
+# This function injects a bunch of testing methods to a numeric
+# field test case.
+#
+# It is meant to be used like this:
+#
+# _inject_numeric_testing_methods(MyNumericFieldTestCase)
+#
+# This function injects:
+#
+# * One testing method for each _TestNumericField._test_binop_*()
+# method, for each binary operator in the _BINOPS tuple.
+#
+# * One testing method for each _TestNumericField._test_unaryop*()
+# method, for each unary operator in the _UNARYOPS tuple.
def _inject_numeric_testing_methods(cls):
def test_binop_name(suffix):
return 'test_binop_{}_{}'.format(name, suffix)
# inject testing methods for each binary operation
for name, binop in _BINOPS:
- setattr(cls, test_binop_name('invalid_unknown'), partialmethod(_TestNumericField._test_binop_invalid_unknown, op=binop))
- setattr(cls, test_binop_name('invalid_none'), partialmethod(_TestNumericField._test_binop_invalid_none, op=binop))
- setattr(cls, test_binop_name('type_true'), partialmethod(_TestNumericField._test_binop_type_true, op=binop))
- setattr(cls, test_binop_name('type_pos_int'), partialmethod(_TestNumericField._test_binop_type_pos_int, op=binop))
- setattr(cls, test_binop_name('type_pos_vint'), partialmethod(_TestNumericField._test_binop_type_pos_vint, op=binop))
- setattr(cls, test_binop_name('value_true'), partialmethod(_TestNumericField._test_binop_value_true, op=binop))
- setattr(cls, test_binop_name('value_pos_int'), partialmethod(_TestNumericField._test_binop_value_pos_int, op=binop))
- setattr(cls, test_binop_name('value_pos_vint'), partialmethod(_TestNumericField._test_binop_value_pos_vint, op=binop))
- setattr(cls, test_binop_name('lhs_addr_same_true'), partialmethod(_TestNumericField._test_binop_lhs_addr_same_true, op=binop))
- setattr(cls, test_binop_name('lhs_addr_same_pos_int'), partialmethod(_TestNumericField._test_binop_lhs_addr_same_pos_int, op=binop))
- setattr(cls, test_binop_name('lhs_addr_same_pos_vint'), partialmethod(_TestNumericField._test_binop_lhs_addr_same_pos_vint, op=binop))
- setattr(cls, test_binop_name('lhs_value_same_true'), partialmethod(_TestNumericField._test_binop_lhs_value_same_true, op=binop))
- setattr(cls, test_binop_name('lhs_value_same_pos_int'), partialmethod(_TestNumericField._test_binop_lhs_value_same_pos_int, op=binop))
- setattr(cls, test_binop_name('lhs_value_same_pos_vint'), partialmethod(_TestNumericField._test_binop_lhs_value_same_pos_vint, op=binop))
- setattr(cls, test_binop_name('type_neg_int'), partialmethod(_TestNumericField._test_binop_type_neg_int, op=binop))
- setattr(cls, test_binop_name('type_neg_vint'), partialmethod(_TestNumericField._test_binop_type_neg_vint, op=binop))
- setattr(cls, test_binop_name('value_neg_int'), partialmethod(_TestNumericField._test_binop_value_neg_int, op=binop))
- setattr(cls, test_binop_name('value_neg_vint'), partialmethod(_TestNumericField._test_binop_value_neg_vint, op=binop))
- setattr(cls, test_binop_name('lhs_addr_same_neg_int'), partialmethod(_TestNumericField._test_binop_lhs_addr_same_neg_int, op=binop))
- setattr(cls, test_binop_name('lhs_addr_same_neg_vint'), partialmethod(_TestNumericField._test_binop_lhs_addr_same_neg_vint, op=binop))
- setattr(cls, test_binop_name('lhs_value_same_neg_int'), partialmethod(_TestNumericField._test_binop_lhs_value_same_neg_int, op=binop))
- setattr(cls, test_binop_name('lhs_value_same_neg_vint'), partialmethod(_TestNumericField._test_binop_lhs_value_same_neg_vint, op=binop))
- setattr(cls, test_binop_name('type_false'), partialmethod(_TestNumericField._test_binop_type_false, op=binop))
- setattr(cls, test_binop_name('type_zero_int'), partialmethod(_TestNumericField._test_binop_type_zero_int, op=binop))
- setattr(cls, test_binop_name('type_zero_vint'), partialmethod(_TestNumericField._test_binop_type_zero_vint, op=binop))
- setattr(cls, test_binop_name('value_false'), partialmethod(_TestNumericField._test_binop_value_false, op=binop))
- setattr(cls, test_binop_name('value_zero_int'), partialmethod(_TestNumericField._test_binop_value_zero_int, op=binop))
- setattr(cls, test_binop_name('value_zero_vint'), partialmethod(_TestNumericField._test_binop_value_zero_vint, op=binop))
- setattr(cls, test_binop_name('lhs_addr_same_false'), partialmethod(_TestNumericField._test_binop_lhs_addr_same_false, op=binop))
- setattr(cls, test_binop_name('lhs_addr_same_zero_int'), partialmethod(_TestNumericField._test_binop_lhs_addr_same_zero_int, op=binop))
- setattr(cls, test_binop_name('lhs_addr_same_zero_vint'), partialmethod(_TestNumericField._test_binop_lhs_addr_same_zero_vint, op=binop))
- setattr(cls, test_binop_name('lhs_value_same_false'), partialmethod(_TestNumericField._test_binop_lhs_value_same_false, op=binop))
- setattr(cls, test_binop_name('lhs_value_same_zero_int'), partialmethod(_TestNumericField._test_binop_lhs_value_same_zero_int, op=binop))
- setattr(cls, test_binop_name('lhs_value_same_zero_vint'), partialmethod(_TestNumericField._test_binop_lhs_value_same_zero_vint, op=binop))
- setattr(cls, test_binop_name('type_pos_float'), partialmethod(_TestNumericField._test_binop_type_pos_float, op=binop))
- setattr(cls, test_binop_name('type_neg_float'), partialmethod(_TestNumericField._test_binop_type_neg_float, op=binop))
- setattr(cls, test_binop_name('type_pos_vfloat'), partialmethod(_TestNumericField._test_binop_type_pos_vfloat, op=binop))
- setattr(cls, test_binop_name('type_neg_vfloat'), partialmethod(_TestNumericField._test_binop_type_neg_vfloat, op=binop))
- setattr(cls, test_binop_name('value_pos_float'), partialmethod(_TestNumericField._test_binop_value_pos_float, op=binop))
- setattr(cls, test_binop_name('value_neg_float'), partialmethod(_TestNumericField._test_binop_value_neg_float, op=binop))
- setattr(cls, test_binop_name('value_pos_vfloat'), partialmethod(_TestNumericField._test_binop_value_pos_vfloat, op=binop))
- setattr(cls, test_binop_name('value_neg_vfloat'), partialmethod(_TestNumericField._test_binop_value_neg_vfloat, op=binop))
- setattr(cls, test_binop_name('lhs_addr_same_pos_float'), partialmethod(_TestNumericField._test_binop_lhs_addr_same_pos_float, op=binop))
- setattr(cls, test_binop_name('lhs_addr_same_neg_float'), partialmethod(_TestNumericField._test_binop_lhs_addr_same_neg_float, op=binop))
- setattr(cls, test_binop_name('lhs_addr_same_pos_vfloat'), partialmethod(_TestNumericField._test_binop_lhs_addr_same_pos_vfloat, op=binop))
- setattr(cls, test_binop_name('lhs_addr_same_neg_vfloat'), partialmethod(_TestNumericField._test_binop_lhs_addr_same_neg_vfloat, op=binop))
- setattr(cls, test_binop_name('lhs_value_same_pos_float'), partialmethod(_TestNumericField._test_binop_lhs_value_same_pos_float, op=binop))
- setattr(cls, test_binop_name('lhs_value_same_neg_float'), partialmethod(_TestNumericField._test_binop_lhs_value_same_neg_float, op=binop))
- setattr(cls, test_binop_name('lhs_value_same_pos_vfloat'), partialmethod(_TestNumericField._test_binop_lhs_value_same_pos_vfloat, op=binop))
- setattr(cls, test_binop_name('lhs_value_same_neg_vfloat'), partialmethod(_TestNumericField._test_binop_lhs_value_same_neg_vfloat, op=binop))
- setattr(cls, test_binop_name('type_zero_float'), partialmethod(_TestNumericField._test_binop_type_zero_float, op=binop))
- setattr(cls, test_binop_name('type_zero_vfloat'), partialmethod(_TestNumericField._test_binop_type_zero_vfloat, op=binop))
- setattr(cls, test_binop_name('value_zero_float'), partialmethod(_TestNumericField._test_binop_value_zero_float, op=binop))
- setattr(cls, test_binop_name('value_zero_vfloat'), partialmethod(_TestNumericField._test_binop_value_zero_vfloat, op=binop))
- setattr(cls, test_binop_name('lhs_addr_same_zero_float'), partialmethod(_TestNumericField._test_binop_lhs_addr_same_zero_float, op=binop))
- setattr(cls, test_binop_name('lhs_addr_same_zero_vfloat'), partialmethod(_TestNumericField._test_binop_lhs_addr_same_zero_vfloat, op=binop))
- setattr(cls, test_binop_name('lhs_value_same_zero_float'), partialmethod(_TestNumericField._test_binop_lhs_value_same_zero_float, op=binop))
- setattr(cls, test_binop_name('lhs_value_same_zero_vfloat'), partialmethod(_TestNumericField._test_binop_lhs_value_same_zero_vfloat, op=binop))
+ setattr(
+ cls,
+ test_binop_name('invalid_unknown'),
+ partialmethod(_TestNumericField._test_binop_invalid_unknown, op=binop),
+ )
+ setattr(
+ cls,
+ test_binop_name('invalid_none'),
+ partialmethod(_TestNumericField._test_binop_invalid_none, op=binop),
+ )
+ setattr(
+ cls,
+ test_binop_name('type_true'),
+ partialmethod(_TestNumericField._test_binop_type_true, op=binop),
+ )
+ setattr(
+ cls,
+ test_binop_name('type_pos_int'),
+ partialmethod(_TestNumericField._test_binop_type_pos_int, op=binop),
+ )
+ setattr(
+ cls,
+ test_binop_name('type_pos_vint'),
+ partialmethod(_TestNumericField._test_binop_type_pos_vint, op=binop),
+ )
+ setattr(
+ cls,
+ test_binop_name('value_true'),
+ partialmethod(_TestNumericField._test_binop_value_true, op=binop),
+ )
+ setattr(
+ cls,
+ test_binop_name('value_pos_int'),
+ partialmethod(_TestNumericField._test_binop_value_pos_int, op=binop),
+ )
+ setattr(
+ cls,
+ test_binop_name('value_pos_vint'),
+ partialmethod(_TestNumericField._test_binop_value_pos_vint, op=binop),
+ )
+ setattr(
+ cls,
+ test_binop_name('lhs_addr_same_true'),
+ partialmethod(_TestNumericField._test_binop_lhs_addr_same_true, op=binop),
+ )
+ setattr(
+ cls,
+ test_binop_name('lhs_addr_same_pos_int'),
+ partialmethod(
+ _TestNumericField._test_binop_lhs_addr_same_pos_int, op=binop
+ ),
+ )
+ setattr(
+ cls,
+ test_binop_name('lhs_addr_same_pos_vint'),
+ partialmethod(
+ _TestNumericField._test_binop_lhs_addr_same_pos_vint, op=binop
+ ),
+ )
+ setattr(
+ cls,
+ test_binop_name('lhs_value_same_true'),
+ partialmethod(_TestNumericField._test_binop_lhs_value_same_true, op=binop),
+ )
+ setattr(
+ cls,
+ test_binop_name('lhs_value_same_pos_int'),
+ partialmethod(
+ _TestNumericField._test_binop_lhs_value_same_pos_int, op=binop
+ ),
+ )
+ setattr(
+ cls,
+ test_binop_name('lhs_value_same_pos_vint'),
+ partialmethod(
+ _TestNumericField._test_binop_lhs_value_same_pos_vint, op=binop
+ ),
+ )
+ setattr(
+ cls,
+ test_binop_name('type_neg_int'),
+ partialmethod(_TestNumericField._test_binop_type_neg_int, op=binop),
+ )
+ setattr(
+ cls,
+ test_binop_name('type_neg_vint'),
+ partialmethod(_TestNumericField._test_binop_type_neg_vint, op=binop),
+ )
+ setattr(
+ cls,
+ test_binop_name('value_neg_int'),
+ partialmethod(_TestNumericField._test_binop_value_neg_int, op=binop),
+ )
+ setattr(
+ cls,
+ test_binop_name('value_neg_vint'),
+ partialmethod(_TestNumericField._test_binop_value_neg_vint, op=binop),
+ )
+ setattr(
+ cls,
+ test_binop_name('lhs_addr_same_neg_int'),
+ partialmethod(
+ _TestNumericField._test_binop_lhs_addr_same_neg_int, op=binop
+ ),
+ )
+ setattr(
+ cls,
+ test_binop_name('lhs_addr_same_neg_vint'),
+ partialmethod(
+ _TestNumericField._test_binop_lhs_addr_same_neg_vint, op=binop
+ ),
+ )
+ setattr(
+ cls,
+ test_binop_name('lhs_value_same_neg_int'),
+ partialmethod(
+ _TestNumericField._test_binop_lhs_value_same_neg_int, op=binop
+ ),
+ )
+ setattr(
+ cls,
+ test_binop_name('lhs_value_same_neg_vint'),
+ partialmethod(
+ _TestNumericField._test_binop_lhs_value_same_neg_vint, op=binop
+ ),
+ )
+ setattr(
+ cls,
+ test_binop_name('type_false'),
+ partialmethod(_TestNumericField._test_binop_type_false, op=binop),
+ )
+ setattr(
+ cls,
+ test_binop_name('type_zero_int'),
+ partialmethod(_TestNumericField._test_binop_type_zero_int, op=binop),
+ )
+ setattr(
+ cls,
+ test_binop_name('type_zero_vint'),
+ partialmethod(_TestNumericField._test_binop_type_zero_vint, op=binop),
+ )
+ setattr(
+ cls,
+ test_binop_name('value_false'),
+ partialmethod(_TestNumericField._test_binop_value_false, op=binop),
+ )
+ setattr(
+ cls,
+ test_binop_name('value_zero_int'),
+ partialmethod(_TestNumericField._test_binop_value_zero_int, op=binop),
+ )
+ setattr(
+ cls,
+ test_binop_name('value_zero_vint'),
+ partialmethod(_TestNumericField._test_binop_value_zero_vint, op=binop),
+ )
+ setattr(
+ cls,
+ test_binop_name('lhs_addr_same_false'),
+ partialmethod(_TestNumericField._test_binop_lhs_addr_same_false, op=binop),
+ )
+ setattr(
+ cls,
+ test_binop_name('lhs_addr_same_zero_int'),
+ partialmethod(
+ _TestNumericField._test_binop_lhs_addr_same_zero_int, op=binop
+ ),
+ )
+ setattr(
+ cls,
+ test_binop_name('lhs_addr_same_zero_vint'),
+ partialmethod(
+ _TestNumericField._test_binop_lhs_addr_same_zero_vint, op=binop
+ ),
+ )
+ setattr(
+ cls,
+ test_binop_name('lhs_value_same_false'),
+ partialmethod(_TestNumericField._test_binop_lhs_value_same_false, op=binop),
+ )
+ setattr(
+ cls,
+ test_binop_name('lhs_value_same_zero_int'),
+ partialmethod(
+ _TestNumericField._test_binop_lhs_value_same_zero_int, op=binop
+ ),
+ )
+ setattr(
+ cls,
+ test_binop_name('lhs_value_same_zero_vint'),
+ partialmethod(
+ _TestNumericField._test_binop_lhs_value_same_zero_vint, op=binop
+ ),
+ )
+ setattr(
+ cls,
+ test_binop_name('type_pos_float'),
+ partialmethod(_TestNumericField._test_binop_type_pos_float, op=binop),
+ )
+ setattr(
+ cls,
+ test_binop_name('type_neg_float'),
+ partialmethod(_TestNumericField._test_binop_type_neg_float, op=binop),
+ )
+ setattr(
+ cls,
+ test_binop_name('type_pos_vfloat'),
+ partialmethod(_TestNumericField._test_binop_type_pos_vfloat, op=binop),
+ )
+ setattr(
+ cls,
+ test_binop_name('type_neg_vfloat'),
+ partialmethod(_TestNumericField._test_binop_type_neg_vfloat, op=binop),
+ )
+ setattr(
+ cls,
+ test_binop_name('value_pos_float'),
+ partialmethod(_TestNumericField._test_binop_value_pos_float, op=binop),
+ )
+ setattr(
+ cls,
+ test_binop_name('value_neg_float'),
+ partialmethod(_TestNumericField._test_binop_value_neg_float, op=binop),
+ )
+ setattr(
+ cls,
+ test_binop_name('value_pos_vfloat'),
+ partialmethod(_TestNumericField._test_binop_value_pos_vfloat, op=binop),
+ )
+ setattr(
+ cls,
+ test_binop_name('value_neg_vfloat'),
+ partialmethod(_TestNumericField._test_binop_value_neg_vfloat, op=binop),
+ )
+ setattr(
+ cls,
+ test_binop_name('lhs_addr_same_pos_float'),
+ partialmethod(
+ _TestNumericField._test_binop_lhs_addr_same_pos_float, op=binop
+ ),
+ )
+ setattr(
+ cls,
+ test_binop_name('lhs_addr_same_neg_float'),
+ partialmethod(
+ _TestNumericField._test_binop_lhs_addr_same_neg_float, op=binop
+ ),
+ )
+ setattr(
+ cls,
+ test_binop_name('lhs_addr_same_pos_vfloat'),
+ partialmethod(
+ _TestNumericField._test_binop_lhs_addr_same_pos_vfloat, op=binop
+ ),
+ )
+ setattr(
+ cls,
+ test_binop_name('lhs_addr_same_neg_vfloat'),
+ partialmethod(
+ _TestNumericField._test_binop_lhs_addr_same_neg_vfloat, op=binop
+ ),
+ )
+ setattr(
+ cls,
+ test_binop_name('lhs_value_same_pos_float'),
+ partialmethod(
+ _TestNumericField._test_binop_lhs_value_same_pos_float, op=binop
+ ),
+ )
+ setattr(
+ cls,
+ test_binop_name('lhs_value_same_neg_float'),
+ partialmethod(
+ _TestNumericField._test_binop_lhs_value_same_neg_float, op=binop
+ ),
+ )
+ setattr(
+ cls,
+ test_binop_name('lhs_value_same_pos_vfloat'),
+ partialmethod(
+ _TestNumericField._test_binop_lhs_value_same_pos_vfloat, op=binop
+ ),
+ )
+ setattr(
+ cls,
+ test_binop_name('lhs_value_same_neg_vfloat'),
+ partialmethod(
+ _TestNumericField._test_binop_lhs_value_same_neg_vfloat, op=binop
+ ),
+ )
+ setattr(
+ cls,
+ test_binop_name('type_zero_float'),
+ partialmethod(_TestNumericField._test_binop_type_zero_float, op=binop),
+ )
+ setattr(
+ cls,
+ test_binop_name('type_zero_vfloat'),
+ partialmethod(_TestNumericField._test_binop_type_zero_vfloat, op=binop),
+ )
+ setattr(
+ cls,
+ test_binop_name('value_zero_float'),
+ partialmethod(_TestNumericField._test_binop_value_zero_float, op=binop),
+ )
+ setattr(
+ cls,
+ test_binop_name('value_zero_vfloat'),
+ partialmethod(_TestNumericField._test_binop_value_zero_vfloat, op=binop),
+ )
+ setattr(
+ cls,
+ test_binop_name('lhs_addr_same_zero_float'),
+ partialmethod(
+ _TestNumericField._test_binop_lhs_addr_same_zero_float, op=binop
+ ),
+ )
+ setattr(
+ cls,
+ test_binop_name('lhs_addr_same_zero_vfloat'),
+ partialmethod(
+ _TestNumericField._test_binop_lhs_addr_same_zero_vfloat, op=binop
+ ),
+ )
+ setattr(
+ cls,
+ test_binop_name('lhs_value_same_zero_float'),
+ partialmethod(
+ _TestNumericField._test_binop_lhs_value_same_zero_float, op=binop
+ ),
+ )
+ setattr(
+ cls,
+ test_binop_name('lhs_value_same_zero_vfloat'),
+ partialmethod(
+ _TestNumericField._test_binop_lhs_value_same_zero_vfloat, op=binop
+ ),
+ )
+ setattr(
+ cls,
+ test_binop_name('type_complex'),
+ partialmethod(_TestNumericField._test_binop_type_complex, op=binop),
+ )
+ setattr(
+ cls,
+ test_binop_name('type_zero_complex'),
+ partialmethod(_TestNumericField._test_binop_type_zero_complex, op=binop),
+ )
+ setattr(
+ cls,
+ test_binop_name('value_complex'),
+ partialmethod(_TestNumericField._test_binop_value_complex, op=binop),
+ )
+ setattr(
+ cls,
+ test_binop_name('value_zero_complex'),
+ partialmethod(_TestNumericField._test_binop_value_zero_complex, op=binop),
+ )
+ setattr(
+ cls,
+ test_binop_name('lhs_addr_same_complex'),
+ partialmethod(
+ _TestNumericField._test_binop_lhs_addr_same_complex, op=binop
+ ),
+ )
+ setattr(
+ cls,
+ test_binop_name('lhs_addr_same_zero_complex'),
+ partialmethod(
+ _TestNumericField._test_binop_lhs_addr_same_zero_complex, op=binop
+ ),
+ )
+ setattr(
+ cls,
+ test_binop_name('lhs_value_same_complex'),
+ partialmethod(
+ _TestNumericField._test_binop_lhs_value_same_complex, op=binop
+ ),
+ )
+ setattr(
+ cls,
+ test_binop_name('lhs_value_same_zero_complex'),
+ partialmethod(
+ _TestNumericField._test_binop_lhs_value_same_zero_complex, op=binop
+ ),
+ )
# inject testing methods for each unary operation
for name, unaryop in _UNARYOPS:
- setattr(cls, test_unaryop_name('type'), partialmethod(_TestNumericField._test_unaryop_type, op=unaryop))
- setattr(cls, test_unaryop_name('value'), partialmethod(_TestNumericField._test_unaryop_value, op=unaryop))
- setattr(cls, test_unaryop_name('addr_same'), partialmethod(_TestNumericField._test_unaryop_addr_same, op=unaryop))
- setattr(cls, test_unaryop_name('value_same'), partialmethod(_TestNumericField._test_unaryop_value_same, op=unaryop))
+ setattr(
+ cls,
+ test_unaryop_name('type'),
+ partialmethod(_TestNumericField._test_unaryop_type, op=unaryop),
+ )
+ setattr(
+ cls,
+ test_unaryop_name('value'),
+ partialmethod(_TestNumericField._test_unaryop_value, op=unaryop),
+ )
+ setattr(
+ cls,
+ test_unaryop_name('addr_same'),
+ partialmethod(_TestNumericField._test_unaryop_addr_same, op=unaryop),
+ )
+ setattr(
+ cls,
+ test_unaryop_name('value_same'),
+ partialmethod(_TestNumericField._test_unaryop_value_same, op=unaryop),
+ )
class _TestIntegerFieldCommon(_TestNumericField):
self._def.value = field
self.assertEqual(self._def, raw)
- def test_assign_float(self):
- raw = 123.456
- self._def.value = raw
- self.assertEqual(self._def, int(raw))
-
def test_assign_invalid_type(self):
with self.assertRaises(TypeError):
self._def.value = 'yes'
field = _create_field(self._tc, uint_fc)
# Larger than the IEEE 754 double-precision exact representation of
# integers.
- raw = (2**53) + 1
- field.value = (2**53) + 1
+ raw = (2 ** 53) + 1
+ field.value = (2 ** 53) + 1
self.assertEqual(field, raw)
def test_assign_uint_invalid_neg(self):
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)
+ fc.add_mapping('something', bt2.SignedIntegerRangeSet([(17, 17)]))
+ fc.add_mapping('speaker', bt2.SignedIntegerRangeSet([(12, 16)]))
+ fc.add_mapping('can', bt2.SignedIntegerRangeSet([(18, 2540)]))
+ fc.add_mapping(
+ 'whole range', bt2.SignedIntegerRangeSet([(-(2 ** 31), (2 ** 31) - 1)])
+ )
+ fc.add_mapping('zip', bt2.SignedIntegerRangeSet([(-45, 1001)]))
return fc
def setUp(self):
# 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
def test_getitem(self):
field = self._def[1]
- self.assertIs(type(field), bt2.field._SignedIntegerField)
+ self.assertIs(type(field), bt2._SignedIntegerField)
self.assertEqual(field, 1847)
def test_eq(self):
field[2] = 1948754
self.assertNotEqual(self._def, field)
+ def test_eq_non_sequence_iterable(self):
+ dct = collections.OrderedDict([(1, 2), (3, 4), (5, 6)])
+ field = _create_int_array_field(self._tc, 3)
+ field[0] = 1
+ field[1] = 3
+ field[2] = 5
+ self.assertEqual(field, list(dct.keys()))
+ self.assertNotEqual(field, dct)
+
def test_setitem(self):
self._def[2] = 24
self.assertEqual(self._def[2], 24)
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,
- 'a_string': 'hello',
- 'another_int': 66
- },
- {
- 'an_int': 1,
- 'a_string': 'goodbye',
- 'another_int': 488
- },
- {
- 'an_int': 156,
- 'a_string': 'or not',
- 'another_int': 4648
- },
+ {'an_int': 42, 'a_string': 'hello', 'another_int': 66},
+ {'an_int': 1, 'a_string': 'goodbye', 'another_int': 488},
+ {'an_int': 156, 'a_string': 'or not', 'another_int': 4648},
]
array = stream.create_packet().context_field['array_field']
def test_str_op(self):
s = str(self._def)
- expected_string = '[{}]'.format(', '.join(
- [repr(v) for v in self._def_value]))
+ expected_string = '[{}]'.format(', '.join([repr(v) for v in self._def_value]))
self.assertEqual(expected_string, s)
'C': 17.5,
'D': 16497,
'E': {},
- 'F': {'F_1': 52}
+ 'F': {'F_1': 52},
}
def _modify_def(self):
def test_getitem(self):
field = self._def['A']
- self.assertIs(type(field), bt2.field._SignedIntegerField)
+ self.assertIs(type(field), bt2._SignedIntegerField)
self.assertEqual(field, -1872)
def test_member_at_index_out_of_bounds_after(self):
self._def.member_at_index(len(self._def_value))
def test_eq(self):
- field = _create_field(self._tc, self._create_fc(self._tc, ))
+ field = _create_field(self._tc, self._create_fc(self._tc))
field['A'] = -1872
field['B'] = 'salut'
field['C'] = 17.5
'C': 17.5,
'D': 16497,
'E': {},
- 'F': {'F_1': 52}
+ 'F': {'F_1': 52},
}
for vkey, vval in self._def.items():
'C': 17.5,
'D': 16497,
'E': {},
- 'F': {'F_1': 52}
+ 'F': {'F_1': 52},
}
self.assertEqual(self._def, orig_values)
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
- }
+ values = {'an_int': 42, 'a_string': 'hello', 'another_int': 66}
struct = _create_field(self._tc, struct_fc)
struct.value = values
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