Bump to 2.3.1 to pick up the missing file.
[python/dscho.git] / Lib / test / test_zlib.py
blobb7d99c32b3066602750248a2863131d4a5697d3f
1 import zlib
2 import sys
3 import imp
4 from test.test_support import TestFailed
6 try:
7 t = imp.find_module('test_zlib')
8 file = t[0]
9 except ImportError:
10 file = open(__file__)
11 buf = file.read() * 8
12 file.close()
14 # test the checksums (hex so the test doesn't break on 64-bit machines)
15 def fix(x):
16 return "0x%x" % (x & 0xffffffffL)
17 print fix(zlib.crc32('penguin')), fix(zlib.crc32('penguin', 1))
18 print fix(zlib.adler32('penguin')), fix(zlib.adler32('penguin', 1))
20 # make sure we generate some expected errors
21 try:
22 zlib.compress('ERROR', zlib.MAX_WBITS + 1)
23 except zlib.error, msg:
24 print "expecting", msg
25 try:
26 zlib.compressobj(1, 8, 0)
27 except ValueError, msg:
28 print "expecting", msg
29 try:
30 zlib.decompressobj(0)
31 except ValueError, msg:
32 print "expecting", msg
34 x = zlib.compress(buf)
35 y = zlib.decompress(x)
36 if buf != y:
37 print "normal compression/decompression failed"
38 else:
39 print "normal compression/decompression succeeded"
41 buf = buf * 16
43 co = zlib.compressobj(8, 8, -15)
44 x1 = co.compress(buf)
45 x2 = co.flush()
46 try:
47 co.flush()
48 print "Oops - second flush worked when it should not have!"
49 except zlib.error:
50 pass
52 x = x1 + x2
54 dc = zlib.decompressobj(-15)
55 y1 = dc.decompress(x)
56 y2 = dc.flush()
57 y = y1 + y2
58 if buf != y:
59 print "compress/decompression obj failed"
60 else:
61 print "compress/decompression obj succeeded"
63 co = zlib.compressobj(2, 8, -12, 9, 1)
64 bufs = []
65 for i in range(0, len(buf), 256):
66 bufs.append(co.compress(buf[i:i+256]))
67 bufs.append(co.flush())
68 combuf = ''.join(bufs)
70 decomp1 = zlib.decompress(combuf, -12, -5)
71 if decomp1 != buf:
72 print "decompress with init options failed"
73 else:
74 print "decompress with init options succeeded"
76 deco = zlib.decompressobj(-12)
77 bufs = []
78 for i in range(0, len(combuf), 128):
79 bufs.append(deco.decompress(combuf[i:i+128]))
80 bufs.append(deco.flush())
81 decomp2 = ''.join(bufs)
82 if decomp2 != buf:
83 print "decompressobj with init options failed"
84 else:
85 print "decompressobj with init options succeeded"
87 print "should be '':", `deco.unconsumed_tail`
89 # Check a decompression object with max_length specified
90 deco = zlib.decompressobj(-12)
91 cb = combuf
92 bufs = []
93 while 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
100 bufs.append(deco.flush())
101 decomp2 = ''.join(buf)
102 if decomp2 != buf:
103 print "max_length decompressobj failed"
104 else:
105 print "max_length decompressobj succeeded"
107 # Misc tests of max_length
108 deco = zlib.decompressobj(-12)
109 try:
110 deco.decompress("", -1)
111 except ValueError:
112 pass
113 else:
114 print "failed to raise value error on bad max_length"
115 print "unconsumed_tail should be '':", `deco.unconsumed_tail`
117 # Test flush() with the various options, using all the different levels
118 # in order to provide more variations.
119 sync_opt = ['Z_NO_FLUSH', 'Z_SYNC_FLUSH', 'Z_FULL_FLUSH']
120 sync_opt = [getattr(zlib, opt) for opt in sync_opt if hasattr(zlib, opt)]
122 for sync in sync_opt:
123 for level in range(10):
124 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
133 # Test for the odd flushing bugs noted in 2.0, and hopefully fixed in 2.1
135 import random
136 random.seed(1)
138 print 'Testing on 17K of random data'
140 if hasattr(zlib, 'Z_SYNC_FLUSH'):
142 # Create compressor and decompressor objects
143 c=zlib.compressobj(9)
144 d=zlib.decompressobj()
146 # 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))
152 # compress, sync-flush, and decompress
153 t = d.decompress( c.compress(a)+c.flush(zlib.Z_SYNC_FLUSH) )
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"
160 def ignore():
161 """An empty function with a big string.
163 Make the compression algorithm work a little harder.
167 LAERTES
169 O, fear me not.
170 I stay too long: but here my father comes.
172 Enter POLONIUS
174 A double blessing is a double grace,
175 Occasion smiles upon a second leave.
177 LORD POLONIUS
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!
207 LAERTES
209 Most humbly do I take my leave, my lord.
211 LORD POLONIUS
213 The time invites you; go; your servants tend.
215 LAERTES
217 Farewell, Ophelia; and remember well
218 What I have said to you.
220 OPHELIA
222 'Tis in my memory lock'd,
223 And you yourself shall keep the key of it.
225 LAERTES
227 Farewell.