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