def test_copy(self): array = [1, 2, 3] Link.copy(array, Link(3, 5).get_byte_interpretation()) self.assertEqual([1, 2, 3, 1, 2, 3, 1, 2], array) Link.copy(array, Link(6, 2).get_byte_interpretation()) self.assertEqual([1, 2, 3, 1, 2, 3, 1, 2, 3, 1], array) Link.copy(array, Link(1, 4).get_byte_interpretation()) self.assertEqual([1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 1, 1, 1, 1], array)
def test_processing_sequence(self): links = {1: Link(1, 4), 7: Link(3, 2)} completed, new_pointer, res = \ zipper.processing_sequence(0, links, b'aaaaabaabcdehgheh') a, b, c, d, e = ord('a'), ord('b'), ord('c'), ord('d'), ord('e') self.assertEqual([72, a, 0, 20, b, a, 0, 50, c, d, e], res) self.assertEqual(12, new_pointer) self.assertTrue(completed) completed = zipper.processing_sequence(0, links, b'aaaaabaab')[0] self.assertFalse(completed) completed = zipper.processing_sequence(8, links, b'abaabasdfghj')[0] self.assertFalse(completed)
def extract_links(string): max_len_phrase = Link.max_len len_buffer = Link.max_offset links = {} buffer = {} pos = 0 while pos < len(string): flag = False i = 1 while string[pos:pos + i:] in buffer and i < max_len_phrase and pos + i < len( string): i += 1 if i - 3 >= 0 and pos - buffer[string[pos:pos + i - 1:]] <= len_buffer: links[pos] = Link(pos - buffer[string[pos:pos + i - 1:]], i - 1) flag = True buffer[string[pos:pos + i - 1:]] = pos buffer[string[pos:pos + i:]] = pos pos += i - 1 if flag else 1 return links
class TestLink(unittest.TestCase): @parameterized.expand([[5, 4], [1, 3], [6, 3]]) def test_creator(self, offset, len_copy): link = Link(offset, len_copy) self.assertEqual(offset, link.offset) self.assertEqual(len_copy, link.len_copy) @parameterized.expand([[0, 39, Link(2, 7)], [0, 16, Link(1, 0)], [2, 15, Link(32, 15)], [255, 255, Link(Link.max_offset, Link.max_len)]]) def test_byte_interpretation(self, first_byte, second_byte, link): self.assertEqual([first_byte, second_byte], link.get_byte_interpretation()) @parameterized.expand([[9, 15, (9 * 16, 15)], [0, 0, (0, 0)], [255, 255, (Link.max_offset, Link.max_len)], [35, 78, (35 * 16 + 4, 14)]]) def test_decode__bytes_as_link(self, first_byte, second_byte, result): self.assertEqual(result, Link.decode_bytes_as_link([first_byte, second_byte])) @parameterized.expand([[Link(25, 10)], [Link(1, 0)], [Link(2341, 14)], [Link(Link.max_offset, Link.max_len)]]) def test_complex_code_decode(self, link): self.assertEqual( (link.offset, link.len_copy), Link.decode_bytes_as_link(link.get_byte_interpretation())) def test_copy(self): array = [1, 2, 3] Link.copy(array, Link(3, 5).get_byte_interpretation()) self.assertEqual([1, 2, 3, 1, 2, 3, 1, 2], array) Link.copy(array, Link(6, 2).get_byte_interpretation()) self.assertEqual([1, 2, 3, 1, 2, 3, 1, 2, 3, 1], array) Link.copy(array, Link(1, 4).get_byte_interpretation()) self.assertEqual([1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 1, 1, 1, 1], array)
def test_creator(self, offset, len_copy): link = Link(offset, len_copy) self.assertEqual(offset, link.offset) self.assertEqual(len_copy, link.len_copy)
def test_coding_string(self): links = {1: Link(1, 4), 7: Link(3, 2)} string = b'aaaaabaabasdfghj' res = zipper.coding_string(string, links) self.assertEqual(bytearray(b'\x48a\x00\x14ba\x00\x32asd'), res[0]) self.assertEqual(string[12::], res[1])