Debian Codegraph @

contrib/python-zstandard/tests/test_compressor_chunker.py

raw
1 import unittest
2
3 import zstandard as zstd
4
5
6 class TestCompressor_chunker(unittest.TestCase):
7 def test_empty(self):
8 cctx = zstd.ZstdCompressor(write_content_size=False)
9 chunker = cctx.chunker()
10
11 it = chunker.compress(b"")
12
13 with self.assertRaises(StopIteration):
14 next(it)
15
16 it = chunker.finish()
17
18 self.assertEqual(next(it), b"\x28\xb5\x2f\xfd\x00\x00\x01\x00\x00")
19
20 with self.assertRaises(StopIteration):
21 next(it)
22
23 def test_simple_input(self):
24 cctx = zstd.ZstdCompressor()
25 chunker = cctx.chunker()
26
27 it = chunker.compress(b"foobar")
28
29 with self.assertRaises(StopIteration):
30 next(it)
31
32 it = chunker.compress(b"baz" * 30)
33
34 with self.assertRaises(StopIteration):
35 next(it)
36
37 it = chunker.finish()
38
39 self.assertEqual(
40 next(it),
41 b"\x28\xb5\x2f\xfd\x00\x58\x7d\x00\x00\x48\x66\x6f"
42 b"\x6f\x62\x61\x72\x62\x61\x7a\x01\x00\xe4\xe4\x8e",
43 )
44
45 with self.assertRaises(StopIteration):
46 next(it)
47
48 def test_input_size(self):
49 cctx = zstd.ZstdCompressor()
50 chunker = cctx.chunker(size=1024)
51
52 it = chunker.compress(b"x" * 1000)
53
54 with self.assertRaises(StopIteration):
55 next(it)
56
57 it = chunker.compress(b"y" * 24)
58
59 with self.assertRaises(StopIteration):
60 next(it)
61
62 chunks = list(chunker.finish())
63
64 self.assertEqual(
65 chunks,
66 [
67 b"\x28\xb5\x2f\xfd\x60\x00\x03\x65\x00\x00\x18\x78\x78\x79\x02\x00"
68 b"\xa0\x16\xe3\x2b\x80\x05"
69 ],
70 )
71
72 dctx = zstd.ZstdDecompressor()
73
74 self.assertEqual(
75 dctx.decompress(b"".join(chunks)), (b"x" * 1000) + (b"y" * 24)
76 )
77
78 def test_small_chunk_size(self):
79 cctx = zstd.ZstdCompressor()
80 chunker = cctx.chunker(chunk_size=1)
81
82 chunks = list(chunker.compress(b"foo" * 1024))
83 self.assertEqual(chunks, [])
84
85 chunks = list(chunker.finish())
86 self.assertTrue(all(len(chunk) == 1 for chunk in chunks))
87
88 self.assertEqual(
89 b"".join(chunks),
90 b"\x28\xb5\x2f\xfd\x00\x58\x55\x00\x00\x18\x66\x6f\x6f\x01\x00"
91 b"\xfa\xd3\x77\x43",
92 )
93
94 dctx = zstd.ZstdDecompressor()
95 self.assertEqual(
96 dctx.decompress(b"".join(chunks), max_output_size=10000),
97 b"foo" * 1024,
98 )
99
100 def test_input_types(self):
101 cctx = zstd.ZstdCompressor()
102
103 mutable_array = bytearray(3)
104 mutable_array[:] = b"foo"
105
106 sources = [
107 memoryview(b"foo"),
108 bytearray(b"foo"),
109 mutable_array,
110 ]
111
112 for source in sources:
113 chunker = cctx.chunker()
114
115 self.assertEqual(list(chunker.compress(source)), [])
116 self.assertEqual(
117 list(chunker.finish()),
118 [b"\x28\xb5\x2f\xfd\x00\x58\x19\x00\x00\x66\x6f\x6f"],
119 )
120
121 def test_flush(self):
122 cctx = zstd.ZstdCompressor()
123 chunker = cctx.chunker()
124
125 self.assertEqual(list(chunker.compress(b"foo" * 1024)), [])
126 self.assertEqual(list(chunker.compress(b"bar" * 1024)), [])
127
128 chunks1 = list(chunker.flush())
129
130 self.assertEqual(
131 chunks1,
132 [
133 b"\x28\xb5\x2f\xfd\x00\x58\x8c\x00\x00\x30\x66\x6f\x6f\x62\x61\x72"
134 b"\x02\x00\xfa\x03\xfe\xd0\x9f\xbe\x1b\x02"
135 ],
136 )
137
138 self.assertEqual(list(chunker.flush()), [])
139 self.assertEqual(list(chunker.flush()), [])
140
141 self.assertEqual(list(chunker.compress(b"baz" * 1024)), [])
142
143 chunks2 = list(chunker.flush())
144 self.assertEqual(len(chunks2), 1)
145
146 chunks3 = list(chunker.finish())
147 self.assertEqual(len(chunks2), 1)
148
149 dctx = zstd.ZstdDecompressor()
150
151 self.assertEqual(
152 dctx.decompress(
153 b"".join(chunks1 + chunks2 + chunks3), max_output_size=10000
154 ),
155 (b"foo" * 1024) + (b"bar" * 1024) + (b"baz" * 1024),
156 )
157
158 def test_compress_after_finish(self):
159 cctx = zstd.ZstdCompressor()
160 chunker = cctx.chunker()
161
162 list(chunker.compress(b"foo"))
163 list(chunker.finish())
164
165 with self.assertRaisesRegex(
166 zstd.ZstdError,
167 r"cannot call compress\(\) after compression finished",
168 ):
169 list(chunker.compress(b"foo"))
170
171 def test_flush_after_finish(self):
172 cctx = zstd.ZstdCompressor()
173 chunker = cctx.chunker()
174
175 list(chunker.compress(b"foo"))
176 list(chunker.finish())
177
178 with self.assertRaisesRegex(
179 zstd.ZstdError, r"cannot call flush\(\) after compression finished"
180 ):
181 list(chunker.flush())
182
183 def test_finish_after_finish(self):
184 cctx = zstd.ZstdCompressor()
185 chunker = cctx.chunker()
186
187 list(chunker.compress(b"foo"))
188 list(chunker.finish())
189
190 with self.assertRaisesRegex(
191 zstd.ZstdError, r"cannot call finish\(\) after compression finished"
192 ):
193 list(chunker.finish())