def test_encode_positive_integer_always_valid(self, integer, prefix_bits): """ So long as the prefix bits are between 1 and 8, any positive integer can be represented. """ result = encode_integer(integer, prefix_bits) assert isinstance(result, bytearray) assert len(result) > 0
def test_encode_decode_round_trips(self, integer, prefix_bits): """ Given valid data, the encoder and decoder can round trip. """ encoded_result = encode_integer(integer, prefix_bits) decoded_integer, consumed = decode_integer(bytes(encoded_result), prefix_bits) assert integer == decoded_integer assert consumed > 0
def test_encode_decode_round_trips(self, integer, prefix_bits): """ Given valid data, the encoder and decoder can round trip. """ encoded_result = encode_integer(integer, prefix_bits) decoded_integer, consumed = decode_integer( bytes(encoded_result), prefix_bits ) assert integer == decoded_integer assert consumed > 0
def test_encoding_42_with_8_bit_prefix(self): val = encode_integer(42, 8) assert len(val) == 1 assert val == bytearray(b'\x2a')
def test_encoding_1337_with_5_bit_prefix(self): val = encode_integer(1337, 5) assert len(val) == 3 assert val == bytearray(b'\x1f\x9a\x0a')
def test_encoding_10_with_5_bit_prefix(self): val = encode_integer(10, 5) assert len(val) == 1 assert val == bytearray(b'\x0a')
def test_encode_fails_for_invalid_prefixes(self, integer, prefix_bits): """ If the prefix is out of the range [1,8], the encoder fails. """ with pytest.raises(ValueError): encode_integer(integer, prefix_bits)
def test_encode_fails_for_negative_integers(self, integer, prefix_bits): """ If the integer to encode is negative, the encoder fails. """ with pytest.raises(ValueError): encode_integer(integer, prefix_bits)
def test_decode_large_integer_small_prefix(self, benchmark): data = bytes(encode_integer(integer=120000, prefix_bits=1)) benchmark(decode_integer, data=data, prefix_bits=1)