Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 1 | import unittest |
| 2 | from test import test_support |
Jeremy Hylton | 6eb4b6a | 1997-08-15 15:59:43 +0000 | [diff] [blame] | 3 | import zlib |
Andrew M. Kuchling | 9a0f98e | 2001-02-21 02:17:01 +0000 | [diff] [blame] | 4 | import random |
Andrew M. Kuchling | 9a0f98e | 2001-02-21 02:17:01 +0000 | [diff] [blame] | 5 | |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 6 | # print test_support.TESTFN |
Andrew M. Kuchling | 9a0f98e | 2001-02-21 02:17:01 +0000 | [diff] [blame] | 7 | |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 8 | def getbuf(): |
| 9 | # This was in the original. Avoid non-repeatable sources. |
| 10 | # Left here (unused) in case something wants to be done with it. |
| 11 | import imp |
| 12 | try: |
| 13 | t = imp.find_module('test_zlib') |
| 14 | file = t[0] |
| 15 | except ImportError: |
| 16 | file = open(__file__) |
| 17 | buf = file.read() * 8 |
| 18 | file.close() |
| 19 | return buf |
Andrew M. Kuchling | 9a0f98e | 2001-02-21 02:17:01 +0000 | [diff] [blame] | 20 | |
Tim Peters | 0009c4e | 2001-02-21 07:29:48 +0000 | [diff] [blame] | 21 | |
Andrew M. Kuchling | 9a0f98e | 2001-02-21 02:17:01 +0000 | [diff] [blame] | 22 | |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 23 | class ChecksumTestCase(unittest.TestCase): |
| 24 | # checksum test cases |
| 25 | def test_crc32start(self): |
| 26 | self.assertEqual(zlib.crc32(""), zlib.crc32("", 0)) |
Andrew M. Kuchling | fcfc8d5 | 2001-08-10 15:50:11 +0000 | [diff] [blame] | 27 | |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 28 | def test_crc32empty(self): |
| 29 | self.assertEqual(zlib.crc32("", 0), 0) |
| 30 | self.assertEqual(zlib.crc32("", 1), 1) |
| 31 | self.assertEqual(zlib.crc32("", 432), 432) |
Andrew M. Kuchling | 9a0f98e | 2001-02-21 02:17:01 +0000 | [diff] [blame] | 32 | |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 33 | def test_adler32start(self): |
| 34 | self.assertEqual(zlib.adler32(""), zlib.adler32("", 1)) |
Jeremy Hylton | 6eb4b6a | 1997-08-15 15:59:43 +0000 | [diff] [blame] | 35 | |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 36 | def test_adler32empty(self): |
| 37 | self.assertEqual(zlib.adler32("", 0), 0) |
| 38 | self.assertEqual(zlib.adler32("", 1), 1) |
| 39 | self.assertEqual(zlib.adler32("", 432), 432) |
Jeremy Hylton | 6eb4b6a | 1997-08-15 15:59:43 +0000 | [diff] [blame] | 40 | |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 41 | def assertEqual32(self, seen, expected): |
| 42 | # 32-bit values masked -- checksums on 32- vs 64- bit machines |
| 43 | # This is important if bit 31 (0x08000000L) is set. |
| 44 | self.assertEqual(seen & 0x0FFFFFFFFL, expected & 0x0FFFFFFFFL) |
| 45 | |
| 46 | def test_penguins(self): |
| 47 | self.assertEqual32(zlib.crc32("penguin", 0), 0x0e5c1a120L) |
| 48 | self.assertEqual32(zlib.crc32("penguin", 1), 0x43b6aa94) |
| 49 | self.assertEqual32(zlib.adler32("penguin", 0), 0x0bcf02f6) |
| 50 | self.assertEqual32(zlib.adler32("penguin", 1), 0x0bd602f7) |
| 51 | |
| 52 | self.assertEqual(zlib.crc32("penguin"), zlib.crc32("penguin", 0)) |
| 53 | self.assertEqual(zlib.adler32("penguin"),zlib.adler32("penguin",1)) |
| 54 | |
| 55 | |
| 56 | |
| 57 | class ExceptionTestCase(unittest.TestCase): |
| 58 | # make sure we generate some expected errors |
| 59 | def test_bigbits(self): |
| 60 | # specifying total bits too large causes an error |
| 61 | self.assertRaises(zlib.error, |
| 62 | zlib.compress, 'ERROR', zlib.MAX_WBITS + 1) |
| 63 | |
| 64 | def test_badcompressobj(self): |
| 65 | # verify failure on building compress object with bad params |
Neil Schemenauer | 94afd3e | 2004-06-05 19:02:52 +0000 | [diff] [blame^] | 66 | self.assertRaises(ValueError, zlib.compressobj, 1, zlib.DEFLATED, 0) |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 67 | |
| 68 | def test_baddecompressobj(self): |
| 69 | # verify failure on building decompress object with bad params |
| 70 | self.assertRaises(ValueError, zlib.decompressobj, 0) |
| 71 | |
| 72 | |
| 73 | |
| 74 | class CompressTestCase(unittest.TestCase): |
| 75 | # Test compression in one go (whole message compression) |
| 76 | def test_speech(self): |
| 77 | # decompress(compress(data)) better be data |
| 78 | x = zlib.compress(hamlet_scene) |
| 79 | self.assertEqual(zlib.decompress(x), hamlet_scene) |
| 80 | |
| 81 | def test_speech8(self): |
| 82 | # decompress(compress(data)) better be data -- more compression chances |
| 83 | data = hamlet_scene * 8 |
| 84 | x = zlib.compress(data) |
| 85 | self.assertEqual(zlib.decompress(x), data) |
| 86 | |
| 87 | def test_speech16(self): |
| 88 | # decompress(compress(data)) better be data -- more compression chances |
| 89 | data = hamlet_scene * 16 |
| 90 | x = zlib.compress(data) |
| 91 | self.assertEqual(zlib.decompress(x), data) |
| 92 | |
| 93 | def test_speech128(self): |
| 94 | # decompress(compress(data)) better be data -- more compression chances |
| 95 | data = hamlet_scene * 8 * 16 |
| 96 | x = zlib.compress(data) |
| 97 | self.assertEqual(zlib.decompress(x), data) |
| 98 | |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 99 | |
| 100 | |
| 101 | |
| 102 | class CompressObjectTestCase(unittest.TestCase): |
| 103 | # Test compression object |
| 104 | def test_pairsmall(self): |
| 105 | # use compress object in straightforward manner, decompress w/ object |
| 106 | data = hamlet_scene |
Neil Schemenauer | 94afd3e | 2004-06-05 19:02:52 +0000 | [diff] [blame^] | 107 | co = zlib.compressobj() |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 108 | x1 = co.compress(data) |
| 109 | x2 = co.flush() |
| 110 | self.assertRaises(zlib.error, co.flush) # second flush should not work |
Neil Schemenauer | 94afd3e | 2004-06-05 19:02:52 +0000 | [diff] [blame^] | 111 | dco = zlib.decompressobj() |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 112 | y1 = dco.decompress(x1 + x2) |
| 113 | y2 = dco.flush() |
| 114 | self.assertEqual(data, y1 + y2) |
| 115 | |
| 116 | def test_pair(self): |
| 117 | # straightforward compress/decompress objects, more compression |
| 118 | data = hamlet_scene * 8 * 16 |
Neil Schemenauer | 94afd3e | 2004-06-05 19:02:52 +0000 | [diff] [blame^] | 119 | co = zlib.compressobj(zlib.Z_BEST_COMPRESSION, zlib.DEFLATED) |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 120 | x1 = co.compress(data) |
| 121 | x2 = co.flush() |
| 122 | self.assertRaises(zlib.error, co.flush) # second flush should not work |
Neil Schemenauer | 94afd3e | 2004-06-05 19:02:52 +0000 | [diff] [blame^] | 123 | dco = zlib.decompressobj() |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 124 | y1 = dco.decompress(x1 + x2) |
| 125 | y2 = dco.flush() |
| 126 | self.assertEqual(data, y1 + y2) |
| 127 | |
Neil Schemenauer | 94afd3e | 2004-06-05 19:02:52 +0000 | [diff] [blame^] | 128 | def test_compressoptions(self): |
| 129 | # specify lots of options to compressobj() |
| 130 | level = 2 |
| 131 | method = zlib.DEFLATED |
| 132 | wbits = -12 |
| 133 | memlevel = 9 |
| 134 | strategy = zlib.Z_FILTERED |
| 135 | co = zlib.compressobj(level, method, wbits, memlevel, strategy) |
| 136 | x1 = co.compress(hamlet_scene) |
| 137 | x2 = co.flush() |
| 138 | dco = zlib.decompressobj(wbits) |
| 139 | y1 = dco.decompress(x1 + x2) |
| 140 | y2 = dco.flush() |
| 141 | self.assertEqual(hamlet_scene, y1 + y2) |
| 142 | |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 143 | def test_compressincremental(self): |
| 144 | # compress object in steps, decompress object as one-shot |
| 145 | data = hamlet_scene * 8 * 16 |
Neil Schemenauer | 94afd3e | 2004-06-05 19:02:52 +0000 | [diff] [blame^] | 146 | co = zlib.compressobj() |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 147 | bufs = [] |
| 148 | for i in range(0, len(data), 256): |
| 149 | bufs.append(co.compress(data[i:i+256])) |
| 150 | bufs.append(co.flush()) |
| 151 | combuf = ''.join(bufs) |
| 152 | |
Neil Schemenauer | 94afd3e | 2004-06-05 19:02:52 +0000 | [diff] [blame^] | 153 | dco = zlib.decompressobj() |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 154 | y1 = dco.decompress(''.join(bufs)) |
| 155 | y2 = dco.flush() |
| 156 | self.assertEqual(data, y1 + y2) |
| 157 | |
| 158 | def test_decompressincremental(self): |
| 159 | # compress object in steps, decompress object in steps |
| 160 | data = hamlet_scene * 8 * 16 |
Neil Schemenauer | 94afd3e | 2004-06-05 19:02:52 +0000 | [diff] [blame^] | 161 | co = zlib.compressobj() |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 162 | bufs = [] |
| 163 | for i in range(0, len(data), 256): |
| 164 | bufs.append(co.compress(data[i:i+256])) |
| 165 | bufs.append(co.flush()) |
| 166 | combuf = ''.join(bufs) |
| 167 | |
Neil Schemenauer | 94afd3e | 2004-06-05 19:02:52 +0000 | [diff] [blame^] | 168 | self.assertEqual(data, zlib.decompress(combuf)) |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 169 | |
Neil Schemenauer | 94afd3e | 2004-06-05 19:02:52 +0000 | [diff] [blame^] | 170 | dco = zlib.decompressobj() |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 171 | bufs = [] |
| 172 | for i in range(0, len(combuf), 128): |
| 173 | bufs.append(dco.decompress(combuf[i:i+128])) |
| 174 | self.assertEqual('', dco.unconsumed_tail, ######## |
| 175 | "(A) uct should be '': not %d long" % |
| 176 | len(dco.unconsumed_tail)) |
| 177 | bufs.append(dco.flush()) |
| 178 | self.assertEqual('', dco.unconsumed_tail, ######## |
| 179 | "(B) uct should be '': not %d long" % |
| 180 | len(dco.unconsumed_tail)) |
| 181 | self.assertEqual(data, ''.join(bufs)) |
| 182 | # Failure means: "decompressobj with init options failed" |
| 183 | |
| 184 | def test_decompinc(self,sizes=[128],flush=True,source=None,cx=256,dcx=64): |
| 185 | # compress object in steps, decompress object in steps, loop sizes |
| 186 | source = source or hamlet_scene |
| 187 | for reps in sizes: |
| 188 | data = source * reps |
Neil Schemenauer | 94afd3e | 2004-06-05 19:02:52 +0000 | [diff] [blame^] | 189 | co = zlib.compressobj() |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 190 | bufs = [] |
| 191 | for i in range(0, len(data), cx): |
| 192 | bufs.append(co.compress(data[i:i+cx])) |
| 193 | bufs.append(co.flush()) |
| 194 | combuf = ''.join(bufs) |
| 195 | |
Neil Schemenauer | 94afd3e | 2004-06-05 19:02:52 +0000 | [diff] [blame^] | 196 | self.assertEqual(data, zlib.decompress(combuf)) |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 197 | |
Neil Schemenauer | 94afd3e | 2004-06-05 19:02:52 +0000 | [diff] [blame^] | 198 | dco = zlib.decompressobj() |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 199 | bufs = [] |
| 200 | for i in range(0, len(combuf), dcx): |
| 201 | bufs.append(dco.decompress(combuf[i:i+dcx])) |
| 202 | self.assertEqual('', dco.unconsumed_tail, ######## |
| 203 | "(A) uct should be '': not %d long" % |
| 204 | len(dco.unconsumed_tail)) |
| 205 | if flush: |
| 206 | bufs.append(dco.flush()) |
| 207 | else: |
| 208 | while True: |
| 209 | chunk = dco.decompress('') |
| 210 | if chunk: |
| 211 | bufs.append(chunk) |
| 212 | else: |
| 213 | break |
| 214 | self.assertEqual('', dco.unconsumed_tail, ######## |
| 215 | "(B) uct should be '': not %d long" % |
| 216 | len(dco.unconsumed_tail)) |
| 217 | self.assertEqual(data, ''.join(bufs)) |
| 218 | # Failure means: "decompressobj with init options failed" |
| 219 | |
| 220 | def test_decompimax(self,sizes=[128],flush=True,source=None,cx=256,dcx=64): |
| 221 | # compress in steps, decompress in length-restricted steps, loop sizes |
| 222 | source = source or hamlet_scene |
| 223 | for reps in sizes: |
| 224 | # Check a decompression object with max_length specified |
| 225 | data = source * reps |
Neil Schemenauer | 94afd3e | 2004-06-05 19:02:52 +0000 | [diff] [blame^] | 226 | co = zlib.compressobj() |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 227 | bufs = [] |
| 228 | for i in range(0, len(data), cx): |
| 229 | bufs.append(co.compress(data[i:i+cx])) |
| 230 | bufs.append(co.flush()) |
| 231 | combuf = ''.join(bufs) |
Neil Schemenauer | 94afd3e | 2004-06-05 19:02:52 +0000 | [diff] [blame^] | 232 | self.assertEqual(data, zlib.decompress(combuf), |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 233 | 'compressed data failure') |
| 234 | |
Neil Schemenauer | 94afd3e | 2004-06-05 19:02:52 +0000 | [diff] [blame^] | 235 | dco = zlib.decompressobj() |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 236 | bufs = [] |
| 237 | cb = combuf |
| 238 | while cb: |
Guido van Rossum | f359410 | 2003-02-27 18:39:18 +0000 | [diff] [blame] | 239 | #max_length = 1 + len(cb)//10 |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 240 | chunk = dco.decompress(cb, dcx) |
| 241 | self.failIf(len(chunk) > dcx, |
| 242 | 'chunk too big (%d>%d)' % (len(chunk), dcx)) |
| 243 | bufs.append(chunk) |
| 244 | cb = dco.unconsumed_tail |
| 245 | if flush: |
| 246 | bufs.append(dco.flush()) |
| 247 | else: |
| 248 | while True: |
| 249 | chunk = dco.decompress('', dcx) |
| 250 | self.failIf(len(chunk) > dcx, |
| 251 | 'chunk too big in tail (%d>%d)' % (len(chunk), dcx)) |
| 252 | if chunk: |
| 253 | bufs.append(chunk) |
| 254 | else: |
| 255 | break |
| 256 | self.assertEqual(len(data), len(''.join(bufs))) |
| 257 | self.assertEqual(data, ''.join(bufs), 'Wrong data retrieved') |
| 258 | |
| 259 | def test_decompressmaxlen(self): |
| 260 | # Check a decompression object with max_length specified |
| 261 | data = hamlet_scene * 8 * 16 |
Neil Schemenauer | 94afd3e | 2004-06-05 19:02:52 +0000 | [diff] [blame^] | 262 | co = zlib.compressobj() |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 263 | bufs = [] |
| 264 | for i in range(0, len(data), 256): |
| 265 | bufs.append(co.compress(data[i:i+256])) |
| 266 | bufs.append(co.flush()) |
| 267 | combuf = ''.join(bufs) |
Neil Schemenauer | 94afd3e | 2004-06-05 19:02:52 +0000 | [diff] [blame^] | 268 | self.assertEqual(data, zlib.decompress(combuf), |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 269 | 'compressed data failure') |
| 270 | |
Neil Schemenauer | 94afd3e | 2004-06-05 19:02:52 +0000 | [diff] [blame^] | 271 | dco = zlib.decompressobj() |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 272 | bufs = [] |
| 273 | cb = combuf |
| 274 | while cb: |
Guido van Rossum | f359410 | 2003-02-27 18:39:18 +0000 | [diff] [blame] | 275 | max_length = 1 + len(cb)//10 |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 276 | chunk = dco.decompress(cb, max_length) |
| 277 | self.failIf(len(chunk) > max_length, |
| 278 | 'chunk too big (%d>%d)' % (len(chunk),max_length)) |
| 279 | bufs.append(chunk) |
| 280 | cb = dco.unconsumed_tail |
| 281 | bufs.append(dco.flush()) |
| 282 | self.assertEqual(len(data), len(''.join(bufs))) |
| 283 | self.assertEqual(data, ''.join(bufs), 'Wrong data retrieved') |
| 284 | |
| 285 | def test_decompressmaxlenflushless(self): |
| 286 | # identical to test_decompressmaxlen except flush is replaced |
| 287 | # with an equivalent. This works and other fails on (eg) 2.2.2 |
| 288 | data = hamlet_scene * 8 * 16 |
Neil Schemenauer | 94afd3e | 2004-06-05 19:02:52 +0000 | [diff] [blame^] | 289 | co = zlib.compressobj() |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 290 | bufs = [] |
| 291 | for i in range(0, len(data), 256): |
| 292 | bufs.append(co.compress(data[i:i+256])) |
| 293 | bufs.append(co.flush()) |
| 294 | combuf = ''.join(bufs) |
Neil Schemenauer | 94afd3e | 2004-06-05 19:02:52 +0000 | [diff] [blame^] | 295 | self.assertEqual(data, zlib.decompress(combuf), |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 296 | 'compressed data mismatch') |
| 297 | |
Neil Schemenauer | 94afd3e | 2004-06-05 19:02:52 +0000 | [diff] [blame^] | 298 | dco = zlib.decompressobj() |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 299 | bufs = [] |
| 300 | cb = combuf |
| 301 | while cb: |
Guido van Rossum | f359410 | 2003-02-27 18:39:18 +0000 | [diff] [blame] | 302 | max_length = 1 + len(cb)//10 |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 303 | chunk = dco.decompress(cb, max_length) |
| 304 | self.failIf(len(chunk) > max_length, |
| 305 | 'chunk too big (%d>%d)' % (len(chunk),max_length)) |
| 306 | bufs.append(chunk) |
| 307 | cb = dco.unconsumed_tail |
| 308 | |
| 309 | #bufs.append(dco.flush()) |
| 310 | while len(chunk): |
| 311 | chunk = dco.decompress('', max_length) |
| 312 | self.failIf(len(chunk) > max_length, |
| 313 | 'chunk too big (%d>%d)' % (len(chunk),max_length)) |
| 314 | bufs.append(chunk) |
| 315 | |
| 316 | self.assertEqual(data, ''.join(bufs), 'Wrong data retrieved') |
| 317 | |
| 318 | def test_maxlenmisc(self): |
| 319 | # Misc tests of max_length |
Neil Schemenauer | 94afd3e | 2004-06-05 19:02:52 +0000 | [diff] [blame^] | 320 | dco = zlib.decompressobj() |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 321 | self.assertRaises(ValueError, dco.decompress, "", -1) |
| 322 | self.assertEqual('', dco.unconsumed_tail) |
| 323 | |
| 324 | def test_flushes(self): |
| 325 | # Test flush() with the various options, using all the |
| 326 | # different levels in order to provide more variations. |
| 327 | sync_opt = ['Z_NO_FLUSH', 'Z_SYNC_FLUSH', 'Z_FULL_FLUSH'] |
| 328 | sync_opt = [getattr(zlib, opt) for opt in sync_opt |
| 329 | if hasattr(zlib, opt)] |
| 330 | data = hamlet_scene * 8 |
| 331 | |
| 332 | for sync in sync_opt: |
| 333 | for level in range(10): |
| 334 | obj = zlib.compressobj( level ) |
| 335 | a = obj.compress( data[:3000] ) |
| 336 | b = obj.flush( sync ) |
| 337 | c = obj.compress( data[3000:] ) |
| 338 | d = obj.flush() |
| 339 | self.assertEqual(zlib.decompress(''.join([a,b,c,d])), |
| 340 | data, ("Decompress failed: flush " |
| 341 | "mode=%i, level=%i") % (sync, level)) |
| 342 | del obj |
| 343 | |
| 344 | def test_odd_flush(self): |
| 345 | # Test for odd flushing bugs noted in 2.0, and hopefully fixed in 2.1 |
| 346 | import random |
| 347 | |
| 348 | if hasattr(zlib, 'Z_SYNC_FLUSH'): |
| 349 | # Testing on 17K of "random" data |
| 350 | |
| 351 | # Create compressor and decompressor objects |
| 352 | co = zlib.compressobj(9) |
| 353 | dco = zlib.decompressobj() |
| 354 | |
| 355 | # Try 17K of data |
| 356 | # generate random data stream |
| 357 | try: |
| 358 | # In 2.3 and later, WichmannHill is the RNG of the bug report |
| 359 | gen = random.WichmannHill() |
| 360 | except AttributeError: |
| 361 | try: |
| 362 | # 2.2 called it Random |
| 363 | gen = random.Random() |
| 364 | except AttributeError: |
| 365 | # others might simply have a single RNG |
| 366 | gen = random |
| 367 | gen.seed(1) |
| 368 | data = genblock(1, 17 * 1024, generator=gen) |
| 369 | |
| 370 | # compress, sync-flush, and decompress |
| 371 | first = co.compress(data) |
| 372 | second = co.flush(zlib.Z_SYNC_FLUSH) |
| 373 | expanded = dco.decompress(first + second) |
| 374 | |
| 375 | # if decompressed data is different from the input data, choke. |
| 376 | self.assertEqual(expanded, data, "17K random source doesn't match") |
| 377 | |
| 378 | def test_manydecompinc(self): |
| 379 | # Run incremental decompress test for a large range of sizes |
| 380 | self.test_decompinc(sizes=[1<<n for n in range(8)], |
| 381 | flush=True, cx=32, dcx=4) |
| 382 | |
| 383 | def test_manydecompimax(self): |
| 384 | # Run incremental decompress maxlen test for a large range of sizes |
| 385 | # avoid the flush bug |
| 386 | self.test_decompimax(sizes=[1<<n for n in range(8)], |
| 387 | flush=False, cx=32, dcx=4) |
| 388 | |
| 389 | def test_manydecompimaxflush(self): |
| 390 | # Run incremental decompress maxlen test for a large range of sizes |
| 391 | # avoid the flush bug |
| 392 | self.test_decompimax(sizes=[1<<n for n in range(8)], |
| 393 | flush=True, cx=32, dcx=4) |
| 394 | |
| 395 | |
| 396 | def genblock(seed, length, step=1024, generator=random): |
| 397 | """length-byte stream of random data from a seed (in step-byte blocks).""" |
| 398 | if seed is not None: |
| 399 | generator.seed(seed) |
| 400 | randint = generator.randint |
| 401 | if length < step or step < 2: |
| 402 | step = length |
| 403 | blocks = [] |
| 404 | for i in range(0, length, step): |
| 405 | blocks.append(''.join([chr(randint(0,255)) |
| 406 | for x in range(step)])) |
| 407 | return ''.join(blocks)[:length] |
| 408 | |
| 409 | |
| 410 | |
| 411 | def choose_lines(source, number, seed=None, generator=random): |
| 412 | """Return a list of number lines randomly chosen from the source""" |
| 413 | if seed is not None: |
| 414 | generator.seed(seed) |
| 415 | sources = source.split('\n') |
| 416 | return [generator.choice(sources) for n in range(number)] |
| 417 | |
| 418 | |
| 419 | |
| 420 | hamlet_scene = """ |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 421 | LAERTES |
Jeremy Hylton | 6eb4b6a | 1997-08-15 15:59:43 +0000 | [diff] [blame] | 422 | |
| 423 | O, fear me not. |
| 424 | I stay too long: but here my father comes. |
| 425 | |
| 426 | Enter POLONIUS |
| 427 | |
| 428 | A double blessing is a double grace, |
| 429 | Occasion smiles upon a second leave. |
| 430 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 431 | LORD POLONIUS |
Jeremy Hylton | 6eb4b6a | 1997-08-15 15:59:43 +0000 | [diff] [blame] | 432 | |
| 433 | Yet here, Laertes! aboard, aboard, for shame! |
| 434 | The wind sits in the shoulder of your sail, |
| 435 | And you are stay'd for. There; my blessing with thee! |
| 436 | And these few precepts in thy memory |
| 437 | See thou character. Give thy thoughts no tongue, |
| 438 | Nor any unproportioned thought his act. |
| 439 | Be thou familiar, but by no means vulgar. |
| 440 | Those friends thou hast, and their adoption tried, |
| 441 | Grapple them to thy soul with hoops of steel; |
| 442 | But do not dull thy palm with entertainment |
| 443 | Of each new-hatch'd, unfledged comrade. Beware |
| 444 | Of entrance to a quarrel, but being in, |
| 445 | Bear't that the opposed may beware of thee. |
| 446 | Give every man thy ear, but few thy voice; |
| 447 | Take each man's censure, but reserve thy judgment. |
| 448 | Costly thy habit as thy purse can buy, |
| 449 | But not express'd in fancy; rich, not gaudy; |
| 450 | For the apparel oft proclaims the man, |
| 451 | And they in France of the best rank and station |
| 452 | Are of a most select and generous chief in that. |
| 453 | Neither a borrower nor a lender be; |
| 454 | For loan oft loses both itself and friend, |
| 455 | And borrowing dulls the edge of husbandry. |
| 456 | This above all: to thine ownself be true, |
| 457 | And it must follow, as the night the day, |
| 458 | Thou canst not then be false to any man. |
| 459 | Farewell: my blessing season this in thee! |
| 460 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 461 | LAERTES |
Jeremy Hylton | 6eb4b6a | 1997-08-15 15:59:43 +0000 | [diff] [blame] | 462 | |
| 463 | Most humbly do I take my leave, my lord. |
| 464 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 465 | LORD POLONIUS |
Jeremy Hylton | 6eb4b6a | 1997-08-15 15:59:43 +0000 | [diff] [blame] | 466 | |
| 467 | The time invites you; go; your servants tend. |
| 468 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 469 | LAERTES |
Jeremy Hylton | 6eb4b6a | 1997-08-15 15:59:43 +0000 | [diff] [blame] | 470 | |
| 471 | Farewell, Ophelia; and remember well |
| 472 | What I have said to you. |
| 473 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 474 | OPHELIA |
Jeremy Hylton | 6eb4b6a | 1997-08-15 15:59:43 +0000 | [diff] [blame] | 475 | |
| 476 | 'Tis in my memory lock'd, |
| 477 | And you yourself shall keep the key of it. |
| 478 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 479 | LAERTES |
Jeremy Hylton | 6eb4b6a | 1997-08-15 15:59:43 +0000 | [diff] [blame] | 480 | |
| 481 | Farewell. |
| 482 | """ |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 483 | |
| 484 | |
| 485 | def test_main(): |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 486 | test_support.run_unittest( |
| 487 | ChecksumTestCase, |
| 488 | ExceptionTestCase, |
| 489 | CompressTestCase, |
| 490 | CompressObjectTestCase |
| 491 | ) |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 492 | |
| 493 | if __name__ == "__main__": |
| 494 | test_main() |
| 495 | |
| 496 | def test(tests=''): |
| 497 | if not tests: tests = 'o' |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 498 | testcases = [] |
| 499 | if 'k' in tests: testcases.append(ChecksumTestCase) |
| 500 | if 'x' in tests: testcases.append(ExceptionTestCase) |
| 501 | if 'c' in tests: testcases.append(CompressTestCase) |
| 502 | if 'o' in tests: testcases.append(CompressObjectTestCase) |
| 503 | test_support.run_unittest(*testcases) |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 504 | |
| 505 | if False: |
| 506 | import sys |
| 507 | sys.path.insert(1, '/Py23Src/python/dist/src/Lib/test') |
| 508 | import test_zlib as tz |
| 509 | ts, ut = tz.test_support, tz.unittest |
| 510 | su = ut.TestSuite() |
| 511 | su.addTest(ut.makeSuite(tz.CompressTestCase)) |
| 512 | ts.run_suite(su) |