4 from test
.test_support
import TestFailed
7 t
= imp
.find_module('test_zlib')
14 # test the checksums (hex so the test doesn't break on 64-bit machines)
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
22 zlib
.compress('ERROR', zlib
.MAX_WBITS
+ 1)
23 except zlib
.error
, msg
:
24 print "expecting", msg
26 zlib
.compressobj(1, 8, 0)
27 except ValueError, msg
:
28 print "expecting", msg
31 except ValueError, msg
:
32 print "expecting", msg
34 x
= zlib
.compress(buf
)
35 y
= zlib
.decompress(x
)
37 print "normal compression/decompression failed"
39 print "normal compression/decompression succeeded"
43 co
= zlib
.compressobj(8, 8, -15)
48 print "Oops - second flush worked when it should not have!"
54 dc
= zlib
.decompressobj(-15)
59 print "compress/decompression obj failed"
61 print "compress/decompression obj succeeded"
63 co
= zlib
.compressobj(2, 8, -12, 9, 1)
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)
72 print "decompress with init options failed"
74 print "decompress with init options succeeded"
76 deco
= zlib
.decompressobj(-12)
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
)
83 print "decompressobj with init options failed"
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)
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
)
99 cb
= deco
.unconsumed_tail
100 bufs
.append(deco
.flush())
101 decomp2
= ''.join(buf
)
103 print "max_length decompressobj failed"
105 print "max_length decompressobj succeeded"
107 # Misc tests of max_length
108 deco
= zlib
.decompressobj(-12)
110 deco
.decompress("", -1)
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:] )
129 if zlib
.decompress(d
) != buf
:
130 print "Decompress failed: flush mode=%i, level=%i" % (sync
,level
)
133 # Test for the odd flushing bugs noted in 2.0, and hopefully fixed in 2.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()
147 # generate random data stream
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.
157 print len(a
),len(t
),len(d
.unused_data
)
158 raise TestFailed
, "output of 17K doesn't match"
161 """An empty function with a big string.
163 Make the compression algorithm work a little harder.
170 I stay too long: but here my father comes.
174 A double blessing is a double grace,
175 Occasion smiles upon a second leave.
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!
209 Most humbly do I take my leave, my lord.
213 The time invites you; go; your servants tend.
217 Farewell, Ophelia; and remember well
218 What I have said to you.
222 'Tis in my memory lock'd,
223 And you yourself shall keep the key of it.