blob: a003611a7cf4fb97ccd8b4c24fb56f973df59ef6 [file] [log] [blame]
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001import unittest
Victor Stinnerf9fb4342011-05-03 15:19:23 +02002from test.test_support import TESTFN, run_unittest, import_module, unlink, requires
Gregory P. Smithc856fa82008-03-18 22:27:41 +00003import binascii
Andrew M. Kuchling9a0f98e2001-02-21 02:17:01 +00004import random
Victor Stinner7fd90c42011-05-04 21:27:39 +02005from test.test_support import precisionbigmemtest, _1G, _4G
Victor Stinnerf9fb4342011-05-03 15:19:23 +02006import sys
Andrew M. Kuchling9a0f98e2001-02-21 02:17:01 +00007
Victor Stinnerf9fb4342011-05-03 15:19:23 +02008try:
9 import mmap
10except ImportError:
11 mmap = None
12
13zlib = import_module('zlib')
R. David Murray3db8a342009-03-30 23:05:48 +000014
Andrew M. Kuchling9a0f98e2001-02-21 02:17:01 +000015
Guido van Rossum7d9ea502003-02-03 20:45:52 +000016class ChecksumTestCase(unittest.TestCase):
17 # checksum test cases
18 def test_crc32start(self):
19 self.assertEqual(zlib.crc32(""), zlib.crc32("", 0))
Benjamin Peterson5c8da862009-06-30 22:57:08 +000020 self.assertTrue(zlib.crc32("abc", 0xffffffff))
Andrew M. Kuchlingfcfc8d52001-08-10 15:50:11 +000021
Guido van Rossum7d9ea502003-02-03 20:45:52 +000022 def test_crc32empty(self):
23 self.assertEqual(zlib.crc32("", 0), 0)
24 self.assertEqual(zlib.crc32("", 1), 1)
25 self.assertEqual(zlib.crc32("", 432), 432)
Andrew M. Kuchling9a0f98e2001-02-21 02:17:01 +000026
Guido van Rossum7d9ea502003-02-03 20:45:52 +000027 def test_adler32start(self):
28 self.assertEqual(zlib.adler32(""), zlib.adler32("", 1))
Benjamin Peterson5c8da862009-06-30 22:57:08 +000029 self.assertTrue(zlib.adler32("abc", 0xffffffff))
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +000030
Guido van Rossum7d9ea502003-02-03 20:45:52 +000031 def test_adler32empty(self):
32 self.assertEqual(zlib.adler32("", 0), 0)
33 self.assertEqual(zlib.adler32("", 1), 1)
34 self.assertEqual(zlib.adler32("", 432), 432)
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +000035
Guido van Rossum7d9ea502003-02-03 20:45:52 +000036 def assertEqual32(self, seen, expected):
37 # 32-bit values masked -- checksums on 32- vs 64- bit machines
38 # This is important if bit 31 (0x08000000L) is set.
39 self.assertEqual(seen & 0x0FFFFFFFFL, expected & 0x0FFFFFFFFL)
40
41 def test_penguins(self):
42 self.assertEqual32(zlib.crc32("penguin", 0), 0x0e5c1a120L)
43 self.assertEqual32(zlib.crc32("penguin", 1), 0x43b6aa94)
44 self.assertEqual32(zlib.adler32("penguin", 0), 0x0bcf02f6)
45 self.assertEqual32(zlib.adler32("penguin", 1), 0x0bd602f7)
46
47 self.assertEqual(zlib.crc32("penguin"), zlib.crc32("penguin", 0))
48 self.assertEqual(zlib.adler32("penguin"),zlib.adler32("penguin",1))
49
Gregory P. Smithf48f9d32008-03-17 18:48:05 +000050 def test_abcdefghijklmnop(self):
51 """test issue1202 compliance: signed crc32, adler32 in 2.x"""
52 foo = 'abcdefghijklmnop'
53 # explicitly test signed behavior
54 self.assertEqual(zlib.crc32(foo), -1808088941)
55 self.assertEqual(zlib.crc32('spam'), 1138425661)
56 self.assertEqual(zlib.adler32(foo+foo), -721416943)
57 self.assertEqual(zlib.adler32('spam'), 72286642)
58
Gregory P. Smithc856fa82008-03-18 22:27:41 +000059 def test_same_as_binascii_crc32(self):
60 foo = 'abcdefghijklmnop'
61 self.assertEqual(binascii.crc32(foo), zlib.crc32(foo))
62 self.assertEqual(binascii.crc32('spam'), zlib.crc32('spam'))
63
Gregory P. Smith88440962008-03-25 06:12:45 +000064 def test_negative_crc_iv_input(self):
65 # The range of valid input values for the crc state should be
66 # -2**31 through 2**32-1 to allow inputs artifically constrained
67 # to a signed 32-bit integer.
68 self.assertEqual(zlib.crc32('ham', -1), zlib.crc32('ham', 0xffffffffL))
69 self.assertEqual(zlib.crc32('spam', -3141593),
70 zlib.crc32('spam', 0xffd01027L))
71 self.assertEqual(zlib.crc32('spam', -(2**31)),
72 zlib.crc32('spam', (2**31)))
Guido van Rossum7d9ea502003-02-03 20:45:52 +000073
74
75class ExceptionTestCase(unittest.TestCase):
76 # make sure we generate some expected errors
Armin Rigoec560192007-10-15 07:48:35 +000077 def test_badlevel(self):
78 # specifying compression level out of range causes an error
79 # (but -1 is Z_DEFAULT_COMPRESSION and apparently the zlib
80 # accepts 0 too)
81 self.assertRaises(zlib.error, zlib.compress, 'ERROR', 10)
Guido van Rossum7d9ea502003-02-03 20:45:52 +000082
83 def test_badcompressobj(self):
84 # verify failure on building compress object with bad params
Neil Schemenauer94afd3e2004-06-05 19:02:52 +000085 self.assertRaises(ValueError, zlib.compressobj, 1, zlib.DEFLATED, 0)
Armin Rigoec560192007-10-15 07:48:35 +000086 # specifying total bits too large causes an error
87 self.assertRaises(ValueError,
88 zlib.compressobj, 1, zlib.DEFLATED, zlib.MAX_WBITS + 1)
Guido van Rossum7d9ea502003-02-03 20:45:52 +000089
90 def test_baddecompressobj(self):
91 # verify failure on building decompress object with bad params
Antoine Pitrou3b4c9892010-04-06 17:21:09 +000092 self.assertRaises(ValueError, zlib.decompressobj, -1)
Guido van Rossum7d9ea502003-02-03 20:45:52 +000093
Gregory P. Smith79e42a02008-04-09 00:25:17 +000094 def test_decompressobj_badflush(self):
95 # verify failure on calling decompressobj.flush with bad params
96 self.assertRaises(ValueError, zlib.decompressobj().flush, 0)
97 self.assertRaises(ValueError, zlib.decompressobj().flush, -1)
98
Guido van Rossum7d9ea502003-02-03 20:45:52 +000099
Antoine Pitrou3843cd82010-05-07 16:50:34 +0000100class BaseCompressTestCase(object):
101 def check_big_compress_buffer(self, size, compress_func):
102 _1M = 1024 * 1024
103 fmt = "%%0%dx" % (2 * _1M)
104 # Generate 10MB worth of random, and expand it by repeating it.
105 # The assumption is that zlib's memory is not big enough to exploit
106 # such spread out redundancy.
107 data = ''.join([binascii.a2b_hex(fmt % random.getrandbits(8 * _1M))
108 for i in range(10)])
109 data = data * (size // len(data) + 1)
110 try:
111 compress_func(data)
112 finally:
113 # Release memory
114 data = None
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000115
Antoine Pitrou3843cd82010-05-07 16:50:34 +0000116 def check_big_decompress_buffer(self, size, decompress_func):
117 data = 'x' * size
118 try:
119 compressed = zlib.compress(data, 1)
120 finally:
121 # Release memory
122 data = None
123 data = decompress_func(compressed)
124 # Sanity check
125 try:
126 self.assertEqual(len(data), size)
127 self.assertEqual(len(data.strip('x')), 0)
128 finally:
129 data = None
130
131
132class CompressTestCase(BaseCompressTestCase, unittest.TestCase):
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000133 # Test compression in one go (whole message compression)
134 def test_speech(self):
Neil Schemenauer6412b122004-06-05 19:34:28 +0000135 x = zlib.compress(HAMLET_SCENE)
136 self.assertEqual(zlib.decompress(x), HAMLET_SCENE)
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000137
138 def test_speech128(self):
Neil Schemenauer6412b122004-06-05 19:34:28 +0000139 # compress more data
140 data = HAMLET_SCENE * 128
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000141 x = zlib.compress(data)
142 self.assertEqual(zlib.decompress(x), data)
143
Antoine Pitroufc3bfad2010-05-11 23:42:28 +0000144 def test_incomplete_stream(self):
145 # An useful error message is given
146 x = zlib.compress(HAMLET_SCENE)
147 self.assertRaisesRegexp(zlib.error,
148 "Error -5 while decompressing data: incomplete or truncated stream",
149 zlib.decompress, x[:-1])
150
Antoine Pitrou3843cd82010-05-07 16:50:34 +0000151 # Memory use of the following functions takes into account overallocation
152
153 @precisionbigmemtest(size=_1G + 1024 * 1024, memuse=3)
154 def test_big_compress_buffer(self, size):
155 compress = lambda s: zlib.compress(s, 1)
156 self.check_big_compress_buffer(size, compress)
157
158 @precisionbigmemtest(size=_1G + 1024 * 1024, memuse=2)
159 def test_big_decompress_buffer(self, size):
160 self.check_big_decompress_buffer(size, zlib.decompress)
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000161
162
Antoine Pitrou3843cd82010-05-07 16:50:34 +0000163class CompressObjectTestCase(BaseCompressTestCase, unittest.TestCase):
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000164 # Test compression object
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000165 def test_pair(self):
Neil Schemenauer6412b122004-06-05 19:34:28 +0000166 # straightforward compress/decompress objects
167 data = HAMLET_SCENE * 128
168 co = zlib.compressobj()
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000169 x1 = co.compress(data)
170 x2 = co.flush()
171 self.assertRaises(zlib.error, co.flush) # second flush should not work
Neil Schemenauer94afd3e2004-06-05 19:02:52 +0000172 dco = zlib.decompressobj()
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000173 y1 = dco.decompress(x1 + x2)
174 y2 = dco.flush()
175 self.assertEqual(data, y1 + y2)
176
Neil Schemenauer94afd3e2004-06-05 19:02:52 +0000177 def test_compressoptions(self):
178 # specify lots of options to compressobj()
179 level = 2
180 method = zlib.DEFLATED
181 wbits = -12
182 memlevel = 9
183 strategy = zlib.Z_FILTERED
184 co = zlib.compressobj(level, method, wbits, memlevel, strategy)
Neil Schemenauer6412b122004-06-05 19:34:28 +0000185 x1 = co.compress(HAMLET_SCENE)
Neil Schemenauer94afd3e2004-06-05 19:02:52 +0000186 x2 = co.flush()
187 dco = zlib.decompressobj(wbits)
188 y1 = dco.decompress(x1 + x2)
189 y2 = dco.flush()
Neil Schemenauer6412b122004-06-05 19:34:28 +0000190 self.assertEqual(HAMLET_SCENE, y1 + y2)
Neil Schemenauer94afd3e2004-06-05 19:02:52 +0000191
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000192 def test_compressincremental(self):
193 # compress object in steps, decompress object as one-shot
Neil Schemenauer6412b122004-06-05 19:34:28 +0000194 data = HAMLET_SCENE * 128
Neil Schemenauer94afd3e2004-06-05 19:02:52 +0000195 co = zlib.compressobj()
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000196 bufs = []
197 for i in range(0, len(data), 256):
198 bufs.append(co.compress(data[i:i+256]))
199 bufs.append(co.flush())
200 combuf = ''.join(bufs)
201
Neil Schemenauer94afd3e2004-06-05 19:02:52 +0000202 dco = zlib.decompressobj()
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000203 y1 = dco.decompress(''.join(bufs))
204 y2 = dco.flush()
205 self.assertEqual(data, y1 + y2)
206
Neil Schemenauer6412b122004-06-05 19:34:28 +0000207 def test_decompinc(self, flush=False, source=None, cx=256, dcx=64):
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000208 # compress object in steps, decompress object in steps
Neil Schemenauer6412b122004-06-05 19:34:28 +0000209 source = source or HAMLET_SCENE
210 data = source * 128
Neil Schemenauer94afd3e2004-06-05 19:02:52 +0000211 co = zlib.compressobj()
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000212 bufs = []
Neil Schemenauer6412b122004-06-05 19:34:28 +0000213 for i in range(0, len(data), cx):
214 bufs.append(co.compress(data[i:i+cx]))
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000215 bufs.append(co.flush())
216 combuf = ''.join(bufs)
217
Neil Schemenauer94afd3e2004-06-05 19:02:52 +0000218 self.assertEqual(data, zlib.decompress(combuf))
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000219
Neil Schemenauer94afd3e2004-06-05 19:02:52 +0000220 dco = zlib.decompressobj()
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000221 bufs = []
Neil Schemenauer6412b122004-06-05 19:34:28 +0000222 for i in range(0, len(combuf), dcx):
223 bufs.append(dco.decompress(combuf[i:i+dcx]))
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000224 self.assertEqual('', dco.unconsumed_tail, ########
225 "(A) uct should be '': not %d long" %
Neil Schemenauer6412b122004-06-05 19:34:28 +0000226 len(dco.unconsumed_tail))
227 if flush:
228 bufs.append(dco.flush())
229 else:
230 while True:
231 chunk = dco.decompress('')
232 if chunk:
233 bufs.append(chunk)
234 else:
235 break
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000236 self.assertEqual('', dco.unconsumed_tail, ########
Neil Schemenauer6412b122004-06-05 19:34:28 +0000237 "(B) uct should be '': not %d long" %
238 len(dco.unconsumed_tail))
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000239 self.assertEqual(data, ''.join(bufs))
240 # Failure means: "decompressobj with init options failed"
241
Neil Schemenauer6412b122004-06-05 19:34:28 +0000242 def test_decompincflush(self):
243 self.test_decompinc(flush=True)
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000244
Neil Schemenauer6412b122004-06-05 19:34:28 +0000245 def test_decompimax(self, source=None, cx=256, dcx=64):
246 # compress in steps, decompress in length-restricted steps
247 source = source or HAMLET_SCENE
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000248 # Check a decompression object with max_length specified
Neil Schemenauer6412b122004-06-05 19:34:28 +0000249 data = source * 128
250 co = zlib.compressobj()
251 bufs = []
252 for i in range(0, len(data), cx):
253 bufs.append(co.compress(data[i:i+cx]))
254 bufs.append(co.flush())
255 combuf = ''.join(bufs)
256 self.assertEqual(data, zlib.decompress(combuf),
257 'compressed data failure')
258
259 dco = zlib.decompressobj()
260 bufs = []
261 cb = combuf
262 while cb:
263 #max_length = 1 + len(cb)//10
264 chunk = dco.decompress(cb, dcx)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000265 self.assertFalse(len(chunk) > dcx,
Neil Schemenauer6412b122004-06-05 19:34:28 +0000266 'chunk too big (%d>%d)' % (len(chunk), dcx))
267 bufs.append(chunk)
268 cb = dco.unconsumed_tail
269 bufs.append(dco.flush())
270 self.assertEqual(data, ''.join(bufs), 'Wrong data retrieved')
271
272 def test_decompressmaxlen(self, flush=False):
273 # Check a decompression object with max_length specified
274 data = HAMLET_SCENE * 128
Neil Schemenauer94afd3e2004-06-05 19:02:52 +0000275 co = zlib.compressobj()
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000276 bufs = []
277 for i in range(0, len(data), 256):
278 bufs.append(co.compress(data[i:i+256]))
279 bufs.append(co.flush())
280 combuf = ''.join(bufs)
Neil Schemenauer94afd3e2004-06-05 19:02:52 +0000281 self.assertEqual(data, zlib.decompress(combuf),
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000282 'compressed data failure')
283
Neil Schemenauer94afd3e2004-06-05 19:02:52 +0000284 dco = zlib.decompressobj()
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000285 bufs = []
286 cb = combuf
287 while cb:
Guido van Rossumf3594102003-02-27 18:39:18 +0000288 max_length = 1 + len(cb)//10
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000289 chunk = dco.decompress(cb, max_length)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000290 self.assertFalse(len(chunk) > max_length,
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000291 'chunk too big (%d>%d)' % (len(chunk),max_length))
292 bufs.append(chunk)
293 cb = dco.unconsumed_tail
Neil Schemenauer6412b122004-06-05 19:34:28 +0000294 if flush:
295 bufs.append(dco.flush())
296 else:
297 while chunk:
298 chunk = dco.decompress('', max_length)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000299 self.assertFalse(len(chunk) > max_length,
Neil Schemenauer6412b122004-06-05 19:34:28 +0000300 'chunk too big (%d>%d)' % (len(chunk),max_length))
301 bufs.append(chunk)
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000302 self.assertEqual(data, ''.join(bufs), 'Wrong data retrieved')
303
Neil Schemenauer6412b122004-06-05 19:34:28 +0000304 def test_decompressmaxlenflush(self):
305 self.test_decompressmaxlen(flush=True)
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000306
307 def test_maxlenmisc(self):
308 # Misc tests of max_length
Neil Schemenauer94afd3e2004-06-05 19:02:52 +0000309 dco = zlib.decompressobj()
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000310 self.assertRaises(ValueError, dco.decompress, "", -1)
311 self.assertEqual('', dco.unconsumed_tail)
312
313 def test_flushes(self):
314 # Test flush() with the various options, using all the
315 # different levels in order to provide more variations.
316 sync_opt = ['Z_NO_FLUSH', 'Z_SYNC_FLUSH', 'Z_FULL_FLUSH']
317 sync_opt = [getattr(zlib, opt) for opt in sync_opt
318 if hasattr(zlib, opt)]
Neil Schemenauer6412b122004-06-05 19:34:28 +0000319 data = HAMLET_SCENE * 8
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000320
321 for sync in sync_opt:
322 for level in range(10):
323 obj = zlib.compressobj( level )
324 a = obj.compress( data[:3000] )
325 b = obj.flush( sync )
326 c = obj.compress( data[3000:] )
327 d = obj.flush()
328 self.assertEqual(zlib.decompress(''.join([a,b,c,d])),
329 data, ("Decompress failed: flush "
330 "mode=%i, level=%i") % (sync, level))
331 del obj
332
333 def test_odd_flush(self):
334 # Test for odd flushing bugs noted in 2.0, and hopefully fixed in 2.1
335 import random
336
337 if hasattr(zlib, 'Z_SYNC_FLUSH'):
338 # Testing on 17K of "random" data
339
340 # Create compressor and decompressor objects
Neil Schemenauer6412b122004-06-05 19:34:28 +0000341 co = zlib.compressobj(zlib.Z_BEST_COMPRESSION)
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000342 dco = zlib.decompressobj()
343
344 # Try 17K of data
345 # generate random data stream
346 try:
347 # In 2.3 and later, WichmannHill is the RNG of the bug report
348 gen = random.WichmannHill()
349 except AttributeError:
350 try:
351 # 2.2 called it Random
352 gen = random.Random()
353 except AttributeError:
354 # others might simply have a single RNG
355 gen = random
356 gen.seed(1)
357 data = genblock(1, 17 * 1024, generator=gen)
358
359 # compress, sync-flush, and decompress
360 first = co.compress(data)
361 second = co.flush(zlib.Z_SYNC_FLUSH)
362 expanded = dco.decompress(first + second)
363
364 # if decompressed data is different from the input data, choke.
365 self.assertEqual(expanded, data, "17K random source doesn't match")
366
Andrew M. Kuchling3b585b32004-12-28 20:10:48 +0000367 def test_empty_flush(self):
368 # Test that calling .flush() on unused objects works.
369 # (Bug #1083110 -- calling .flush() on decompress objects
370 # caused a core dump.)
371
372 co = zlib.compressobj(zlib.Z_BEST_COMPRESSION)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000373 self.assertTrue(co.flush()) # Returns a zlib header
Andrew M. Kuchling3b585b32004-12-28 20:10:48 +0000374 dco = zlib.decompressobj()
375 self.assertEqual(dco.flush(), "") # Returns nothing
Tim Peters5a9fb3c2005-01-07 16:01:32 +0000376
Antoine Pitrou37ffc3e2010-05-11 23:32:31 +0000377 def test_decompress_incomplete_stream(self):
378 # This is 'foo', deflated
379 x = 'x\x9cK\xcb\xcf\x07\x00\x02\x82\x01E'
380 # For the record
381 self.assertEqual(zlib.decompress(x), 'foo')
382 self.assertRaises(zlib.error, zlib.decompress, x[:-5])
383 # Omitting the stream end works with decompressor objects
384 # (see issue #8672).
385 dco = zlib.decompressobj()
386 y = dco.decompress(x[:-5])
387 y += dco.flush()
388 self.assertEqual(y, 'foo')
389
Neal Norwitz6e73aaa2006-06-12 03:33:09 +0000390 if hasattr(zlib.compressobj(), "copy"):
391 def test_compresscopy(self):
392 # Test copying a compression object
393 data0 = HAMLET_SCENE
394 data1 = HAMLET_SCENE.swapcase()
395 c0 = zlib.compressobj(zlib.Z_BEST_COMPRESSION)
396 bufs0 = []
397 bufs0.append(c0.compress(data0))
Georg Brandl8d3342b2006-05-16 07:38:27 +0000398
Neal Norwitz6e73aaa2006-06-12 03:33:09 +0000399 c1 = c0.copy()
400 bufs1 = bufs0[:]
Georg Brandl8d3342b2006-05-16 07:38:27 +0000401
Neal Norwitz6e73aaa2006-06-12 03:33:09 +0000402 bufs0.append(c0.compress(data0))
403 bufs0.append(c0.flush())
404 s0 = ''.join(bufs0)
Georg Brandl8d3342b2006-05-16 07:38:27 +0000405
Neal Norwitz6e73aaa2006-06-12 03:33:09 +0000406 bufs1.append(c1.compress(data1))
407 bufs1.append(c1.flush())
408 s1 = ''.join(bufs1)
Georg Brandl8d3342b2006-05-16 07:38:27 +0000409
Neal Norwitz6e73aaa2006-06-12 03:33:09 +0000410 self.assertEqual(zlib.decompress(s0),data0+data0)
411 self.assertEqual(zlib.decompress(s1),data0+data1)
Georg Brandl8d3342b2006-05-16 07:38:27 +0000412
Neal Norwitz6e73aaa2006-06-12 03:33:09 +0000413 def test_badcompresscopy(self):
414 # Test copying a compression object in an inconsistent state
415 c = zlib.compressobj()
416 c.compress(HAMLET_SCENE)
417 c.flush()
418 self.assertRaises(ValueError, c.copy)
Georg Brandl8d3342b2006-05-16 07:38:27 +0000419
Neal Norwitz6e73aaa2006-06-12 03:33:09 +0000420 if hasattr(zlib.decompressobj(), "copy"):
421 def test_decompresscopy(self):
422 # Test copying a decompression object
423 data = HAMLET_SCENE
424 comp = zlib.compress(data)
Georg Brandl8d3342b2006-05-16 07:38:27 +0000425
Neal Norwitz6e73aaa2006-06-12 03:33:09 +0000426 d0 = zlib.decompressobj()
427 bufs0 = []
428 bufs0.append(d0.decompress(comp[:32]))
Georg Brandl8d3342b2006-05-16 07:38:27 +0000429
Neal Norwitz6e73aaa2006-06-12 03:33:09 +0000430 d1 = d0.copy()
431 bufs1 = bufs0[:]
Georg Brandl8d3342b2006-05-16 07:38:27 +0000432
Neal Norwitz6e73aaa2006-06-12 03:33:09 +0000433 bufs0.append(d0.decompress(comp[32:]))
434 s0 = ''.join(bufs0)
Georg Brandl8d3342b2006-05-16 07:38:27 +0000435
Neal Norwitz6e73aaa2006-06-12 03:33:09 +0000436 bufs1.append(d1.decompress(comp[32:]))
437 s1 = ''.join(bufs1)
Georg Brandl8d3342b2006-05-16 07:38:27 +0000438
Neal Norwitz6e73aaa2006-06-12 03:33:09 +0000439 self.assertEqual(s0,s1)
440 self.assertEqual(s0,data)
Georg Brandl8d3342b2006-05-16 07:38:27 +0000441
Neal Norwitz6e73aaa2006-06-12 03:33:09 +0000442 def test_baddecompresscopy(self):
443 # Test copying a compression object in an inconsistent state
444 data = zlib.compress(HAMLET_SCENE)
445 d = zlib.decompressobj()
446 d.decompress(data)
447 d.flush()
448 self.assertRaises(ValueError, d.copy)
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000449
Antoine Pitrou3843cd82010-05-07 16:50:34 +0000450 # Memory use of the following functions takes into account overallocation
451
452 @precisionbigmemtest(size=_1G + 1024 * 1024, memuse=3)
453 def test_big_compress_buffer(self, size):
454 c = zlib.compressobj(1)
455 compress = lambda s: c.compress(s) + c.flush()
456 self.check_big_compress_buffer(size, compress)
457
458 @precisionbigmemtest(size=_1G + 1024 * 1024, memuse=2)
459 def test_big_decompress_buffer(self, size):
460 d = zlib.decompressobj()
461 decompress = lambda s: d.decompress(s) + d.flush()
462 self.check_big_decompress_buffer(size, decompress)
463
464
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000465def genblock(seed, length, step=1024, generator=random):
466 """length-byte stream of random data from a seed (in step-byte blocks)."""
467 if seed is not None:
468 generator.seed(seed)
469 randint = generator.randint
470 if length < step or step < 2:
471 step = length
472 blocks = []
473 for i in range(0, length, step):
474 blocks.append(''.join([chr(randint(0,255))
475 for x in range(step)]))
476 return ''.join(blocks)[:length]
477
478
479
480def choose_lines(source, number, seed=None, generator=random):
481 """Return a list of number lines randomly chosen from the source"""
482 if seed is not None:
483 generator.seed(seed)
484 sources = source.split('\n')
485 return [generator.choice(sources) for n in range(number)]
486
487
488
Neil Schemenauer6412b122004-06-05 19:34:28 +0000489HAMLET_SCENE = """
Fred Drake004d5e62000-10-23 17:22:08 +0000490LAERTES
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +0000491
492 O, fear me not.
493 I stay too long: but here my father comes.
494
495 Enter POLONIUS
496
497 A double blessing is a double grace,
498 Occasion smiles upon a second leave.
499
Fred Drake004d5e62000-10-23 17:22:08 +0000500LORD POLONIUS
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +0000501
502 Yet here, Laertes! aboard, aboard, for shame!
503 The wind sits in the shoulder of your sail,
504 And you are stay'd for. There; my blessing with thee!
505 And these few precepts in thy memory
506 See thou character. Give thy thoughts no tongue,
507 Nor any unproportioned thought his act.
508 Be thou familiar, but by no means vulgar.
509 Those friends thou hast, and their adoption tried,
510 Grapple them to thy soul with hoops of steel;
511 But do not dull thy palm with entertainment
512 Of each new-hatch'd, unfledged comrade. Beware
513 Of entrance to a quarrel, but being in,
514 Bear't that the opposed may beware of thee.
515 Give every man thy ear, but few thy voice;
516 Take each man's censure, but reserve thy judgment.
517 Costly thy habit as thy purse can buy,
518 But not express'd in fancy; rich, not gaudy;
519 For the apparel oft proclaims the man,
520 And they in France of the best rank and station
521 Are of a most select and generous chief in that.
522 Neither a borrower nor a lender be;
523 For loan oft loses both itself and friend,
524 And borrowing dulls the edge of husbandry.
525 This above all: to thine ownself be true,
526 And it must follow, as the night the day,
527 Thou canst not then be false to any man.
528 Farewell: my blessing season this in thee!
529
Fred Drake004d5e62000-10-23 17:22:08 +0000530LAERTES
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +0000531
532 Most humbly do I take my leave, my lord.
533
Fred Drake004d5e62000-10-23 17:22:08 +0000534LORD POLONIUS
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +0000535
536 The time invites you; go; your servants tend.
537
Fred Drake004d5e62000-10-23 17:22:08 +0000538LAERTES
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +0000539
540 Farewell, Ophelia; and remember well
541 What I have said to you.
542
Fred Drake004d5e62000-10-23 17:22:08 +0000543OPHELIA
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +0000544
545 'Tis in my memory lock'd,
546 And you yourself shall keep the key of it.
547
Fred Drake004d5e62000-10-23 17:22:08 +0000548LAERTES
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +0000549
550 Farewell.
551"""
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000552
553
554def test_main():
Victor Stinnerf9fb4342011-05-03 15:19:23 +0200555 run_unittest(
Walter Dörwald21d3a322003-05-01 17:45:56 +0000556 ChecksumTestCase,
557 ExceptionTestCase,
558 CompressTestCase,
559 CompressObjectTestCase
560 )
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000561
562if __name__ == "__main__":
563 test_main()