blob: bf1e63913953087fe7f82dd6b2a0aa5b5a7d3545 [file] [log] [blame]
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001import unittest
Benjamin Petersonee8712c2008-05-20 21:35:26 +00002from test import support
Christian Heimesd5e2b6f2008-03-19 21:50:51 +00003import binascii
Andrew M. Kuchling9a0f98e2001-02-21 02:17:01 +00004import random
Andrew M. Kuchling9a0f98e2001-02-21 02:17:01 +00005
R. David Murraya21e4ca2009-03-31 23:16:50 +00006zlib = support.import_module('zlib')
7
Andrew M. Kuchling9a0f98e2001-02-21 02:17:01 +00008
Guido van Rossum7d9ea502003-02-03 20:45:52 +00009class ChecksumTestCase(unittest.TestCase):
10 # checksum test cases
11 def test_crc32start(self):
Guido van Rossum776152b2007-05-22 22:44:07 +000012 self.assertEqual(zlib.crc32(b""), zlib.crc32(b"", 0))
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000013 self.assertTrue(zlib.crc32(b"abc", 0xffffffff))
Andrew M. Kuchlingfcfc8d52001-08-10 15:50:11 +000014
Guido van Rossum7d9ea502003-02-03 20:45:52 +000015 def test_crc32empty(self):
Guido van Rossum776152b2007-05-22 22:44:07 +000016 self.assertEqual(zlib.crc32(b"", 0), 0)
17 self.assertEqual(zlib.crc32(b"", 1), 1)
18 self.assertEqual(zlib.crc32(b"", 432), 432)
Andrew M. Kuchling9a0f98e2001-02-21 02:17:01 +000019
Guido van Rossum7d9ea502003-02-03 20:45:52 +000020 def test_adler32start(self):
Guido van Rossum776152b2007-05-22 22:44:07 +000021 self.assertEqual(zlib.adler32(b""), zlib.adler32(b"", 1))
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000022 self.assertTrue(zlib.adler32(b"abc", 0xffffffff))
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +000023
Guido van Rossum7d9ea502003-02-03 20:45:52 +000024 def test_adler32empty(self):
Guido van Rossum776152b2007-05-22 22:44:07 +000025 self.assertEqual(zlib.adler32(b"", 0), 0)
26 self.assertEqual(zlib.adler32(b"", 1), 1)
27 self.assertEqual(zlib.adler32(b"", 432), 432)
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +000028
Guido van Rossum7d9ea502003-02-03 20:45:52 +000029 def assertEqual32(self, seen, expected):
30 # 32-bit values masked -- checksums on 32- vs 64- bit machines
31 # This is important if bit 31 (0x08000000L) is set.
Guido van Rossume2a383d2007-01-15 16:59:06 +000032 self.assertEqual(seen & 0x0FFFFFFFF, expected & 0x0FFFFFFFF)
Guido van Rossum7d9ea502003-02-03 20:45:52 +000033
34 def test_penguins(self):
Guido van Rossum776152b2007-05-22 22:44:07 +000035 self.assertEqual32(zlib.crc32(b"penguin", 0), 0x0e5c1a120)
36 self.assertEqual32(zlib.crc32(b"penguin", 1), 0x43b6aa94)
37 self.assertEqual32(zlib.adler32(b"penguin", 0), 0x0bcf02f6)
38 self.assertEqual32(zlib.adler32(b"penguin", 1), 0x0bd602f7)
Guido van Rossum7d9ea502003-02-03 20:45:52 +000039
Guido van Rossum776152b2007-05-22 22:44:07 +000040 self.assertEqual(zlib.crc32(b"penguin"), zlib.crc32(b"penguin", 0))
41 self.assertEqual(zlib.adler32(b"penguin"),zlib.adler32(b"penguin",1))
Guido van Rossum7d9ea502003-02-03 20:45:52 +000042
Gregory P. Smithab0d8a12008-03-17 20:24:09 +000043 def test_crc32_adler32_unsigned(self):
Antoine Pitrou77b338b2009-12-14 18:00:06 +000044 foo = b'abcdefghijklmnop'
Gregory P. Smithab0d8a12008-03-17 20:24:09 +000045 # explicitly test signed behavior
Gregory P. Smith27275032008-03-20 06:20:09 +000046 self.assertEqual(zlib.crc32(foo), 2486878355)
Antoine Pitrou77b338b2009-12-14 18:00:06 +000047 self.assertEqual(zlib.crc32(b'spam'), 1138425661)
Gregory P. Smithab0d8a12008-03-17 20:24:09 +000048 self.assertEqual(zlib.adler32(foo+foo), 3573550353)
Antoine Pitrou77b338b2009-12-14 18:00:06 +000049 self.assertEqual(zlib.adler32(b'spam'), 72286642)
Gregory P. Smithab0d8a12008-03-17 20:24:09 +000050
Christian Heimesd5e2b6f2008-03-19 21:50:51 +000051 def test_same_as_binascii_crc32(self):
Martin v. Löwis15b16a32008-12-02 06:00:15 +000052 foo = b'abcdefghijklmnop'
Gregory P. Smith27275032008-03-20 06:20:09 +000053 crc = 2486878355
Christian Heimesd5e2b6f2008-03-19 21:50:51 +000054 self.assertEqual(binascii.crc32(foo), crc)
55 self.assertEqual(zlib.crc32(foo), crc)
Martin v. Löwis15b16a32008-12-02 06:00:15 +000056 self.assertEqual(binascii.crc32(b'spam'), zlib.crc32(b'spam'))
Guido van Rossum7d9ea502003-02-03 20:45:52 +000057
58
Christian Heimesb186d002008-03-18 15:15:01 +000059
Guido van Rossum7d9ea502003-02-03 20:45:52 +000060class ExceptionTestCase(unittest.TestCase):
61 # make sure we generate some expected errors
Guido van Rossum8ce8a782007-11-01 19:42:39 +000062 def test_badlevel(self):
63 # specifying compression level out of range causes an error
64 # (but -1 is Z_DEFAULT_COMPRESSION and apparently the zlib
65 # accepts 0 too)
Antoine Pitrou77b338b2009-12-14 18:00:06 +000066 self.assertRaises(zlib.error, zlib.compress, b'ERROR', 10)
67
68 def test_badargs(self):
69 self.assertRaises(TypeError, zlib.adler32)
70 self.assertRaises(TypeError, zlib.crc32)
71 self.assertRaises(TypeError, zlib.compress)
72 self.assertRaises(TypeError, zlib.decompress)
73 for arg in (42, None, '', 'abc', (), []):
74 self.assertRaises(TypeError, zlib.adler32, arg)
75 self.assertRaises(TypeError, zlib.crc32, arg)
76 self.assertRaises(TypeError, zlib.compress, arg)
77 self.assertRaises(TypeError, zlib.decompress, arg)
Guido van Rossum7d9ea502003-02-03 20:45:52 +000078
79 def test_badcompressobj(self):
80 # verify failure on building compress object with bad params
Neil Schemenauer94afd3e2004-06-05 19:02:52 +000081 self.assertRaises(ValueError, zlib.compressobj, 1, zlib.DEFLATED, 0)
Guido van Rossum8ce8a782007-11-01 19:42:39 +000082 # specifying total bits too large causes an error
83 self.assertRaises(ValueError,
84 zlib.compressobj, 1, zlib.DEFLATED, zlib.MAX_WBITS + 1)
Guido van Rossum7d9ea502003-02-03 20:45:52 +000085
86 def test_baddecompressobj(self):
87 # verify failure on building decompress object with bad params
88 self.assertRaises(ValueError, zlib.decompressobj, 0)
89
Christian Heimes5e696852008-04-09 08:37:03 +000090 def test_decompressobj_badflush(self):
91 # verify failure on calling decompressobj.flush with bad params
92 self.assertRaises(ValueError, zlib.decompressobj().flush, 0)
93 self.assertRaises(ValueError, zlib.decompressobj().flush, -1)
94
Guido van Rossum7d9ea502003-02-03 20:45:52 +000095
96
97class CompressTestCase(unittest.TestCase):
98 # Test compression in one go (whole message compression)
99 def test_speech(self):
Neil Schemenauer6412b122004-06-05 19:34:28 +0000100 x = zlib.compress(HAMLET_SCENE)
101 self.assertEqual(zlib.decompress(x), HAMLET_SCENE)
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000102
103 def test_speech128(self):
Neil Schemenauer6412b122004-06-05 19:34:28 +0000104 # compress more data
105 data = HAMLET_SCENE * 128
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000106 x = zlib.compress(data)
Antoine Pitrou77b338b2009-12-14 18:00:06 +0000107 self.assertEqual(zlib.compress(bytearray(data)), x)
108 for ob in x, bytearray(x):
109 self.assertEqual(zlib.decompress(ob), data)
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000110
111
112
113class CompressObjectTestCase(unittest.TestCase):
114 # Test compression object
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000115 def test_pair(self):
Neil Schemenauer6412b122004-06-05 19:34:28 +0000116 # straightforward compress/decompress objects
Antoine Pitrou77b338b2009-12-14 18:00:06 +0000117 datasrc = HAMLET_SCENE * 128
118 datazip = zlib.compress(datasrc)
119 # should compress both bytes and bytearray data
120 for data in (datasrc, bytearray(datasrc)):
121 co = zlib.compressobj()
122 x1 = co.compress(data)
123 x2 = co.flush()
124 self.assertRaises(zlib.error, co.flush) # second flush should not work
125 self.assertEqual(x1 + x2, datazip)
126 for v1, v2 in ((x1, x2), (bytearray(x1), bytearray(x2))):
127 dco = zlib.decompressobj()
128 y1 = dco.decompress(v1 + v2)
129 y2 = dco.flush()
130 self.assertEqual(data, y1 + y2)
131 self.assertIsInstance(dco.unconsumed_tail, bytes)
132 self.assertIsInstance(dco.unused_data, bytes)
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000133
Neil Schemenauer94afd3e2004-06-05 19:02:52 +0000134 def test_compressoptions(self):
135 # specify lots of options to compressobj()
136 level = 2
137 method = zlib.DEFLATED
138 wbits = -12
139 memlevel = 9
140 strategy = zlib.Z_FILTERED
141 co = zlib.compressobj(level, method, wbits, memlevel, strategy)
Neil Schemenauer6412b122004-06-05 19:34:28 +0000142 x1 = co.compress(HAMLET_SCENE)
Neil Schemenauer94afd3e2004-06-05 19:02:52 +0000143 x2 = co.flush()
144 dco = zlib.decompressobj(wbits)
145 y1 = dco.decompress(x1 + x2)
146 y2 = dco.flush()
Neil Schemenauer6412b122004-06-05 19:34:28 +0000147 self.assertEqual(HAMLET_SCENE, y1 + y2)
Neil Schemenauer94afd3e2004-06-05 19:02:52 +0000148
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000149 def test_compressincremental(self):
150 # compress object in steps, decompress object as one-shot
Neil Schemenauer6412b122004-06-05 19:34:28 +0000151 data = HAMLET_SCENE * 128
Neil Schemenauer94afd3e2004-06-05 19:02:52 +0000152 co = zlib.compressobj()
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000153 bufs = []
154 for i in range(0, len(data), 256):
155 bufs.append(co.compress(data[i:i+256]))
156 bufs.append(co.flush())
Guido van Rossum776152b2007-05-22 22:44:07 +0000157 combuf = b''.join(bufs)
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000158
Neil Schemenauer94afd3e2004-06-05 19:02:52 +0000159 dco = zlib.decompressobj()
Guido van Rossum776152b2007-05-22 22:44:07 +0000160 y1 = dco.decompress(b''.join(bufs))
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000161 y2 = dco.flush()
162 self.assertEqual(data, y1 + y2)
163
Neil Schemenauer6412b122004-06-05 19:34:28 +0000164 def test_decompinc(self, flush=False, source=None, cx=256, dcx=64):
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000165 # compress object in steps, decompress object in steps
Neil Schemenauer6412b122004-06-05 19:34:28 +0000166 source = source or HAMLET_SCENE
167 data = source * 128
Neil Schemenauer94afd3e2004-06-05 19:02:52 +0000168 co = zlib.compressobj()
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000169 bufs = []
Neil Schemenauer6412b122004-06-05 19:34:28 +0000170 for i in range(0, len(data), cx):
171 bufs.append(co.compress(data[i:i+cx]))
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000172 bufs.append(co.flush())
Guido van Rossum776152b2007-05-22 22:44:07 +0000173 combuf = b''.join(bufs)
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000174
Gregory P. Smith693fc462008-09-06 20:13:06 +0000175 decombuf = zlib.decompress(combuf)
176 # Test type of return value
Ezio Melottie9615932010-01-24 19:26:24 +0000177 self.assertIsInstance(decombuf, bytes)
Gregory P. Smith693fc462008-09-06 20:13:06 +0000178
179 self.assertEqual(data, decombuf)
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000180
Neil Schemenauer94afd3e2004-06-05 19:02:52 +0000181 dco = zlib.decompressobj()
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000182 bufs = []
Neil Schemenauer6412b122004-06-05 19:34:28 +0000183 for i in range(0, len(combuf), dcx):
184 bufs.append(dco.decompress(combuf[i:i+dcx]))
Guido van Rossum776152b2007-05-22 22:44:07 +0000185 self.assertEqual(b'', dco.unconsumed_tail, ########
186 "(A) uct should be b'': not %d long" %
Neil Schemenauer6412b122004-06-05 19:34:28 +0000187 len(dco.unconsumed_tail))
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000188 self.assertEqual(b'', dco.unused_data)
Neil Schemenauer6412b122004-06-05 19:34:28 +0000189 if flush:
190 bufs.append(dco.flush())
191 else:
192 while True:
Antoine Pitrou77b338b2009-12-14 18:00:06 +0000193 chunk = dco.decompress(b'')
Neil Schemenauer6412b122004-06-05 19:34:28 +0000194 if chunk:
195 bufs.append(chunk)
196 else:
197 break
Guido van Rossum776152b2007-05-22 22:44:07 +0000198 self.assertEqual(b'', dco.unconsumed_tail, ########
199 "(B) uct should be b'': not %d long" %
Neil Schemenauer6412b122004-06-05 19:34:28 +0000200 len(dco.unconsumed_tail))
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000201 self.assertEqual(b'', dco.unused_data)
Guido van Rossum776152b2007-05-22 22:44:07 +0000202 self.assertEqual(data, b''.join(bufs))
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000203 # Failure means: "decompressobj with init options failed"
204
Neil Schemenauer6412b122004-06-05 19:34:28 +0000205 def test_decompincflush(self):
206 self.test_decompinc(flush=True)
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000207
Neil Schemenauer6412b122004-06-05 19:34:28 +0000208 def test_decompimax(self, source=None, cx=256, dcx=64):
209 # compress in steps, decompress in length-restricted steps
210 source = source or HAMLET_SCENE
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000211 # Check a decompression object with max_length specified
Neil Schemenauer6412b122004-06-05 19:34:28 +0000212 data = source * 128
213 co = zlib.compressobj()
214 bufs = []
215 for i in range(0, len(data), cx):
216 bufs.append(co.compress(data[i:i+cx]))
217 bufs.append(co.flush())
Guido van Rossum776152b2007-05-22 22:44:07 +0000218 combuf = b''.join(bufs)
Neil Schemenauer6412b122004-06-05 19:34:28 +0000219 self.assertEqual(data, zlib.decompress(combuf),
220 'compressed data failure')
221
222 dco = zlib.decompressobj()
223 bufs = []
224 cb = combuf
225 while cb:
226 #max_length = 1 + len(cb)//10
227 chunk = dco.decompress(cb, dcx)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000228 self.assertFalse(len(chunk) > dcx,
Neil Schemenauer6412b122004-06-05 19:34:28 +0000229 'chunk too big (%d>%d)' % (len(chunk), dcx))
230 bufs.append(chunk)
231 cb = dco.unconsumed_tail
232 bufs.append(dco.flush())
Guido van Rossum776152b2007-05-22 22:44:07 +0000233 self.assertEqual(data, b''.join(bufs), 'Wrong data retrieved')
Neil Schemenauer6412b122004-06-05 19:34:28 +0000234
235 def test_decompressmaxlen(self, flush=False):
236 # Check a decompression object with max_length specified
237 data = HAMLET_SCENE * 128
Neil Schemenauer94afd3e2004-06-05 19:02:52 +0000238 co = zlib.compressobj()
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000239 bufs = []
240 for i in range(0, len(data), 256):
241 bufs.append(co.compress(data[i:i+256]))
242 bufs.append(co.flush())
Guido van Rossum776152b2007-05-22 22:44:07 +0000243 combuf = b''.join(bufs)
Neil Schemenauer94afd3e2004-06-05 19:02:52 +0000244 self.assertEqual(data, zlib.decompress(combuf),
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000245 'compressed data failure')
246
Neil Schemenauer94afd3e2004-06-05 19:02:52 +0000247 dco = zlib.decompressobj()
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000248 bufs = []
249 cb = combuf
250 while cb:
Guido van Rossumf3594102003-02-27 18:39:18 +0000251 max_length = 1 + len(cb)//10
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000252 chunk = dco.decompress(cb, max_length)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000253 self.assertFalse(len(chunk) > max_length,
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000254 'chunk too big (%d>%d)' % (len(chunk),max_length))
255 bufs.append(chunk)
256 cb = dco.unconsumed_tail
Neil Schemenauer6412b122004-06-05 19:34:28 +0000257 if flush:
258 bufs.append(dco.flush())
259 else:
260 while chunk:
Antoine Pitrou77b338b2009-12-14 18:00:06 +0000261 chunk = dco.decompress(b'', max_length)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000262 self.assertFalse(len(chunk) > max_length,
Neil Schemenauer6412b122004-06-05 19:34:28 +0000263 'chunk too big (%d>%d)' % (len(chunk),max_length))
264 bufs.append(chunk)
Guido van Rossum776152b2007-05-22 22:44:07 +0000265 self.assertEqual(data, b''.join(bufs), 'Wrong data retrieved')
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000266
Neil Schemenauer6412b122004-06-05 19:34:28 +0000267 def test_decompressmaxlenflush(self):
268 self.test_decompressmaxlen(flush=True)
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000269
270 def test_maxlenmisc(self):
271 # Misc tests of max_length
Neil Schemenauer94afd3e2004-06-05 19:02:52 +0000272 dco = zlib.decompressobj()
Antoine Pitrou77b338b2009-12-14 18:00:06 +0000273 self.assertRaises(ValueError, dco.decompress, b"", -1)
Guido van Rossum776152b2007-05-22 22:44:07 +0000274 self.assertEqual(b'', dco.unconsumed_tail)
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000275
276 def test_flushes(self):
277 # Test flush() with the various options, using all the
278 # different levels in order to provide more variations.
279 sync_opt = ['Z_NO_FLUSH', 'Z_SYNC_FLUSH', 'Z_FULL_FLUSH']
280 sync_opt = [getattr(zlib, opt) for opt in sync_opt
281 if hasattr(zlib, opt)]
Neil Schemenauer6412b122004-06-05 19:34:28 +0000282 data = HAMLET_SCENE * 8
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000283
284 for sync in sync_opt:
285 for level in range(10):
286 obj = zlib.compressobj( level )
287 a = obj.compress( data[:3000] )
288 b = obj.flush( sync )
289 c = obj.compress( data[3000:] )
290 d = obj.flush()
Guido van Rossum776152b2007-05-22 22:44:07 +0000291 self.assertEqual(zlib.decompress(b''.join([a,b,c,d])),
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000292 data, ("Decompress failed: flush "
293 "mode=%i, level=%i") % (sync, level))
294 del obj
295
296 def test_odd_flush(self):
297 # Test for odd flushing bugs noted in 2.0, and hopefully fixed in 2.1
298 import random
299
300 if hasattr(zlib, 'Z_SYNC_FLUSH'):
301 # Testing on 17K of "random" data
302
303 # Create compressor and decompressor objects
Neil Schemenauer6412b122004-06-05 19:34:28 +0000304 co = zlib.compressobj(zlib.Z_BEST_COMPRESSION)
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000305 dco = zlib.decompressobj()
306
307 # Try 17K of data
308 # generate random data stream
309 try:
310 # In 2.3 and later, WichmannHill is the RNG of the bug report
311 gen = random.WichmannHill()
312 except AttributeError:
313 try:
314 # 2.2 called it Random
315 gen = random.Random()
316 except AttributeError:
317 # others might simply have a single RNG
318 gen = random
319 gen.seed(1)
320 data = genblock(1, 17 * 1024, generator=gen)
321
322 # compress, sync-flush, and decompress
323 first = co.compress(data)
324 second = co.flush(zlib.Z_SYNC_FLUSH)
325 expanded = dco.decompress(first + second)
326
327 # if decompressed data is different from the input data, choke.
328 self.assertEqual(expanded, data, "17K random source doesn't match")
329
Andrew M. Kuchling3b585b32004-12-28 20:10:48 +0000330 def test_empty_flush(self):
331 # Test that calling .flush() on unused objects works.
332 # (Bug #1083110 -- calling .flush() on decompress objects
333 # caused a core dump.)
334
335 co = zlib.compressobj(zlib.Z_BEST_COMPRESSION)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000336 self.assertTrue(co.flush()) # Returns a zlib header
Andrew M. Kuchling3b585b32004-12-28 20:10:48 +0000337 dco = zlib.decompressobj()
Guido van Rossum776152b2007-05-22 22:44:07 +0000338 self.assertEqual(dco.flush(), b"") # Returns nothing
Tim Peters5a9fb3c2005-01-07 16:01:32 +0000339
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000340 if hasattr(zlib.compressobj(), "copy"):
341 def test_compresscopy(self):
342 # Test copying a compression object
343 data0 = HAMLET_SCENE
Guido van Rossum776152b2007-05-22 22:44:07 +0000344 data1 = bytes(str(HAMLET_SCENE, "ascii").swapcase(), "ascii")
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000345 c0 = zlib.compressobj(zlib.Z_BEST_COMPRESSION)
346 bufs0 = []
347 bufs0.append(c0.compress(data0))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000348
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000349 c1 = c0.copy()
350 bufs1 = bufs0[:]
Thomas Wouters477c8d52006-05-27 19:21:47 +0000351
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000352 bufs0.append(c0.compress(data0))
353 bufs0.append(c0.flush())
Guido van Rossum776152b2007-05-22 22:44:07 +0000354 s0 = b''.join(bufs0)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000355
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000356 bufs1.append(c1.compress(data1))
357 bufs1.append(c1.flush())
Guido van Rossum776152b2007-05-22 22:44:07 +0000358 s1 = b''.join(bufs1)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000359
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000360 self.assertEqual(zlib.decompress(s0),data0+data0)
361 self.assertEqual(zlib.decompress(s1),data0+data1)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000362
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000363 def test_badcompresscopy(self):
364 # Test copying a compression object in an inconsistent state
365 c = zlib.compressobj()
366 c.compress(HAMLET_SCENE)
367 c.flush()
368 self.assertRaises(ValueError, c.copy)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000369
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000370 if hasattr(zlib.decompressobj(), "copy"):
371 def test_decompresscopy(self):
372 # Test copying a decompression object
373 data = HAMLET_SCENE
374 comp = zlib.compress(data)
Gregory P. Smith693fc462008-09-06 20:13:06 +0000375 # Test type of return value
Ezio Melottie9615932010-01-24 19:26:24 +0000376 self.assertIsInstance(comp, bytes)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000377
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000378 d0 = zlib.decompressobj()
379 bufs0 = []
380 bufs0.append(d0.decompress(comp[:32]))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000381
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000382 d1 = d0.copy()
383 bufs1 = bufs0[:]
Thomas Wouters477c8d52006-05-27 19:21:47 +0000384
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000385 bufs0.append(d0.decompress(comp[32:]))
Guido van Rossum776152b2007-05-22 22:44:07 +0000386 s0 = b''.join(bufs0)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000387
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000388 bufs1.append(d1.decompress(comp[32:]))
Guido van Rossum776152b2007-05-22 22:44:07 +0000389 s1 = b''.join(bufs1)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000390
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000391 self.assertEqual(s0,s1)
392 self.assertEqual(s0,data)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000393
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000394 def test_baddecompresscopy(self):
395 # Test copying a compression object in an inconsistent state
396 data = zlib.compress(HAMLET_SCENE)
397 d = zlib.decompressobj()
398 d.decompress(data)
399 d.flush()
400 self.assertRaises(ValueError, d.copy)
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000401
402def genblock(seed, length, step=1024, generator=random):
403 """length-byte stream of random data from a seed (in step-byte blocks)."""
404 if seed is not None:
405 generator.seed(seed)
406 randint = generator.randint
407 if length < step or step < 2:
408 step = length
Guido van Rossum776152b2007-05-22 22:44:07 +0000409 blocks = bytes()
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000410 for i in range(0, length, step):
Guido van Rossum776152b2007-05-22 22:44:07 +0000411 blocks += bytes(randint(0, 255) for x in range(step))
412 return blocks
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000413
414
415
416def choose_lines(source, number, seed=None, generator=random):
417 """Return a list of number lines randomly chosen from the source"""
418 if seed is not None:
419 generator.seed(seed)
420 sources = source.split('\n')
421 return [generator.choice(sources) for n in range(number)]
422
423
424
Guido van Rossum776152b2007-05-22 22:44:07 +0000425HAMLET_SCENE = b"""
Fred Drake004d5e62000-10-23 17:22:08 +0000426LAERTES
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +0000427
428 O, fear me not.
429 I stay too long: but here my father comes.
430
431 Enter POLONIUS
432
433 A double blessing is a double grace,
434 Occasion smiles upon a second leave.
435
Fred Drake004d5e62000-10-23 17:22:08 +0000436LORD POLONIUS
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +0000437
438 Yet here, Laertes! aboard, aboard, for shame!
439 The wind sits in the shoulder of your sail,
440 And you are stay'd for. There; my blessing with thee!
441 And these few precepts in thy memory
442 See thou character. Give thy thoughts no tongue,
443 Nor any unproportioned thought his act.
444 Be thou familiar, but by no means vulgar.
445 Those friends thou hast, and their adoption tried,
446 Grapple them to thy soul with hoops of steel;
447 But do not dull thy palm with entertainment
448 Of each new-hatch'd, unfledged comrade. Beware
449 Of entrance to a quarrel, but being in,
450 Bear't that the opposed may beware of thee.
451 Give every man thy ear, but few thy voice;
452 Take each man's censure, but reserve thy judgment.
453 Costly thy habit as thy purse can buy,
454 But not express'd in fancy; rich, not gaudy;
455 For the apparel oft proclaims the man,
456 And they in France of the best rank and station
457 Are of a most select and generous chief in that.
458 Neither a borrower nor a lender be;
459 For loan oft loses both itself and friend,
460 And borrowing dulls the edge of husbandry.
461 This above all: to thine ownself be true,
462 And it must follow, as the night the day,
463 Thou canst not then be false to any man.
464 Farewell: my blessing season this in thee!
465
Fred Drake004d5e62000-10-23 17:22:08 +0000466LAERTES
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +0000467
468 Most humbly do I take my leave, my lord.
469
Fred Drake004d5e62000-10-23 17:22:08 +0000470LORD POLONIUS
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +0000471
472 The time invites you; go; your servants tend.
473
Fred Drake004d5e62000-10-23 17:22:08 +0000474LAERTES
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +0000475
476 Farewell, Ophelia; and remember well
477 What I have said to you.
478
Fred Drake004d5e62000-10-23 17:22:08 +0000479OPHELIA
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +0000480
481 'Tis in my memory lock'd,
482 And you yourself shall keep the key of it.
483
Fred Drake004d5e62000-10-23 17:22:08 +0000484LAERTES
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +0000485
486 Farewell.
487"""
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000488
489
490def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000491 support.run_unittest(
Walter Dörwald21d3a322003-05-01 17:45:56 +0000492 ChecksumTestCase,
493 ExceptionTestCase,
494 CompressTestCase,
495 CompressObjectTestCase
496 )
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000497
498if __name__ == "__main__":
Guido van Rossum776152b2007-05-22 22:44:07 +0000499 unittest.main() # XXX
500 ###test_main()