X-Git-Url: http://git.efficios.com/?a=blobdiff_plain;f=tests%2Fbindings%2Fpython%2Fbt2%2Ftest_field.py;h=cdacd7ffa63971e462029877080ef744a58dc58a;hb=f0a42b33ac3951cd5cb2ee0f66ac04437a681621;hp=03235cc35c9e811d31d900bda20124e9f285c66d;hpb=00512c974abe06c4de9a25181eb22c0b35047a9b;p=babeltrace.git diff --git a/tests/bindings/python/bt2/test_field.py b/tests/bindings/python/bt2/test_field.py index 03235cc3..cdacd7ff 100644 --- a/tests/bindings/python/bt2/test_field.py +++ b/tests/bindings/python/bt2/test_field.py @@ -22,14 +22,12 @@ import unittest import math import copy import itertools +import collections import bt2 -from utils import get_default_trace_class +from utils import get_default_trace_class, TestOutputPortMessageIterator -_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 @@ -37,13 +35,16 @@ _COMP_BINOPS = ( # # 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 @@ -54,6 +55,7 @@ def _create_stream(tc, ctx_field_classes): # 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)]) @@ -61,11 +63,56 @@ def _create_field(tc, field_class): return packet.context_field[field_name] +# Create a const 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_const_field(tc, field_class, field_value_setter_fn): + field_name = 'const field' + + class MyIter(bt2._UserMessageIterator): + def __init__(self, self_port_output): + nonlocal field_class + nonlocal field_value_setter_fn + stream = _create_stream(tc, [(field_name, field_class)]) + packet = stream.create_packet() + + field_value_setter_fn(packet.context_field[field_name]) + + self._msgs = [ + self._create_stream_beginning_message(stream), + self._create_packet_beginning_message(packet), + ] + + def __next__(self): + if len(self._msgs) == 0: + raise StopIteration + + return self._msgs.pop(0) + + class MySrc(bt2._UserSourceComponent, message_iterator_class=MyIter): + def __init__(self, params, obj): + self._add_output_port('out', params) + + graph = bt2.Graph() + src_comp = graph.add_component(MySrc, 'my_source', None) + msg_iter = TestOutputPortMessageIterator(graph, src_comp.output_ports['out']) + + # Ignore first message, stream beginning + _ = next(msg_iter) + packet_beg_msg = next(msg_iter) + + return packet_beg_msg.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())]) @@ -79,6 +126,7 @@ def _create_string_field(tc): # 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) @@ -94,6 +142,7 @@ def _create_int_array_field(tc, 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) @@ -111,6 +160,7 @@ def _create_dynamic_array(tc): # 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) @@ -120,6 +170,52 @@ def _create_struct_array_field(tc, length): 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 @@ -352,7 +448,7 @@ class _TestNumericField: test_cb(op, bt2.create_value(0.0)) def _test_binop_rhs_complex(self, test_cb, op): - test_cb(op, -23+19j) + test_cb(op, -23 + 19j) def _test_binop_rhs_zero_complex(self, test_cb, op): test_cb(op, 0j) @@ -568,13 +664,13 @@ class _TestNumericField: # 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 @@ -660,79 +756,472 @@ def _inject_numeric_testing_methods(cls): # 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('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)) + 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 BoolFieldTestCase(_TestNumericField, unittest.TestCase): + @staticmethod + def _const_value_setter(field): + field.value = True + + def _create_fc(self, tc): + return tc.create_bool_field_class() + + def setUp(self): + self._tc = get_default_trace_class() + self._def = _create_field(self._tc, self._create_fc(self._tc)) + self._def.value = True + self._def_value = True + self._def_const = _create_const_field( + self._tc, self._tc.create_bool_field_class(), self._const_value_setter + ) + self._def_new_value = False + + def test_classes(self): + self.assertIs(type(self._def), bt2._BoolField) + self.assertIs(type(self._def_const), bt2._BoolFieldConst) + + def test_assign_true(self): + raw = True + self._def.value = raw + self.assertEqual(self._def, raw) + + def test_assign_false(self): + raw = False + self._def.value = raw + self.assertEqual(self._def, raw) + + def test_assign_field_true(self): + field = _create_field(self._tc, self._create_fc(self._tc)) + raw = True + field.value = raw + self._def.value = field + self.assertEqual(self._def, raw) + + def test_assign_field_false(self): + field = _create_field(self._tc, self._create_fc(self._tc)) + raw = False + field.value = raw + self._def.value = field + self.assertEqual(self._def, raw) + + def test_assign_invalid_type(self): + with self.assertRaises(TypeError): + self._def.value = 17 + + def test_str_op(self): + self.assertEqual(str(self._def), str(self._def_value)) + + +_inject_numeric_testing_methods(BoolFieldTestCase) class _TestIntegerFieldCommon(_TestNumericField): @@ -779,8 +1268,8 @@ class _TestIntegerFieldCommon(_TestNumericField): 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): @@ -814,11 +1303,13 @@ class SignedIntegerFieldTestCase(_TestIntegerFieldCommon, unittest.TestCase): 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): @@ -836,8 +1327,7 @@ class SignedEnumerationFieldTestCase(_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 @@ -935,10 +1425,17 @@ _inject_numeric_testing_methods(RealFieldTestCase) class StringFieldTestCase(unittest.TestCase): + @staticmethod + def _const_value_setter(field): + field.value = 'Hello, World!' + def setUp(self): self._tc = get_default_trace_class() self._def_value = 'Hello, World!' self._def = _create_string_field(self._tc) + self._def_const = _create_const_field( + self._tc, self._tc.create_string_field_class(), self._const_value_setter + ) self._def.value = self._def_value self._def_new_value = 'Yes!' @@ -955,6 +1452,9 @@ class StringFieldTestCase(unittest.TestCase): def test_eq(self): self.assertEqual(self._def, self._def_value) + def test_const_eq(self): + self.assertEqual(self._def_const, self._def_value) + def test_not_eq(self): self.assertNotEqual(self._def, 23) @@ -1018,12 +1518,21 @@ class StringFieldTestCase(unittest.TestCase): def test_getitem(self): self.assertEqual(self._def[5], self._def_value[5]) + def test_const_getitem(self): + self.assertEqual(self._def_const[5], self._def_value[5]) + def test_append_str(self): to_append = 'meow meow meow' self._def += to_append self._def_value += to_append self.assertEqual(self._def, self._def_value) + def test_const_append_str(self): + to_append = 'meow meow meow' + with self.assertRaises(TypeError): + self._def_const += to_append + self.assertEqual(self._def_const, self._def_value) + def test_append_string_field(self): field = _create_string_field(self._tc) to_append = 'meow meow meow' @@ -1048,7 +1557,12 @@ class _TestArrayFieldCommon: 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_const_getitem(self): + field = self._def_const[1] + self.assertIs(type(field), bt2._SignedIntegerFieldConst) self.assertEqual(field, 1847) def test_eq(self): @@ -1074,6 +1588,15 @@ class _TestArrayFieldCommon: 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) @@ -1106,10 +1629,18 @@ class _TestArrayFieldCommon: with self.assertRaises(IndexError): self._def[len(self._def)] = 134679 + def test_const_setitem(self): + with self.assertRaises(TypeError): + self._def_const[0] = 134679 + def test_iter(self): for field, value in zip(self._def, (45, 1847, 1948754)): self.assertEqual(field, value) + def test_const_iter(self): + for field, value in zip(self._def_const, (45, 1847, 1948754)): + self.assertEqual(field, value) + def test_value_int_field(self): values = [45646, 145, 12145] self._def.value = values @@ -1136,21 +1667,9 @@ class _TestArrayFieldCommon: 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'] @@ -1162,12 +1681,15 @@ class _TestArrayFieldCommon: 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) class StaticArrayFieldTestCase(_TestArrayFieldCommon, unittest.TestCase): + @staticmethod + def _const_value_setter(field): + field.value = [45, 1847, 1948754] + def setUp(self): self._tc = get_default_trace_class() self._def = _create_int_array_field(self._tc, 3) @@ -1175,6 +1697,13 @@ class StaticArrayFieldTestCase(_TestArrayFieldCommon, unittest.TestCase): self._def[1] = 1847 self._def[2] = 1948754 self._def_value = [45, 1847, 1948754] + self._def_const = _create_const_field( + self._tc, + self._tc.create_static_array_field_class( + self._tc.create_signed_integer_field_class(32), 3 + ), + self._const_value_setter, + ) def test_value_wrong_len(self): values = [45, 1847] @@ -1183,6 +1712,10 @@ class StaticArrayFieldTestCase(_TestArrayFieldCommon, unittest.TestCase): class DynamicArrayFieldTestCase(_TestArrayFieldCommon, unittest.TestCase): + @staticmethod + def _const_value_setter(field): + field.value = [45, 1847, 1948754] + def setUp(self): self._tc = get_default_trace_class() self._def = _create_dynamic_array(self._tc) @@ -1190,6 +1723,13 @@ class DynamicArrayFieldTestCase(_TestArrayFieldCommon, unittest.TestCase): self._def[1] = 1847 self._def[2] = 1948754 self._def_value = [45, 1847, 1948754] + self._def_const = _create_const_field( + self._tc, + self._tc.create_dynamic_array_field_class( + self._tc.create_signed_integer_field_class(32) + ), + self._const_value_setter, + ) def test_value_resize(self): new_values = [1, 2, 3, 4] @@ -1201,12 +1741,28 @@ class DynamicArrayFieldTestCase(_TestArrayFieldCommon, unittest.TestCase): self._def[3] = 0 self.assertEqual(len(self._def), 4) + def test_const_set_length(self): + with self.assertRaises(AttributeError): + self._def_const.length = 4 + self.assertEqual(len(self._def), 3) + def test_set_invalid_length(self): with self.assertRaises(TypeError): self._def.length = 'cheval' class StructureFieldTestCase(unittest.TestCase): + @staticmethod + def _const_value_setter(field): + field.value = { + 'A': -1872, + 'B': 'salut', + 'C': 17.5, + 'D': 16497, + 'E': {}, + 'F': {'F_1': 52}, + } + def _create_fc(self, tc): fc = tc.create_structure_field_class() fc.append_member('A', self._fc0_fn()) @@ -1243,9 +1799,13 @@ class StructureFieldTestCase(unittest.TestCase): 'C': 17.5, 'D': 16497, 'E': {}, - 'F': {'F_1': 52} + 'F': {'F_1': 52}, } + self._def_const = _create_const_field( + self._tc, self._create_fc(self._tc), self._const_value_setter + ) + def _modify_def(self): self._def['B'] = 'hola' @@ -1260,16 +1820,63 @@ class StructureFieldTestCase(unittest.TestCase): self.assertEqual(len(self._def), len(self._def_value)) def test_getitem(self): - field = self._def['A'] - self.assertIs(type(field), bt2.field._SignedIntegerField) - self.assertEqual(field, -1872) + field1 = self._def['A'] + field2 = self._def['B'] + field3 = self._def['C'] + field4 = self._def['D'] + field5 = self._def['E'] + field6 = self._def['F'] + + self.assertIs(type(field1), bt2._SignedIntegerField) + self.assertEqual(field1, -1872) + + self.assertIs(type(field2), bt2._StringField) + self.assertEqual(field2, 'salut') + + self.assertIs(type(field3), bt2._RealField) + self.assertEqual(field3, 17.5) + + self.assertIs(type(field4), bt2._SignedIntegerField) + self.assertEqual(field4, 16497) + + self.assertIs(type(field5), bt2._StructureField) + self.assertEqual(field5, {}) + + self.assertIs(type(field6), bt2._StructureField) + self.assertEqual(field6, {'F_1': 52}) + + def test_const_getitem(self): + field1 = self._def_const['A'] + field2 = self._def_const['B'] + field3 = self._def_const['C'] + field4 = self._def_const['D'] + field5 = self._def_const['E'] + field6 = self._def_const['F'] + + self.assertIs(type(field1), bt2._SignedIntegerFieldConst) + self.assertEqual(field1, -1872) + + self.assertIs(type(field2), bt2._StringFieldConst) + self.assertEqual(field2, 'salut') + + self.assertIs(type(field3), bt2._RealFieldConst) + self.assertEqual(field3, 17.5) + + self.assertIs(type(field4), bt2._SignedIntegerFieldConst) + self.assertEqual(field4, 16497) + + self.assertIs(type(field5), bt2._StructureFieldConst) + self.assertEqual(field5, {}) + + self.assertIs(type(field6), bt2._StructureFieldConst) + self.assertEqual(field6, {'F_1': 52}) def test_member_at_index_out_of_bounds_after(self): with self.assertRaises(IndexError): 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 @@ -1278,6 +1885,16 @@ class StructureFieldTestCase(unittest.TestCase): field['F'] = {'F_1': 52} self.assertEqual(self._def, field) + def test_const_eq(self): + 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_const, field) + def test_eq_invalid_type(self): self.assertNotEqual(self._def, 23) @@ -1341,6 +1958,10 @@ class StructureFieldTestCase(unittest.TestCase): self._def['C'] = -18.47 self.assertEqual(self._def['C'], -18.47) + def test_const_setitem(self): + with self.assertRaises(TypeError): + self._def_const['A'] = 134679 + def test_setitem_int_field(self): int_fc = self._tc.create_signed_integer_field_class(32) int_field = _create_field(self._tc, int_fc) @@ -1373,6 +1994,9 @@ class StructureFieldTestCase(unittest.TestCase): def test_member_at_index(self): self.assertEqual(self._def.member_at_index(1), 'salut') + def test_const_member_at_index(self): + self.assertEqual(self._def_const.member_at_index(1), 'salut') + def test_iter(self): orig_values = { 'A': -1872, @@ -1380,7 +2004,7 @@ class StructureFieldTestCase(unittest.TestCase): 'C': 17.5, 'D': 16497, 'E': {}, - 'F': {'F_1': 52} + 'F': {'F_1': 52}, } for vkey, vval in self._def.items(): @@ -1394,7 +2018,7 @@ class StructureFieldTestCase(unittest.TestCase): 'C': 17.5, 'D': 16497, 'E': {}, - 'F': {'F_1': 52} + 'F': {'F_1': 52}, } self.assertEqual(self._def, orig_values) @@ -1406,11 +2030,7 @@ class StructureFieldTestCase(unittest.TestCase): 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 @@ -1442,28 +2062,124 @@ class StructureFieldTestCase(unittest.TestCase): self.assertTrue(expected_string_found) -class VariantFieldTestCase(unittest.TestCase): +class OptionFieldTestCase(unittest.TestCase): + @staticmethod + def _const_value_setter(field): + field.value = {'opt_field': 'hiboux'} + 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) + fc = tc.create_option_field_class(tc.create_string_field_class()) + top_fc = tc.create_structure_field_class() + top_fc.append_member('opt_field', fc) + return top_fc + + def setUp(self): + self._tc = get_default_trace_class() + fld = _create_field(self._tc, self._create_fc(self._tc)) + self._def = fld['opt_field'] + self._def_value = 'hiboux' + self._def_const = _create_const_field( + self._tc, self._create_fc(self._tc), self._const_value_setter + )['opt_field'] + + def test_value_prop(self): + self._def.value = 'hiboux' + self.assertEqual(self._def.field, 'hiboux') + self.assertIs(type(self._def), bt2._OptionField) + self.assertIs(type(self._def.field), bt2._StringField) + self.assertTrue(self._def.has_field) + + def test_const_value_prop(self): + self.assertEqual(self._def_const.field, 'hiboux') + self.assertIs(type(self._def_const), bt2._OptionFieldConst) + self.assertIs(type(self._def_const.field), bt2._StringFieldConst) + self.assertTrue(self._def_const.has_field) + + def test_has_field_prop_true(self): + self._def.has_field = True + self.assertTrue(self._def.has_field) + + def test_has_field_prop_true(self): + self._def.has_field = False + self.assertFalse(self._def.has_field) + def test_bool_op_true(self): + self._def.value = 'allo' + self.assertTrue(self._def) + + def test_bool_op_true(self): + self._def.has_field = False + self.assertFalse(self._def) + + def test_field_prop_existing(self): + self._def.value = 'meow' + field = self._def.field + self.assertEqual(field, 'meow') + + def test_field_prop_none(self): + self._def.has_field = False + field = self._def.field + self.assertIsNone(field) + + def test_const_field_prop(self): + with self.assertRaises(AttributeError): + self._def_const.has_field = False + + self.assertEqual(self._def_const, self._def_value) + self.assertTrue(self._def_const.has_field) + + def test_field_prop_existing_then_none(self): + self._def.value = 'meow' + field = self._def.field + self.assertEqual(field, 'meow') + self._def.has_field = False + field = self._def.field + self.assertIsNone(field) + + def test_eq(self): + field = _create_field(self._tc, self._create_fc(self._tc)) + field = field['opt_field'] + field.value = 'walk' + self._def.value = 'walk' + self.assertEqual(self._def, field) + + def test_const_eq(self): + field = _create_field(self._tc, self._create_fc(self._tc)) + field = field['opt_field'] + field.value = 'hiboux' + self.assertEqual(self._def_const, field) + self.assertEqual(self._def_const, self._def_value) + + def test_eq_invalid_type(self): + self._def.value = 'gerry' + self.assertNotEqual(self._def, 23) + + def test_str_op(self): + self._def.value = 'marcel' + self.assertEqual(str(self._def), str(self._def.field)) + + def test_repr_op(self): + self._def.value = 'mireille' + self.assertEqual(repr(self._def), repr(self._def.field)) + + +class VariantFieldTestCase(unittest.TestCase): + @staticmethod + def _const_value_setter(field): + field.selected_option_index = 3 + field.value = 1334 + + def _create_fc(self, tc): 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 @@ -1472,6 +2188,15 @@ class VariantFieldTestCase(unittest.TestCase): fld = _create_field(self._tc, self._create_fc(self._tc)) self._def = fld['variant_field'] + self._def_value = 1334 + self._def_selected_index = 3 + const_fc = self._create_fc(self._tc)['variant_field'] + + fld_const = _create_const_field( + self._tc, const_fc.field_class, self._const_value_setter + ) + self._def_const = fld_const + def test_bool_op(self): self._def.selected_option_index = 2 self._def.value = -17.34 @@ -1482,14 +2207,35 @@ class VariantFieldTestCase(unittest.TestCase): self._def.selected_option_index = 2 self.assertEqual(self._def.selected_option_index, 2) + def test_selected_option_index_above_range(self): + with self.assertRaises(IndexError): + self._def.selected_option_index = 4 + + def test_selected_option_index_below_range(self): + with self.assertRaises(IndexError): + self._def.selected_option_index = -1 + + def test_const_selected_option_index(self): + with self.assertRaises(AttributeError): + self._def_const.selected_option_index = 2 + self.assertEqual(self._def_const.selected_option_index, 3) + 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.assertEqual(type(self._def.selected_option), bt2._RealField) self._def.selected_option_index = 3 self._def.value = 1921 self.assertEqual(self._def.selected_option, 1921) + self.assertEqual(type(self._def.selected_option), bt2._SignedIntegerField) + + def test_const_selected_option(self): + self.assertEqual(self._def_const.selected_option, 1334) + self.assertEqual( + type(self._def_const.selected_option), bt2._SignedIntegerFieldConst + ) def test_eq(self): field = _create_field(self._tc, self._create_fc(self._tc)) @@ -1500,6 +2246,16 @@ class VariantFieldTestCase(unittest.TestCase): self._def.value = 1774 self.assertEqual(self._def, field) + def test_const_eq(self): + field = _create_field(self._tc, self._create_fc(self._tc)) + field = field['variant_field'] + field.selected_option_index = 3 + field.value = 1334 + self.assertEqual(self._def_const, field) + + def test_len(self): + self.assertEqual(len(self._def), 4) + def test_eq_invalid_type(self): self._def.selected_option_index = 1 self._def.value = 'gerry'