Alex Nicksay | a260b6b | 2016-10-31 08:24:01 -0400 | [diff] [blame] | 1 | # Copyright 2016 The Brotli Authors. All rights reserved. |
| 2 | # |
| 3 | # Distributed under MIT license. |
| 4 | # See file LICENSE for detail or copy at https://opensource.org/licenses/MIT |
| 5 | |
Alex Nicksay | a260b6b | 2016-10-31 08:24:01 -0400 | [diff] [blame] | 6 | import functools |
Alex Nicksay | a260b6b | 2016-10-31 08:24:01 -0400 | [diff] [blame] | 7 | import unittest |
| 8 | |
Alex Nicksay | 89a5b6e | 2016-12-20 08:40:47 -0500 | [diff] [blame^] | 9 | from . import _test_utils |
Alex Nicksay | a260b6b | 2016-10-31 08:24:01 -0400 | [diff] [blame] | 10 | import brotli |
| 11 | |
| 12 | |
Alex Nicksay | 1e5ea6a | 2016-11-09 06:21:13 -0500 | [diff] [blame] | 13 | # Do not inherit from TestCase here to ensure that test methods |
Alex Nicksay | a260b6b | 2016-10-31 08:24:01 -0400 | [diff] [blame] | 14 | # are not run automatically and instead are run as part of a specific |
| 15 | # configuration below. |
| 16 | class _TestCompressor(object): |
| 17 | |
Alex Nicksay | 1e5ea6a | 2016-11-09 06:21:13 -0500 | [diff] [blame] | 18 | CHUNK_SIZE = 2048 |
| 19 | |
| 20 | def _check_decompression(self, test_data): |
Alex Nicksay | a260b6b | 2016-10-31 08:24:01 -0400 | [diff] [blame] | 21 | # Write decompression to temp file and verify it matches the original. |
Alex Nicksay | 1e5ea6a | 2016-11-09 06:21:13 -0500 | [diff] [blame] | 22 | temp_uncompressed = _test_utils.get_temp_uncompressed_name(test_data) |
| 23 | temp_compressed = _test_utils.get_temp_compressed_name(test_data) |
| 24 | original = test_data |
| 25 | with open(temp_uncompressed, 'wb') as out_file: |
| 26 | with open(temp_compressed, 'rb') as in_file: |
Alex Nicksay | a260b6b | 2016-10-31 08:24:01 -0400 | [diff] [blame] | 27 | out_file.write(brotli.decompress(in_file.read())) |
Alex Nicksay | 1e5ea6a | 2016-11-09 06:21:13 -0500 | [diff] [blame] | 28 | self.assertFilesMatch(temp_uncompressed, original) |
Alex Nicksay | a260b6b | 2016-10-31 08:24:01 -0400 | [diff] [blame] | 29 | |
| 30 | def _test_single_process(self, test_data): |
| 31 | # Write single-shot compression to temp file. |
Alex Nicksay | 1e5ea6a | 2016-11-09 06:21:13 -0500 | [diff] [blame] | 32 | temp_compressed = _test_utils.get_temp_compressed_name(test_data) |
| 33 | with open(temp_compressed, 'wb') as out_file: |
Alex Nicksay | a260b6b | 2016-10-31 08:24:01 -0400 | [diff] [blame] | 34 | with open(test_data, 'rb') as in_file: |
| 35 | out_file.write(self.compressor.process(in_file.read())) |
| 36 | out_file.write(self.compressor.finish()) |
Alex Nicksay | 1e5ea6a | 2016-11-09 06:21:13 -0500 | [diff] [blame] | 37 | self._check_decompression(test_data) |
Alex Nicksay | a260b6b | 2016-10-31 08:24:01 -0400 | [diff] [blame] | 38 | |
| 39 | def _test_multiple_process(self, test_data): |
| 40 | # Write chunked compression to temp file. |
Alex Nicksay | 1e5ea6a | 2016-11-09 06:21:13 -0500 | [diff] [blame] | 41 | temp_compressed = _test_utils.get_temp_compressed_name(test_data) |
| 42 | with open(temp_compressed, 'wb') as out_file: |
Alex Nicksay | a260b6b | 2016-10-31 08:24:01 -0400 | [diff] [blame] | 43 | with open(test_data, 'rb') as in_file: |
Alex Nicksay | 1e5ea6a | 2016-11-09 06:21:13 -0500 | [diff] [blame] | 44 | read_chunk = functools.partial(in_file.read, self.CHUNK_SIZE) |
Alex Nicksay | a260b6b | 2016-10-31 08:24:01 -0400 | [diff] [blame] | 45 | for data in iter(read_chunk, b''): |
| 46 | out_file.write(self.compressor.process(data)) |
| 47 | out_file.write(self.compressor.finish()) |
Alex Nicksay | 1e5ea6a | 2016-11-09 06:21:13 -0500 | [diff] [blame] | 48 | self._check_decompression(test_data) |
Alex Nicksay | a260b6b | 2016-10-31 08:24:01 -0400 | [diff] [blame] | 49 | |
| 50 | def _test_multiple_process_and_flush(self, test_data): |
| 51 | # Write chunked and flushed compression to temp file. |
Alex Nicksay | 1e5ea6a | 2016-11-09 06:21:13 -0500 | [diff] [blame] | 52 | temp_compressed = _test_utils.get_temp_compressed_name(test_data) |
| 53 | with open(temp_compressed, 'wb') as out_file: |
Alex Nicksay | a260b6b | 2016-10-31 08:24:01 -0400 | [diff] [blame] | 54 | with open(test_data, 'rb') as in_file: |
Alex Nicksay | 1e5ea6a | 2016-11-09 06:21:13 -0500 | [diff] [blame] | 55 | read_chunk = functools.partial(in_file.read, self.CHUNK_SIZE) |
Alex Nicksay | a260b6b | 2016-10-31 08:24:01 -0400 | [diff] [blame] | 56 | for data in iter(read_chunk, b''): |
| 57 | out_file.write(self.compressor.process(data)) |
| 58 | out_file.write(self.compressor.flush()) |
| 59 | out_file.write(self.compressor.finish()) |
Alex Nicksay | 1e5ea6a | 2016-11-09 06:21:13 -0500 | [diff] [blame] | 60 | self._check_decompression(test_data) |
Alex Nicksay | a260b6b | 2016-10-31 08:24:01 -0400 | [diff] [blame] | 61 | |
| 62 | |
Alex Nicksay | 1e5ea6a | 2016-11-09 06:21:13 -0500 | [diff] [blame] | 63 | _test_utils.generate_test_methods(_TestCompressor) |
Alex Nicksay | a260b6b | 2016-10-31 08:24:01 -0400 | [diff] [blame] | 64 | |
| 65 | |
Alex Nicksay | 1e5ea6a | 2016-11-09 06:21:13 -0500 | [diff] [blame] | 66 | class TestCompressorQuality1(_TestCompressor, _test_utils.TestCase): |
Alex Nicksay | a260b6b | 2016-10-31 08:24:01 -0400 | [diff] [blame] | 67 | |
| 68 | def setUp(self): |
| 69 | self.compressor = brotli.Compressor(quality=1) |
| 70 | |
| 71 | |
Alex Nicksay | 1e5ea6a | 2016-11-09 06:21:13 -0500 | [diff] [blame] | 72 | class TestCompressorQuality6(_TestCompressor, _test_utils.TestCase): |
Alex Nicksay | a260b6b | 2016-10-31 08:24:01 -0400 | [diff] [blame] | 73 | |
| 74 | def setUp(self): |
| 75 | self.compressor = brotli.Compressor(quality=6) |
| 76 | |
| 77 | |
Alex Nicksay | 1e5ea6a | 2016-11-09 06:21:13 -0500 | [diff] [blame] | 78 | class TestCompressorQuality9(_TestCompressor, _test_utils.TestCase): |
Alex Nicksay | a260b6b | 2016-10-31 08:24:01 -0400 | [diff] [blame] | 79 | |
| 80 | def setUp(self): |
| 81 | self.compressor = brotli.Compressor(quality=9) |
| 82 | |
| 83 | |
Alex Nicksay | 1e5ea6a | 2016-11-09 06:21:13 -0500 | [diff] [blame] | 84 | class TestCompressorQuality11(_TestCompressor, _test_utils.TestCase): |
Alex Nicksay | a260b6b | 2016-10-31 08:24:01 -0400 | [diff] [blame] | 85 | |
| 86 | def setUp(self): |
| 87 | self.compressor = brotli.Compressor(quality=11) |
| 88 | |
| 89 | |
| 90 | if __name__ == '__main__': |
| 91 | unittest.main() |