blob: b7d99c32b3066602750248a2863131d4a5697d3f [file] [log] [blame]
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +00001import zlib
2import sys
Guido van Rossumc95a6c11997-08-15 16:23:32 +00003import imp
Barry Warsaw04f357c2002-07-23 19:04:11 +00004from test.test_support import TestFailed
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +00005
Guido van Rossum629bcfb1997-12-18 05:21:07 +00006try:
7 t = imp.find_module('test_zlib')
8 file = t[0]
9except ImportError:
10 file = open(__file__)
Guido van Rossumc95a6c11997-08-15 16:23:32 +000011buf = file.read() * 8
12file.close()
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +000013
Jeremy Hyltona05e2932000-06-28 14:48:01 +000014# test the checksums (hex so the test doesn't break on 64-bit machines)
Guido van Rossuma6fa0e62002-08-12 15:26:05 +000015def fix(x):
16 return "0x%x" % (x & 0xffffffffL)
17print fix(zlib.crc32('penguin')), fix(zlib.crc32('penguin', 1))
18print fix(zlib.adler32('penguin')), fix(zlib.adler32('penguin', 1))
Jeremy Hylton9dc2b8e1997-09-04 23:41:37 +000019
20# make sure we generate some expected errors
21try:
22 zlib.compress('ERROR', zlib.MAX_WBITS + 1)
23except zlib.error, msg:
24 print "expecting", msg
25try:
26 zlib.compressobj(1, 8, 0)
27except ValueError, msg:
28 print "expecting", msg
29try:
30 zlib.decompressobj(0)
31except ValueError, msg:
32 print "expecting", msg
33
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +000034x = zlib.compress(buf)
35y = zlib.decompress(x)
36if buf != y:
37 print "normal compression/decompression failed"
Jeremy Hylton9dc2b8e1997-09-04 23:41:37 +000038else:
39 print "normal compression/decompression succeeded"
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +000040
41buf = buf * 16
42
43co = zlib.compressobj(8, 8, -15)
44x1 = co.compress(buf)
45x2 = co.flush()
Jeremy Hyltonc72737e2002-04-19 14:37:07 +000046try:
47 co.flush()
48 print "Oops - second flush worked when it should not have!"
49except zlib.error:
50 pass
51
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +000052x = x1 + x2
53
54dc = zlib.decompressobj(-15)
55y1 = dc.decompress(x)
56y2 = dc.flush()
57y = y1 + y2
58if buf != y:
59 print "compress/decompression obj failed"
Jeremy Hylton9dc2b8e1997-09-04 23:41:37 +000060else:
61 print "compress/decompression obj succeeded"
62
63co = zlib.compressobj(2, 8, -12, 9, 1)
64bufs = []
65for i in range(0, len(buf), 256):
66 bufs.append(co.compress(buf[i:i+256]))
67bufs.append(co.flush())
Eric S. Raymond83ff7492001-02-09 12:03:45 +000068combuf = ''.join(bufs)
Jeremy Hylton9dc2b8e1997-09-04 23:41:37 +000069
70decomp1 = zlib.decompress(combuf, -12, -5)
71if decomp1 != buf:
72 print "decompress with init options failed"
73else:
74 print "decompress with init options succeeded"
75
76deco = zlib.decompressobj(-12)
77bufs = []
78for i in range(0, len(combuf), 128):
79 bufs.append(deco.decompress(combuf[i:i+128]))
80bufs.append(deco.flush())
Andrew M. Kuchling8e6d44e2001-02-14 17:46:20 +000081decomp2 = ''.join(bufs)
Jeremy Hylton9dc2b8e1997-09-04 23:41:37 +000082if decomp2 != buf:
83 print "decompressobj with init options failed"
84else:
85 print "decompressobj with init options succeeded"
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +000086
Jeremy Hylton511e2ca2001-10-16 20:39:49 +000087print "should be '':", `deco.unconsumed_tail`
88
89# Check a decompression object with max_length specified
90deco = zlib.decompressobj(-12)
91cb = combuf
92bufs = []
93while cb:
94 max_length = 1 + len(cb)/10
95 chunk = deco.decompress(cb, max_length)
96 if len(chunk) > max_length:
97 print 'chunk too big (%d>%d)' % (len(chunk),max_length)
98 bufs.append(chunk)
99 cb = deco.unconsumed_tail
100bufs.append(deco.flush())
101decomp2 = ''.join(buf)
102if decomp2 != buf:
103 print "max_length decompressobj failed"
104else:
105 print "max_length decompressobj succeeded"
Tim Peterse0c446b2001-10-18 21:57:37 +0000106
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000107# Misc tests of max_length
108deco = zlib.decompressobj(-12)
109try:
110 deco.decompress("", -1)
111except ValueError:
112 pass
113else:
114 print "failed to raise value error on bad max_length"
115print "unconsumed_tail should be '':", `deco.unconsumed_tail`
116
Andrew M. Kuchlingdca7e001999-03-22 19:23:17 +0000117# Test flush() with the various options, using all the different levels
118# in order to provide more variations.
Andrew M. Kuchlingfcfc8d52001-08-10 15:50:11 +0000119sync_opt = ['Z_NO_FLUSH', 'Z_SYNC_FLUSH', 'Z_FULL_FLUSH']
120sync_opt = [getattr(zlib, opt) for opt in sync_opt if hasattr(zlib, opt)]
121
122for sync in sync_opt:
Andrew M. Kuchlingdca7e001999-03-22 19:23:17 +0000123 for level in range(10):
Fred Drakead892dc2000-02-10 15:31:07 +0000124 obj = zlib.compressobj( level )
125 d = obj.compress( buf[:3000] )
126 d = d + obj.flush( sync )
127 d = d + obj.compress( buf[3000:] )
128 d = d + obj.flush()
129 if zlib.decompress(d) != buf:
130 print "Decompress failed: flush mode=%i, level=%i" % (sync,level)
131 del obj
Andrew M. Kuchlingdca7e001999-03-22 19:23:17 +0000132
Andrew M. Kuchling9a0f98e2001-02-21 02:17:01 +0000133# Test for the odd flushing bugs noted in 2.0, and hopefully fixed in 2.1
134
135import random
Tim Peters0009c4e2001-02-21 07:29:48 +0000136random.seed(1)
Andrew M. Kuchling9a0f98e2001-02-21 02:17:01 +0000137
138print 'Testing on 17K of random data'
139
Andrew M. Kuchlingfcfc8d52001-08-10 15:50:11 +0000140if hasattr(zlib, 'Z_SYNC_FLUSH'):
Andrew M. Kuchling9a0f98e2001-02-21 02:17:01 +0000141
Andrew M. Kuchlingfcfc8d52001-08-10 15:50:11 +0000142 # Create compressor and decompressor objects
143 c=zlib.compressobj(9)
144 d=zlib.decompressobj()
Tim Peters0009c4e2001-02-21 07:29:48 +0000145
Andrew M. Kuchlingfcfc8d52001-08-10 15:50:11 +0000146 # Try 17K of data
147 # generate random data stream
148 a=""
149 for i in range(17*1024):
150 a=a+chr(random.randint(0,255))
Andrew M. Kuchling9a0f98e2001-02-21 02:17:01 +0000151
Andrew M. Kuchlingfcfc8d52001-08-10 15:50:11 +0000152 # compress, sync-flush, and decompress
153 t = d.decompress( c.compress(a)+c.flush(zlib.Z_SYNC_FLUSH) )
154
155 # if decompressed data is different from the input data, choke.
156 if len(t) != len(a):
157 print len(a),len(t),len(d.unused_data)
158 raise TestFailed, "output of 17K doesn't match"
Andrew M. Kuchling9a0f98e2001-02-21 02:17:01 +0000159
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +0000160def ignore():
161 """An empty function with a big string.
162
163 Make the compression algorithm work a little harder.
164 """
165
166 """
Fred Drake004d5e62000-10-23 17:22:08 +0000167LAERTES
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +0000168
169 O, fear me not.
170 I stay too long: but here my father comes.
171
172 Enter POLONIUS
173
174 A double blessing is a double grace,
175 Occasion smiles upon a second leave.
176
Fred Drake004d5e62000-10-23 17:22:08 +0000177LORD POLONIUS
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +0000178
179 Yet here, Laertes! aboard, aboard, for shame!
180 The wind sits in the shoulder of your sail,
181 And you are stay'd for. There; my blessing with thee!
182 And these few precepts in thy memory
183 See thou character. Give thy thoughts no tongue,
184 Nor any unproportioned thought his act.
185 Be thou familiar, but by no means vulgar.
186 Those friends thou hast, and their adoption tried,
187 Grapple them to thy soul with hoops of steel;
188 But do not dull thy palm with entertainment
189 Of each new-hatch'd, unfledged comrade. Beware
190 Of entrance to a quarrel, but being in,
191 Bear't that the opposed may beware of thee.
192 Give every man thy ear, but few thy voice;
193 Take each man's censure, but reserve thy judgment.
194 Costly thy habit as thy purse can buy,
195 But not express'd in fancy; rich, not gaudy;
196 For the apparel oft proclaims the man,
197 And they in France of the best rank and station
198 Are of a most select and generous chief in that.
199 Neither a borrower nor a lender be;
200 For loan oft loses both itself and friend,
201 And borrowing dulls the edge of husbandry.
202 This above all: to thine ownself be true,
203 And it must follow, as the night the day,
204 Thou canst not then be false to any man.
205 Farewell: my blessing season this in thee!
206
Fred Drake004d5e62000-10-23 17:22:08 +0000207LAERTES
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +0000208
209 Most humbly do I take my leave, my lord.
210
Fred Drake004d5e62000-10-23 17:22:08 +0000211LORD POLONIUS
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +0000212
213 The time invites you; go; your servants tend.
214
Fred Drake004d5e62000-10-23 17:22:08 +0000215LAERTES
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +0000216
217 Farewell, Ophelia; and remember well
218 What I have said to you.
219
Fred Drake004d5e62000-10-23 17:22:08 +0000220OPHELIA
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +0000221
222 'Tis in my memory lock'd,
223 And you yourself shall keep the key of it.
224
Fred Drake004d5e62000-10-23 17:22:08 +0000225LAERTES
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +0000226
227 Farewell.
228"""