aboutsummaryrefslogtreecommitdiff
path: root/python/tests/compress_test.py
blob: 62a07a1c75322858bd0f449d2e26384129674509 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# Copyright 2016 The Brotli Authors. All rights reserved.
#
# Distributed under MIT license.
# See file LICENSE for detail or copy at https://opensource.org/licenses/MIT

import unittest

import _test_utils
import brotli


class TestCompress(_test_utils.TestCase):

    def _check_decompression(self, test_data, **kwargs):
        # Write decompression to temp file and verify it matches the original.
        temp_uncompressed = _test_utils.get_temp_uncompressed_name(test_data)
        temp_compressed = _test_utils.get_temp_compressed_name(test_data)
        original = test_data
        with open(temp_uncompressed, 'wb') as out_file:
            with open(temp_compressed, 'rb') as in_file:
                out_file.write(brotli.decompress(in_file.read(), **kwargs))
        self.assertFilesMatch(temp_uncompressed, original)

    def _compress(self, test_data, **kwargs):
        temp_compressed = _test_utils.get_temp_compressed_name(test_data)
        with open(temp_compressed, 'wb') as out_file:
            with open(test_data, 'rb') as in_file:
                out_file.write(brotli.compress(in_file.read(), **kwargs))

    def _test_compress_quality_1(self, test_data):
        self._compress(test_data, quality=1)
        self._check_decompression(test_data)

    def _test_compress_quality_6(self, test_data):
        self._compress(test_data, quality=6)
        self._check_decompression(test_data)

    def _test_compress_quality_9(self, test_data):
        self._compress(test_data, quality=9)
        self._check_decompression(test_data)

    def _test_compress_quality_11(self, test_data):
        self._compress(test_data, quality=11)
        self._check_decompression(test_data)

    def _test_compress_quality_1_lgwin_10(self, test_data):
        self._compress(test_data, quality=1, lgwin=10)
        self._check_decompression(test_data)

    def _test_compress_quality_6_lgwin_15(self, test_data):
        self._compress(test_data, quality=6, lgwin=15)
        self._check_decompression(test_data)

    def _test_compress_quality_9_lgwin_20(self, test_data):
        self._compress(test_data, quality=9, lgwin=20)
        self._check_decompression(test_data)

    def _test_compress_quality_11_lgwin_24(self, test_data):
        self._compress(test_data, quality=11, lgwin=24)
        self._check_decompression(test_data)

    def _test_compress_quality_1_custom_dictionary(self, test_data):
        with open(test_data, 'rb') as in_file:
            dictionary = in_file.read()
        self._compress(test_data, quality=1, dictionary=dictionary)
        self._check_decompression(test_data, dictionary=dictionary)

    def _test_compress_quality_6_custom_dictionary(self, test_data):
        with open(test_data, 'rb') as in_file:
            dictionary = in_file.read()
        self._compress(test_data, quality=6, dictionary=dictionary)
        self._check_decompression(test_data, dictionary=dictionary)

    def _test_compress_quality_9_custom_dictionary(self, test_data):
        with open(test_data, 'rb') as in_file:
            dictionary = in_file.read()
        self._compress(test_data, quality=9, dictionary=dictionary)
        self._check_decompression(test_data, dictionary=dictionary)

    def _test_compress_quality_11_custom_dictionary(self, test_data):
        with open(test_data, 'rb') as in_file:
            dictionary = in_file.read()
        self._compress(test_data, quality=11, dictionary=dictionary)
        self._check_decompression(test_data, dictionary=dictionary)

    def _test_compress_quality_1_lgwin_10_custom_dictionary(self, test_data):
        with open(test_data, 'rb') as in_file:
            dictionary = in_file.read()
        self._compress(test_data, quality=1, lgwin=10, dictionary=dictionary)
        self._check_decompression(test_data, dictionary=dictionary)

    def _test_compress_quality_6_lgwin_15_custom_dictionary(self, test_data):
        with open(test_data, 'rb') as in_file:
            dictionary = in_file.read()
        self._compress(test_data, quality=6, lgwin=15, dictionary=dictionary)
        self._check_decompression(test_data, dictionary=dictionary)

    def _test_compress_quality_9_lgwin_20_custom_dictionary(self, test_data):
        with open(test_data, 'rb') as in_file:
            dictionary = in_file.read()
        self._compress(test_data, quality=9, lgwin=20, dictionary=dictionary)
        self._check_decompression(test_data, dictionary=dictionary)

    def _test_compress_quality_11_lgwin_24_custom_dictionary(self, test_data):
        with open(test_data, 'rb') as in_file:
            dictionary = in_file.read()
        self._compress(test_data, quality=11, lgwin=24, dictionary=dictionary)
        self._check_decompression(test_data, dictionary=dictionary)


_test_utils.generate_test_methods(TestCompress)

if __name__ == '__main__':
    unittest.main()