def test_signed_cases(self): test_cases = ( (-64, [0x40]), (2, [0x02]), (-2, [0x7E]), (127, [0xFF, 0x00]), (-127, [0x81, 0x7F]), (128, [0x80, 0x01]), (-128, [0x80, 0x7F]), (129, [0x81, 0x01]), (-129, [0xFF, 0x7E]), ) for value, data in test_cases: expected_data = bytes(data) encoded_data = signed_leb128_encode(value) self.assertEqual(expected_data, encoded_data) decoded_value = signed_leb128_decode(iter(expected_data)) self.assertEqual(value, decoded_value)
def test_signed_range(self): for x in range(-1000, 1000): data = signed_leb128_encode(x) y = signed_leb128_decode(iter(data)) self.assertEqual(x, y)
def test_signed_wiki_example(self): """ Test wikipedia example """ data = signed_leb128_encode(-624485) self.assertEqual(bytes([0x9B, 0xF1, 0x59]), data) self.assertEqual(-624485, signed_leb128_decode(iter(data)))