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