Example #1
0
def test_encode_unsigned_fixed(value, value_bit_size, frac_places,
                               data_byte_size):
    if value_bit_size > data_byte_size * 8:
        pattern = r'Value byte size exceeds data size'
        with pytest.raises(ValueError, match=pattern):
            UnsignedFixedEncoder(
                value_bit_size=value_bit_size,
                frac_places=frac_places,
                data_byte_size=data_byte_size,
            )
        return

    encoder = UnsignedFixedEncoder(
        value_bit_size=value_bit_size,
        frac_places=frac_places,
        data_byte_size=data_byte_size,
    )

    if not is_number(value):
        pattern = r'Value of type .*NoneType.* cannot be encoded by UnsignedFixedEncoder'
        with pytest.raises(EncodingTypeError, match=pattern):
            encoder(value)
        return

    if UnsignedFixedEncoder.illegal_value_fn(value):
        pattern = r'Value .*(NaN|Infinity|-Infinity).* cannot be encoded by UnsignedFixedEncoder'
        with pytest.raises(IllegalValue, match=pattern):
            encoder(value)
        return

    lower, upper = compute_unsigned_fixed_bounds(value_bit_size, frac_places)
    if value < lower or value > upper:
        pattern = r'Value .* cannot be encoded in .* bits'
        with pytest.raises(ValueOutOfBounds, match=pattern):
            encoder(value)
        return

    with decimal.localcontext(abi_decimal_context):
        residue = value % (TEN**-frac_places)
    if residue > 0:
        pattern = re.escape(
            'UnsignedFixedEncoder cannot encode value {}: '
            'residue {} outside allowed fractional precision of {}'.format(
                repr(value),
                repr(residue),
                frac_places,
            ))
        with pytest.raises(IllegalValue, match=pattern):
            encoder(value)
        return

    # Ensure no exception
    encoder(value)
def test_encode_unsigned_fixed(value,
                               value_bit_size,
                               frac_places,
                               data_byte_size):
    if value_bit_size > data_byte_size * 8:
        pattern = r'Value byte size exceeds data size'
        with pytest.raises(ValueError, match=pattern):
            UnsignedFixedEncoder(
                value_bit_size=value_bit_size,
                frac_places=frac_places,
                data_byte_size=data_byte_size,
            )
        return

    encoder = UnsignedFixedEncoder(
        value_bit_size=value_bit_size,
        frac_places=frac_places,
        data_byte_size=data_byte_size,
    )

    if not is_number(value):
        pattern = r'Value `None` of type .*NoneType.* cannot be encoded by UnsignedFixedEncoder'
        with pytest.raises(EncodingTypeError, match=pattern):
            encoder(value)
        return

    if UnsignedFixedEncoder.illegal_value_fn(value):
        pattern = r'Value .*(NaN|Infinity|-Infinity).* cannot be encoded by UnsignedFixedEncoder'
        with pytest.raises(IllegalValue, match=pattern):
            encoder(value)
        return

    lower, upper = compute_unsigned_fixed_bounds(value_bit_size, frac_places)
    if value < lower or value > upper:
        pattern = (
            r'Value .* cannot be encoded by UnsignedFixedEncoder: '
            r'Cannot be encoded in .* bits'
        )
        with pytest.raises(ValueOutOfBounds, match=pattern):
            encoder(value)
        return

    with decimal.localcontext(abi_decimal_context):
        residue = value % (TEN ** -frac_places)
    if residue > 0:
        pattern = r'Value .* cannot be encoded by UnsignedFixedEncoder: residue .* outside allowed'
        with pytest.raises(IllegalValue, match=pattern):
            encoder(value)
        return

    # Ensure no exception
    encoder(value)
Example #3
0
 def bounds_fn(self, value_bit_size):
     return compute_unsigned_fixed_bounds(self.value_bit_size, self.frac_places)
 def bounds_fn(self, value_bit_size):
     return compute_unsigned_fixed_bounds(self.value_bit_size, self.frac_places)