Exemplo n.º 1
0
def test_encode_unsigned_real(base_integer_value,
                              value_bit_size,
                              high_bit_size,
                              low_bit_size,
                              data_byte_size):
    if value_bit_size > data_byte_size * 8:
        with pytest.raises(ValueError):
            UnsignedRealEncoder.as_encoder(
                value_bit_size=value_bit_size,
                high_bit_size=high_bit_size,
                low_bit_size=low_bit_size,
                data_byte_size=data_byte_size,
            )
        return
    elif high_bit_size + low_bit_size != value_bit_size:
        with pytest.raises(ValueError):
            UnsignedRealEncoder.as_encoder(
                value_bit_size=value_bit_size,
                high_bit_size=high_bit_size,
                low_bit_size=low_bit_size,
                data_byte_size=data_byte_size,
            )
        return

    encoder = UnsignedRealEncoder.as_encoder(
        value_bit_size=value_bit_size,
        high_bit_size=high_bit_size,
        low_bit_size=low_bit_size,
        data_byte_size=data_byte_size,
    )

    if not is_number(base_integer_value):
        with pytest.raises(EncodingTypeError) as exception_info:
            encoder(base_integer_value)
        assert 'UnsignedReal' in str(exception_info.value)
        return

    with decimal.localcontext(abi_decimal_context):
        real_value = decimal.Decimal(base_integer_value) / 2 ** low_bit_size

    lower_bound, upper_bound = compute_unsigned_real_bounds(
        high_bit_size,
        low_bit_size,
    )

    if real_value < lower_bound or real_value > upper_bound:
        with pytest.raises(ValueOutOfBounds):
            encoder(base_integer_value)
        return

    expected_value = zpad(int_to_big_endian(base_integer_value), data_byte_size)
    encoded_value = encoder(real_value)

    assert encoded_value == expected_value
Exemplo n.º 2
0
 def bounds_fn(self, value_bit_size):
     return compute_unsigned_real_bounds(self.high_bit_size,
                                         self.low_bit_size)
Exemplo n.º 3
0
 def bounds_fn(cls, value_bit_size):
     return compute_unsigned_real_bounds(cls.high_bit_size,
                                         cls.low_bit_size)