Jeremy Hylton | 6eb4b6a | 1997-08-15 15:59:43 +0000 | [diff] [blame] | 1 | import zlib |
| 2 | import sys |
Guido van Rossum | c95a6c1 | 1997-08-15 16:23:32 +0000 | [diff] [blame] | 3 | import imp |
Jeremy Hylton | 6eb4b6a | 1997-08-15 15:59:43 +0000 | [diff] [blame] | 4 | |
Guido van Rossum | 629bcfb | 1997-12-18 05:21:07 +0000 | [diff] [blame] | 5 | try: |
| 6 | t = imp.find_module('test_zlib') |
| 7 | file = t[0] |
| 8 | except ImportError: |
| 9 | file = open(__file__) |
Guido van Rossum | c95a6c1 | 1997-08-15 16:23:32 +0000 | [diff] [blame] | 10 | buf = file.read() * 8 |
| 11 | file.close() |
Jeremy Hylton | 6eb4b6a | 1997-08-15 15:59:43 +0000 | [diff] [blame] | 12 | |
Jeremy Hylton | a05e293 | 2000-06-28 14:48:01 +0000 | [diff] [blame] | 13 | # test the checksums (hex so the test doesn't break on 64-bit machines) |
Guido van Rossum | 446898f | 1998-04-24 18:31:28 +0000 | [diff] [blame] | 14 | print hex(zlib.crc32('penguin')), hex(zlib.crc32('penguin', 1)) |
| 15 | print hex(zlib.adler32('penguin')), hex(zlib.adler32('penguin', 1)) |
Jeremy Hylton | 9dc2b8e | 1997-09-04 23:41:37 +0000 | [diff] [blame] | 16 | |
| 17 | # make sure we generate some expected errors |
| 18 | try: |
| 19 | zlib.compress('ERROR', zlib.MAX_WBITS + 1) |
| 20 | except zlib.error, msg: |
| 21 | print "expecting", msg |
| 22 | try: |
| 23 | zlib.compressobj(1, 8, 0) |
| 24 | except ValueError, msg: |
| 25 | print "expecting", msg |
| 26 | try: |
| 27 | zlib.decompressobj(0) |
| 28 | except ValueError, msg: |
| 29 | print "expecting", msg |
| 30 | |
Jeremy Hylton | 6eb4b6a | 1997-08-15 15:59:43 +0000 | [diff] [blame] | 31 | x = zlib.compress(buf) |
| 32 | y = zlib.decompress(x) |
| 33 | if buf != y: |
| 34 | print "normal compression/decompression failed" |
Jeremy Hylton | 9dc2b8e | 1997-09-04 23:41:37 +0000 | [diff] [blame] | 35 | else: |
| 36 | print "normal compression/decompression succeeded" |
Jeremy Hylton | 6eb4b6a | 1997-08-15 15:59:43 +0000 | [diff] [blame] | 37 | |
| 38 | buf = buf * 16 |
| 39 | |
| 40 | co = zlib.compressobj(8, 8, -15) |
| 41 | x1 = co.compress(buf) |
| 42 | x2 = co.flush() |
| 43 | x = x1 + x2 |
| 44 | |
| 45 | dc = zlib.decompressobj(-15) |
| 46 | y1 = dc.decompress(x) |
| 47 | y2 = dc.flush() |
| 48 | y = y1 + y2 |
| 49 | if buf != y: |
| 50 | print "compress/decompression obj failed" |
Jeremy Hylton | 9dc2b8e | 1997-09-04 23:41:37 +0000 | [diff] [blame] | 51 | else: |
| 52 | print "compress/decompression obj succeeded" |
| 53 | |
| 54 | co = zlib.compressobj(2, 8, -12, 9, 1) |
| 55 | bufs = [] |
| 56 | for i in range(0, len(buf), 256): |
| 57 | bufs.append(co.compress(buf[i:i+256])) |
| 58 | bufs.append(co.flush()) |
Eric S. Raymond | 83ff749 | 2001-02-09 12:03:45 +0000 | [diff] [blame] | 59 | combuf = ''.join(bufs) |
Jeremy Hylton | 9dc2b8e | 1997-09-04 23:41:37 +0000 | [diff] [blame] | 60 | |
| 61 | decomp1 = zlib.decompress(combuf, -12, -5) |
| 62 | if decomp1 != buf: |
| 63 | print "decompress with init options failed" |
| 64 | else: |
| 65 | print "decompress with init options succeeded" |
| 66 | |
| 67 | deco = zlib.decompressobj(-12) |
| 68 | bufs = [] |
| 69 | for i in range(0, len(combuf), 128): |
| 70 | bufs.append(deco.decompress(combuf[i:i+128])) |
| 71 | bufs.append(deco.flush()) |
Eric S. Raymond | 83ff749 | 2001-02-09 12:03:45 +0000 | [diff] [blame] | 72 | decomp2 = ''.join(buf) |
Jeremy Hylton | 9dc2b8e | 1997-09-04 23:41:37 +0000 | [diff] [blame] | 73 | if decomp2 != buf: |
| 74 | print "decompressobj with init options failed" |
| 75 | else: |
| 76 | print "decompressobj with init options succeeded" |
Jeremy Hylton | 6eb4b6a | 1997-08-15 15:59:43 +0000 | [diff] [blame] | 77 | |
Andrew M. Kuchling | dca7e00 | 1999-03-22 19:23:17 +0000 | [diff] [blame] | 78 | # Test flush() with the various options, using all the different levels |
| 79 | # in order to provide more variations. |
| 80 | for sync in [zlib.Z_NO_FLUSH, zlib.Z_SYNC_FLUSH, zlib.Z_FULL_FLUSH]: |
| 81 | for level in range(10): |
Fred Drake | ad892dc | 2000-02-10 15:31:07 +0000 | [diff] [blame] | 82 | 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. Kuchling | dca7e00 | 1999-03-22 19:23:17 +0000 | [diff] [blame] | 90 | |
Jeremy Hylton | 6eb4b6a | 1997-08-15 15:59:43 +0000 | [diff] [blame] | 91 | def ignore(): |
| 92 | """An empty function with a big string. |
| 93 | |
| 94 | Make the compression algorithm work a little harder. |
| 95 | """ |
| 96 | |
| 97 | """ |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 98 | LAERTES |
Jeremy Hylton | 6eb4b6a | 1997-08-15 15:59:43 +0000 | [diff] [blame] | 99 | |
| 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 Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 108 | LORD POLONIUS |
Jeremy Hylton | 6eb4b6a | 1997-08-15 15:59:43 +0000 | [diff] [blame] | 109 | |
| 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 Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 138 | LAERTES |
Jeremy Hylton | 6eb4b6a | 1997-08-15 15:59:43 +0000 | [diff] [blame] | 139 | |
| 140 | Most humbly do I take my leave, my lord. |
| 141 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 142 | LORD POLONIUS |
Jeremy Hylton | 6eb4b6a | 1997-08-15 15:59:43 +0000 | [diff] [blame] | 143 | |
| 144 | The time invites you; go; your servants tend. |
| 145 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 146 | LAERTES |
Jeremy Hylton | 6eb4b6a | 1997-08-15 15:59:43 +0000 | [diff] [blame] | 147 | |
| 148 | Farewell, Ophelia; and remember well |
| 149 | What I have said to you. |
| 150 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 151 | OPHELIA |
Jeremy Hylton | 6eb4b6a | 1997-08-15 15:59:43 +0000 | [diff] [blame] | 152 | |
| 153 | 'Tis in my memory lock'd, |
| 154 | And you yourself shall keep the key of it. |
| 155 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 156 | LAERTES |
Jeremy Hylton | 6eb4b6a | 1997-08-15 15:59:43 +0000 | [diff] [blame] | 157 | |
| 158 | Farewell. |
| 159 | """ |