blob: f1f9d3809061891af3793cc3dabb5a405dedd9d5 [file] [log] [blame]
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001import unittest
2from test import test_support
Gregory P. Smithc856fa82008-03-18 22:27:41 +00003import binascii
Andrew M. Kuchling9a0f98e2001-02-21 02:17:01 +00004import random
Andrew M. Kuchling9a0f98e2001-02-21 02:17:01 +00005
R. David Murray3db8a342009-03-30 23:05:48 +00006zlib = test_support.import_module('zlib')
7
Andrew M. Kuchling9a0f98e2001-02-21 02:17:01 +00008
Guido van Rossum7d9ea502003-02-03 20:45:52 +00009class ChecksumTestCase(unittest.TestCase):
10 # checksum test cases
11 def test_crc32start(self):
12 self.assertEqual(zlib.crc32(""), zlib.crc32("", 0))
Benjamin Peterson5c8da862009-06-30 22:57:08 +000013 self.assertTrue(zlib.crc32("abc", 0xffffffff))
Andrew M. Kuchlingfcfc8d52001-08-10 15:50:11 +000014
Guido van Rossum7d9ea502003-02-03 20:45:52 +000015 def test_crc32empty(self):
16 self.assertEqual(zlib.crc32("", 0), 0)
17 self.assertEqual(zlib.crc32("", 1), 1)
18 self.assertEqual(zlib.crc32("", 432), 432)
Andrew M. Kuchling9a0f98e2001-02-21 02:17:01 +000019
Guido van Rossum7d9ea502003-02-03 20:45:52 +000020 def test_adler32start(self):
21 self.assertEqual(zlib.adler32(""), zlib.adler32("", 1))
Benjamin Peterson5c8da862009-06-30 22:57:08 +000022 self.assertTrue(zlib.adler32("abc", 0xffffffff))
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +000023
Guido van Rossum7d9ea502003-02-03 20:45:52 +000024 def test_adler32empty(self):
25 self.assertEqual(zlib.adler32("", 0), 0)
26 self.assertEqual(zlib.adler32("", 1), 1)
27 self.assertEqual(zlib.adler32("", 432), 432)
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +000028
Guido van Rossum7d9ea502003-02-03 20:45:52 +000029 def assertEqual32(self, seen, expected):
30 # 32-bit values masked -- checksums on 32- vs 64- bit machines
31 # This is important if bit 31 (0x08000000L) is set.
32 self.assertEqual(seen & 0x0FFFFFFFFL, expected & 0x0FFFFFFFFL)
33
34 def test_penguins(self):
35 self.assertEqual32(zlib.crc32("penguin", 0), 0x0e5c1a120L)
36 self.assertEqual32(zlib.crc32("penguin", 1), 0x43b6aa94)
37 self.assertEqual32(zlib.adler32("penguin", 0), 0x0bcf02f6)
38 self.assertEqual32(zlib.adler32("penguin", 1), 0x0bd602f7)
39
40 self.assertEqual(zlib.crc32("penguin"), zlib.crc32("penguin", 0))
41 self.assertEqual(zlib.adler32("penguin"),zlib.adler32("penguin",1))
42
Gregory P. Smithf48f9d32008-03-17 18:48:05 +000043 def test_abcdefghijklmnop(self):
44 """test issue1202 compliance: signed crc32, adler32 in 2.x"""
45 foo = 'abcdefghijklmnop'
46 # explicitly test signed behavior
47 self.assertEqual(zlib.crc32(foo), -1808088941)
48 self.assertEqual(zlib.crc32('spam'), 1138425661)
49 self.assertEqual(zlib.adler32(foo+foo), -721416943)
50 self.assertEqual(zlib.adler32('spam'), 72286642)
51
Gregory P. Smithc856fa82008-03-18 22:27:41 +000052 def test_same_as_binascii_crc32(self):
53 foo = 'abcdefghijklmnop'
54 self.assertEqual(binascii.crc32(foo), zlib.crc32(foo))
55 self.assertEqual(binascii.crc32('spam'), zlib.crc32('spam'))
56
Gregory P. Smith88440962008-03-25 06:12:45 +000057 def test_negative_crc_iv_input(self):
58 # The range of valid input values for the crc state should be
59 # -2**31 through 2**32-1 to allow inputs artifically constrained
60 # to a signed 32-bit integer.
61 self.assertEqual(zlib.crc32('ham', -1), zlib.crc32('ham', 0xffffffffL))
62 self.assertEqual(zlib.crc32('spam', -3141593),
63 zlib.crc32('spam', 0xffd01027L))
64 self.assertEqual(zlib.crc32('spam', -(2**31)),
65 zlib.crc32('spam', (2**31)))
Guido van Rossum7d9ea502003-02-03 20:45:52 +000066
67
68class ExceptionTestCase(unittest.TestCase):
69 # make sure we generate some expected errors
Armin Rigoec560192007-10-15 07:48:35 +000070 def test_badlevel(self):
71 # specifying compression level out of range causes an error
72 # (but -1 is Z_DEFAULT_COMPRESSION and apparently the zlib
73 # accepts 0 too)
74 self.assertRaises(zlib.error, zlib.compress, 'ERROR', 10)
Guido van Rossum7d9ea502003-02-03 20:45:52 +000075
76 def test_badcompressobj(self):
77 # verify failure on building compress object with bad params
Neil Schemenauer94afd3e2004-06-05 19:02:52 +000078 self.assertRaises(ValueError, zlib.compressobj, 1, zlib.DEFLATED, 0)
Armin Rigoec560192007-10-15 07:48:35 +000079 # specifying total bits too large causes an error
80 self.assertRaises(ValueError,
81 zlib.compressobj, 1, zlib.DEFLATED, zlib.MAX_WBITS + 1)
Guido van Rossum7d9ea502003-02-03 20:45:52 +000082
83 def test_baddecompressobj(self):
84 # verify failure on building decompress object with bad params
85 self.assertRaises(ValueError, zlib.decompressobj, 0)
86
Gregory P. Smith79e42a02008-04-09 00:25:17 +000087 def test_decompressobj_badflush(self):
88 # verify failure on calling decompressobj.flush with bad params
89 self.assertRaises(ValueError, zlib.decompressobj().flush, 0)
90 self.assertRaises(ValueError, zlib.decompressobj().flush, -1)
91
Guido van Rossum7d9ea502003-02-03 20:45:52 +000092
93
94class CompressTestCase(unittest.TestCase):
95 # Test compression in one go (whole message compression)
96 def test_speech(self):
Neil Schemenauer6412b122004-06-05 19:34:28 +000097 x = zlib.compress(HAMLET_SCENE)
98 self.assertEqual(zlib.decompress(x), HAMLET_SCENE)
Guido van Rossum7d9ea502003-02-03 20:45:52 +000099
100 def test_speech128(self):
Neil Schemenauer6412b122004-06-05 19:34:28 +0000101 # compress more data
102 data = HAMLET_SCENE * 128
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000103 x = zlib.compress(data)
104 self.assertEqual(zlib.decompress(x), data)
105
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000106
107
108
109class CompressObjectTestCase(unittest.TestCase):
110 # Test compression object
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000111 def test_pair(self):
Neil Schemenauer6412b122004-06-05 19:34:28 +0000112 # straightforward compress/decompress objects
113 data = HAMLET_SCENE * 128
114 co = zlib.compressobj()
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000115 x1 = co.compress(data)
116 x2 = co.flush()
117 self.assertRaises(zlib.error, co.flush) # second flush should not work
Neil Schemenauer94afd3e2004-06-05 19:02:52 +0000118 dco = zlib.decompressobj()
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000119 y1 = dco.decompress(x1 + x2)
120 y2 = dco.flush()
121 self.assertEqual(data, y1 + y2)
122
Neil Schemenauer94afd3e2004-06-05 19:02:52 +0000123 def test_compressoptions(self):
124 # specify lots of options to compressobj()
125 level = 2
126 method = zlib.DEFLATED
127 wbits = -12
128 memlevel = 9
129 strategy = zlib.Z_FILTERED
130 co = zlib.compressobj(level, method, wbits, memlevel, strategy)
Neil Schemenauer6412b122004-06-05 19:34:28 +0000131 x1 = co.compress(HAMLET_SCENE)
Neil Schemenauer94afd3e2004-06-05 19:02:52 +0000132 x2 = co.flush()
133 dco = zlib.decompressobj(wbits)
134 y1 = dco.decompress(x1 + x2)
135 y2 = dco.flush()
Neil Schemenauer6412b122004-06-05 19:34:28 +0000136 self.assertEqual(HAMLET_SCENE, y1 + y2)
Neil Schemenauer94afd3e2004-06-05 19:02:52 +0000137
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000138 def test_compressincremental(self):
139 # compress object in steps, decompress object as one-shot
Neil Schemenauer6412b122004-06-05 19:34:28 +0000140 data = HAMLET_SCENE * 128
Neil Schemenauer94afd3e2004-06-05 19:02:52 +0000141 co = zlib.compressobj()
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000142 bufs = []
143 for i in range(0, len(data), 256):
144 bufs.append(co.compress(data[i:i+256]))
145 bufs.append(co.flush())
146 combuf = ''.join(bufs)
147
Neil Schemenauer94afd3e2004-06-05 19:02:52 +0000148 dco = zlib.decompressobj()
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000149 y1 = dco.decompress(''.join(bufs))
150 y2 = dco.flush()
151 self.assertEqual(data, y1 + y2)
152
Neil Schemenauer6412b122004-06-05 19:34:28 +0000153 def test_decompinc(self, flush=False, source=None, cx=256, dcx=64):
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000154 # compress object in steps, decompress object in steps
Neil Schemenauer6412b122004-06-05 19:34:28 +0000155 source = source or HAMLET_SCENE
156 data = source * 128
Neil Schemenauer94afd3e2004-06-05 19:02:52 +0000157 co = zlib.compressobj()
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000158 bufs = []
Neil Schemenauer6412b122004-06-05 19:34:28 +0000159 for i in range(0, len(data), cx):
160 bufs.append(co.compress(data[i:i+cx]))
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000161 bufs.append(co.flush())
162 combuf = ''.join(bufs)
163
Neil Schemenauer94afd3e2004-06-05 19:02:52 +0000164 self.assertEqual(data, zlib.decompress(combuf))
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000165
Neil Schemenauer94afd3e2004-06-05 19:02:52 +0000166 dco = zlib.decompressobj()
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000167 bufs = []
Neil Schemenauer6412b122004-06-05 19:34:28 +0000168 for i in range(0, len(combuf), dcx):
169 bufs.append(dco.decompress(combuf[i:i+dcx]))
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000170 self.assertEqual('', dco.unconsumed_tail, ########
171 "(A) uct should be '': not %d long" %
Neil Schemenauer6412b122004-06-05 19:34:28 +0000172 len(dco.unconsumed_tail))
173 if flush:
174 bufs.append(dco.flush())
175 else:
176 while True:
177 chunk = dco.decompress('')
178 if chunk:
179 bufs.append(chunk)
180 else:
181 break
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000182 self.assertEqual('', dco.unconsumed_tail, ########
Neil Schemenauer6412b122004-06-05 19:34:28 +0000183 "(B) uct should be '': not %d long" %
184 len(dco.unconsumed_tail))
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000185 self.assertEqual(data, ''.join(bufs))
186 # Failure means: "decompressobj with init options failed"
187
Neil Schemenauer6412b122004-06-05 19:34:28 +0000188 def test_decompincflush(self):
189 self.test_decompinc(flush=True)
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000190
Neil Schemenauer6412b122004-06-05 19:34:28 +0000191 def test_decompimax(self, source=None, cx=256, dcx=64):
192 # compress in steps, decompress in length-restricted steps
193 source = source or HAMLET_SCENE
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000194 # Check a decompression object with max_length specified
Neil Schemenauer6412b122004-06-05 19:34:28 +0000195 data = source * 128
196 co = zlib.compressobj()
197 bufs = []
198 for i in range(0, len(data), cx):
199 bufs.append(co.compress(data[i:i+cx]))
200 bufs.append(co.flush())
201 combuf = ''.join(bufs)
202 self.assertEqual(data, zlib.decompress(combuf),
203 'compressed data failure')
204
205 dco = zlib.decompressobj()
206 bufs = []
207 cb = combuf
208 while cb:
209 #max_length = 1 + len(cb)//10
210 chunk = dco.decompress(cb, dcx)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000211 self.assertFalse(len(chunk) > dcx,
Neil Schemenauer6412b122004-06-05 19:34:28 +0000212 'chunk too big (%d>%d)' % (len(chunk), dcx))
213 bufs.append(chunk)
214 cb = dco.unconsumed_tail
215 bufs.append(dco.flush())
216 self.assertEqual(data, ''.join(bufs), 'Wrong data retrieved')
217
218 def test_decompressmaxlen(self, flush=False):
219 # Check a decompression object with max_length specified
220 data = HAMLET_SCENE * 128
Neil Schemenauer94afd3e2004-06-05 19:02:52 +0000221 co = zlib.compressobj()
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000222 bufs = []
223 for i in range(0, len(data), 256):
224 bufs.append(co.compress(data[i:i+256]))
225 bufs.append(co.flush())
226 combuf = ''.join(bufs)
Neil Schemenauer94afd3e2004-06-05 19:02:52 +0000227 self.assertEqual(data, zlib.decompress(combuf),
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000228 'compressed data failure')
229
Neil Schemenauer94afd3e2004-06-05 19:02:52 +0000230 dco = zlib.decompressobj()
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000231 bufs = []
232 cb = combuf
233 while cb:
Guido van Rossumf3594102003-02-27 18:39:18 +0000234 max_length = 1 + len(cb)//10
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000235 chunk = dco.decompress(cb, max_length)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000236 self.assertFalse(len(chunk) > max_length,
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000237 'chunk too big (%d>%d)' % (len(chunk),max_length))
238 bufs.append(chunk)
239 cb = dco.unconsumed_tail
Neil Schemenauer6412b122004-06-05 19:34:28 +0000240 if flush:
241 bufs.append(dco.flush())
242 else:
243 while chunk:
244 chunk = dco.decompress('', max_length)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000245 self.assertFalse(len(chunk) > max_length,
Neil Schemenauer6412b122004-06-05 19:34:28 +0000246 'chunk too big (%d>%d)' % (len(chunk),max_length))
247 bufs.append(chunk)
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000248 self.assertEqual(data, ''.join(bufs), 'Wrong data retrieved')
249
Neil Schemenauer6412b122004-06-05 19:34:28 +0000250 def test_decompressmaxlenflush(self):
251 self.test_decompressmaxlen(flush=True)
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000252
253 def test_maxlenmisc(self):
254 # Misc tests of max_length
Neil Schemenauer94afd3e2004-06-05 19:02:52 +0000255 dco = zlib.decompressobj()
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000256 self.assertRaises(ValueError, dco.decompress, "", -1)
257 self.assertEqual('', dco.unconsumed_tail)
258
259 def test_flushes(self):
260 # Test flush() with the various options, using all the
261 # different levels in order to provide more variations.
262 sync_opt = ['Z_NO_FLUSH', 'Z_SYNC_FLUSH', 'Z_FULL_FLUSH']
263 sync_opt = [getattr(zlib, opt) for opt in sync_opt
264 if hasattr(zlib, opt)]
Neil Schemenauer6412b122004-06-05 19:34:28 +0000265 data = HAMLET_SCENE * 8
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000266
267 for sync in sync_opt:
268 for level in range(10):
269 obj = zlib.compressobj( level )
270 a = obj.compress( data[:3000] )
271 b = obj.flush( sync )
272 c = obj.compress( data[3000:] )
273 d = obj.flush()
274 self.assertEqual(zlib.decompress(''.join([a,b,c,d])),
275 data, ("Decompress failed: flush "
276 "mode=%i, level=%i") % (sync, level))
277 del obj
278
279 def test_odd_flush(self):
280 # Test for odd flushing bugs noted in 2.0, and hopefully fixed in 2.1
281 import random
282
283 if hasattr(zlib, 'Z_SYNC_FLUSH'):
284 # Testing on 17K of "random" data
285
286 # Create compressor and decompressor objects
Neil Schemenauer6412b122004-06-05 19:34:28 +0000287 co = zlib.compressobj(zlib.Z_BEST_COMPRESSION)
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000288 dco = zlib.decompressobj()
289
290 # Try 17K of data
291 # generate random data stream
292 try:
293 # In 2.3 and later, WichmannHill is the RNG of the bug report
294 gen = random.WichmannHill()
295 except AttributeError:
296 try:
297 # 2.2 called it Random
298 gen = random.Random()
299 except AttributeError:
300 # others might simply have a single RNG
301 gen = random
302 gen.seed(1)
303 data = genblock(1, 17 * 1024, generator=gen)
304
305 # compress, sync-flush, and decompress
306 first = co.compress(data)
307 second = co.flush(zlib.Z_SYNC_FLUSH)
308 expanded = dco.decompress(first + second)
309
310 # if decompressed data is different from the input data, choke.
311 self.assertEqual(expanded, data, "17K random source doesn't match")
312
Andrew M. Kuchling3b585b32004-12-28 20:10:48 +0000313 def test_empty_flush(self):
314 # Test that calling .flush() on unused objects works.
315 # (Bug #1083110 -- calling .flush() on decompress objects
316 # caused a core dump.)
317
318 co = zlib.compressobj(zlib.Z_BEST_COMPRESSION)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000319 self.assertTrue(co.flush()) # Returns a zlib header
Andrew M. Kuchling3b585b32004-12-28 20:10:48 +0000320 dco = zlib.decompressobj()
321 self.assertEqual(dco.flush(), "") # Returns nothing
Tim Peters5a9fb3c2005-01-07 16:01:32 +0000322
Neal Norwitz6e73aaa2006-06-12 03:33:09 +0000323 if hasattr(zlib.compressobj(), "copy"):
324 def test_compresscopy(self):
325 # Test copying a compression object
326 data0 = HAMLET_SCENE
327 data1 = HAMLET_SCENE.swapcase()
328 c0 = zlib.compressobj(zlib.Z_BEST_COMPRESSION)
329 bufs0 = []
330 bufs0.append(c0.compress(data0))
Georg Brandl8d3342b2006-05-16 07:38:27 +0000331
Neal Norwitz6e73aaa2006-06-12 03:33:09 +0000332 c1 = c0.copy()
333 bufs1 = bufs0[:]
Georg Brandl8d3342b2006-05-16 07:38:27 +0000334
Neal Norwitz6e73aaa2006-06-12 03:33:09 +0000335 bufs0.append(c0.compress(data0))
336 bufs0.append(c0.flush())
337 s0 = ''.join(bufs0)
Georg Brandl8d3342b2006-05-16 07:38:27 +0000338
Neal Norwitz6e73aaa2006-06-12 03:33:09 +0000339 bufs1.append(c1.compress(data1))
340 bufs1.append(c1.flush())
341 s1 = ''.join(bufs1)
Georg Brandl8d3342b2006-05-16 07:38:27 +0000342
Neal Norwitz6e73aaa2006-06-12 03:33:09 +0000343 self.assertEqual(zlib.decompress(s0),data0+data0)
344 self.assertEqual(zlib.decompress(s1),data0+data1)
Georg Brandl8d3342b2006-05-16 07:38:27 +0000345
Neal Norwitz6e73aaa2006-06-12 03:33:09 +0000346 def test_badcompresscopy(self):
347 # Test copying a compression object in an inconsistent state
348 c = zlib.compressobj()
349 c.compress(HAMLET_SCENE)
350 c.flush()
351 self.assertRaises(ValueError, c.copy)
Georg Brandl8d3342b2006-05-16 07:38:27 +0000352
Neal Norwitz6e73aaa2006-06-12 03:33:09 +0000353 if hasattr(zlib.decompressobj(), "copy"):
354 def test_decompresscopy(self):
355 # Test copying a decompression object
356 data = HAMLET_SCENE
357 comp = zlib.compress(data)
Georg Brandl8d3342b2006-05-16 07:38:27 +0000358
Neal Norwitz6e73aaa2006-06-12 03:33:09 +0000359 d0 = zlib.decompressobj()
360 bufs0 = []
361 bufs0.append(d0.decompress(comp[:32]))
Georg Brandl8d3342b2006-05-16 07:38:27 +0000362
Neal Norwitz6e73aaa2006-06-12 03:33:09 +0000363 d1 = d0.copy()
364 bufs1 = bufs0[:]
Georg Brandl8d3342b2006-05-16 07:38:27 +0000365
Neal Norwitz6e73aaa2006-06-12 03:33:09 +0000366 bufs0.append(d0.decompress(comp[32:]))
367 s0 = ''.join(bufs0)
Georg Brandl8d3342b2006-05-16 07:38:27 +0000368
Neal Norwitz6e73aaa2006-06-12 03:33:09 +0000369 bufs1.append(d1.decompress(comp[32:]))
370 s1 = ''.join(bufs1)
Georg Brandl8d3342b2006-05-16 07:38:27 +0000371
Neal Norwitz6e73aaa2006-06-12 03:33:09 +0000372 self.assertEqual(s0,s1)
373 self.assertEqual(s0,data)
Georg Brandl8d3342b2006-05-16 07:38:27 +0000374
Neal Norwitz6e73aaa2006-06-12 03:33:09 +0000375 def test_baddecompresscopy(self):
376 # Test copying a compression object in an inconsistent state
377 data = zlib.compress(HAMLET_SCENE)
378 d = zlib.decompressobj()
379 d.decompress(data)
380 d.flush()
381 self.assertRaises(ValueError, d.copy)
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000382
383def genblock(seed, length, step=1024, generator=random):
384 """length-byte stream of random data from a seed (in step-byte blocks)."""
385 if seed is not None:
386 generator.seed(seed)
387 randint = generator.randint
388 if length < step or step < 2:
389 step = length
390 blocks = []
391 for i in range(0, length, step):
392 blocks.append(''.join([chr(randint(0,255))
393 for x in range(step)]))
394 return ''.join(blocks)[:length]
395
396
397
398def choose_lines(source, number, seed=None, generator=random):
399 """Return a list of number lines randomly chosen from the source"""
400 if seed is not None:
401 generator.seed(seed)
402 sources = source.split('\n')
403 return [generator.choice(sources) for n in range(number)]
404
405
406
Neil Schemenauer6412b122004-06-05 19:34:28 +0000407HAMLET_SCENE = """
Fred Drake004d5e62000-10-23 17:22:08 +0000408LAERTES
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +0000409
410 O, fear me not.
411 I stay too long: but here my father comes.
412
413 Enter POLONIUS
414
415 A double blessing is a double grace,
416 Occasion smiles upon a second leave.
417
Fred Drake004d5e62000-10-23 17:22:08 +0000418LORD POLONIUS
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +0000419
420 Yet here, Laertes! aboard, aboard, for shame!
421 The wind sits in the shoulder of your sail,
422 And you are stay'd for. There; my blessing with thee!
423 And these few precepts in thy memory
424 See thou character. Give thy thoughts no tongue,
425 Nor any unproportioned thought his act.
426 Be thou familiar, but by no means vulgar.
427 Those friends thou hast, and their adoption tried,
428 Grapple them to thy soul with hoops of steel;
429 But do not dull thy palm with entertainment
430 Of each new-hatch'd, unfledged comrade. Beware
431 Of entrance to a quarrel, but being in,
432 Bear't that the opposed may beware of thee.
433 Give every man thy ear, but few thy voice;
434 Take each man's censure, but reserve thy judgment.
435 Costly thy habit as thy purse can buy,
436 But not express'd in fancy; rich, not gaudy;
437 For the apparel oft proclaims the man,
438 And they in France of the best rank and station
439 Are of a most select and generous chief in that.
440 Neither a borrower nor a lender be;
441 For loan oft loses both itself and friend,
442 And borrowing dulls the edge of husbandry.
443 This above all: to thine ownself be true,
444 And it must follow, as the night the day,
445 Thou canst not then be false to any man.
446 Farewell: my blessing season this in thee!
447
Fred Drake004d5e62000-10-23 17:22:08 +0000448LAERTES
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +0000449
450 Most humbly do I take my leave, my lord.
451
Fred Drake004d5e62000-10-23 17:22:08 +0000452LORD POLONIUS
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +0000453
454 The time invites you; go; your servants tend.
455
Fred Drake004d5e62000-10-23 17:22:08 +0000456LAERTES
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +0000457
458 Farewell, Ophelia; and remember well
459 What I have said to you.
460
Fred Drake004d5e62000-10-23 17:22:08 +0000461OPHELIA
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +0000462
463 'Tis in my memory lock'd,
464 And you yourself shall keep the key of it.
465
Fred Drake004d5e62000-10-23 17:22:08 +0000466LAERTES
Jeremy Hylton6eb4b6a1997-08-15 15:59:43 +0000467
468 Farewell.
469"""
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000470
471
472def test_main():
Walter Dörwald21d3a322003-05-01 17:45:56 +0000473 test_support.run_unittest(
474 ChecksumTestCase,
475 ExceptionTestCase,
476 CompressTestCase,
477 CompressObjectTestCase
478 )
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000479
480if __name__ == "__main__":
481 test_main()