Ejemplo n.º 1
0
def test_decode_signed_real(high_bit_size, low_bit_size, integer_bit_size,
                            stream_bytes, data_byte_size):
    if integer_bit_size > data_byte_size * 8:
        with pytest.raises(ValueError):
            SignedRealDecoder.as_decoder(
                value_bit_size=integer_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 != integer_bit_size:
        with pytest.raises(ValueError):
            SignedRealDecoder.as_decoder(
                value_bit_size=integer_bit_size,
                high_bit_size=high_bit_size,
                low_bit_size=low_bit_size,
                data_byte_size=data_byte_size,
            )
        return
    else:
        decoder = SignedRealDecoder.as_decoder(
            value_bit_size=integer_bit_size,
            high_bit_size=high_bit_size,
            low_bit_size=low_bit_size,
            data_byte_size=data_byte_size,
        )

    stream = BytesIO(stream_bytes)
    padding_bytes = stream_bytes[:data_byte_size][:data_byte_size -
                                                  integer_bit_size // 8]

    if len(stream_bytes) < data_byte_size:
        with pytest.raises(InsufficientDataBytes):
            decoder(stream)
        return
    elif is_non_empty_non_null_byte_string(padding_bytes):
        with pytest.raises(NonEmptyPaddingBytes):
            decoder(stream)
        return
    else:
        decoded_value = decoder(stream)

    _, upper_bound = compute_signed_integer_bounds(high_bit_size +
                                                   low_bit_size)

    unsigned_integer_value = big_endian_to_int(stream_bytes[:data_byte_size])
    if unsigned_integer_value >= upper_bound:
        signed_integer_value = unsigned_integer_value - 2**(high_bit_size +
                                                            low_bit_size)
    else:
        signed_integer_value = unsigned_integer_value

    with decimal.localcontext(abi_decimal_context):
        raw_actual_value = decimal.Decimal(
            signed_integer_value) / 2**low_bit_size

    actual_value = quantize_value(raw_actual_value, low_bit_size)

    assert decoded_value == actual_value
Ejemplo n.º 2
0
def test_encode_signed_integer(integer_value, value_bit_size, data_byte_size):
    if value_bit_size > data_byte_size * 8:
        with pytest.raises(ValueError):
            SignedIntegerEncoder.as_encoder(
                value_bit_size=value_bit_size,
                data_byte_size=data_byte_size,
            )
        return

    encoder = SignedIntegerEncoder.as_encoder(
        value_bit_size=value_bit_size,
        data_byte_size=data_byte_size,
    )

    lower_bound, upper_bound = compute_signed_integer_bounds(value_bit_size)

    if not is_integer(integer_value):
        with pytest.raises(EncodingTypeError) as exception_info:
            encoder(integer_value)
        assert 'SignedInteger' in str(exception_info.value)
        return
    elif integer_value < lower_bound or integer_value > upper_bound:
        with pytest.raises(ValueOutOfBounds):
            encoder(integer_value)
        return

    if integer_value >= 0:
        expected_value = zpad(
            int_to_big_endian(integer_value % 2**value_bit_size),
            data_byte_size
        )
    else:
        expected_value = fpad(
            int_to_big_endian(integer_value % 2**value_bit_size),
            data_byte_size
        )

    encoded_value = encoder(integer_value)

    assert encoded_value == expected_value
def test_encode_signed_integer(integer_value, value_bit_size, data_byte_size):
    if value_bit_size > data_byte_size * 8:
        with pytest.raises(ValueError):
            SignedIntegerEncoder(
                value_bit_size=value_bit_size,
                data_byte_size=data_byte_size,
            )
        return

    encoder = SignedIntegerEncoder(
        value_bit_size=value_bit_size,
        data_byte_size=data_byte_size,
    )

    lower_bound, upper_bound = compute_signed_integer_bounds(value_bit_size)

    if not is_integer(integer_value):
        with pytest.raises(EncodingTypeError) as exception_info:
            encoder(integer_value)
        assert 'SignedInteger' in str(exception_info.value)
        return
    elif integer_value < lower_bound or integer_value > upper_bound:
        with pytest.raises(ValueOutOfBounds):
            encoder(integer_value)
        return

    if integer_value >= 0:
        expected_value = zpad(
            int_to_big_endian(integer_value % 2**value_bit_size),
            data_byte_size
        )
    else:
        expected_value = fpad(
            int_to_big_endian(integer_value % 2**value_bit_size),
            data_byte_size
        )

    encoded_value = encoder(integer_value)

    assert encoded_value == expected_value
Ejemplo n.º 4
0
def test_decode_signed_real(high_bit_size, low_bit_size, integer_bit_size,
                            stream_bytes, data_byte_size):
    if integer_bit_size > data_byte_size * 8:
        with pytest.raises(ValueError):
            SignedRealDecoder(
                value_bit_size=integer_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 != integer_bit_size:
        with pytest.raises(ValueError):
            SignedRealDecoder(
                value_bit_size=integer_bit_size,
                high_bit_size=high_bit_size,
                low_bit_size=low_bit_size,
                data_byte_size=data_byte_size,
            )
        return
    else:
        decoder = SignedRealDecoder(
            value_bit_size=integer_bit_size,
            high_bit_size=high_bit_size,
            low_bit_size=low_bit_size,
            data_byte_size=data_byte_size,
        )

    stream = ContextFramesBytesIO(stream_bytes)

    padding_offset = data_byte_size - integer_bit_size // 8
    data_offset = padding_offset + integer_bit_size // 8

    padding_bytes = stream_bytes[:data_byte_size][:padding_offset]
    data_bytes = stream_bytes[:data_byte_size][padding_offset:data_offset]

    if len(stream_bytes) < data_byte_size:
        with pytest.raises(InsufficientDataBytes):
            decoder(stream)
        return
    elif not is_valid_padding_bytes(padding_bytes, data_bytes):
        with pytest.raises(NonEmptyPaddingBytes):
            decoder(stream)
        return
    else:
        decoded_value = decoder(stream)

    if padding_bytes:
        if decoded_value >= 0:
            assert bytes(set(padding_bytes)) == b'\x00'
        else:
            assert bytes(set(padding_bytes)) == b'\xff'

    _, upper_bound = compute_signed_integer_bounds(high_bit_size +
                                                   low_bit_size)

    unsigned_integer_value = big_endian_to_int(data_bytes)
    if unsigned_integer_value >= upper_bound:
        signed_integer_value = unsigned_integer_value - 2**(high_bit_size +
                                                            low_bit_size)
    else:
        signed_integer_value = unsigned_integer_value

    with decimal.localcontext(abi_decimal_context):
        raw_actual_value = decimal.Decimal(
            signed_integer_value) / 2**low_bit_size

    actual_value = quantize_value(raw_actual_value, low_bit_size)

    assert decoded_value == actual_value