def unzip_string(string, context): result = context.copy() flags = _get_list_flags(string) last_ = len(string) < 1024 * 1024 if last_: max_ind = len(flags) else: max_ind = len(flags) - 1 for i in range(max_ind): f = flags[i] flag = _get_flag(string[f]) pointer = f + 1 for j in flag: if pointer >= len(string): break if j: Link.copy(result, [string[pointer], string[pointer + 1]]) pointer += 2 else: result.append(string[pointer]) pointer += 1 if last_: return result[len(context)::], b'', b'' if len(result) >= Link.max_offset: next_context = result[-Link.max_offset::] else: next_context = result next_residue_string = string[flags[-1]::] return result[len(context)::], next_residue_string, next_context
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_complex_code_decode(self, link): self.assertEqual( (link.offset, link.len_copy), Link.decode_bytes_as_link(link.get_byte_interpretation()))
def test_decode__bytes_as_link(self, first_byte, second_byte, result): self.assertEqual(result, Link.decode_bytes_as_link([first_byte, second_byte]))
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])