xref: /aosp_15_r20/external/brotli/python/tests/compressor_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 functools
7*f4ee7fbaSAndroid Build Coastguard Workerimport unittest
8*f4ee7fbaSAndroid Build Coastguard Worker
9*f4ee7fbaSAndroid Build Coastguard Workerfrom . import _test_utils
10*f4ee7fbaSAndroid Build Coastguard Workerimport brotli
11*f4ee7fbaSAndroid Build Coastguard Worker
12*f4ee7fbaSAndroid Build Coastguard Worker
13*f4ee7fbaSAndroid Build Coastguard Worker# Do not inherit from TestCase here to ensure that test methods
14*f4ee7fbaSAndroid Build Coastguard Worker# are not run automatically and instead are run as part of a specific
15*f4ee7fbaSAndroid Build Coastguard Worker# configuration below.
16*f4ee7fbaSAndroid Build Coastguard Workerclass _TestCompressor(object):
17*f4ee7fbaSAndroid Build Coastguard Worker
18*f4ee7fbaSAndroid Build Coastguard Worker    CHUNK_SIZE = 2048
19*f4ee7fbaSAndroid Build Coastguard Worker
20*f4ee7fbaSAndroid Build Coastguard Worker    def tearDown(self):
21*f4ee7fbaSAndroid Build Coastguard Worker        self.compressor = None
22*f4ee7fbaSAndroid Build Coastguard Worker
23*f4ee7fbaSAndroid Build Coastguard Worker    def _check_decompression(self, test_data):
24*f4ee7fbaSAndroid Build Coastguard Worker        # Write decompression to temp file and verify it matches the original.
25*f4ee7fbaSAndroid Build Coastguard Worker        temp_uncompressed = _test_utils.get_temp_uncompressed_name(test_data)
26*f4ee7fbaSAndroid Build Coastguard Worker        temp_compressed = _test_utils.get_temp_compressed_name(test_data)
27*f4ee7fbaSAndroid Build Coastguard Worker        original = test_data
28*f4ee7fbaSAndroid Build Coastguard Worker        with open(temp_uncompressed, 'wb') as out_file:
29*f4ee7fbaSAndroid Build Coastguard Worker            with open(temp_compressed, 'rb') as in_file:
30*f4ee7fbaSAndroid Build Coastguard Worker                out_file.write(brotli.decompress(in_file.read()))
31*f4ee7fbaSAndroid Build Coastguard Worker        self.assertFilesMatch(temp_uncompressed, original)
32*f4ee7fbaSAndroid Build Coastguard Worker
33*f4ee7fbaSAndroid Build Coastguard Worker    def _test_single_process(self, test_data):
34*f4ee7fbaSAndroid Build Coastguard Worker        # Write single-shot compression to temp file.
35*f4ee7fbaSAndroid Build Coastguard Worker        temp_compressed = _test_utils.get_temp_compressed_name(test_data)
36*f4ee7fbaSAndroid Build Coastguard Worker        with open(temp_compressed, 'wb') as out_file:
37*f4ee7fbaSAndroid Build Coastguard Worker            with open(test_data, 'rb') as in_file:
38*f4ee7fbaSAndroid Build Coastguard Worker                out_file.write(self.compressor.process(in_file.read()))
39*f4ee7fbaSAndroid Build Coastguard Worker            out_file.write(self.compressor.finish())
40*f4ee7fbaSAndroid Build Coastguard Worker        self._check_decompression(test_data)
41*f4ee7fbaSAndroid Build Coastguard Worker
42*f4ee7fbaSAndroid Build Coastguard Worker    def _test_multiple_process(self, test_data):
43*f4ee7fbaSAndroid Build Coastguard Worker        # Write chunked compression to temp file.
44*f4ee7fbaSAndroid Build Coastguard Worker        temp_compressed = _test_utils.get_temp_compressed_name(test_data)
45*f4ee7fbaSAndroid Build Coastguard Worker        with open(temp_compressed, 'wb') as out_file:
46*f4ee7fbaSAndroid Build Coastguard Worker            with open(test_data, 'rb') as in_file:
47*f4ee7fbaSAndroid Build Coastguard Worker                read_chunk = functools.partial(in_file.read, self.CHUNK_SIZE)
48*f4ee7fbaSAndroid Build Coastguard Worker                for data in iter(read_chunk, b''):
49*f4ee7fbaSAndroid Build Coastguard Worker                    out_file.write(self.compressor.process(data))
50*f4ee7fbaSAndroid Build Coastguard Worker            out_file.write(self.compressor.finish())
51*f4ee7fbaSAndroid Build Coastguard Worker        self._check_decompression(test_data)
52*f4ee7fbaSAndroid Build Coastguard Worker
53*f4ee7fbaSAndroid Build Coastguard Worker    def _test_multiple_process_and_flush(self, test_data):
54*f4ee7fbaSAndroid Build Coastguard Worker        # Write chunked and flushed compression to temp file.
55*f4ee7fbaSAndroid Build Coastguard Worker        temp_compressed = _test_utils.get_temp_compressed_name(test_data)
56*f4ee7fbaSAndroid Build Coastguard Worker        with open(temp_compressed, 'wb') as out_file:
57*f4ee7fbaSAndroid Build Coastguard Worker            with open(test_data, 'rb') as in_file:
58*f4ee7fbaSAndroid Build Coastguard Worker                read_chunk = functools.partial(in_file.read, self.CHUNK_SIZE)
59*f4ee7fbaSAndroid Build Coastguard Worker                for data in iter(read_chunk, b''):
60*f4ee7fbaSAndroid Build Coastguard Worker                    out_file.write(self.compressor.process(data))
61*f4ee7fbaSAndroid Build Coastguard Worker                    out_file.write(self.compressor.flush())
62*f4ee7fbaSAndroid Build Coastguard Worker            out_file.write(self.compressor.finish())
63*f4ee7fbaSAndroid Build Coastguard Worker        self._check_decompression(test_data)
64*f4ee7fbaSAndroid Build Coastguard Worker
65*f4ee7fbaSAndroid Build Coastguard Worker
66*f4ee7fbaSAndroid Build Coastguard Worker_test_utils.generate_test_methods(_TestCompressor)
67*f4ee7fbaSAndroid Build Coastguard Worker
68*f4ee7fbaSAndroid Build Coastguard Worker
69*f4ee7fbaSAndroid Build Coastguard Workerclass TestCompressorQuality1(_TestCompressor, _test_utils.TestCase):
70*f4ee7fbaSAndroid Build Coastguard Worker
71*f4ee7fbaSAndroid Build Coastguard Worker    def setUp(self):
72*f4ee7fbaSAndroid Build Coastguard Worker        self.compressor = brotli.Compressor(quality=1)
73*f4ee7fbaSAndroid Build Coastguard Worker
74*f4ee7fbaSAndroid Build Coastguard Worker
75*f4ee7fbaSAndroid Build Coastguard Workerclass TestCompressorQuality6(_TestCompressor, _test_utils.TestCase):
76*f4ee7fbaSAndroid Build Coastguard Worker
77*f4ee7fbaSAndroid Build Coastguard Worker    def setUp(self):
78*f4ee7fbaSAndroid Build Coastguard Worker        self.compressor = brotli.Compressor(quality=6)
79*f4ee7fbaSAndroid Build Coastguard Worker
80*f4ee7fbaSAndroid Build Coastguard Worker
81*f4ee7fbaSAndroid Build Coastguard Workerclass TestCompressorQuality9(_TestCompressor, _test_utils.TestCase):
82*f4ee7fbaSAndroid Build Coastguard Worker
83*f4ee7fbaSAndroid Build Coastguard Worker    def setUp(self):
84*f4ee7fbaSAndroid Build Coastguard Worker        self.compressor = brotli.Compressor(quality=9)
85*f4ee7fbaSAndroid Build Coastguard Worker
86*f4ee7fbaSAndroid Build Coastguard Worker
87*f4ee7fbaSAndroid Build Coastguard Workerclass TestCompressorQuality11(_TestCompressor, _test_utils.TestCase):
88*f4ee7fbaSAndroid Build Coastguard Worker
89*f4ee7fbaSAndroid Build Coastguard Worker    def setUp(self):
90*f4ee7fbaSAndroid Build Coastguard Worker        self.compressor = brotli.Compressor(quality=11)
91*f4ee7fbaSAndroid Build Coastguard Worker
92*f4ee7fbaSAndroid Build Coastguard Worker
93*f4ee7fbaSAndroid Build Coastguard Workerif __name__ == '__main__':
94*f4ee7fbaSAndroid Build Coastguard Worker    unittest.main()
95