def __compress_and_pack(self, data: bytes): lz77_codec = LZ77Codec(256) huffman_codec = HuffmanCodec() checksum = huffman_codec.count_checksum(data) codewords = lz77_codec.encode(data) codewords_bytes = bytearray() for codeword in codewords: codewords_bytes.append(codeword.offset) codewords_bytes.append(codeword.length) codewords_bytes.append(codeword.char) encoded_data, codes_table = \ huffman_codec.encode(bytes(codewords_bytes)) packed_data = self._pack_data(encoded_data, checksum, codes_table) return packed_data
def test_decode(data, expected): lz77_codec = LZ77Codec(len(data)) decoded = lz77_codec.decode(data) assert decoded == expected
def test_decode_length_over_512(): lz77_codec = LZ77Codec(256) data_to_encode = [Codeword(0, 0, 0), Codeword(1, 255, 0), Codeword(2, 255, 0), Codeword(3, 41, 0)] decoded = lz77_codec.decode(data_to_encode) assert decoded == bytes(555)
def test_encode_length_over_512(): lz77_codec = LZ77Codec(256) data = bytes(555) encoded = lz77_codec.encode(data) assert encoded == [Codeword(0, 0, 0), Codeword(1, 255, 0), Codeword(2, 255, 0), Codeword(3, 41, 0)]
def test_encode_length_over_255(): lz77_codec = LZ77Codec(256) data = bytes(261) encoded = lz77_codec.encode(data) assert encoded == [Codeword(0, 0, 0), Codeword(1, 255, 0), Codeword(2, 3, 0)]
def test_encode(data, expected): lz77_codec = LZ77Codec(len(data)) encoded = lz77_codec.encode(data) assert encoded == expected