| 1 |
import struct |
| 2 |
import unittest |
| 3 |
|
| 4 |
import zstandard as zstd |
| 5 |
|
| 6 |
from .common import ( |
| 7 |
generate_samples, |
| 8 |
get_optimal_dict_size_heuristically, |
| 9 |
random_input_data, |
| 10 |
) |
| 11 |
|
| 12 |
|
| 13 |
class TestTrainDictionary(unittest.TestCase): |
| 14 |
def test_no_args(self): |
| 15 |
with self.assertRaises(TypeError): |
| 16 |
zstd.train_dictionary() |
| 17 |
|
| 18 |
def test_bad_args(self): |
| 19 |
with self.assertRaises(TypeError): |
| 20 |
zstd.train_dictionary(8192, "foo") |
| 21 |
|
| 22 |
with self.assertRaises(ValueError): |
| 23 |
zstd.train_dictionary(8192, ["foo"]) |
| 24 |
|
| 25 |
def test_no_params(self): |
| 26 |
d = zstd.train_dictionary(8192, random_input_data()) |
| 27 |
self.assertIsInstance(d.dict_id(), int) |
| 28 |
|
| 29 |
# The dictionary ID may be different across platforms. |
| 30 |
expected = b"\x37\xa4\x30\xec" + struct.pack("<I", d.dict_id()) |
| 31 |
|
| 32 |
data = d.as_bytes() |
| 33 |
self.assertEqual(data[0:8], expected) |
| 34 |
|
| 35 |
def test_basic(self): |
| 36 |
d = zstd.train_dictionary(8192, generate_samples(), k=500, d=8) |
| 37 |
self.assertIsInstance(d.dict_id(), int) |
| 38 |
|
| 39 |
data = d.as_bytes() |
| 40 |
self.assertEqual(data[0:4], b"\x37\xa4\x30\xec") |
| 41 |
|
| 42 |
self.assertEqual(d.k, 500) |
| 43 |
self.assertEqual(d.d, 8) |
| 44 |
|
| 45 |
def test_set_dict_id(self): |
| 46 |
samples = generate_samples() |
| 47 |
d = zstd.train_dictionary( |
| 48 |
get_optimal_dict_size_heuristically(samples), |
| 49 |
samples, |
| 50 |
k=64, |
| 51 |
d=8, |
| 52 |
dict_id=42, |
| 53 |
) |
| 54 |
self.assertEqual(d.dict_id(), 42) |
| 55 |
|
| 56 |
def test_optimize(self): |
| 57 |
samples = generate_samples() |
| 58 |
d = zstd.train_dictionary( |
| 59 |
get_optimal_dict_size_heuristically(samples), |
| 60 |
samples, |
| 61 |
threads=-1, |
| 62 |
steps=1, |
| 63 |
d=6, |
| 64 |
notifications=2, |
| 65 |
) |
| 66 |
|
| 67 |
# This varies by platform. |
| 68 |
self.assertIn(d.k, (50, 2000)) |
| 69 |
self.assertEqual(d.d, 6) |
| 70 |
|
| 71 |
|
| 72 |
class TestCompressionDict(unittest.TestCase): |
| 73 |
def test_bad_mode(self): |
| 74 |
with self.assertRaisesRegex(ValueError, "invalid dictionary load mode"): |
| 75 |
zstd.ZstdCompressionDict(b"foo", dict_type=42) |
| 76 |
|
| 77 |
def test_bad_precompute_compress(self): |
| 78 |
samples = generate_samples() |
| 79 |
d = zstd.train_dictionary( |
| 80 |
get_optimal_dict_size_heuristically(samples), samples, k=64, d=8 |
| 81 |
) |
| 82 |
|
| 83 |
with self.assertRaisesRegex( |
| 84 |
ValueError, "must specify one of level or " |
| 85 |
): |
| 86 |
d.precompute_compress() |
| 87 |
|
| 88 |
with self.assertRaisesRegex( |
| 89 |
ValueError, "must only specify one of level or " |
| 90 |
): |
| 91 |
d.precompute_compress( |
| 92 |
level=3, compression_params=zstd.ZstdCompressionParameters() |
| 93 |
) |
| 94 |
|
| 95 |
def test_precompute_compress_rawcontent(self): |
| 96 |
d = zstd.ZstdCompressionDict( |
| 97 |
b"dictcontent" * 64, dict_type=zstd.DICT_TYPE_RAWCONTENT |
| 98 |
) |
| 99 |
d.precompute_compress(level=1) |
| 100 |
|
| 101 |
d = zstd.ZstdCompressionDict( |
| 102 |
b"dictcontent" * 64, dict_type=zstd.DICT_TYPE_FULLDICT |
| 103 |
) |
| 104 |
with self.assertRaisesRegex( |
| 105 |
zstd.ZstdError, "unable to precompute dictionary" |
| 106 |
): |
| 107 |
d.precompute_compress(level=1) |