5 |
5 |
6 try: |
6 try: |
7 import hypothesis |
7 import hypothesis |
8 import hypothesis.strategies as strategies |
8 import hypothesis.strategies as strategies |
9 except ImportError: |
9 except ImportError: |
10 raise unittest.SkipTest('hypothesis not available') |
10 raise unittest.SkipTest("hypothesis not available") |
11 |
11 |
12 import zstandard as zstd |
12 import zstandard as zstd |
13 |
13 |
14 from .common import ( |
14 from .common import ( |
15 make_cffi, |
15 make_cffi, |
|
16 TestCase, |
16 ) |
17 ) |
17 |
18 |
18 |
19 |
19 s_windowlog = strategies.integers(min_value=zstd.WINDOWLOG_MIN, |
20 s_windowlog = strategies.integers( |
20 max_value=zstd.WINDOWLOG_MAX) |
21 min_value=zstd.WINDOWLOG_MIN, max_value=zstd.WINDOWLOG_MAX |
21 s_chainlog = strategies.integers(min_value=zstd.CHAINLOG_MIN, |
22 ) |
22 max_value=zstd.CHAINLOG_MAX) |
23 s_chainlog = strategies.integers( |
23 s_hashlog = strategies.integers(min_value=zstd.HASHLOG_MIN, |
24 min_value=zstd.CHAINLOG_MIN, max_value=zstd.CHAINLOG_MAX |
24 max_value=zstd.HASHLOG_MAX) |
25 ) |
25 s_searchlog = strategies.integers(min_value=zstd.SEARCHLOG_MIN, |
26 s_hashlog = strategies.integers(min_value=zstd.HASHLOG_MIN, max_value=zstd.HASHLOG_MAX) |
26 max_value=zstd.SEARCHLOG_MAX) |
27 s_searchlog = strategies.integers( |
27 s_minmatch = strategies.integers(min_value=zstd.MINMATCH_MIN, |
28 min_value=zstd.SEARCHLOG_MIN, max_value=zstd.SEARCHLOG_MAX |
28 max_value=zstd.MINMATCH_MAX) |
29 ) |
29 s_targetlength = strategies.integers(min_value=zstd.TARGETLENGTH_MIN, |
30 s_minmatch = strategies.integers( |
30 max_value=zstd.TARGETLENGTH_MAX) |
31 min_value=zstd.MINMATCH_MIN, max_value=zstd.MINMATCH_MAX |
31 s_strategy = strategies.sampled_from((zstd.STRATEGY_FAST, |
32 ) |
32 zstd.STRATEGY_DFAST, |
33 s_targetlength = strategies.integers( |
33 zstd.STRATEGY_GREEDY, |
34 min_value=zstd.TARGETLENGTH_MIN, max_value=zstd.TARGETLENGTH_MAX |
34 zstd.STRATEGY_LAZY, |
35 ) |
35 zstd.STRATEGY_LAZY2, |
36 s_strategy = strategies.sampled_from( |
36 zstd.STRATEGY_BTLAZY2, |
37 ( |
37 zstd.STRATEGY_BTOPT, |
38 zstd.STRATEGY_FAST, |
38 zstd.STRATEGY_BTULTRA, |
39 zstd.STRATEGY_DFAST, |
39 zstd.STRATEGY_BTULTRA2)) |
40 zstd.STRATEGY_GREEDY, |
|
41 zstd.STRATEGY_LAZY, |
|
42 zstd.STRATEGY_LAZY2, |
|
43 zstd.STRATEGY_BTLAZY2, |
|
44 zstd.STRATEGY_BTOPT, |
|
45 zstd.STRATEGY_BTULTRA, |
|
46 zstd.STRATEGY_BTULTRA2, |
|
47 ) |
|
48 ) |
40 |
49 |
41 |
50 |
42 @make_cffi |
51 @make_cffi |
43 @unittest.skipUnless('ZSTD_SLOW_TESTS' in os.environ, 'ZSTD_SLOW_TESTS not set') |
52 @unittest.skipUnless("ZSTD_SLOW_TESTS" in os.environ, "ZSTD_SLOW_TESTS not set") |
44 class TestCompressionParametersHypothesis(unittest.TestCase): |
53 class TestCompressionParametersHypothesis(TestCase): |
45 @hypothesis.given(s_windowlog, s_chainlog, s_hashlog, s_searchlog, |
54 @hypothesis.given( |
46 s_minmatch, s_targetlength, s_strategy) |
55 s_windowlog, |
47 def test_valid_init(self, windowlog, chainlog, hashlog, searchlog, |
56 s_chainlog, |
48 minmatch, targetlength, strategy): |
57 s_hashlog, |
49 zstd.ZstdCompressionParameters(window_log=windowlog, |
58 s_searchlog, |
50 chain_log=chainlog, |
59 s_minmatch, |
51 hash_log=hashlog, |
60 s_targetlength, |
52 search_log=searchlog, |
61 s_strategy, |
53 min_match=minmatch, |
62 ) |
54 target_length=targetlength, |
63 def test_valid_init( |
55 strategy=strategy) |
64 self, windowlog, chainlog, hashlog, searchlog, minmatch, targetlength, strategy |
|
65 ): |
|
66 zstd.ZstdCompressionParameters( |
|
67 window_log=windowlog, |
|
68 chain_log=chainlog, |
|
69 hash_log=hashlog, |
|
70 search_log=searchlog, |
|
71 min_match=minmatch, |
|
72 target_length=targetlength, |
|
73 strategy=strategy, |
|
74 ) |
56 |
75 |
57 @hypothesis.given(s_windowlog, s_chainlog, s_hashlog, s_searchlog, |
76 @hypothesis.given( |
58 s_minmatch, s_targetlength, s_strategy) |
77 s_windowlog, |
59 def test_estimated_compression_context_size(self, windowlog, chainlog, |
78 s_chainlog, |
60 hashlog, searchlog, |
79 s_hashlog, |
61 minmatch, targetlength, |
80 s_searchlog, |
62 strategy): |
81 s_minmatch, |
63 if minmatch == zstd.MINMATCH_MIN and strategy in (zstd.STRATEGY_FAST, zstd.STRATEGY_GREEDY): |
82 s_targetlength, |
|
83 s_strategy, |
|
84 ) |
|
85 def test_estimated_compression_context_size( |
|
86 self, windowlog, chainlog, hashlog, searchlog, minmatch, targetlength, strategy |
|
87 ): |
|
88 if minmatch == zstd.MINMATCH_MIN and strategy in ( |
|
89 zstd.STRATEGY_FAST, |
|
90 zstd.STRATEGY_GREEDY, |
|
91 ): |
64 minmatch += 1 |
92 minmatch += 1 |
65 elif minmatch == zstd.MINMATCH_MAX and strategy != zstd.STRATEGY_FAST: |
93 elif minmatch == zstd.MINMATCH_MAX and strategy != zstd.STRATEGY_FAST: |
66 minmatch -= 1 |
94 minmatch -= 1 |
67 |
95 |
68 p = zstd.ZstdCompressionParameters(window_log=windowlog, |
96 p = zstd.ZstdCompressionParameters( |
69 chain_log=chainlog, |
97 window_log=windowlog, |
70 hash_log=hashlog, |
98 chain_log=chainlog, |
71 search_log=searchlog, |
99 hash_log=hashlog, |
72 min_match=minmatch, |
100 search_log=searchlog, |
73 target_length=targetlength, |
101 min_match=minmatch, |
74 strategy=strategy) |
102 target_length=targetlength, |
|
103 strategy=strategy, |
|
104 ) |
75 size = p.estimated_compression_context_size() |
105 size = p.estimated_compression_context_size() |
76 |
|