Jeremy Hylton | 6eb4b6a | 1997-08-15 15:59:43 +0000 | [diff] [blame] | 1 | import zlib |
Andrew M. Kuchling | 9a0f98e | 2001-02-21 02:17:01 +0000 | [diff] [blame] | 2 | from test_support import TestFailed |
Jeremy Hylton | 6eb4b6a | 1997-08-15 15:59:43 +0000 | [diff] [blame] | 3 | import sys |
Guido van Rossum | c95a6c1 | 1997-08-15 16:23:32 +0000 | [diff] [blame] | 4 | import imp |
Jeremy Hylton | 6eb4b6a | 1997-08-15 15:59:43 +0000 | [diff] [blame] | 5 | |
Guido van Rossum | 629bcfb | 1997-12-18 05:21:07 +0000 | [diff] [blame] | 6 | try: |
| 7 | t = imp.find_module('test_zlib') |
| 8 | file = t[0] |
| 9 | except ImportError: |
| 10 | file = open(__file__) |
Guido van Rossum | c95a6c1 | 1997-08-15 16:23:32 +0000 | [diff] [blame] | 11 | buf = file.read() * 8 |
| 12 | file.close() |
Jeremy Hylton | 6eb4b6a | 1997-08-15 15:59:43 +0000 | [diff] [blame] | 13 | |
Jeremy Hylton | a05e293 | 2000-06-28 14:48:01 +0000 | [diff] [blame] | 14 | # 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] | 15 | print hex(zlib.crc32('penguin')), hex(zlib.crc32('penguin', 1)) |
| 16 | print hex(zlib.adler32('penguin')), hex(zlib.adler32('penguin', 1)) |
Jeremy Hylton | 9dc2b8e | 1997-09-04 23:41:37 +0000 | [diff] [blame] | 17 | |
| 18 | # make sure we generate some expected errors |
| 19 | try: |
| 20 | zlib.compress('ERROR', zlib.MAX_WBITS + 1) |
| 21 | except zlib.error, msg: |
| 22 | print "expecting", msg |
| 23 | try: |
| 24 | zlib.compressobj(1, 8, 0) |
| 25 | except ValueError, msg: |
| 26 | print "expecting", msg |
| 27 | try: |
| 28 | zlib.decompressobj(0) |
| 29 | except ValueError, msg: |
| 30 | print "expecting", msg |
| 31 | |
Jeremy Hylton | 6eb4b6a | 1997-08-15 15:59:43 +0000 | [diff] [blame] | 32 | x = zlib.compress(buf) |
| 33 | y = zlib.decompress(x) |
| 34 | if buf != y: |
| 35 | print "normal compression/decompression failed" |
Jeremy Hylton | 9dc2b8e | 1997-09-04 23:41:37 +0000 | [diff] [blame] | 36 | else: |
| 37 | print "normal compression/decompression succeeded" |
Jeremy Hylton | 6eb4b6a | 1997-08-15 15:59:43 +0000 | [diff] [blame] | 38 | |
| 39 | buf = buf * 16 |
| 40 | |
| 41 | co = zlib.compressobj(8, 8, -15) |
| 42 | x1 = co.compress(buf) |
| 43 | x2 = co.flush() |
| 44 | x = x1 + x2 |
| 45 | |
| 46 | dc = zlib.decompressobj(-15) |
| 47 | y1 = dc.decompress(x) |
| 48 | y2 = dc.flush() |
| 49 | y = y1 + y2 |
| 50 | if buf != y: |
| 51 | print "compress/decompression obj failed" |
Jeremy Hylton | 9dc2b8e | 1997-09-04 23:41:37 +0000 | [diff] [blame] | 52 | else: |
| 53 | print "compress/decompression obj succeeded" |
| 54 | |
| 55 | co = zlib.compressobj(2, 8, -12, 9, 1) |
| 56 | bufs = [] |
| 57 | for i in range(0, len(buf), 256): |
| 58 | bufs.append(co.compress(buf[i:i+256])) |
| 59 | bufs.append(co.flush()) |
Eric S. Raymond | 83ff749 | 2001-02-09 12:03:45 +0000 | [diff] [blame] | 60 | combuf = ''.join(bufs) |
Jeremy Hylton | 9dc2b8e | 1997-09-04 23:41:37 +0000 | [diff] [blame] | 61 | |
| 62 | decomp1 = zlib.decompress(combuf, -12, -5) |
| 63 | if decomp1 != buf: |
| 64 | print "decompress with init options failed" |
| 65 | else: |
| 66 | print "decompress with init options succeeded" |
| 67 | |
| 68 | deco = zlib.decompressobj(-12) |
| 69 | bufs = [] |
| 70 | for i in range(0, len(combuf), 128): |
| 71 | bufs.append(deco.decompress(combuf[i:i+128])) |
| 72 | bufs.append(deco.flush()) |
Andrew M. Kuchling | 8e6d44e | 2001-02-14 17:46:20 +0000 | [diff] [blame] | 73 | decomp2 = ''.join(bufs) |
Jeremy Hylton | 9dc2b8e | 1997-09-04 23:41:37 +0000 | [diff] [blame] | 74 | if decomp2 != buf: |
| 75 | print "decompressobj with init options failed" |
| 76 | else: |
| 77 | print "decompressobj with init options succeeded" |
Jeremy Hylton | 6eb4b6a | 1997-08-15 15:59:43 +0000 | [diff] [blame] | 78 | |
Jeremy Hylton | 511e2ca | 2001-10-16 20:39:49 +0000 | [diff] [blame] | 79 | print "should be '':", `deco.unconsumed_tail` |
| 80 | |
| 81 | # Check a decompression object with max_length specified |
| 82 | deco = zlib.decompressobj(-12) |
| 83 | cb = combuf |
| 84 | bufs = [] |
| 85 | while cb: |
| 86 | max_length = 1 + len(cb)/10 |
| 87 | chunk = deco.decompress(cb, max_length) |
| 88 | if len(chunk) > max_length: |
| 89 | print 'chunk too big (%d>%d)' % (len(chunk),max_length) |
| 90 | bufs.append(chunk) |
| 91 | cb = deco.unconsumed_tail |
| 92 | bufs.append(deco.flush()) |
| 93 | decomp2 = ''.join(buf) |
| 94 | if decomp2 != buf: |
| 95 | print "max_length decompressobj failed" |
| 96 | else: |
| 97 | print "max_length decompressobj succeeded" |
Tim Peters | e0c446b | 2001-10-18 21:57:37 +0000 | [diff] [blame] | 98 | |
Jeremy Hylton | 511e2ca | 2001-10-16 20:39:49 +0000 | [diff] [blame] | 99 | # Misc tests of max_length |
| 100 | deco = zlib.decompressobj(-12) |
| 101 | try: |
| 102 | deco.decompress("", -1) |
| 103 | except ValueError: |
| 104 | pass |
| 105 | else: |
| 106 | print "failed to raise value error on bad max_length" |
| 107 | print "unconsumed_tail should be '':", `deco.unconsumed_tail` |
| 108 | |
Andrew M. Kuchling | dca7e00 | 1999-03-22 19:23:17 +0000 | [diff] [blame] | 109 | # Test flush() with the various options, using all the different levels |
| 110 | # in order to provide more variations. |
Andrew M. Kuchling | fcfc8d5 | 2001-08-10 15:50:11 +0000 | [diff] [blame] | 111 | sync_opt = ['Z_NO_FLUSH', 'Z_SYNC_FLUSH', 'Z_FULL_FLUSH'] |
| 112 | sync_opt = [getattr(zlib, opt) for opt in sync_opt if hasattr(zlib, opt)] |
| 113 | |
| 114 | for sync in sync_opt: |
Andrew M. Kuchling | dca7e00 | 1999-03-22 19:23:17 +0000 | [diff] [blame] | 115 | for level in range(10): |
Fred Drake | ad892dc | 2000-02-10 15:31:07 +0000 | [diff] [blame] | 116 | obj = zlib.compressobj( level ) |
| 117 | d = obj.compress( buf[:3000] ) |
| 118 | d = d + obj.flush( sync ) |
| 119 | d = d + obj.compress( buf[3000:] ) |
| 120 | d = d + obj.flush() |
| 121 | if zlib.decompress(d) != buf: |
| 122 | print "Decompress failed: flush mode=%i, level=%i" % (sync,level) |
| 123 | del obj |
Andrew M. Kuchling | dca7e00 | 1999-03-22 19:23:17 +0000 | [diff] [blame] | 124 | |
Andrew M. Kuchling | 9a0f98e | 2001-02-21 02:17:01 +0000 | [diff] [blame] | 125 | # Test for the odd flushing bugs noted in 2.0, and hopefully fixed in 2.1 |
| 126 | |
| 127 | import random |
Tim Peters | 0009c4e | 2001-02-21 07:29:48 +0000 | [diff] [blame] | 128 | random.seed(1) |
Andrew M. Kuchling | 9a0f98e | 2001-02-21 02:17:01 +0000 | [diff] [blame] | 129 | |
| 130 | print 'Testing on 17K of random data' |
| 131 | |
Andrew M. Kuchling | fcfc8d5 | 2001-08-10 15:50:11 +0000 | [diff] [blame] | 132 | if hasattr(zlib, 'Z_SYNC_FLUSH'): |
Andrew M. Kuchling | 9a0f98e | 2001-02-21 02:17:01 +0000 | [diff] [blame] | 133 | |
Andrew M. Kuchling | fcfc8d5 | 2001-08-10 15:50:11 +0000 | [diff] [blame] | 134 | # Create compressor and decompressor objects |
| 135 | c=zlib.compressobj(9) |
| 136 | d=zlib.decompressobj() |
Tim Peters | 0009c4e | 2001-02-21 07:29:48 +0000 | [diff] [blame] | 137 | |
Andrew M. Kuchling | fcfc8d5 | 2001-08-10 15:50:11 +0000 | [diff] [blame] | 138 | # Try 17K of data |
| 139 | # generate random data stream |
| 140 | a="" |
| 141 | for i in range(17*1024): |
| 142 | a=a+chr(random.randint(0,255)) |
Andrew M. Kuchling | 9a0f98e | 2001-02-21 02:17:01 +0000 | [diff] [blame] | 143 | |
Andrew M. Kuchling | fcfc8d5 | 2001-08-10 15:50:11 +0000 | [diff] [blame] | 144 | # compress, sync-flush, and decompress |
| 145 | t = d.decompress( c.compress(a)+c.flush(zlib.Z_SYNC_FLUSH) ) |
| 146 | |
| 147 | # if decompressed data is different from the input data, choke. |
| 148 | if len(t) != len(a): |
| 149 | print len(a),len(t),len(d.unused_data) |
| 150 | raise TestFailed, "output of 17K doesn't match" |
Andrew M. Kuchling | 9a0f98e | 2001-02-21 02:17:01 +0000 | [diff] [blame] | 151 | |
Jeremy Hylton | 6eb4b6a | 1997-08-15 15:59:43 +0000 | [diff] [blame] | 152 | def ignore(): |
| 153 | """An empty function with a big string. |
| 154 | |
| 155 | Make the compression algorithm work a little harder. |
| 156 | """ |
| 157 | |
| 158 | """ |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 159 | LAERTES |
Jeremy Hylton | 6eb4b6a | 1997-08-15 15:59:43 +0000 | [diff] [blame] | 160 | |
| 161 | O, fear me not. |
| 162 | I stay too long: but here my father comes. |
| 163 | |
| 164 | Enter POLONIUS |
| 165 | |
| 166 | A double blessing is a double grace, |
| 167 | Occasion smiles upon a second leave. |
| 168 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 169 | LORD POLONIUS |
Jeremy Hylton | 6eb4b6a | 1997-08-15 15:59:43 +0000 | [diff] [blame] | 170 | |
| 171 | Yet here, Laertes! aboard, aboard, for shame! |
| 172 | The wind sits in the shoulder of your sail, |
| 173 | And you are stay'd for. There; my blessing with thee! |
| 174 | And these few precepts in thy memory |
| 175 | See thou character. Give thy thoughts no tongue, |
| 176 | Nor any unproportioned thought his act. |
| 177 | Be thou familiar, but by no means vulgar. |
| 178 | Those friends thou hast, and their adoption tried, |
| 179 | Grapple them to thy soul with hoops of steel; |
| 180 | But do not dull thy palm with entertainment |
| 181 | Of each new-hatch'd, unfledged comrade. Beware |
| 182 | Of entrance to a quarrel, but being in, |
| 183 | Bear't that the opposed may beware of thee. |
| 184 | Give every man thy ear, but few thy voice; |
| 185 | Take each man's censure, but reserve thy judgment. |
| 186 | Costly thy habit as thy purse can buy, |
| 187 | But not express'd in fancy; rich, not gaudy; |
| 188 | For the apparel oft proclaims the man, |
| 189 | And they in France of the best rank and station |
| 190 | Are of a most select and generous chief in that. |
| 191 | Neither a borrower nor a lender be; |
| 192 | For loan oft loses both itself and friend, |
| 193 | And borrowing dulls the edge of husbandry. |
| 194 | This above all: to thine ownself be true, |
| 195 | And it must follow, as the night the day, |
| 196 | Thou canst not then be false to any man. |
| 197 | Farewell: my blessing season this in thee! |
| 198 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 199 | LAERTES |
Jeremy Hylton | 6eb4b6a | 1997-08-15 15:59:43 +0000 | [diff] [blame] | 200 | |
| 201 | Most humbly do I take my leave, my lord. |
| 202 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 203 | LORD POLONIUS |
Jeremy Hylton | 6eb4b6a | 1997-08-15 15:59:43 +0000 | [diff] [blame] | 204 | |
| 205 | The time invites you; go; your servants tend. |
| 206 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 207 | LAERTES |
Jeremy Hylton | 6eb4b6a | 1997-08-15 15:59:43 +0000 | [diff] [blame] | 208 | |
| 209 | Farewell, Ophelia; and remember well |
| 210 | What I have said to you. |
| 211 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 212 | OPHELIA |
Jeremy Hylton | 6eb4b6a | 1997-08-15 15:59:43 +0000 | [diff] [blame] | 213 | |
| 214 | 'Tis in my memory lock'd, |
| 215 | And you yourself shall keep the key of it. |
| 216 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 217 | LAERTES |
Jeremy Hylton | 6eb4b6a | 1997-08-15 15:59:43 +0000 | [diff] [blame] | 218 | |
| 219 | Farewell. |
| 220 | """ |