blob: 99c8ddac7151c250d4c9364781e2a869f10eae0b [file] [log] [blame]
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001import unittest
Benjamin Petersonee8712c2008-05-20 21:35:26 +00002from test import support
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +00003import zlib
Christian Heimesd5e2b6f2008-03-19 21:50:51 +00004import binascii
Andrew M. Kuchling9a0f98e2001-02-21 02:17:01 +00005import random
Andrew M. Kuchling9a0f98e2001-02-21 02:17:01 +00006
Andrew M. Kuchling9a0f98e2001-02-21 02:17:01 +00007
Guido van Rossum7d9ea502003-02-03 20:45:52 +00008class ChecksumTestCase(unittest.TestCase):
9 # checksum test cases
10 def test_crc32start(self):
Guido van Rossum776152b2007-05-22 22:44:07 +000011 self.assertEqual(zlib.crc32(b""), zlib.crc32(b"", 0))
12 self.assert_(zlib.crc32(b"abc", 0xffffffff))
Andrew M. Kuchlingfcfc8d52001-08-10 15:50:11 +000013
Guido van Rossum7d9ea502003-02-03 20:45:52 +000014 def test_crc32empty(self):
Guido van Rossum776152b2007-05-22 22:44:07 +000015 self.assertEqual(zlib.crc32(b"", 0), 0)
16 self.assertEqual(zlib.crc32(b"", 1), 1)
17 self.assertEqual(zlib.crc32(b"", 432), 432)
Andrew M. Kuchling9a0f98e2001-02-21 02:17:01 +000018
Guido van Rossum7d9ea502003-02-03 20:45:52 +000019 def test_adler32start(self):
Guido van Rossum776152b2007-05-22 22:44:07 +000020 self.assertEqual(zlib.adler32(b""), zlib.adler32(b"", 1))
21 self.assert_(zlib.adler32(b"abc", 0xffffffff))
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +000022
Guido van Rossum7d9ea502003-02-03 20:45:52 +000023 def test_adler32empty(self):
Guido van Rossum776152b2007-05-22 22:44:07 +000024 self.assertEqual(zlib.adler32(b"", 0), 0)
25 self.assertEqual(zlib.adler32(b"", 1), 1)
26 self.assertEqual(zlib.adler32(b"", 432), 432)
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +000027
Guido van Rossum7d9ea502003-02-03 20:45:52 +000028 def assertEqual32(self, seen, expected):
29 # 32-bit values masked -- checksums on 32- vs 64- bit machines
30 # This is important if bit 31 (0x08000000L) is set.
Guido van Rossume2a383d2007-01-15 16:59:06 +000031 self.assertEqual(seen & 0x0FFFFFFFF, expected & 0x0FFFFFFFF)
Guido van Rossum7d9ea502003-02-03 20:45:52 +000032
33 def test_penguins(self):
Guido van Rossum776152b2007-05-22 22:44:07 +000034 self.assertEqual32(zlib.crc32(b"penguin", 0), 0x0e5c1a120)
35 self.assertEqual32(zlib.crc32(b"penguin", 1), 0x43b6aa94)
36 self.assertEqual32(zlib.adler32(b"penguin", 0), 0x0bcf02f6)
37 self.assertEqual32(zlib.adler32(b"penguin", 1), 0x0bd602f7)
Guido van Rossum7d9ea502003-02-03 20:45:52 +000038
Guido van Rossum776152b2007-05-22 22:44:07 +000039 self.assertEqual(zlib.crc32(b"penguin"), zlib.crc32(b"penguin", 0))
40 self.assertEqual(zlib.adler32(b"penguin"),zlib.adler32(b"penguin",1))
Guido van Rossum7d9ea502003-02-03 20:45:52 +000041
Gregory P. Smithab0d8a12008-03-17 20:24:09 +000042 def test_crc32_adler32_unsigned(self):
43 foo = 'abcdefghijklmnop'
44 # explicitly test signed behavior
Gregory P. Smith27275032008-03-20 06:20:09 +000045 self.assertEqual(zlib.crc32(foo), 2486878355)
Gregory P. Smithab0d8a12008-03-17 20:24:09 +000046 self.assertEqual(zlib.crc32('spam'), 1138425661)
47 self.assertEqual(zlib.adler32(foo+foo), 3573550353)
48 self.assertEqual(zlib.adler32('spam'), 72286642)
49
Christian Heimesd5e2b6f2008-03-19 21:50:51 +000050 def test_same_as_binascii_crc32(self):
Martin v. Löwis15b16a32008-12-02 06:00:15 +000051 foo = b'abcdefghijklmnop'
Gregory P. Smith27275032008-03-20 06:20:09 +000052 crc = 2486878355
Christian Heimesd5e2b6f2008-03-19 21:50:51 +000053 self.assertEqual(binascii.crc32(foo), crc)
54 self.assertEqual(zlib.crc32(foo), crc)
Martin v. Löwis15b16a32008-12-02 06:00:15 +000055 self.assertEqual(binascii.crc32(b'spam'), zlib.crc32(b'spam'))
Guido van Rossum7d9ea502003-02-03 20:45:52 +000056
57
Christian Heimesb186d002008-03-18 15:15:01 +000058
Guido van Rossum7d9ea502003-02-03 20:45:52 +000059class ExceptionTestCase(unittest.TestCase):
60 # make sure we generate some expected errors
Guido van Rossum8ce8a782007-11-01 19:42:39 +000061 def test_badlevel(self):
62 # specifying compression level out of range causes an error
63 # (but -1 is Z_DEFAULT_COMPRESSION and apparently the zlib
64 # accepts 0 too)
65 self.assertRaises(zlib.error, zlib.compress, 'ERROR', 10)
Guido van Rossum7d9ea502003-02-03 20:45:52 +000066
67 def test_badcompressobj(self):
68 # verify failure on building compress object with bad params
Neil Schemenauer94afd3e2004-06-05 19:02:52 +000069 self.assertRaises(ValueError, zlib.compressobj, 1, zlib.DEFLATED, 0)
Guido van Rossum8ce8a782007-11-01 19:42:39 +000070 # specifying total bits too large causes an error
71 self.assertRaises(ValueError,
72 zlib.compressobj, 1, zlib.DEFLATED, zlib.MAX_WBITS + 1)
Guido van Rossum7d9ea502003-02-03 20:45:52 +000073
74 def test_baddecompressobj(self):
75 # verify failure on building decompress object with bad params
76 self.assertRaises(ValueError, zlib.decompressobj, 0)
77
Christian Heimes5e696852008-04-09 08:37:03 +000078 def test_decompressobj_badflush(self):
79 # verify failure on calling decompressobj.flush with bad params
80 self.assertRaises(ValueError, zlib.decompressobj().flush, 0)
81 self.assertRaises(ValueError, zlib.decompressobj().flush, -1)
82
Guido van Rossum7d9ea502003-02-03 20:45:52 +000083
84
85class CompressTestCase(unittest.TestCase):
86 # Test compression in one go (whole message compression)
87 def test_speech(self):
Neil Schemenauer6412b122004-06-05 19:34:28 +000088 x = zlib.compress(HAMLET_SCENE)
89 self.assertEqual(zlib.decompress(x), HAMLET_SCENE)
Guido van Rossum7d9ea502003-02-03 20:45:52 +000090
91 def test_speech128(self):
Neil Schemenauer6412b122004-06-05 19:34:28 +000092 # compress more data
93 data = HAMLET_SCENE * 128
Guido van Rossum7d9ea502003-02-03 20:45:52 +000094 x = zlib.compress(data)
95 self.assertEqual(zlib.decompress(x), data)
96
Guido van Rossum7d9ea502003-02-03 20:45:52 +000097
98
99
100class CompressObjectTestCase(unittest.TestCase):
101 # Test compression object
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000102 def test_pair(self):
Neil Schemenauer6412b122004-06-05 19:34:28 +0000103 # straightforward compress/decompress objects
104 data = HAMLET_SCENE * 128
105 co = zlib.compressobj()
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000106 x1 = co.compress(data)
107 x2 = co.flush()
108 self.assertRaises(zlib.error, co.flush) # second flush should not work
Neil Schemenauer94afd3e2004-06-05 19:02:52 +0000109 dco = zlib.decompressobj()
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000110 y1 = dco.decompress(x1 + x2)
111 y2 = dco.flush()
112 self.assertEqual(data, y1 + y2)
Gregory P. Smith693fc462008-09-06 20:13:06 +0000113 self.assert_(isinstance(dco.unconsumed_tail, bytes))
114 self.assert_(isinstance(dco.unused_data, bytes))
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000115
Neil Schemenauer94afd3e2004-06-05 19:02:52 +0000116 def test_compressoptions(self):
117 # specify lots of options to compressobj()
118 level = 2
119 method = zlib.DEFLATED
120 wbits = -12
121 memlevel = 9
122 strategy = zlib.Z_FILTERED
123 co = zlib.compressobj(level, method, wbits, memlevel, strategy)
Neil Schemenauer6412b122004-06-05 19:34:28 +0000124 x1 = co.compress(HAMLET_SCENE)
Neil Schemenauer94afd3e2004-06-05 19:02:52 +0000125 x2 = co.flush()
126 dco = zlib.decompressobj(wbits)
127 y1 = dco.decompress(x1 + x2)
128 y2 = dco.flush()
Neil Schemenauer6412b122004-06-05 19:34:28 +0000129 self.assertEqual(HAMLET_SCENE, y1 + y2)
Neil Schemenauer94afd3e2004-06-05 19:02:52 +0000130
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000131 def test_compressincremental(self):
132 # compress object in steps, decompress object as one-shot
Neil Schemenauer6412b122004-06-05 19:34:28 +0000133 data = HAMLET_SCENE * 128
Neil Schemenauer94afd3e2004-06-05 19:02:52 +0000134 co = zlib.compressobj()
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000135 bufs = []
136 for i in range(0, len(data), 256):
137 bufs.append(co.compress(data[i:i+256]))
138 bufs.append(co.flush())
Guido van Rossum776152b2007-05-22 22:44:07 +0000139 combuf = b''.join(bufs)
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000140
Neil Schemenauer94afd3e2004-06-05 19:02:52 +0000141 dco = zlib.decompressobj()
Guido van Rossum776152b2007-05-22 22:44:07 +0000142 y1 = dco.decompress(b''.join(bufs))
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000143 y2 = dco.flush()
144 self.assertEqual(data, y1 + y2)
145
Neil Schemenauer6412b122004-06-05 19:34:28 +0000146 def test_decompinc(self, flush=False, source=None, cx=256, dcx=64):
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000147 # compress object in steps, decompress object in steps
Neil Schemenauer6412b122004-06-05 19:34:28 +0000148 source = source or HAMLET_SCENE
149 data = source * 128
Neil Schemenauer94afd3e2004-06-05 19:02:52 +0000150 co = zlib.compressobj()
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000151 bufs = []
Neil Schemenauer6412b122004-06-05 19:34:28 +0000152 for i in range(0, len(data), cx):
153 bufs.append(co.compress(data[i:i+cx]))
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000154 bufs.append(co.flush())
Guido van Rossum776152b2007-05-22 22:44:07 +0000155 combuf = b''.join(bufs)
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000156
Gregory P. Smith693fc462008-09-06 20:13:06 +0000157 decombuf = zlib.decompress(combuf)
158 # Test type of return value
159 self.assert_(isinstance(decombuf, bytes))
160
161 self.assertEqual(data, decombuf)
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000162
Neil Schemenauer94afd3e2004-06-05 19:02:52 +0000163 dco = zlib.decompressobj()
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000164 bufs = []
Neil Schemenauer6412b122004-06-05 19:34:28 +0000165 for i in range(0, len(combuf), dcx):
166 bufs.append(dco.decompress(combuf[i:i+dcx]))
Guido van Rossum776152b2007-05-22 22:44:07 +0000167 self.assertEqual(b'', dco.unconsumed_tail, ########
168 "(A) uct should be b'': not %d long" %
Neil Schemenauer6412b122004-06-05 19:34:28 +0000169 len(dco.unconsumed_tail))
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000170 self.assertEqual(b'', dco.unused_data)
Neil Schemenauer6412b122004-06-05 19:34:28 +0000171 if flush:
172 bufs.append(dco.flush())
173 else:
174 while True:
175 chunk = dco.decompress('')
176 if chunk:
177 bufs.append(chunk)
178 else:
179 break
Guido van Rossum776152b2007-05-22 22:44:07 +0000180 self.assertEqual(b'', dco.unconsumed_tail, ########
181 "(B) uct should be b'': not %d long" %
Neil Schemenauer6412b122004-06-05 19:34:28 +0000182 len(dco.unconsumed_tail))
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000183 self.assertEqual(b'', dco.unused_data)
Guido van Rossum776152b2007-05-22 22:44:07 +0000184 self.assertEqual(data, b''.join(bufs))
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000185 # Failure means: "decompressobj with init options failed"
186
Neil Schemenauer6412b122004-06-05 19:34:28 +0000187 def test_decompincflush(self):
188 self.test_decompinc(flush=True)
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000189
Neil Schemenauer6412b122004-06-05 19:34:28 +0000190 def test_decompimax(self, source=None, cx=256, dcx=64):
191 # compress in steps, decompress in length-restricted steps
192 source = source or HAMLET_SCENE
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000193 # Check a decompression object with max_length specified
Neil Schemenauer6412b122004-06-05 19:34:28 +0000194 data = source * 128
195 co = zlib.compressobj()
196 bufs = []
197 for i in range(0, len(data), cx):
198 bufs.append(co.compress(data[i:i+cx]))
199 bufs.append(co.flush())
Guido van Rossum776152b2007-05-22 22:44:07 +0000200 combuf = b''.join(bufs)
Neil Schemenauer6412b122004-06-05 19:34:28 +0000201 self.assertEqual(data, zlib.decompress(combuf),
202 'compressed data failure')
203
204 dco = zlib.decompressobj()
205 bufs = []
206 cb = combuf
207 while cb:
208 #max_length = 1 + len(cb)//10
209 chunk = dco.decompress(cb, dcx)
210 self.failIf(len(chunk) > dcx,
211 'chunk too big (%d>%d)' % (len(chunk), dcx))
212 bufs.append(chunk)
213 cb = dco.unconsumed_tail
214 bufs.append(dco.flush())
Guido van Rossum776152b2007-05-22 22:44:07 +0000215 self.assertEqual(data, b''.join(bufs), 'Wrong data retrieved')
Neil Schemenauer6412b122004-06-05 19:34:28 +0000216
217 def test_decompressmaxlen(self, flush=False):
218 # Check a decompression object with max_length specified
219 data = HAMLET_SCENE * 128
Neil Schemenauer94afd3e2004-06-05 19:02:52 +0000220 co = zlib.compressobj()
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000221 bufs = []
222 for i in range(0, len(data), 256):
223 bufs.append(co.compress(data[i:i+256]))
224 bufs.append(co.flush())
Guido van Rossum776152b2007-05-22 22:44:07 +0000225 combuf = b''.join(bufs)
Neil Schemenauer94afd3e2004-06-05 19:02:52 +0000226 self.assertEqual(data, zlib.decompress(combuf),
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000227 'compressed data failure')
228
Neil Schemenauer94afd3e2004-06-05 19:02:52 +0000229 dco = zlib.decompressobj()
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000230 bufs = []
231 cb = combuf
232 while cb:
Guido van Rossumf3594102003-02-27 18:39:18 +0000233 max_length = 1 + len(cb)//10
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000234 chunk = dco.decompress(cb, max_length)
235 self.failIf(len(chunk) > max_length,
236 'chunk too big (%d>%d)' % (len(chunk),max_length))
237 bufs.append(chunk)
238 cb = dco.unconsumed_tail
Neil Schemenauer6412b122004-06-05 19:34:28 +0000239 if flush:
240 bufs.append(dco.flush())
241 else:
242 while chunk:
243 chunk = dco.decompress('', max_length)
244 self.failIf(len(chunk) > max_length,
245 'chunk too big (%d>%d)' % (len(chunk),max_length))
246 bufs.append(chunk)
Guido van Rossum776152b2007-05-22 22:44:07 +0000247 self.assertEqual(data, b''.join(bufs), 'Wrong data retrieved')
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000248
Neil Schemenauer6412b122004-06-05 19:34:28 +0000249 def test_decompressmaxlenflush(self):
250 self.test_decompressmaxlen(flush=True)
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000251
252 def test_maxlenmisc(self):
253 # Misc tests of max_length
Neil Schemenauer94afd3e2004-06-05 19:02:52 +0000254 dco = zlib.decompressobj()
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000255 self.assertRaises(ValueError, dco.decompress, "", -1)
Guido van Rossum776152b2007-05-22 22:44:07 +0000256 self.assertEqual(b'', dco.unconsumed_tail)
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000257
258 def test_flushes(self):
259 # Test flush() with the various options, using all the
260 # different levels in order to provide more variations.
261 sync_opt = ['Z_NO_FLUSH', 'Z_SYNC_FLUSH', 'Z_FULL_FLUSH']
262 sync_opt = [getattr(zlib, opt) for opt in sync_opt
263 if hasattr(zlib, opt)]
Neil Schemenauer6412b122004-06-05 19:34:28 +0000264 data = HAMLET_SCENE * 8
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000265
266 for sync in sync_opt:
267 for level in range(10):
268 obj = zlib.compressobj( level )
269 a = obj.compress( data[:3000] )
270 b = obj.flush( sync )
271 c = obj.compress( data[3000:] )
272 d = obj.flush()
Guido van Rossum776152b2007-05-22 22:44:07 +0000273 self.assertEqual(zlib.decompress(b''.join([a,b,c,d])),
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000274 data, ("Decompress failed: flush "
275 "mode=%i, level=%i") % (sync, level))
276 del obj
277
278 def test_odd_flush(self):
279 # Test for odd flushing bugs noted in 2.0, and hopefully fixed in 2.1
280 import random
281
282 if hasattr(zlib, 'Z_SYNC_FLUSH'):
283 # Testing on 17K of "random" data
284
285 # Create compressor and decompressor objects
Neil Schemenauer6412b122004-06-05 19:34:28 +0000286 co = zlib.compressobj(zlib.Z_BEST_COMPRESSION)
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000287 dco = zlib.decompressobj()
288
289 # Try 17K of data
290 # generate random data stream
291 try:
292 # In 2.3 and later, WichmannHill is the RNG of the bug report
293 gen = random.WichmannHill()
294 except AttributeError:
295 try:
296 # 2.2 called it Random
297 gen = random.Random()
298 except AttributeError:
299 # others might simply have a single RNG
300 gen = random
301 gen.seed(1)
302 data = genblock(1, 17 * 1024, generator=gen)
303
304 # compress, sync-flush, and decompress
305 first = co.compress(data)
306 second = co.flush(zlib.Z_SYNC_FLUSH)
307 expanded = dco.decompress(first + second)
308
309 # if decompressed data is different from the input data, choke.
310 self.assertEqual(expanded, data, "17K random source doesn't match")
311
Andrew M. Kuchling3b585b32004-12-28 20:10:48 +0000312 def test_empty_flush(self):
313 # Test that calling .flush() on unused objects works.
314 # (Bug #1083110 -- calling .flush() on decompress objects
315 # caused a core dump.)
316
317 co = zlib.compressobj(zlib.Z_BEST_COMPRESSION)
318 self.failUnless(co.flush()) # Returns a zlib header
319 dco = zlib.decompressobj()
Guido van Rossum776152b2007-05-22 22:44:07 +0000320 self.assertEqual(dco.flush(), b"") # Returns nothing
Tim Peters5a9fb3c2005-01-07 16:01:32 +0000321
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000322 if hasattr(zlib.compressobj(), "copy"):
323 def test_compresscopy(self):
324 # Test copying a compression object
325 data0 = HAMLET_SCENE
Guido van Rossum776152b2007-05-22 22:44:07 +0000326 data1 = bytes(str(HAMLET_SCENE, "ascii").swapcase(), "ascii")
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000327 c0 = zlib.compressobj(zlib.Z_BEST_COMPRESSION)
328 bufs0 = []
329 bufs0.append(c0.compress(data0))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000330
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000331 c1 = c0.copy()
332 bufs1 = bufs0[:]
Thomas Wouters477c8d52006-05-27 19:21:47 +0000333
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000334 bufs0.append(c0.compress(data0))
335 bufs0.append(c0.flush())
Guido van Rossum776152b2007-05-22 22:44:07 +0000336 s0 = b''.join(bufs0)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000337
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000338 bufs1.append(c1.compress(data1))
339 bufs1.append(c1.flush())
Guido van Rossum776152b2007-05-22 22:44:07 +0000340 s1 = b''.join(bufs1)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000341
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000342 self.assertEqual(zlib.decompress(s0),data0+data0)
343 self.assertEqual(zlib.decompress(s1),data0+data1)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000344
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000345 def test_badcompresscopy(self):
346 # Test copying a compression object in an inconsistent state
347 c = zlib.compressobj()
348 c.compress(HAMLET_SCENE)
349 c.flush()
350 self.assertRaises(ValueError, c.copy)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000351
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000352 if hasattr(zlib.decompressobj(), "copy"):
353 def test_decompresscopy(self):
354 # Test copying a decompression object
355 data = HAMLET_SCENE
356 comp = zlib.compress(data)
Gregory P. Smith693fc462008-09-06 20:13:06 +0000357 # Test type of return value
358 self.assert_(isinstance(comp, bytes))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000359
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000360 d0 = zlib.decompressobj()
361 bufs0 = []
362 bufs0.append(d0.decompress(comp[:32]))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000363
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000364 d1 = d0.copy()
365 bufs1 = bufs0[:]
Thomas Wouters477c8d52006-05-27 19:21:47 +0000366
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000367 bufs0.append(d0.decompress(comp[32:]))
Guido van Rossum776152b2007-05-22 22:44:07 +0000368 s0 = b''.join(bufs0)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000369
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000370 bufs1.append(d1.decompress(comp[32:]))
Guido van Rossum776152b2007-05-22 22:44:07 +0000371 s1 = b''.join(bufs1)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000372
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000373 self.assertEqual(s0,s1)
374 self.assertEqual(s0,data)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000375
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000376 def test_baddecompresscopy(self):
377 # Test copying a compression object in an inconsistent state
378 data = zlib.compress(HAMLET_SCENE)
379 d = zlib.decompressobj()
380 d.decompress(data)
381 d.flush()
382 self.assertRaises(ValueError, d.copy)
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000383
384def genblock(seed, length, step=1024, generator=random):
385 """length-byte stream of random data from a seed (in step-byte blocks)."""
386 if seed is not None:
387 generator.seed(seed)
388 randint = generator.randint
389 if length < step or step < 2:
390 step = length
Guido van Rossum776152b2007-05-22 22:44:07 +0000391 blocks = bytes()
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000392 for i in range(0, length, step):
Guido van Rossum776152b2007-05-22 22:44:07 +0000393 blocks += bytes(randint(0, 255) for x in range(step))
394 return blocks
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000395
396
397
398def choose_lines(source, number, seed=None, generator=random):
399 """Return a list of number lines randomly chosen from the source"""
400 if seed is not None:
401 generator.seed(seed)
402 sources = source.split('\n')
403 return [generator.choice(sources) for n in range(number)]
404
405
406
Guido van Rossum776152b2007-05-22 22:44:07 +0000407HAMLET_SCENE = b"""
Fred Drake004d5e62000-10-23 17:22:08 +0000408LAERTES
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +0000409
410 O, fear me not.
411 I stay too long: but here my father comes.
412
413 Enter POLONIUS
414
415 A double blessing is a double grace,
416 Occasion smiles upon a second leave.
417
Fred Drake004d5e62000-10-23 17:22:08 +0000418LORD POLONIUS
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +0000419
420 Yet here, Laertes! aboard, aboard, for shame!
421 The wind sits in the shoulder of your sail,
422 And you are stay'd for. There; my blessing with thee!
423 And these few precepts in thy memory
424 See thou character. Give thy thoughts no tongue,
425 Nor any unproportioned thought his act.
426 Be thou familiar, but by no means vulgar.
427 Those friends thou hast, and their adoption tried,
428 Grapple them to thy soul with hoops of steel;
429 But do not dull thy palm with entertainment
430 Of each new-hatch'd, unfledged comrade. Beware
431 Of entrance to a quarrel, but being in,
432 Bear't that the opposed may beware of thee.
433 Give every man thy ear, but few thy voice;
434 Take each man's censure, but reserve thy judgment.
435 Costly thy habit as thy purse can buy,
436 But not express'd in fancy; rich, not gaudy;
437 For the apparel oft proclaims the man,
438 And they in France of the best rank and station
439 Are of a most select and generous chief in that.
440 Neither a borrower nor a lender be;
441 For loan oft loses both itself and friend,
442 And borrowing dulls the edge of husbandry.
443 This above all: to thine ownself be true,
444 And it must follow, as the night the day,
445 Thou canst not then be false to any man.
446 Farewell: my blessing season this in thee!
447
Fred Drake004d5e62000-10-23 17:22:08 +0000448LAERTES
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +0000449
450 Most humbly do I take my leave, my lord.
451
Fred Drake004d5e62000-10-23 17:22:08 +0000452LORD POLONIUS
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +0000453
454 The time invites you; go; your servants tend.
455
Fred Drake004d5e62000-10-23 17:22:08 +0000456LAERTES
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +0000457
458 Farewell, Ophelia; and remember well
459 What I have said to you.
460
Fred Drake004d5e62000-10-23 17:22:08 +0000461OPHELIA
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +0000462
463 'Tis in my memory lock'd,
464 And you yourself shall keep the key of it.
465
Fred Drake004d5e62000-10-23 17:22:08 +0000466LAERTES
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +0000467
468 Farewell.
469"""
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000470
471
472def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000473 support.run_unittest(
Walter Dörwald21d3a322003-05-01 17:45:56 +0000474 ChecksumTestCase,
475 ExceptionTestCase,
476 CompressTestCase,
477 CompressObjectTestCase
478 )
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000479
480if __name__ == "__main__":
Guido van Rossum776152b2007-05-22 22:44:07 +0000481 unittest.main() # XXX
482 ###test_main()