blob: 35bca88432050a9f62d99c49e8e598c413e16447 [file] [log] [blame]
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +00001import zlib
Andrew M. Kuchling9a0f98e2001-02-21 02:17:01 +00002from test_support import TestFailed
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +00003import sys
Guido van Rossumc95a6c11997-08-15 16:23:32 +00004import imp
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 Rossum446898f1998-04-24 18:31:28 +000015print hex(zlib.crc32('penguin')), hex(zlib.crc32('penguin', 1))
16print hex(zlib.adler32('penguin')), hex(zlib.adler32('penguin', 1))
Jeremy Hylton9dc2b8e1997-09-04 23:41:37 +000017
18# make sure we generate some expected errors
19try:
20 zlib.compress('ERROR', zlib.MAX_WBITS + 1)
21except zlib.error, msg:
22 print "expecting", msg
23try:
24 zlib.compressobj(1, 8, 0)
25except ValueError, msg:
26 print "expecting", msg
27try:
28 zlib.decompressobj(0)
29except ValueError, msg:
30 print "expecting", msg
31
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +000032x = zlib.compress(buf)
33y = zlib.decompress(x)
34if buf != y:
35 print "normal compression/decompression failed"
Jeremy Hylton9dc2b8e1997-09-04 23:41:37 +000036else:
37 print "normal compression/decompression succeeded"
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +000038
39buf = buf * 16
40
41co = zlib.compressobj(8, 8, -15)
42x1 = co.compress(buf)
43x2 = co.flush()
Jeremy Hyltonc72737e2002-04-19 14:37:07 +000044try:
45 co.flush()
46 print "Oops - second flush worked when it should not have!"
47except zlib.error:
48 pass
49
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +000050x = x1 + x2
51
52dc = zlib.decompressobj(-15)
53y1 = dc.decompress(x)
54y2 = dc.flush()
55y = y1 + y2
56if buf != y:
57 print "compress/decompression obj failed"
Jeremy Hylton9dc2b8e1997-09-04 23:41:37 +000058else:
59 print "compress/decompression obj succeeded"
60
61co = zlib.compressobj(2, 8, -12, 9, 1)
62bufs = []
63for i in range(0, len(buf), 256):
64 bufs.append(co.compress(buf[i:i+256]))
65bufs.append(co.flush())
Eric S. Raymond83ff7492001-02-09 12:03:45 +000066combuf = ''.join(bufs)
Jeremy Hylton9dc2b8e1997-09-04 23:41:37 +000067
68decomp1 = zlib.decompress(combuf, -12, -5)
69if decomp1 != buf:
70 print "decompress with init options failed"
71else:
72 print "decompress with init options succeeded"
73
74deco = zlib.decompressobj(-12)
75bufs = []
76for i in range(0, len(combuf), 128):
77 bufs.append(deco.decompress(combuf[i:i+128]))
78bufs.append(deco.flush())
Andrew M. Kuchling8e6d44e2001-02-14 17:46:20 +000079decomp2 = ''.join(bufs)
Jeremy Hylton9dc2b8e1997-09-04 23:41:37 +000080if decomp2 != buf:
81 print "decompressobj with init options failed"
82else:
83 print "decompressobj with init options succeeded"
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +000084
Jeremy Hylton511e2ca2001-10-16 20:39:49 +000085print "should be '':", `deco.unconsumed_tail`
86
87# Check a decompression object with max_length specified
88deco = zlib.decompressobj(-12)
89cb = combuf
90bufs = []
91while cb:
92 max_length = 1 + len(cb)/10
93 chunk = deco.decompress(cb, max_length)
94 if len(chunk) > max_length:
95 print 'chunk too big (%d>%d)' % (len(chunk),max_length)
96 bufs.append(chunk)
97 cb = deco.unconsumed_tail
98bufs.append(deco.flush())
99decomp2 = ''.join(buf)
100if decomp2 != buf:
101 print "max_length decompressobj failed"
102else:
103 print "max_length decompressobj succeeded"
Tim Peterse0c446b2001-10-18 21:57:37 +0000104
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000105# Misc tests of max_length
106deco = zlib.decompressobj(-12)
107try:
108 deco.decompress("", -1)
109except ValueError:
110 pass
111else:
112 print "failed to raise value error on bad max_length"
113print "unconsumed_tail should be '':", `deco.unconsumed_tail`
114
Andrew M. Kuchlingdca7e001999-03-22 19:23:17 +0000115# Test flush() with the various options, using all the different levels
116# in order to provide more variations.
Andrew M. Kuchlingfcfc8d52001-08-10 15:50:11 +0000117sync_opt = ['Z_NO_FLUSH', 'Z_SYNC_FLUSH', 'Z_FULL_FLUSH']
118sync_opt = [getattr(zlib, opt) for opt in sync_opt if hasattr(zlib, opt)]
119
120for sync in sync_opt:
Andrew M. Kuchlingdca7e001999-03-22 19:23:17 +0000121 for level in range(10):
Fred Drakead892dc2000-02-10 15:31:07 +0000122 obj = zlib.compressobj( level )
123 d = obj.compress( buf[:3000] )
124 d = d + obj.flush( sync )
125 d = d + obj.compress( buf[3000:] )
126 d = d + obj.flush()
127 if zlib.decompress(d) != buf:
128 print "Decompress failed: flush mode=%i, level=%i" % (sync,level)
129 del obj
Andrew M. Kuchlingdca7e001999-03-22 19:23:17 +0000130
Andrew M. Kuchling9a0f98e2001-02-21 02:17:01 +0000131# Test for the odd flushing bugs noted in 2.0, and hopefully fixed in 2.1
132
133import random
Tim Peters0009c4e2001-02-21 07:29:48 +0000134random.seed(1)
Andrew M. Kuchling9a0f98e2001-02-21 02:17:01 +0000135
136print 'Testing on 17K of random data'
137
Andrew M. Kuchlingfcfc8d52001-08-10 15:50:11 +0000138if hasattr(zlib, 'Z_SYNC_FLUSH'):
Andrew M. Kuchling9a0f98e2001-02-21 02:17:01 +0000139
Andrew M. Kuchlingfcfc8d52001-08-10 15:50:11 +0000140 # Create compressor and decompressor objects
141 c=zlib.compressobj(9)
142 d=zlib.decompressobj()
Tim Peters0009c4e2001-02-21 07:29:48 +0000143
Andrew M. Kuchlingfcfc8d52001-08-10 15:50:11 +0000144 # Try 17K of data
145 # generate random data stream
146 a=""
147 for i in range(17*1024):
148 a=a+chr(random.randint(0,255))
Andrew M. Kuchling9a0f98e2001-02-21 02:17:01 +0000149
Andrew M. Kuchlingfcfc8d52001-08-10 15:50:11 +0000150 # compress, sync-flush, and decompress
151 t = d.decompress( c.compress(a)+c.flush(zlib.Z_SYNC_FLUSH) )
152
153 # if decompressed data is different from the input data, choke.
154 if len(t) != len(a):
155 print len(a),len(t),len(d.unused_data)
156 raise TestFailed, "output of 17K doesn't match"
Andrew M. Kuchling9a0f98e2001-02-21 02:17:01 +0000157
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +0000158def ignore():
159 """An empty function with a big string.
160
161 Make the compression algorithm work a little harder.
162 """
163
164 """
Fred Drake004d5e62000-10-23 17:22:08 +0000165LAERTES
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +0000166
167 O, fear me not.
168 I stay too long: but here my father comes.
169
170 Enter POLONIUS
171
172 A double blessing is a double grace,
173 Occasion smiles upon a second leave.
174
Fred Drake004d5e62000-10-23 17:22:08 +0000175LORD POLONIUS
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +0000176
177 Yet here, Laertes! aboard, aboard, for shame!
178 The wind sits in the shoulder of your sail,
179 And you are stay'd for. There; my blessing with thee!
180 And these few precepts in thy memory
181 See thou character. Give thy thoughts no tongue,
182 Nor any unproportioned thought his act.
183 Be thou familiar, but by no means vulgar.
184 Those friends thou hast, and their adoption tried,
185 Grapple them to thy soul with hoops of steel;
186 But do not dull thy palm with entertainment
187 Of each new-hatch'd, unfledged comrade. Beware
188 Of entrance to a quarrel, but being in,
189 Bear't that the opposed may beware of thee.
190 Give every man thy ear, but few thy voice;
191 Take each man's censure, but reserve thy judgment.
192 Costly thy habit as thy purse can buy,
193 But not express'd in fancy; rich, not gaudy;
194 For the apparel oft proclaims the man,
195 And they in France of the best rank and station
196 Are of a most select and generous chief in that.
197 Neither a borrower nor a lender be;
198 For loan oft loses both itself and friend,
199 And borrowing dulls the edge of husbandry.
200 This above all: to thine ownself be true,
201 And it must follow, as the night the day,
202 Thou canst not then be false to any man.
203 Farewell: my blessing season this in thee!
204
Fred Drake004d5e62000-10-23 17:22:08 +0000205LAERTES
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +0000206
207 Most humbly do I take my leave, my lord.
208
Fred Drake004d5e62000-10-23 17:22:08 +0000209LORD POLONIUS
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +0000210
211 The time invites you; go; your servants tend.
212
Fred Drake004d5e62000-10-23 17:22:08 +0000213LAERTES
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +0000214
215 Farewell, Ophelia; and remember well
216 What I have said to you.
217
Fred Drake004d5e62000-10-23 17:22:08 +0000218OPHELIA
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +0000219
220 'Tis in my memory lock'd,
221 And you yourself shall keep the key of it.
222
Fred Drake004d5e62000-10-23 17:22:08 +0000223LAERTES
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +0000224
225 Farewell.
226"""