test_train_dictionary.py
87 lines
| 2.8 KiB
| text/x-python
|
PythonLexer
|
r37513 | import struct | ||
|
r30435 | import sys | ||
|
r37513 | import unittest | ||
|
r30435 | |||
|
r37513 | import zstandard as zstd | ||
|
r30435 | |||
|
r30895 | from . common import ( | ||
|
r37513 | generate_samples, | ||
|
r30895 | make_cffi, | ||
) | ||||
|
r30435 | |||
if sys.version_info[0] >= 3: | ||||
int_type = int | ||||
else: | ||||
int_type = long | ||||
|
r30895 | @make_cffi | ||
|
r30435 | class TestTrainDictionary(unittest.TestCase): | ||
def test_no_args(self): | ||||
with self.assertRaises(TypeError): | ||||
zstd.train_dictionary() | ||||
def test_bad_args(self): | ||||
with self.assertRaises(TypeError): | ||||
zstd.train_dictionary(8192, u'foo') | ||||
with self.assertRaises(ValueError): | ||||
zstd.train_dictionary(8192, [u'foo']) | ||||
|
r37513 | def test_no_params(self): | ||
d = zstd.train_dictionary(8192, generate_samples()) | ||||
self.assertIsInstance(d.dict_id(), int_type) | ||||
|
r30435 | |||
|
r37513 | # The dictionary ID may be different across platforms. | ||
expected = b'\x37\xa4\x30\xec' + struct.pack('<I', d.dict_id()) | ||||
|
r30435 | |||
data = d.as_bytes() | ||||
|
r37513 | self.assertEqual(data[0:8], expected) | ||
|
r31796 | |||
def test_basic(self): | ||||
|
r37513 | d = zstd.train_dictionary(8192, generate_samples(), k=64, d=16) | ||
|
r31796 | self.assertIsInstance(d.dict_id(), int_type) | ||
data = d.as_bytes() | ||||
self.assertEqual(data[0:4], b'\x37\xa4\x30\xec') | ||||
self.assertEqual(d.k, 64) | ||||
self.assertEqual(d.d, 16) | ||||
def test_set_dict_id(self): | ||||
|
r37513 | d = zstd.train_dictionary(8192, generate_samples(), k=64, d=16, | ||
dict_id=42) | ||||
|
r31796 | self.assertEqual(d.dict_id(), 42) | ||
def test_optimize(self): | ||||
|
r37513 | d = zstd.train_dictionary(8192, generate_samples(), threads=-1, steps=1, | ||
d=16) | ||||
self.assertEqual(d.k, 50) | ||||
self.assertEqual(d.d, 16) | ||||
@make_cffi | ||||
class TestCompressionDict(unittest.TestCase): | ||||
def test_bad_mode(self): | ||||
with self.assertRaisesRegexp(ValueError, 'invalid dictionary load mode'): | ||||
zstd.ZstdCompressionDict(b'foo', dict_type=42) | ||||
def test_bad_precompute_compress(self): | ||||
d = zstd.train_dictionary(8192, generate_samples(), k=64, d=16) | ||||
|
r31796 | |||
|
r37513 | with self.assertRaisesRegexp(ValueError, 'must specify one of level or '): | ||
d.precompute_compress() | ||||
with self.assertRaisesRegexp(ValueError, 'must only specify one of level or '): | ||||
d.precompute_compress(level=3, | ||||
compression_params=zstd.CompressionParameters()) | ||||
|
r31796 | |||
|
r37513 | def test_precompute_compress_rawcontent(self): | ||
d = zstd.ZstdCompressionDict(b'dictcontent' * 64, | ||||
dict_type=zstd.DICT_TYPE_RAWCONTENT) | ||||
d.precompute_compress(level=1) | ||||
d = zstd.ZstdCompressionDict(b'dictcontent' * 64, | ||||
dict_type=zstd.DICT_TYPE_FULLDICT) | ||||
with self.assertRaisesRegexp(zstd.ZstdError, 'unable to precompute dictionary'): | ||||
d.precompute_compress(level=1) | ||||