blob: c672ef7c666df361489d6ca5e805f4fbf41e2ea9 [file] [log] [blame]
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +00001import zlib
2import sys
Guido van Rossumc95a6c11997-08-15 16:23:32 +00003import imp
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +00004
Guido van Rossum629bcfb1997-12-18 05:21:07 +00005try:
6 t = imp.find_module('test_zlib')
7 file = t[0]
8except ImportError:
9 file = open(__file__)
Guido van Rossumc95a6c11997-08-15 16:23:32 +000010buf = file.read() * 8
11file.close()
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +000012
Jeremy Hyltona05e2932000-06-28 14:48:01 +000013# test the checksums (hex so the test doesn't break on 64-bit machines)
Guido van Rossum446898f1998-04-24 18:31:28 +000014print hex(zlib.crc32('penguin')), hex(zlib.crc32('penguin', 1))
15print hex(zlib.adler32('penguin')), hex(zlib.adler32('penguin', 1))
Jeremy Hylton9dc2b8e1997-09-04 23:41:37 +000016
17# make sure we generate some expected errors
18try:
19 zlib.compress('ERROR', zlib.MAX_WBITS + 1)
20except zlib.error, msg:
21 print "expecting", msg
22try:
23 zlib.compressobj(1, 8, 0)
24except ValueError, msg:
25 print "expecting", msg
26try:
27 zlib.decompressobj(0)
28except ValueError, msg:
29 print "expecting", msg
30
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +000031x = zlib.compress(buf)
32y = zlib.decompress(x)
33if buf != y:
34 print "normal compression/decompression failed"
Jeremy Hylton9dc2b8e1997-09-04 23:41:37 +000035else:
36 print "normal compression/decompression succeeded"
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +000037
38buf = buf * 16
39
40co = zlib.compressobj(8, 8, -15)
41x1 = co.compress(buf)
42x2 = co.flush()
43x = x1 + x2
44
45dc = zlib.decompressobj(-15)
46y1 = dc.decompress(x)
47y2 = dc.flush()
48y = y1 + y2
49if buf != y:
50 print "compress/decompression obj failed"
Jeremy Hylton9dc2b8e1997-09-04 23:41:37 +000051else:
52 print "compress/decompression obj succeeded"
53
54co = zlib.compressobj(2, 8, -12, 9, 1)
55bufs = []
56for i in range(0, len(buf), 256):
57 bufs.append(co.compress(buf[i:i+256]))
58bufs.append(co.flush())
Eric S. Raymond83ff7492001-02-09 12:03:45 +000059combuf = ''.join(bufs)
Jeremy Hylton9dc2b8e1997-09-04 23:41:37 +000060
61decomp1 = zlib.decompress(combuf, -12, -5)
62if decomp1 != buf:
63 print "decompress with init options failed"
64else:
65 print "decompress with init options succeeded"
66
67deco = zlib.decompressobj(-12)
68bufs = []
69for i in range(0, len(combuf), 128):
70 bufs.append(deco.decompress(combuf[i:i+128]))
71bufs.append(deco.flush())
Eric S. Raymond83ff7492001-02-09 12:03:45 +000072decomp2 = ''.join(buf)
Jeremy Hylton9dc2b8e1997-09-04 23:41:37 +000073if decomp2 != buf:
74 print "decompressobj with init options failed"
75else:
76 print "decompressobj with init options succeeded"
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +000077
Andrew M. Kuchlingdca7e001999-03-22 19:23:17 +000078# Test flush() with the various options, using all the different levels
79# in order to provide more variations.
80for sync in [zlib.Z_NO_FLUSH, zlib.Z_SYNC_FLUSH, zlib.Z_FULL_FLUSH]:
81 for level in range(10):
Fred Drakead892dc2000-02-10 15:31:07 +000082 obj = zlib.compressobj( level )
83 d = obj.compress( buf[:3000] )
84 d = d + obj.flush( sync )
85 d = d + obj.compress( buf[3000:] )
86 d = d + obj.flush()
87 if zlib.decompress(d) != buf:
88 print "Decompress failed: flush mode=%i, level=%i" % (sync,level)
89 del obj
Andrew M. Kuchlingdca7e001999-03-22 19:23:17 +000090
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +000091def ignore():
92 """An empty function with a big string.
93
94 Make the compression algorithm work a little harder.
95 """
96
97 """
Fred Drake004d5e62000-10-23 17:22:08 +000098LAERTES
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +000099
100 O, fear me not.
101 I stay too long: but here my father comes.
102
103 Enter POLONIUS
104
105 A double blessing is a double grace,
106 Occasion smiles upon a second leave.
107
Fred Drake004d5e62000-10-23 17:22:08 +0000108LORD POLONIUS
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +0000109
110 Yet here, Laertes! aboard, aboard, for shame!
111 The wind sits in the shoulder of your sail,
112 And you are stay'd for. There; my blessing with thee!
113 And these few precepts in thy memory
114 See thou character. Give thy thoughts no tongue,
115 Nor any unproportioned thought his act.
116 Be thou familiar, but by no means vulgar.
117 Those friends thou hast, and their adoption tried,
118 Grapple them to thy soul with hoops of steel;
119 But do not dull thy palm with entertainment
120 Of each new-hatch'd, unfledged comrade. Beware
121 Of entrance to a quarrel, but being in,
122 Bear't that the opposed may beware of thee.
123 Give every man thy ear, but few thy voice;
124 Take each man's censure, but reserve thy judgment.
125 Costly thy habit as thy purse can buy,
126 But not express'd in fancy; rich, not gaudy;
127 For the apparel oft proclaims the man,
128 And they in France of the best rank and station
129 Are of a most select and generous chief in that.
130 Neither a borrower nor a lender be;
131 For loan oft loses both itself and friend,
132 And borrowing dulls the edge of husbandry.
133 This above all: to thine ownself be true,
134 And it must follow, as the night the day,
135 Thou canst not then be false to any man.
136 Farewell: my blessing season this in thee!
137
Fred Drake004d5e62000-10-23 17:22:08 +0000138LAERTES
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +0000139
140 Most humbly do I take my leave, my lord.
141
Fred Drake004d5e62000-10-23 17:22:08 +0000142LORD POLONIUS
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +0000143
144 The time invites you; go; your servants tend.
145
Fred Drake004d5e62000-10-23 17:22:08 +0000146LAERTES
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +0000147
148 Farewell, Ophelia; and remember well
149 What I have said to you.
150
Fred Drake004d5e62000-10-23 17:22:08 +0000151OPHELIA
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +0000152
153 'Tis in my memory lock'd,
154 And you yourself shall keep the key of it.
155
Fred Drake004d5e62000-10-23 17:22:08 +0000156LAERTES
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +0000157
158 Farewell.
159"""