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