blob: 439db2273906ca26818f4632af57869515e03aea [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()
44x = x1 + x2
45
46dc = zlib.decompressobj(-15)
47y1 = dc.decompress(x)
48y2 = dc.flush()
49y = y1 + y2
50if buf != y:
51 print "compress/decompression obj failed"
Jeremy Hylton9dc2b8e1997-09-04 23:41:37 +000052else:
53 print "compress/decompression obj succeeded"
54
55co = zlib.compressobj(2, 8, -12, 9, 1)
56bufs = []
57for i in range(0, len(buf), 256):
58 bufs.append(co.compress(buf[i:i+256]))
59bufs.append(co.flush())
Eric S. Raymond83ff7492001-02-09 12:03:45 +000060combuf = ''.join(bufs)
Jeremy Hylton9dc2b8e1997-09-04 23:41:37 +000061
62decomp1 = zlib.decompress(combuf, -12, -5)
63if decomp1 != buf:
64 print "decompress with init options failed"
65else:
66 print "decompress with init options succeeded"
67
68deco = zlib.decompressobj(-12)
69bufs = []
70for i in range(0, len(combuf), 128):
71 bufs.append(deco.decompress(combuf[i:i+128]))
72bufs.append(deco.flush())
Andrew M. Kuchling8e6d44e2001-02-14 17:46:20 +000073decomp2 = ''.join(bufs)
Jeremy Hylton9dc2b8e1997-09-04 23:41:37 +000074if decomp2 != buf:
75 print "decompressobj with init options failed"
76else:
77 print "decompressobj with init options succeeded"
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +000078
Andrew M. Kuchlingdca7e001999-03-22 19:23:17 +000079# Test flush() with the various options, using all the different levels
80# in order to provide more variations.
Andrew M. Kuchlingfcfc8d52001-08-10 15:50:11 +000081sync_opt = ['Z_NO_FLUSH', 'Z_SYNC_FLUSH', 'Z_FULL_FLUSH']
82sync_opt = [getattr(zlib, opt) for opt in sync_opt if hasattr(zlib, opt)]
83
84for sync in sync_opt:
Andrew M. Kuchlingdca7e001999-03-22 19:23:17 +000085 for level in range(10):
Fred Drakead892dc2000-02-10 15:31:07 +000086 obj = zlib.compressobj( level )
87 d = obj.compress( buf[:3000] )
88 d = d + obj.flush( sync )
89 d = d + obj.compress( buf[3000:] )
90 d = d + obj.flush()
91 if zlib.decompress(d) != buf:
92 print "Decompress failed: flush mode=%i, level=%i" % (sync,level)
93 del obj
Andrew M. Kuchlingdca7e001999-03-22 19:23:17 +000094
Andrew M. Kuchling9a0f98e2001-02-21 02:17:01 +000095# Test for the odd flushing bugs noted in 2.0, and hopefully fixed in 2.1
96
97import random
Tim Peters0009c4e2001-02-21 07:29:48 +000098random.seed(1)
Andrew M. Kuchling9a0f98e2001-02-21 02:17:01 +000099
100print 'Testing on 17K of random data'
101
Andrew M. Kuchlingfcfc8d52001-08-10 15:50:11 +0000102if hasattr(zlib, 'Z_SYNC_FLUSH'):
Andrew M. Kuchling9a0f98e2001-02-21 02:17:01 +0000103
Andrew M. Kuchlingfcfc8d52001-08-10 15:50:11 +0000104 # Create compressor and decompressor objects
105 c=zlib.compressobj(9)
106 d=zlib.decompressobj()
Tim Peters0009c4e2001-02-21 07:29:48 +0000107
Andrew M. Kuchlingfcfc8d52001-08-10 15:50:11 +0000108 # Try 17K of data
109 # generate random data stream
110 a=""
111 for i in range(17*1024):
112 a=a+chr(random.randint(0,255))
Andrew M. Kuchling9a0f98e2001-02-21 02:17:01 +0000113
Andrew M. Kuchlingfcfc8d52001-08-10 15:50:11 +0000114 # compress, sync-flush, and decompress
115 t = d.decompress( c.compress(a)+c.flush(zlib.Z_SYNC_FLUSH) )
116
117 # if decompressed data is different from the input data, choke.
118 if len(t) != len(a):
119 print len(a),len(t),len(d.unused_data)
120 raise TestFailed, "output of 17K doesn't match"
Andrew M. Kuchling9a0f98e2001-02-21 02:17:01 +0000121
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +0000122def ignore():
123 """An empty function with a big string.
124
125 Make the compression algorithm work a little harder.
126 """
127
128 """
Fred Drake004d5e62000-10-23 17:22:08 +0000129LAERTES
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +0000130
131 O, fear me not.
132 I stay too long: but here my father comes.
133
134 Enter POLONIUS
135
136 A double blessing is a double grace,
137 Occasion smiles upon a second leave.
138
Fred Drake004d5e62000-10-23 17:22:08 +0000139LORD POLONIUS
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +0000140
141 Yet here, Laertes! aboard, aboard, for shame!
142 The wind sits in the shoulder of your sail,
143 And you are stay'd for. There; my blessing with thee!
144 And these few precepts in thy memory
145 See thou character. Give thy thoughts no tongue,
146 Nor any unproportioned thought his act.
147 Be thou familiar, but by no means vulgar.
148 Those friends thou hast, and their adoption tried,
149 Grapple them to thy soul with hoops of steel;
150 But do not dull thy palm with entertainment
151 Of each new-hatch'd, unfledged comrade. Beware
152 Of entrance to a quarrel, but being in,
153 Bear't that the opposed may beware of thee.
154 Give every man thy ear, but few thy voice;
155 Take each man's censure, but reserve thy judgment.
156 Costly thy habit as thy purse can buy,
157 But not express'd in fancy; rich, not gaudy;
158 For the apparel oft proclaims the man,
159 And they in France of the best rank and station
160 Are of a most select and generous chief in that.
161 Neither a borrower nor a lender be;
162 For loan oft loses both itself and friend,
163 And borrowing dulls the edge of husbandry.
164 This above all: to thine ownself be true,
165 And it must follow, as the night the day,
166 Thou canst not then be false to any man.
167 Farewell: my blessing season this in thee!
168
Fred Drake004d5e62000-10-23 17:22:08 +0000169LAERTES
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +0000170
171 Most humbly do I take my leave, my lord.
172
Fred Drake004d5e62000-10-23 17:22:08 +0000173LORD POLONIUS
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +0000174
175 The time invites you; go; your servants tend.
176
Fred Drake004d5e62000-10-23 17:22:08 +0000177LAERTES
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +0000178
179 Farewell, Ophelia; and remember well
180 What I have said to you.
181
Fred Drake004d5e62000-10-23 17:22:08 +0000182OPHELIA
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +0000183
184 'Tis in my memory lock'd,
185 And you yourself shall keep the key of it.
186
Fred Drake004d5e62000-10-23 17:22:08 +0000187LAERTES
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +0000188
189 Farewell.
190"""