Mercurial > public > mercurial-scm > hg
comparison contrib/python-zstandard/tests/test_data_structures.py @ 31796:e0dc40530c5a
zstd: vendor python-zstandard 0.8.0
Commit 81e1f5bbf1fc54808649562d3ed829730765c540 from
https://github.com/indygreg/python-zstandard is imported without
modifications (other than removing unwanted files).
Updates relevant to Mercurial include:
* Support for multi-threaded compression (we can use this for
bundle and wire protocol compression).
* APIs for batch compression and decompression operations using
multiple threads and optimal memory allocation mechanism. (Can
be useful for revlog perf improvements.)
* A ``BufferWithSegments`` type that models a single memory buffer
containing N discrete items of known lengths. This type can be
used for very efficient 0-copy data operations.
# no-check-commit
author | Gregory Szorc <gregory.szorc@gmail.com> |
---|---|
date | Sat, 01 Apr 2017 15:24:03 -0700 |
parents | c32454d69b85 |
children | b1fb341d8a61 |
comparison
equal
deleted
inserted
replaced
31795:2b130e26c3a4 | 31796:e0dc40530c5a |
---|---|
1 import io | |
2 | |
3 try: | 1 try: |
4 import unittest2 as unittest | 2 import unittest2 as unittest |
5 except ImportError: | 3 except ImportError: |
6 import unittest | 4 import unittest |
7 | |
8 try: | |
9 import hypothesis | |
10 import hypothesis.strategies as strategies | |
11 except ImportError: | |
12 hypothesis = None | |
13 | 5 |
14 import zstd | 6 import zstd |
15 | 7 |
16 from . common import ( | 8 from . common import ( |
17 make_cffi, | 9 make_cffi, |
30 def test_bounds(self): | 22 def test_bounds(self): |
31 zstd.CompressionParameters(zstd.WINDOWLOG_MIN, | 23 zstd.CompressionParameters(zstd.WINDOWLOG_MIN, |
32 zstd.CHAINLOG_MIN, | 24 zstd.CHAINLOG_MIN, |
33 zstd.HASHLOG_MIN, | 25 zstd.HASHLOG_MIN, |
34 zstd.SEARCHLOG_MIN, | 26 zstd.SEARCHLOG_MIN, |
35 zstd.SEARCHLENGTH_MIN, | 27 zstd.SEARCHLENGTH_MIN + 1, |
36 zstd.TARGETLENGTH_MIN, | 28 zstd.TARGETLENGTH_MIN, |
37 zstd.STRATEGY_FAST) | 29 zstd.STRATEGY_FAST) |
38 | 30 |
39 zstd.CompressionParameters(zstd.WINDOWLOG_MAX, | 31 zstd.CompressionParameters(zstd.WINDOWLOG_MAX, |
40 zstd.CHAINLOG_MAX, | 32 zstd.CHAINLOG_MAX, |
41 zstd.HASHLOG_MAX, | 33 zstd.HASHLOG_MAX, |
42 zstd.SEARCHLOG_MAX, | 34 zstd.SEARCHLOG_MAX, |
43 zstd.SEARCHLENGTH_MAX, | 35 zstd.SEARCHLENGTH_MAX - 1, |
44 zstd.TARGETLENGTH_MAX, | 36 zstd.TARGETLENGTH_MAX, |
45 zstd.STRATEGY_BTOPT) | 37 zstd.STRATEGY_BTOPT) |
46 | 38 |
47 def test_get_compression_parameters(self): | 39 def test_get_compression_parameters(self): |
48 p = zstd.get_compression_parameters(1) | 40 p = zstd.get_compression_parameters(1) |
57 self.assertEqual(p.hash_log, 7) | 49 self.assertEqual(p.hash_log, 7) |
58 self.assertEqual(p.search_log, 4) | 50 self.assertEqual(p.search_log, 4) |
59 self.assertEqual(p.search_length, 5) | 51 self.assertEqual(p.search_length, 5) |
60 self.assertEqual(p.target_length, 8) | 52 self.assertEqual(p.target_length, 8) |
61 self.assertEqual(p.strategy, 1) | 53 self.assertEqual(p.strategy, 1) |
54 | |
55 def test_estimated_compression_context_size(self): | |
56 p = zstd.CompressionParameters(20, 16, 17, 1, 5, 16, zstd.STRATEGY_DFAST) | |
57 | |
58 # 32-bit has slightly different values from 64-bit. | |
59 self.assertAlmostEqual(p.estimated_compression_context_size(), 1287076, | |
60 delta=110) | |
62 | 61 |
63 | 62 |
64 @make_cffi | 63 @make_cffi |
65 class TestFrameParameters(unittest.TestCase): | 64 class TestFrameParameters(unittest.TestCase): |
66 def test_invalid_type(self): | 65 def test_invalid_type(self): |
120 params = zstd.get_frame_parameters(zstd.FRAME_HEADER + b'\x45\x40\x0f\x10\x00') | 119 params = zstd.get_frame_parameters(zstd.FRAME_HEADER + b'\x45\x40\x0f\x10\x00') |
121 self.assertEqual(params.content_size, 272) | 120 self.assertEqual(params.content_size, 272) |
122 self.assertEqual(params.window_size, 262144) | 121 self.assertEqual(params.window_size, 262144) |
123 self.assertEqual(params.dict_id, 15) | 122 self.assertEqual(params.dict_id, 15) |
124 self.assertTrue(params.has_checksum) | 123 self.assertTrue(params.has_checksum) |
125 | |
126 | |
127 if hypothesis: | |
128 s_windowlog = strategies.integers(min_value=zstd.WINDOWLOG_MIN, | |
129 max_value=zstd.WINDOWLOG_MAX) | |
130 s_chainlog = strategies.integers(min_value=zstd.CHAINLOG_MIN, | |
131 max_value=zstd.CHAINLOG_MAX) | |
132 s_hashlog = strategies.integers(min_value=zstd.HASHLOG_MIN, | |
133 max_value=zstd.HASHLOG_MAX) | |
134 s_searchlog = strategies.integers(min_value=zstd.SEARCHLOG_MIN, | |
135 max_value=zstd.SEARCHLOG_MAX) | |
136 s_searchlength = strategies.integers(min_value=zstd.SEARCHLENGTH_MIN, | |
137 max_value=zstd.SEARCHLENGTH_MAX) | |
138 s_targetlength = strategies.integers(min_value=zstd.TARGETLENGTH_MIN, | |
139 max_value=zstd.TARGETLENGTH_MAX) | |
140 s_strategy = strategies.sampled_from((zstd.STRATEGY_FAST, | |
141 zstd.STRATEGY_DFAST, | |
142 zstd.STRATEGY_GREEDY, | |
143 zstd.STRATEGY_LAZY, | |
144 zstd.STRATEGY_LAZY2, | |
145 zstd.STRATEGY_BTLAZY2, | |
146 zstd.STRATEGY_BTOPT)) | |
147 | |
148 | |
149 @make_cffi | |
150 class TestCompressionParametersHypothesis(unittest.TestCase): | |
151 @hypothesis.given(s_windowlog, s_chainlog, s_hashlog, s_searchlog, | |
152 s_searchlength, s_targetlength, s_strategy) | |
153 def test_valid_init(self, windowlog, chainlog, hashlog, searchlog, | |
154 searchlength, targetlength, strategy): | |
155 p = zstd.CompressionParameters(windowlog, chainlog, hashlog, | |
156 searchlog, searchlength, | |
157 targetlength, strategy) | |
158 | |
159 # Verify we can instantiate a compressor with the supplied values. | |
160 # ZSTD_checkCParams moves the goal posts on us from what's advertised | |
161 # in the constants. So move along with them. | |
162 if searchlength == zstd.SEARCHLENGTH_MIN and strategy in (zstd.STRATEGY_FAST, zstd.STRATEGY_GREEDY): | |
163 searchlength += 1 | |
164 p = zstd.CompressionParameters(windowlog, chainlog, hashlog, | |
165 searchlog, searchlength, | |
166 targetlength, strategy) | |
167 elif searchlength == zstd.SEARCHLENGTH_MAX and strategy != zstd.STRATEGY_FAST: | |
168 searchlength -= 1 | |
169 p = zstd.CompressionParameters(windowlog, chainlog, hashlog, | |
170 searchlog, searchlength, | |
171 targetlength, strategy) | |
172 | |
173 cctx = zstd.ZstdCompressor(compression_params=p) | |
174 with cctx.write_to(io.BytesIO()): | |
175 pass | |
176 | |
177 @hypothesis.given(s_windowlog, s_chainlog, s_hashlog, s_searchlog, | |
178 s_searchlength, s_targetlength, s_strategy) | |
179 def test_estimate_compression_context_size(self, windowlog, chainlog, | |
180 hashlog, searchlog, | |
181 searchlength, targetlength, | |
182 strategy): | |
183 p = zstd.CompressionParameters(windowlog, chainlog, hashlog, | |
184 searchlog, searchlength, | |
185 targetlength, strategy) | |
186 size = zstd.estimate_compression_context_size(p) |