示例#1
0
 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
示例#2
0
 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
示例#3
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
示例#4
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
示例#5
0
 def test_encoding_42_with_8_bit_prefix(self):
     val = encode_integer(42, 8)
     assert len(val) == 1
     assert val == bytearray(b'\x2a')
示例#6
0
 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')
示例#7
0
 def test_encoding_10_with_5_bit_prefix(self):
     val = encode_integer(10, 5)
     assert len(val) == 1
     assert val == bytearray(b'\x0a')
示例#8
0
 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)
示例#9
0
 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)
示例#10
0
 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)
示例#11
0
 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)
示例#12
0
 def test_encoding_42_with_8_bit_prefix(self):
     val = encode_integer(42, 8)
     assert len(val) == 1
     assert val == bytearray(b'\x2a')
示例#13
0
 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')
示例#14
0
 def test_encoding_10_with_5_bit_prefix(self):
     val = encode_integer(10, 5)
     assert len(val) == 1
     assert val == bytearray(b'\x0a')
示例#15
0
 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)
示例#16
0
 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)