xref: /aosp_15_r20/external/brotli/python/tests/compress_test.py (revision f4ee7fba7774faf2a30f13154332c0a06550dbc4)
1*f4ee7fbaSAndroid Build Coastguard Worker# Copyright 2016 The Brotli Authors. All rights reserved.
2*f4ee7fbaSAndroid Build Coastguard Worker#
3*f4ee7fbaSAndroid Build Coastguard Worker# Distributed under MIT license.
4*f4ee7fbaSAndroid Build Coastguard Worker# See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
5*f4ee7fbaSAndroid Build Coastguard Worker
6*f4ee7fbaSAndroid Build Coastguard Workerimport unittest
7*f4ee7fbaSAndroid Build Coastguard Worker
8*f4ee7fbaSAndroid Build Coastguard Workerfrom . import _test_utils
9*f4ee7fbaSAndroid Build Coastguard Workerimport brotli
10*f4ee7fbaSAndroid Build Coastguard Worker
11*f4ee7fbaSAndroid Build Coastguard Worker
12*f4ee7fbaSAndroid Build Coastguard Workerclass TestCompress(_test_utils.TestCase):
13*f4ee7fbaSAndroid Build Coastguard Worker
14*f4ee7fbaSAndroid Build Coastguard Worker    VARIANTS = {'quality': (1, 6, 9, 11), 'lgwin': (10, 15, 20, 24)}
15*f4ee7fbaSAndroid Build Coastguard Worker
16*f4ee7fbaSAndroid Build Coastguard Worker    def _check_decompression(self, test_data, **kwargs):
17*f4ee7fbaSAndroid Build Coastguard Worker        kwargs = {}
18*f4ee7fbaSAndroid Build Coastguard Worker        # Write decompression to temp file and verify it matches the original.
19*f4ee7fbaSAndroid Build Coastguard Worker        temp_uncompressed = _test_utils.get_temp_uncompressed_name(test_data)
20*f4ee7fbaSAndroid Build Coastguard Worker        temp_compressed = _test_utils.get_temp_compressed_name(test_data)
21*f4ee7fbaSAndroid Build Coastguard Worker        original = test_data
22*f4ee7fbaSAndroid Build Coastguard Worker        with open(temp_uncompressed, 'wb') as out_file:
23*f4ee7fbaSAndroid Build Coastguard Worker            with open(temp_compressed, 'rb') as in_file:
24*f4ee7fbaSAndroid Build Coastguard Worker                out_file.write(brotli.decompress(in_file.read(), **kwargs))
25*f4ee7fbaSAndroid Build Coastguard Worker        self.assertFilesMatch(temp_uncompressed, original)
26*f4ee7fbaSAndroid Build Coastguard Worker
27*f4ee7fbaSAndroid Build Coastguard Worker    def _compress(self, test_data, **kwargs):
28*f4ee7fbaSAndroid Build Coastguard Worker        temp_compressed = _test_utils.get_temp_compressed_name(test_data)
29*f4ee7fbaSAndroid Build Coastguard Worker        with open(temp_compressed, 'wb') as out_file:
30*f4ee7fbaSAndroid Build Coastguard Worker            with open(test_data, 'rb') as in_file:
31*f4ee7fbaSAndroid Build Coastguard Worker                out_file.write(brotli.compress(in_file.read(), **kwargs))
32*f4ee7fbaSAndroid Build Coastguard Worker
33*f4ee7fbaSAndroid Build Coastguard Worker    def _test_compress(self, test_data, **kwargs):
34*f4ee7fbaSAndroid Build Coastguard Worker        self._compress(test_data, **kwargs)
35*f4ee7fbaSAndroid Build Coastguard Worker        self._check_decompression(test_data, **kwargs)
36*f4ee7fbaSAndroid Build Coastguard Worker
37*f4ee7fbaSAndroid Build Coastguard Worker
38*f4ee7fbaSAndroid Build Coastguard Worker_test_utils.generate_test_methods(TestCompress, variants=TestCompress.VARIANTS)
39*f4ee7fbaSAndroid Build Coastguard Worker
40*f4ee7fbaSAndroid Build Coastguard Workerif __name__ == '__main__':
41*f4ee7fbaSAndroid Build Coastguard Worker    unittest.main()
42