| 1 |
import struct |
| 2 |
import unittest |
| 3 |
|
| 4 |
import zstandard as zstd |
| 5 |
|
| 6 |
|
| 7 |
def multithreaded_chunk_size(level, source_size=0): |
| 8 |
params = zstd.ZstdCompressionParameters.from_level( |
| 9 |
level, source_size=source_size |
| 10 |
) |
| 11 |
|
| 12 |
return 1 << (params.window_log + 2) |
| 13 |
|
| 14 |
|
| 15 |
class TestCompressor_compress(unittest.TestCase): |
| 16 |
def test_compress_empty(self): |
| 17 |
cctx = zstd.ZstdCompressor(level=1, write_content_size=False) |
| 18 |
result = cctx.compress(b"") |
| 19 |
self.assertEqual(result, b"\x28\xb5\x2f\xfd\x00\x00\x01\x00\x00") |
| 20 |
params = zstd.get_frame_parameters(result) |
| 21 |
self.assertEqual(params.content_size, zstd.CONTENTSIZE_UNKNOWN) |
| 22 |
self.assertEqual(params.window_size, 1024) |
| 23 |
self.assertEqual(params.dict_id, 0) |
| 24 |
self.assertFalse(params.has_checksum, 0) |
| 25 |
|
| 26 |
cctx = zstd.ZstdCompressor() |
| 27 |
result = cctx.compress(b"") |
| 28 |
self.assertEqual(result, b"\x28\xb5\x2f\xfd\x20\x00\x01\x00\x00") |
| 29 |
params = zstd.get_frame_parameters(result) |
| 30 |
self.assertEqual(params.content_size, 0) |
| 31 |
|
| 32 |
def test_input_types(self): |
| 33 |
cctx = zstd.ZstdCompressor(level=1, write_content_size=False) |
| 34 |
expected = b"\x28\xb5\x2f\xfd\x00\x00\x19\x00\x00\x66\x6f\x6f" |
| 35 |
|
| 36 |
mutable_array = bytearray(3) |
| 37 |
mutable_array[:] = b"foo" |
| 38 |
|
| 39 |
sources = [ |
| 40 |
memoryview(b"foo"), |
| 41 |
bytearray(b"foo"), |
| 42 |
mutable_array, |
| 43 |
] |
| 44 |
|
| 45 |
for source in sources: |
| 46 |
self.assertEqual(cctx.compress(source), expected) |
| 47 |
|
| 48 |
def test_compress_large(self): |
| 49 |
chunks = [] |
| 50 |
for i in range(255): |
| 51 |
chunks.append(struct.Struct(">B").pack(i) * 16384) |
| 52 |
|
| 53 |
cctx = zstd.ZstdCompressor(level=3, write_content_size=False) |
| 54 |
result = cctx.compress(b"".join(chunks)) |
| 55 |
self.assertEqual(len(result), 999) |
| 56 |
self.assertEqual(result[0:4], b"\x28\xb5\x2f\xfd") |
| 57 |
|
| 58 |
# This matches the test for read_to_iter() below. |
| 59 |
cctx = zstd.ZstdCompressor(level=1, write_content_size=False) |
| 60 |
result = cctx.compress( |
| 61 |
b"f" * zstd.COMPRESSION_RECOMMENDED_INPUT_SIZE + b"o" |
| 62 |
) |
| 63 |
self.assertEqual( |
| 64 |
result, |
| 65 |
b"\x28\xb5\x2f\xfd\x00\x40\x54\x00\x00" |
| 66 |
b"\x10\x66\x66\x01\x00\xfb\xff\x39\xc0" |
| 67 |
b"\x02\x09\x00\x00\x6f", |
| 68 |
) |
| 69 |
|
| 70 |
def test_negative_level(self): |
| 71 |
cctx = zstd.ZstdCompressor(level=-4) |
| 72 |
result = cctx.compress(b"foo" * 256) |
| 73 |
|
| 74 |
def test_no_magic(self): |
| 75 |
params = zstd.ZstdCompressionParameters.from_level( |
| 76 |
1, format=zstd.FORMAT_ZSTD1 |
| 77 |
) |
| 78 |
cctx = zstd.ZstdCompressor(compression_params=params) |
| 79 |
magic = cctx.compress(b"foobar") |
| 80 |
|
| 81 |
params = zstd.ZstdCompressionParameters.from_level( |
| 82 |
1, format=zstd.FORMAT_ZSTD1_MAGICLESS |
| 83 |
) |
| 84 |
cctx = zstd.ZstdCompressor(compression_params=params) |
| 85 |
no_magic = cctx.compress(b"foobar") |
| 86 |
|
| 87 |
self.assertEqual(magic[0:4], b"\x28\xb5\x2f\xfd") |
| 88 |
self.assertEqual(magic[4:], no_magic) |
| 89 |
|
| 90 |
def test_write_checksum(self): |
| 91 |
cctx = zstd.ZstdCompressor(level=1) |
| 92 |
no_checksum = cctx.compress(b"foobar") |
| 93 |
cctx = zstd.ZstdCompressor(level=1, write_checksum=True) |
| 94 |
with_checksum = cctx.compress(b"foobar") |
| 95 |
|
| 96 |
self.assertEqual(len(with_checksum), len(no_checksum) + 4) |
| 97 |
|
| 98 |
no_params = zstd.get_frame_parameters(no_checksum) |
| 99 |
with_params = zstd.get_frame_parameters(with_checksum) |
| 100 |
|
| 101 |
self.assertFalse(no_params.has_checksum) |
| 102 |
self.assertTrue(with_params.has_checksum) |
| 103 |
|
| 104 |
def test_write_content_size(self): |
| 105 |
cctx = zstd.ZstdCompressor(level=1) |
| 106 |
with_size = cctx.compress(b"foobar" * 256) |
| 107 |
cctx = zstd.ZstdCompressor(level=1, write_content_size=False) |
| 108 |
no_size = cctx.compress(b"foobar" * 256) |
| 109 |
|
| 110 |
self.assertEqual(len(with_size), len(no_size) + 1) |
| 111 |
|
| 112 |
no_params = zstd.get_frame_parameters(no_size) |
| 113 |
with_params = zstd.get_frame_parameters(with_size) |
| 114 |
self.assertEqual(no_params.content_size, zstd.CONTENTSIZE_UNKNOWN) |
| 115 |
self.assertEqual(with_params.content_size, 1536) |
| 116 |
|
| 117 |
def test_no_dict_id(self): |
| 118 |
samples = [] |
| 119 |
for i in range(128): |
| 120 |
samples.append(b"foo" * 64) |
| 121 |
samples.append(b"bar" * 64) |
| 122 |
samples.append(b"foobar" * 64) |
| 123 |
|
| 124 |
d = zstd.train_dictionary(1024, samples) |
| 125 |
|
| 126 |
cctx = zstd.ZstdCompressor(level=1, dict_data=d) |
| 127 |
with_dict_id = cctx.compress(b"foobarfoobar") |
| 128 |
|
| 129 |
cctx = zstd.ZstdCompressor(level=1, dict_data=d, write_dict_id=False) |
| 130 |
no_dict_id = cctx.compress(b"foobarfoobar") |
| 131 |
|
| 132 |
self.assertEqual(len(with_dict_id), len(no_dict_id) + 4) |
| 133 |
|
| 134 |
no_params = zstd.get_frame_parameters(no_dict_id) |
| 135 |
with_params = zstd.get_frame_parameters(with_dict_id) |
| 136 |
self.assertEqual(no_params.dict_id, 0) |
| 137 |
self.assertEqual(with_params.dict_id, 1123828263) |
| 138 |
|
| 139 |
def test_compress_dict_multiple(self): |
| 140 |
samples = [] |
| 141 |
for i in range(128): |
| 142 |
samples.append(b"foo" * 64) |
| 143 |
samples.append(b"bar" * 64) |
| 144 |
samples.append(b"foobar" * 64) |
| 145 |
|
| 146 |
d = zstd.train_dictionary(8192, samples) |
| 147 |
|
| 148 |
cctx = zstd.ZstdCompressor(level=1, dict_data=d) |
| 149 |
|
| 150 |
for i in range(32): |
| 151 |
cctx.compress(b"foo bar foobar foo bar foobar") |
| 152 |
|
| 153 |
def test_dict_precompute(self): |
| 154 |
samples = [] |
| 155 |
for i in range(128): |
| 156 |
samples.append(b"foo" * 64) |
| 157 |
samples.append(b"bar" * 64) |
| 158 |
samples.append(b"foobar" * 64) |
| 159 |
|
| 160 |
d = zstd.train_dictionary(8192, samples) |
| 161 |
d.precompute_compress(level=1) |
| 162 |
|
| 163 |
cctx = zstd.ZstdCompressor(level=1, dict_data=d) |
| 164 |
|
| 165 |
for i in range(32): |
| 166 |
cctx.compress(b"foo bar foobar foo bar foobar") |
| 167 |
|
| 168 |
def test_multithreaded(self): |
| 169 |
chunk_size = multithreaded_chunk_size(1) |
| 170 |
source = b"".join([b"x" * chunk_size, b"y" * chunk_size]) |
| 171 |
|
| 172 |
cctx = zstd.ZstdCompressor(level=1, threads=2) |
| 173 |
compressed = cctx.compress(source) |
| 174 |
|
| 175 |
params = zstd.get_frame_parameters(compressed) |
| 176 |
self.assertEqual(params.content_size, chunk_size * 2) |
| 177 |
self.assertEqual(params.dict_id, 0) |
| 178 |
self.assertFalse(params.has_checksum) |
| 179 |
|
| 180 |
dctx = zstd.ZstdDecompressor() |
| 181 |
self.assertEqual(dctx.decompress(compressed), source) |
| 182 |
|
| 183 |
def test_multithreaded_dict(self): |
| 184 |
samples = [] |
| 185 |
for i in range(128): |
| 186 |
samples.append(b"foo" * 64) |
| 187 |
samples.append(b"bar" * 64) |
| 188 |
samples.append(b"foobar" * 64) |
| 189 |
|
| 190 |
d = zstd.train_dictionary(1024, samples) |
| 191 |
|
| 192 |
cctx = zstd.ZstdCompressor(dict_data=d, threads=2) |
| 193 |
|
| 194 |
result = cctx.compress(b"foo") |
| 195 |
params = zstd.get_frame_parameters(result) |
| 196 |
self.assertEqual(params.content_size, 3) |
| 197 |
self.assertEqual(params.dict_id, d.dict_id()) |
| 198 |
|
| 199 |
self.assertEqual( |
| 200 |
result, |
| 201 |
b"\x28\xb5\x2f\xfd\x23\x27\x42\xfc\x42\x03\x19\x00\x00" |
| 202 |
b"\x66\x6f\x6f", |
| 203 |
) |
| 204 |
|
| 205 |
def test_multithreaded_compression_params(self): |
| 206 |
params = zstd.ZstdCompressionParameters.from_level(0, threads=2) |
| 207 |
cctx = zstd.ZstdCompressor(compression_params=params) |
| 208 |
|
| 209 |
result = cctx.compress(b"foo") |
| 210 |
params = zstd.get_frame_parameters(result) |
| 211 |
self.assertEqual(params.content_size, 3) |
| 212 |
|
| 213 |
self.assertEqual( |
| 214 |
result, b"\x28\xb5\x2f\xfd\x20\x03\x19\x00\x00\x66\x6f\x6f" |
| 215 |
) |
| 216 |
|
| 217 |
def test_explicit_default_params(self): |
| 218 |
cctx = zstd.ZstdCompressor( |
| 219 |
level=3, |
| 220 |
dict_data=None, |
| 221 |
compression_params=None, |
| 222 |
write_checksum=None, |
| 223 |
write_content_size=None, |
| 224 |
write_dict_id=None, |
| 225 |
threads=0, |
| 226 |
) |
| 227 |
result = cctx.compress(b"") |
| 228 |
self.assertEqual(result, b"\x28\xb5\x2f\xfd\x20\x00\x01\x00\x00") |
| 229 |
|
| 230 |
def test_compression_params_with_other_params(self): |
| 231 |
params = zstd.ZstdCompressionParameters.from_level(3) |
| 232 |
cctx = zstd.ZstdCompressor( |
| 233 |
level=3, |
| 234 |
dict_data=None, |
| 235 |
compression_params=params, |
| 236 |
write_checksum=None, |
| 237 |
write_content_size=None, |
| 238 |
write_dict_id=None, |
| 239 |
threads=0, |
| 240 |
) |
| 241 |
result = cctx.compress(b"") |
| 242 |
self.assertEqual(result, b"\x28\xb5\x2f\xfd\x20\x00\x01\x00\x00") |
| 243 |
|
| 244 |
with self.assertRaises(ValueError): |
| 245 |
cctx = zstd.ZstdCompressor( |
| 246 |
level=3, |
| 247 |
dict_data=None, |
| 248 |
compression_params=params, |
| 249 |
write_checksum=False, |
| 250 |
write_content_size=None, |
| 251 |
write_dict_id=None, |
| 252 |
threads=0, |
| 253 |
) |
| 254 |
|
| 255 |
with self.assertRaises(ValueError): |
| 256 |
cctx = zstd.ZstdCompressor( |
| 257 |
level=3, |
| 258 |
dict_data=None, |
| 259 |
compression_params=params, |
| 260 |
write_checksum=None, |
| 261 |
write_content_size=True, |
| 262 |
write_dict_id=None, |
| 263 |
threads=0, |
| 264 |
) |
| 265 |
|
| 266 |
with self.assertRaises(ValueError): |
| 267 |
cctx = zstd.ZstdCompressor( |
| 268 |
level=3, |
| 269 |
dict_data=None, |
| 270 |
compression_params=params, |
| 271 |
write_checksum=None, |
| 272 |
write_content_size=None, |
| 273 |
write_dict_id=True, |
| 274 |
threads=0, |
| 275 |
) |
| 276 |
|
| 277 |
with self.assertRaises(ValueError): |
| 278 |
cctx = zstd.ZstdCompressor( |
| 279 |
level=3, |
| 280 |
dict_data=None, |
| 281 |
compression_params=params, |
| 282 |
write_checksum=None, |
| 283 |
write_content_size=None, |
| 284 |
write_dict_id=True, |
| 285 |
threads=2, |
| 286 |
) |