def test_compression_real(self):
        byte = b"\x01\x02\x03"
        byte = byte + byte + byte + byte
        byte = byte + byte + byte + byte
        byte = byte + byte + byte + byte
        byte = byte + byte + byte + byte
        byte = byte + byte + byte + byte
        byte = byte + byte + byte + byte
        byte = byte + byte + byte + byte
        byte = byte + byte + byte + byte
        byte = byte + byte + byte + byte
        byte = byte + byte + byte + byte
        byte = byte + byte + byte + byte
        file_handle = BytesIO(byte)
        output_file_handle = BytesIO()
        output_binary_file_stream = OutputBinaryFileStream(output_file_handle)
        max_bits_size = 10
        compressed_stream = OutputCompressedFileStream(output_binary_file_stream, max_bits_size)
        compressed_stream.compress(file_handle)

        output_binary_file_stream.flush()
        output_file_handle.seek(0)

        input_binary_file_stream = InputBinaryFileStream(output_file_handle, output_binary_file_stream.counter)
        decompresed_file_stream = InputCompressedFileStream(input_binary_file_stream)
        decompresed_file_handle = BytesIO()
        decompresed_file_stream.decompress(decompresed_file_handle, compressed_stream.validator)
        self.assertEquals(decompresed_file_handle.getvalue(), byte)
Beispiel #2
0
def compress(input_file_object, output_file_object, parsed_args):
    input_file_size = os.path.getsize(parsed_args.i)
    output_file_stream = OutputBinaryFileStream(output_file_object)
    output_compressed_stream = OutputCompressedFileStream(output_file_stream, parsed_args.b)
    output_compressed_stream.compress(input_file_object)
    output_file_stream.flush()
    print("Bits per output symbol = {bits:.6f}".format(bits=output_file_stream.sum_written_bits/output_file_stream.num_writen_symbols))
    print("Bits per input symbol = {bits:.6f}".format(bits=output_file_stream.sum_written_bits/input_file_size))
    print("Compression Ratio = {ratio:.6f}".format(ratio=input_file_size/output_file_stream.counter))
    output_file_object.flush()
 def test_can_write_bits_to_stream(self):
     file_handle = BytesIO()
     output_binary_file_stream = OutputBinaryFileStream(file_handle)
     output_binary_file_stream.write((1 << 9) - 1)
     output_binary_file_stream.write((1 << 9) - 1)
     output_binary_file_stream.write((1 << 9) - 1)
     output_binary_file_stream.write((1 << 9) - 1)
     output_binary_file_stream.write((1 << 9) - 1)
     output_binary_file_stream.write((1 << 9) - 1)
     output_binary_file_stream.write((1 << 9) - 1)
     output_binary_file_stream.write((1 << 9) - 1)
    def test_random_data(self):
        for i in range(100):
            bytes = bytearray(os.urandom(10000))
            file_handle = BytesIO(bytes)
            output_file_handle = BytesIO()
            output_binary_file_stream = OutputBinaryFileStream(output_file_handle)
            max_bits_size = random.randint(9, 32)
            compressed_stream = OutputCompressedFileStream(output_binary_file_stream, max_bits_size)
            compressed_stream.compress(file_handle)

            output_binary_file_stream.flush()
            output_file_handle.seek(0)

            input_binary_file_stream = InputBinaryFileStream(output_file_handle, output_binary_file_stream.counter)
            decompresed_file_stream = InputCompressedFileStream(input_binary_file_stream)
            decompresed_file_handle = BytesIO()
            decompresed_file_stream.decompress(decompresed_file_handle, compressed_stream.validator)
            self.assertEquals(decompresed_file_handle.getvalue(), bytes)
    def test_write_read_inc(self):
        file_handle = BytesIO()
        output_binary_file_stream = OutputBinaryFileStream(file_handle)
        for i in range(500):
            output_binary_file_stream.write(i)

        output_binary_file_stream.increase_bit_code_size()

        for i in range(1000):
            output_binary_file_stream.write(i)

        output_binary_file_stream.flush()
        file_handle.seek(0)

        input_binary_file_stream = InputBinaryFileStream(file_handle, output_binary_file_stream.counter)
        for i in range(500):
            self.assertEquals(input_binary_file_stream.read(), i)

        input_binary_file_stream.increase_bit_code_size()

        for i in range(1000):
            self.assertEquals(input_binary_file_stream.read(), i)
 def test_can_create(self):
     file_handle = BytesIO()
     output_binary_file_stream = OutputBinaryFileStream(file_handle)