blob: 72dbcfa20a16184c613a4b38ea2b74d3e075792c [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 Hylton9dc2b8e1997-09-04 23:41:37 +00004import string
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 Hylton9dc2b8e1997-09-04 23:41:37 +000014# test the chucksums
15print zlib.crc32('penguin'), zlib.crc32('penguin', 1)
16print zlib.adler32('penguin'), zlib.adler32('penguin', 1)
17
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())
60combuf = string.join(bufs, '')
61
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())
73decomp2 = string.join(buf, '')
74if 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
79def ignore():
80 """An empty function with a big string.
81
82 Make the compression algorithm work a little harder.
83 """
84
85 """
86LAERTES
87
88 O, fear me not.
89 I stay too long: but here my father comes.
90
91 Enter POLONIUS
92
93 A double blessing is a double grace,
94 Occasion smiles upon a second leave.
95
96LORD POLONIUS
97
98 Yet here, Laertes! aboard, aboard, for shame!
99 The wind sits in the shoulder of your sail,
100 And you are stay'd for. There; my blessing with thee!
101 And these few precepts in thy memory
102 See thou character. Give thy thoughts no tongue,
103 Nor any unproportioned thought his act.
104 Be thou familiar, but by no means vulgar.
105 Those friends thou hast, and their adoption tried,
106 Grapple them to thy soul with hoops of steel;
107 But do not dull thy palm with entertainment
108 Of each new-hatch'd, unfledged comrade. Beware
109 Of entrance to a quarrel, but being in,
110 Bear't that the opposed may beware of thee.
111 Give every man thy ear, but few thy voice;
112 Take each man's censure, but reserve thy judgment.
113 Costly thy habit as thy purse can buy,
114 But not express'd in fancy; rich, not gaudy;
115 For the apparel oft proclaims the man,
116 And they in France of the best rank and station
117 Are of a most select and generous chief in that.
118 Neither a borrower nor a lender be;
119 For loan oft loses both itself and friend,
120 And borrowing dulls the edge of husbandry.
121 This above all: to thine ownself be true,
122 And it must follow, as the night the day,
123 Thou canst not then be false to any man.
124 Farewell: my blessing season this in thee!
125
126LAERTES
127
128 Most humbly do I take my leave, my lord.
129
130LORD POLONIUS
131
132 The time invites you; go; your servants tend.
133
134LAERTES
135
136 Farewell, Ophelia; and remember well
137 What I have said to you.
138
139OPHELIA
140
141 'Tis in my memory lock'd,
142 And you yourself shall keep the key of it.
143
144LAERTES
145
146 Farewell.
147"""
148