| 1 |
import io |
| 2 |
import struct |
| 3 |
import unittest |
| 4 |
|
| 5 |
import zstandard as zstd |
| 6 |
|
| 7 |
|
| 8 |
class TestCompressor_compressobj(unittest.TestCase): |
| 9 |
def test_compressobj_empty(self): |
| 10 |
cctx = zstd.ZstdCompressor(level=1, write_content_size=False) |
| 11 |
cobj = cctx.compressobj() |
| 12 |
self.assertEqual(cobj.compress(b""), b"") |
| 13 |
self.assertEqual(cobj.flush(), b"\x28\xb5\x2f\xfd\x00\x00\x01\x00\x00") |
| 14 |
|
| 15 |
def test_input_types(self): |
| 16 |
expected = b"\x28\xb5\x2f\xfd\x00\x48\x19\x00\x00\x66\x6f\x6f" |
| 17 |
cctx = zstd.ZstdCompressor(level=1, write_content_size=False) |
| 18 |
|
| 19 |
mutable_array = bytearray(3) |
| 20 |
mutable_array[:] = b"foo" |
| 21 |
|
| 22 |
sources = [ |
| 23 |
memoryview(b"foo"), |
| 24 |
bytearray(b"foo"), |
| 25 |
mutable_array, |
| 26 |
] |
| 27 |
|
| 28 |
for source in sources: |
| 29 |
cobj = cctx.compressobj() |
| 30 |
self.assertEqual(cobj.compress(source), b"") |
| 31 |
self.assertEqual(cobj.flush(), expected) |
| 32 |
|
| 33 |
def test_compressobj_large(self): |
| 34 |
chunks = [] |
| 35 |
for i in range(255): |
| 36 |
chunks.append(struct.Struct(">B").pack(i) * 16384) |
| 37 |
|
| 38 |
cctx = zstd.ZstdCompressor(level=3) |
| 39 |
cobj = cctx.compressobj() |
| 40 |
|
| 41 |
result = cobj.compress(b"".join(chunks)) + cobj.flush() |
| 42 |
self.assertEqual(len(result), 999) |
| 43 |
self.assertEqual(result[0:4], b"\x28\xb5\x2f\xfd") |
| 44 |
|
| 45 |
params = zstd.get_frame_parameters(result) |
| 46 |
self.assertEqual(params.content_size, zstd.CONTENTSIZE_UNKNOWN) |
| 47 |
self.assertEqual(params.window_size, 2097152) |
| 48 |
self.assertEqual(params.dict_id, 0) |
| 49 |
self.assertFalse(params.has_checksum) |
| 50 |
|
| 51 |
def test_write_checksum(self): |
| 52 |
cctx = zstd.ZstdCompressor(level=1) |
| 53 |
cobj = cctx.compressobj() |
| 54 |
no_checksum = cobj.compress(b"foobar") + cobj.flush() |
| 55 |
cctx = zstd.ZstdCompressor(level=1, write_checksum=True) |
| 56 |
cobj = cctx.compressobj() |
| 57 |
with_checksum = cobj.compress(b"foobar") + cobj.flush() |
| 58 |
|
| 59 |
no_params = zstd.get_frame_parameters(no_checksum) |
| 60 |
with_params = zstd.get_frame_parameters(with_checksum) |
| 61 |
self.assertEqual(no_params.content_size, zstd.CONTENTSIZE_UNKNOWN) |
| 62 |
self.assertEqual(with_params.content_size, zstd.CONTENTSIZE_UNKNOWN) |
| 63 |
self.assertEqual(no_params.dict_id, 0) |
| 64 |
self.assertEqual(with_params.dict_id, 0) |
| 65 |
self.assertFalse(no_params.has_checksum) |
| 66 |
self.assertTrue(with_params.has_checksum) |
| 67 |
|
| 68 |
self.assertEqual(len(with_checksum), len(no_checksum) + 4) |
| 69 |
|
| 70 |
def test_write_content_size(self): |
| 71 |
cctx = zstd.ZstdCompressor(level=1) |
| 72 |
cobj = cctx.compressobj(size=len(b"foobar" * 256)) |
| 73 |
with_size = cobj.compress(b"foobar" * 256) + cobj.flush() |
| 74 |
cctx = zstd.ZstdCompressor(level=1, write_content_size=False) |
| 75 |
cobj = cctx.compressobj(size=len(b"foobar" * 256)) |
| 76 |
no_size = cobj.compress(b"foobar" * 256) + cobj.flush() |
| 77 |
|
| 78 |
no_params = zstd.get_frame_parameters(no_size) |
| 79 |
with_params = zstd.get_frame_parameters(with_size) |
| 80 |
self.assertEqual(no_params.content_size, zstd.CONTENTSIZE_UNKNOWN) |
| 81 |
self.assertEqual(with_params.content_size, 1536) |
| 82 |
self.assertEqual(no_params.dict_id, 0) |
| 83 |
self.assertEqual(with_params.dict_id, 0) |
| 84 |
self.assertFalse(no_params.has_checksum) |
| 85 |
self.assertFalse(with_params.has_checksum) |
| 86 |
|
| 87 |
self.assertEqual(len(with_size), len(no_size) + 1) |
| 88 |
|
| 89 |
def test_compress_after_finished(self): |
| 90 |
cctx = zstd.ZstdCompressor() |
| 91 |
cobj = cctx.compressobj() |
| 92 |
|
| 93 |
cobj.compress(b"foo") |
| 94 |
cobj.flush() |
| 95 |
|
| 96 |
with self.assertRaisesRegex( |
| 97 |
zstd.ZstdError, r"cannot call compress\(\) after compressor" |
| 98 |
): |
| 99 |
cobj.compress(b"foo") |
| 100 |
|
| 101 |
with self.assertRaisesRegex( |
| 102 |
zstd.ZstdError, "compressor object already finished" |
| 103 |
): |
| 104 |
cobj.flush() |
| 105 |
|
| 106 |
def test_flush_block_repeated(self): |
| 107 |
cctx = zstd.ZstdCompressor(level=1) |
| 108 |
cobj = cctx.compressobj() |
| 109 |
|
| 110 |
self.assertEqual(cobj.compress(b"foo"), b"") |
| 111 |
self.assertEqual( |
| 112 |
cobj.flush(zstd.COMPRESSOBJ_FLUSH_BLOCK), |
| 113 |
b"\x28\xb5\x2f\xfd\x00\x48\x18\x00\x00foo", |
| 114 |
) |
| 115 |
self.assertEqual(cobj.compress(b"bar"), b"") |
| 116 |
# 3 byte header plus content. |
| 117 |
self.assertEqual( |
| 118 |
cobj.flush(zstd.COMPRESSOBJ_FLUSH_BLOCK), b"\x18\x00\x00bar" |
| 119 |
) |
| 120 |
self.assertEqual(cobj.flush(), b"\x01\x00\x00") |
| 121 |
|
| 122 |
def test_flush_empty_block(self): |
| 123 |
cctx = zstd.ZstdCompressor(write_checksum=True) |
| 124 |
cobj = cctx.compressobj() |
| 125 |
|
| 126 |
cobj.compress(b"foobar") |
| 127 |
cobj.flush(zstd.COMPRESSOBJ_FLUSH_BLOCK) |
| 128 |
# No-op if no block is active (this is internal to zstd). |
| 129 |
self.assertEqual(cobj.flush(zstd.COMPRESSOBJ_FLUSH_BLOCK), b"") |
| 130 |
|
| 131 |
trailing = cobj.flush() |
| 132 |
# 3 bytes block header + 4 bytes frame checksum |
| 133 |
self.assertEqual(len(trailing), 7) |
| 134 |
header = trailing[0:3] |
| 135 |
self.assertEqual(header, b"\x01\x00\x00") |
| 136 |
|
| 137 |
def test_multithreaded(self): |
| 138 |
source = io.BytesIO() |
| 139 |
source.write(b"a" * 1048576) |
| 140 |
source.write(b"b" * 1048576) |
| 141 |
source.write(b"c" * 1048576) |
| 142 |
source.seek(0) |
| 143 |
|
| 144 |
cctx = zstd.ZstdCompressor(level=1, threads=2) |
| 145 |
cobj = cctx.compressobj() |
| 146 |
|
| 147 |
chunks = [] |
| 148 |
while True: |
| 149 |
d = source.read(8192) |
| 150 |
if not d: |
| 151 |
break |
| 152 |
|
| 153 |
chunks.append(cobj.compress(d)) |
| 154 |
|
| 155 |
chunks.append(cobj.flush()) |
| 156 |
|
| 157 |
compressed = b"".join(chunks) |
| 158 |
|
| 159 |
self.assertEqual(len(compressed), 119) |
| 160 |
|
| 161 |
def test_frame_progression(self): |
| 162 |
cctx = zstd.ZstdCompressor() |
| 163 |
|
| 164 |
self.assertEqual(cctx.frame_progression(), (0, 0, 0)) |
| 165 |
|
| 166 |
cobj = cctx.compressobj() |
| 167 |
|
| 168 |
cobj.compress(b"foobar") |
| 169 |
self.assertEqual(cctx.frame_progression(), (6, 0, 0)) |
| 170 |
|
| 171 |
cobj.flush() |
| 172 |
self.assertEqual(cctx.frame_progression(), (6, 6, 15)) |
| 173 |
|
| 174 |
def test_bad_size(self): |
| 175 |
cctx = zstd.ZstdCompressor() |
| 176 |
|
| 177 |
cobj = cctx.compressobj(size=2) |
| 178 |
with self.assertRaisesRegex(zstd.ZstdError, "Src size is incorrect"): |
| 179 |
cobj.compress(b"foo") |
| 180 |
|
| 181 |
# Try another operation on this instance. |
| 182 |
with self.assertRaisesRegex(zstd.ZstdError, "Src size is incorrect"): |
| 183 |
cobj.compress(b"aa") |
| 184 |
|
| 185 |
# Try another operation on the compressor. |
| 186 |
cctx.compressobj(size=4) |
| 187 |
cctx.compress(b"foobar") |