blob: 62c26048eb8e2f71afc1b7738b0af50af888e46f [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 Rossumc95a6c11997-08-15 16:23:32 +00006t = imp.find_module('test_zlib')
7file = t[0]
8buf = file.read() * 8
9file.close()
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +000010
Jeremy Hylton9dc2b8e1997-09-04 23:41:37 +000011# test the chucksums
12print zlib.crc32('penguin'), zlib.crc32('penguin', 1)
13print zlib.adler32('penguin'), zlib.adler32('penguin', 1)
14
15# make sure we generate some expected errors
16try:
17 zlib.compress('ERROR', zlib.MAX_WBITS + 1)
18except zlib.error, msg:
19 print "expecting", msg
20try:
21 zlib.compressobj(1, 8, 0)
22except ValueError, msg:
23 print "expecting", msg
24try:
25 zlib.decompressobj(0)
26except ValueError, msg:
27 print "expecting", msg
28
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +000029x = zlib.compress(buf)
30y = zlib.decompress(x)
31if buf != y:
32 print "normal compression/decompression failed"
Jeremy Hylton9dc2b8e1997-09-04 23:41:37 +000033else:
34 print "normal compression/decompression succeeded"
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +000035
36buf = buf * 16
37
38co = zlib.compressobj(8, 8, -15)
39x1 = co.compress(buf)
40x2 = co.flush()
41x = x1 + x2
42
43dc = zlib.decompressobj(-15)
44y1 = dc.decompress(x)
45y2 = dc.flush()
46y = y1 + y2
47if buf != y:
48 print "compress/decompression obj failed"
Jeremy Hylton9dc2b8e1997-09-04 23:41:37 +000049else:
50 print "compress/decompression obj succeeded"
51
52co = zlib.compressobj(2, 8, -12, 9, 1)
53bufs = []
54for i in range(0, len(buf), 256):
55 bufs.append(co.compress(buf[i:i+256]))
56bufs.append(co.flush())
57combuf = string.join(bufs, '')
58
59decomp1 = zlib.decompress(combuf, -12, -5)
60if decomp1 != buf:
61 print "decompress with init options failed"
62else:
63 print "decompress with init options succeeded"
64
65deco = zlib.decompressobj(-12)
66bufs = []
67for i in range(0, len(combuf), 128):
68 bufs.append(deco.decompress(combuf[i:i+128]))
69bufs.append(deco.flush())
70decomp2 = string.join(buf, '')
71if decomp2 != buf:
72 print "decompressobj with init options failed"
73else:
74 print "decompressobj with init options succeeded"
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +000075
76def ignore():
77 """An empty function with a big string.
78
79 Make the compression algorithm work a little harder.
80 """
81
82 """
83LAERTES
84
85 O, fear me not.
86 I stay too long: but here my father comes.
87
88 Enter POLONIUS
89
90 A double blessing is a double grace,
91 Occasion smiles upon a second leave.
92
93LORD POLONIUS
94
95 Yet here, Laertes! aboard, aboard, for shame!
96 The wind sits in the shoulder of your sail,
97 And you are stay'd for. There; my blessing with thee!
98 And these few precepts in thy memory
99 See thou character. Give thy thoughts no tongue,
100 Nor any unproportioned thought his act.
101 Be thou familiar, but by no means vulgar.
102 Those friends thou hast, and their adoption tried,
103 Grapple them to thy soul with hoops of steel;
104 But do not dull thy palm with entertainment
105 Of each new-hatch'd, unfledged comrade. Beware
106 Of entrance to a quarrel, but being in,
107 Bear't that the opposed may beware of thee.
108 Give every man thy ear, but few thy voice;
109 Take each man's censure, but reserve thy judgment.
110 Costly thy habit as thy purse can buy,
111 But not express'd in fancy; rich, not gaudy;
112 For the apparel oft proclaims the man,
113 And they in France of the best rank and station
114 Are of a most select and generous chief in that.
115 Neither a borrower nor a lender be;
116 For loan oft loses both itself and friend,
117 And borrowing dulls the edge of husbandry.
118 This above all: to thine ownself be true,
119 And it must follow, as the night the day,
120 Thou canst not then be false to any man.
121 Farewell: my blessing season this in thee!
122
123LAERTES
124
125 Most humbly do I take my leave, my lord.
126
127LORD POLONIUS
128
129 The time invites you; go; your servants tend.
130
131LAERTES
132
133 Farewell, Ophelia; and remember well
134 What I have said to you.
135
136OPHELIA
137
138 'Tis in my memory lock'd,
139 And you yourself shall keep the key of it.
140
141LAERTES
142
143 Farewell.
144"""
145