Пример #1
0
def test_encode_unsigned_integer(integer_value, value_bit_size,
                                 data_byte_size):
    if value_bit_size > data_byte_size * 8:
        with pytest.raises(ValueError) as exception_info:
            UnsignedIntegerEncoder(
                value_bit_size=value_bit_size,
                data_byte_size=data_byte_size,
            )
        return

    encoder = UnsignedIntegerEncoder(
        value_bit_size=value_bit_size,
        data_byte_size=data_byte_size,
    )
    lower_bound, upper_bound = compute_unsigned_integer_bounds(value_bit_size)

    if not is_integer(integer_value):
        with pytest.raises(EncodingTypeError) as exception_info:
            encoder(integer_value)
        assert 'UnsignedInteger' 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), data_byte_size)
    else:
        expected_value = fpad(int_to_big_endian(integer_value), data_byte_size)

    encoded_value = encoder(integer_value)

    assert encoded_value == expected_value
Пример #2
0
def test_multi_encoder():
    encoder = MultiEncoder.as_encoder(encoders=(
        UnsignedIntegerEncoder.as_encoder(value_bit_size=256),
        StringEncoder.as_encoder(),
    ))
    expected = decode_hex('0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000')
    actual = encoder((0, b''))
    assert actual == expected
Пример #3
0
def test_tuple_encoder():
    encoder = TupleEncoder(encoders=(
        UnsignedIntegerEncoder(value_bit_size=256),
        ByteStringEncoder(),
    ))
    expected = decode_hex(
        '0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000'
    )  # noqa: E501
    actual = encoder((0, b''))
    assert actual == expected