Ejemplo n.º 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
Ejemplo n.º 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
Ejemplo n.º 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
Ejemplo n.º 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
Ejemplo n.º 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')
Ejemplo n.º 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')
Ejemplo n.º 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')
Ejemplo n.º 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)
Ejemplo n.º 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)
Ejemplo n.º 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)
Ejemplo n.º 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)
Ejemplo n.º 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')
Ejemplo n.º 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')
Ejemplo n.º 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')
Ejemplo n.º 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)
Ejemplo n.º 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)