def test_encode_signed_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): SignedFixedEncoder( value_bit_size=value_bit_size, frac_places=frac_places, data_byte_size=data_byte_size, ) return encoder = SignedFixedEncoder( 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 SignedFixedEncoder' with pytest.raises(EncodingTypeError, match=pattern): encoder(value) return if SignedFixedEncoder.illegal_value_fn(value): pattern = r'Value .*(NaN|Infinity|-Infinity).* cannot be encoded by SignedFixedEncoder' with pytest.raises(IllegalValue, match=pattern): encoder(value) return lower, upper = compute_signed_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( 'SignedFixedEncoder 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_signed_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): SignedFixedEncoder( value_bit_size=value_bit_size, frac_places=frac_places, data_byte_size=data_byte_size, ) return encoder = SignedFixedEncoder( 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 SignedFixedEncoder' with pytest.raises(EncodingTypeError, match=pattern): encoder(value) return if SignedFixedEncoder.illegal_value_fn(value): pattern = r'Value .*(NaN|Infinity|-Infinity).* cannot be encoded by SignedFixedEncoder' with pytest.raises(IllegalValue, match=pattern): encoder(value) return lower, upper = compute_signed_fixed_bounds(value_bit_size, frac_places) if value < lower or value > upper: pattern = r'Value .* cannot be encoded by SignedFixedEncoder: 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 SignedFixedEncoder: residue .* outside allowed' with pytest.raises(IllegalValue, match=pattern): encoder(value) return # Ensure no exception encoder(value)
def bounds_fn(self, value_bit_size): return compute_signed_fixed_bounds(self.value_bit_size, self.frac_places)