126fa459cSmrg# Copyright 2016 The Brotli Authors. All rights reserved. 226fa459cSmrg# 326fa459cSmrg# Distributed under MIT license. 426fa459cSmrg# See file LICENSE for detail or copy at https://opensource.org/licenses/MIT 526fa459cSmrg 626fa459cSmrgimport functools 726fa459cSmrgimport unittest 826fa459cSmrg 926fa459cSmrgfrom . import _test_utils 1026fa459cSmrgimport brotli 1126fa459cSmrg 1226fa459cSmrg 1326fa459cSmrg# Do not inherit from TestCase here to ensure that test methods 1426fa459cSmrg# are not run automatically and instead are run as part of a specific 1526fa459cSmrg# configuration below. 1626fa459cSmrgclass _TestCompressor(object): 1726fa459cSmrg 1826fa459cSmrg CHUNK_SIZE = 2048 1926fa459cSmrg 2026fa459cSmrg def tearDown(self): 2126fa459cSmrg self.compressor = None 2226fa459cSmrg 2326fa459cSmrg def _check_decompression(self, test_data): 2426fa459cSmrg # Write decompression to temp file and verify it matches the original. 2526fa459cSmrg temp_uncompressed = _test_utils.get_temp_uncompressed_name(test_data) 2626fa459cSmrg temp_compressed = _test_utils.get_temp_compressed_name(test_data) 2726fa459cSmrg original = test_data 2826fa459cSmrg with open(temp_uncompressed, 'wb') as out_file: 2926fa459cSmrg with open(temp_compressed, 'rb') as in_file: 3026fa459cSmrg out_file.write(brotli.decompress(in_file.read())) 3126fa459cSmrg self.assertFilesMatch(temp_uncompressed, original) 3226fa459cSmrg 3326fa459cSmrg def _test_single_process(self, test_data): 3426fa459cSmrg # Write single-shot compression to temp file. 3526fa459cSmrg temp_compressed = _test_utils.get_temp_compressed_name(test_data) 3626fa459cSmrg with open(temp_compressed, 'wb') as out_file: 3726fa459cSmrg with open(test_data, 'rb') as in_file: 3826fa459cSmrg out_file.write(self.compressor.process(in_file.read())) 3926fa459cSmrg out_file.write(self.compressor.finish()) 4026fa459cSmrg self._check_decompression(test_data) 4126fa459cSmrg 4226fa459cSmrg def _test_multiple_process(self, test_data): 4326fa459cSmrg # Write chunked compression to temp file. 4426fa459cSmrg temp_compressed = _test_utils.get_temp_compressed_name(test_data) 4526fa459cSmrg with open(temp_compressed, 'wb') as out_file: 4626fa459cSmrg with open(test_data, 'rb') as in_file: 4726fa459cSmrg read_chunk = functools.partial(in_file.read, self.CHUNK_SIZE) 4826fa459cSmrg for data in iter(read_chunk, b''): 4926fa459cSmrg out_file.write(self.compressor.process(data)) 5026fa459cSmrg out_file.write(self.compressor.finish()) 5126fa459cSmrg self._check_decompression(test_data) 5226fa459cSmrg 5326fa459cSmrg def _test_multiple_process_and_flush(self, test_data): 5426fa459cSmrg # Write chunked and flushed compression to temp file. 5526fa459cSmrg temp_compressed = _test_utils.get_temp_compressed_name(test_data) 5626fa459cSmrg with open(temp_compressed, 'wb') as out_file: 5726fa459cSmrg with open(test_data, 'rb') as in_file: 5826fa459cSmrg read_chunk = functools.partial(in_file.read, self.CHUNK_SIZE) 5926fa459cSmrg for data in iter(read_chunk, b''): 6026fa459cSmrg out_file.write(self.compressor.process(data)) 6126fa459cSmrg out_file.write(self.compressor.flush()) 6226fa459cSmrg out_file.write(self.compressor.finish()) 6326fa459cSmrg self._check_decompression(test_data) 6426fa459cSmrg 6526fa459cSmrg 6626fa459cSmrg_test_utils.generate_test_methods(_TestCompressor) 6726fa459cSmrg 6826fa459cSmrg 6926fa459cSmrgclass TestCompressorQuality1(_TestCompressor, _test_utils.TestCase): 7026fa459cSmrg 7126fa459cSmrg def setUp(self): 7226fa459cSmrg self.compressor = brotli.Compressor(quality=1) 7326fa459cSmrg 7426fa459cSmrg 7526fa459cSmrgclass TestCompressorQuality6(_TestCompressor, _test_utils.TestCase): 7626fa459cSmrg 7726fa459cSmrg def setUp(self): 7826fa459cSmrg self.compressor = brotli.Compressor(quality=6) 7926fa459cSmrg 8026fa459cSmrg 8126fa459cSmrgclass TestCompressorQuality9(_TestCompressor, _test_utils.TestCase): 8226fa459cSmrg 8326fa459cSmrg def setUp(self): 8426fa459cSmrg self.compressor = brotli.Compressor(quality=9) 8526fa459cSmrg 8626fa459cSmrg 8726fa459cSmrgclass TestCompressorQuality11(_TestCompressor, _test_utils.TestCase): 8826fa459cSmrg 8926fa459cSmrg def setUp(self): 9026fa459cSmrg self.compressor = brotli.Compressor(quality=11) 9126fa459cSmrg 9226fa459cSmrg 9326fa459cSmrgif __name__ == '__main__': 9426fa459cSmrg unittest.main() 95