blob: 22e3bc5a8a5270479ab4af64de5005cb6721cba8 [file] [log] [blame]
Alex Nicksaya260b6b2016-10-31 08:24:01 -04001# 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 Nicksaya260b6b2016-10-31 08:24:01 -04006import functools
Alex Nicksaya260b6b2016-10-31 08:24:01 -04007import unittest
8
Alex Nicksay89a5b6e2016-12-20 08:40:47 -05009from . import _test_utils
Alex Nicksaya260b6b2016-10-31 08:24:01 -040010import brotli
11
12
Alex Nicksay1e5ea6a2016-11-09 06:21:13 -050013# Do not inherit from TestCase here to ensure that test methods
Alex Nicksaya260b6b2016-10-31 08:24:01 -040014# are not run automatically and instead are run as part of a specific
15# configuration below.
16class _TestCompressor(object):
17
Alex Nicksay1e5ea6a2016-11-09 06:21:13 -050018 CHUNK_SIZE = 2048
19
20 def _check_decompression(self, test_data):
Alex Nicksaya260b6b2016-10-31 08:24:01 -040021 # Write decompression to temp file and verify it matches the original.
Alex Nicksay1e5ea6a2016-11-09 06:21:13 -050022 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 Nicksaya260b6b2016-10-31 08:24:01 -040027 out_file.write(brotli.decompress(in_file.read()))
Alex Nicksay1e5ea6a2016-11-09 06:21:13 -050028 self.assertFilesMatch(temp_uncompressed, original)
Alex Nicksaya260b6b2016-10-31 08:24:01 -040029
30 def _test_single_process(self, test_data):
31 # Write single-shot compression to temp file.
Alex Nicksay1e5ea6a2016-11-09 06:21:13 -050032 temp_compressed = _test_utils.get_temp_compressed_name(test_data)
33 with open(temp_compressed, 'wb') as out_file:
Alex Nicksaya260b6b2016-10-31 08:24:01 -040034 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 Nicksay1e5ea6a2016-11-09 06:21:13 -050037 self._check_decompression(test_data)
Alex Nicksaya260b6b2016-10-31 08:24:01 -040038
39 def _test_multiple_process(self, test_data):
40 # Write chunked compression to temp file.
Alex Nicksay1e5ea6a2016-11-09 06:21:13 -050041 temp_compressed = _test_utils.get_temp_compressed_name(test_data)
42 with open(temp_compressed, 'wb') as out_file:
Alex Nicksaya260b6b2016-10-31 08:24:01 -040043 with open(test_data, 'rb') as in_file:
Alex Nicksay1e5ea6a2016-11-09 06:21:13 -050044 read_chunk = functools.partial(in_file.read, self.CHUNK_SIZE)
Alex Nicksaya260b6b2016-10-31 08:24:01 -040045 for data in iter(read_chunk, b''):
46 out_file.write(self.compressor.process(data))
47 out_file.write(self.compressor.finish())
Alex Nicksay1e5ea6a2016-11-09 06:21:13 -050048 self._check_decompression(test_data)
Alex Nicksaya260b6b2016-10-31 08:24:01 -040049
50 def _test_multiple_process_and_flush(self, test_data):
51 # Write chunked and flushed compression to temp file.
Alex Nicksay1e5ea6a2016-11-09 06:21:13 -050052 temp_compressed = _test_utils.get_temp_compressed_name(test_data)
53 with open(temp_compressed, 'wb') as out_file:
Alex Nicksaya260b6b2016-10-31 08:24:01 -040054 with open(test_data, 'rb') as in_file:
Alex Nicksay1e5ea6a2016-11-09 06:21:13 -050055 read_chunk = functools.partial(in_file.read, self.CHUNK_SIZE)
Alex Nicksaya260b6b2016-10-31 08:24:01 -040056 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 Nicksay1e5ea6a2016-11-09 06:21:13 -050060 self._check_decompression(test_data)
Alex Nicksaya260b6b2016-10-31 08:24:01 -040061
62
Alex Nicksay1e5ea6a2016-11-09 06:21:13 -050063_test_utils.generate_test_methods(_TestCompressor)
Alex Nicksaya260b6b2016-10-31 08:24:01 -040064
65
Alex Nicksay1e5ea6a2016-11-09 06:21:13 -050066class TestCompressorQuality1(_TestCompressor, _test_utils.TestCase):
Alex Nicksaya260b6b2016-10-31 08:24:01 -040067
68 def setUp(self):
69 self.compressor = brotli.Compressor(quality=1)
70
71
Alex Nicksay1e5ea6a2016-11-09 06:21:13 -050072class TestCompressorQuality6(_TestCompressor, _test_utils.TestCase):
Alex Nicksaya260b6b2016-10-31 08:24:01 -040073
74 def setUp(self):
75 self.compressor = brotli.Compressor(quality=6)
76
77
Alex Nicksay1e5ea6a2016-11-09 06:21:13 -050078class TestCompressorQuality9(_TestCompressor, _test_utils.TestCase):
Alex Nicksaya260b6b2016-10-31 08:24:01 -040079
80 def setUp(self):
81 self.compressor = brotli.Compressor(quality=9)
82
83
Alex Nicksay1e5ea6a2016-11-09 06:21:13 -050084class TestCompressorQuality11(_TestCompressor, _test_utils.TestCase):
Alex Nicksaya260b6b2016-10-31 08:24:01 -040085
86 def setUp(self):
87 self.compressor = brotli.Compressor(quality=11)
88
89
90if __name__ == '__main__':
91 unittest.main()