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