annotate Lib/test/test_zlib.py @ 54294:dba767bc3155 legacy-trunk

remove mention of old ctypes version
author Benjamin Peterson <benjamin@python.org>
date Sat, 23 May 2009 20:59:09 +0000
parents f4c5e3a64be2
children 6768b2350c57
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
27653
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
1 import unittest
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
2 from test import test_support
45886
2893c0129fe5 Add a test to make sure zlib.crc32 and binascii.crc32 return the same thing.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45798
diff changeset
3 import binascii
27653
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
4 import random
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
5
52491
f4c5e3a64be2 Change more tests to use import_module for the modules that
R. David Murray <rdmurray@bitdance.com>
parents: 46363
diff changeset
6 zlib = test_support.import_module('zlib')
f4c5e3a64be2 Change more tests to use import_module for the modules that
R. David Murray <rdmurray@bitdance.com>
parents: 46363
diff changeset
7
6069
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
8
27653
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
9 class ChecksumTestCase(unittest.TestCase):
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
10 # checksum test cases
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
11 def test_crc32start(self):
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
12 self.assertEqual(zlib.crc32(""), zlib.crc32("", 0))
35914
3fd97de0a790 [Patch #1350573] zlib.crc32 doesn't handle 0xffffffff seed. Add tests and bugfix. Bug reported by John Schmidt; bugfix by Danny Yoo.
Andrew M. Kuchling <amk@amk.ca>
parents: 35003
diff changeset
13 self.assert_(zlib.crc32("abc", 0xffffffff))
27653
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
14
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
15 def test_crc32empty(self):
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
16 self.assertEqual(zlib.crc32("", 0), 0)
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
17 self.assertEqual(zlib.crc32("", 1), 1)
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
18 self.assertEqual(zlib.crc32("", 432), 432)
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
19
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
20 def test_adler32start(self):
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
21 self.assertEqual(zlib.adler32(""), zlib.adler32("", 1))
35914
3fd97de0a790 [Patch #1350573] zlib.crc32 doesn't handle 0xffffffff seed. Add tests and bugfix. Bug reported by John Schmidt; bugfix by Danny Yoo.
Andrew M. Kuchling <amk@amk.ca>
parents: 35003
diff changeset
22 self.assert_(zlib.adler32("abc", 0xffffffff))
27653
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
23
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
24 def test_adler32empty(self):
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
25 self.assertEqual(zlib.adler32("", 0), 0)
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
26 self.assertEqual(zlib.adler32("", 1), 1)
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
27 self.assertEqual(zlib.adler32("", 432), 432)
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
28
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
29 def assertEqual32(self, seen, expected):
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
30 # 32-bit values masked -- checksums on 32- vs 64- bit machines
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
31 # This is important if bit 31 (0x08000000L) is set.
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
32 self.assertEqual(seen & 0x0FFFFFFFFL, expected & 0x0FFFFFFFFL)
6069
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
33
27653
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
34 def test_penguins(self):
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
35 self.assertEqual32(zlib.crc32("penguin", 0), 0x0e5c1a120L)
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
36 self.assertEqual32(zlib.crc32("penguin", 1), 0x43b6aa94)
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
37 self.assertEqual32(zlib.adler32("penguin", 0), 0x0bcf02f6)
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
38 self.assertEqual32(zlib.adler32("penguin", 1), 0x0bd602f7)
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
39
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
40 self.assertEqual(zlib.crc32("penguin"), zlib.crc32("penguin", 0))
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
41 self.assertEqual(zlib.adler32("penguin"),zlib.adler32("penguin",1))
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
42
45798
710478a1977a Force zlib.crc32 and zlib.adler32 to return a signed integer on all platforms
Gregory P. Smith <greg@mad-scientist.com>
parents: 43570
diff changeset
43 def test_abcdefghijklmnop(self):
710478a1977a Force zlib.crc32 and zlib.adler32 to return a signed integer on all platforms
Gregory P. Smith <greg@mad-scientist.com>
parents: 43570
diff changeset
44 """test issue1202 compliance: signed crc32, adler32 in 2.x"""
710478a1977a Force zlib.crc32 and zlib.adler32 to return a signed integer on all platforms
Gregory P. Smith <greg@mad-scientist.com>
parents: 43570
diff changeset
45 foo = 'abcdefghijklmnop'
710478a1977a Force zlib.crc32 and zlib.adler32 to return a signed integer on all platforms
Gregory P. Smith <greg@mad-scientist.com>
parents: 43570
diff changeset
46 # explicitly test signed behavior
710478a1977a Force zlib.crc32 and zlib.adler32 to return a signed integer on all platforms
Gregory P. Smith <greg@mad-scientist.com>
parents: 43570
diff changeset
47 self.assertEqual(zlib.crc32(foo), -1808088941)
710478a1977a Force zlib.crc32 and zlib.adler32 to return a signed integer on all platforms
Gregory P. Smith <greg@mad-scientist.com>
parents: 43570
diff changeset
48 self.assertEqual(zlib.crc32('spam'), 1138425661)
710478a1977a Force zlib.crc32 and zlib.adler32 to return a signed integer on all platforms
Gregory P. Smith <greg@mad-scientist.com>
parents: 43570
diff changeset
49 self.assertEqual(zlib.adler32(foo+foo), -721416943)
710478a1977a Force zlib.crc32 and zlib.adler32 to return a signed integer on all platforms
Gregory P. Smith <greg@mad-scientist.com>
parents: 43570
diff changeset
50 self.assertEqual(zlib.adler32('spam'), 72286642)
710478a1977a Force zlib.crc32 and zlib.adler32 to return a signed integer on all platforms
Gregory P. Smith <greg@mad-scientist.com>
parents: 43570
diff changeset
51
45886
2893c0129fe5 Add a test to make sure zlib.crc32 and binascii.crc32 return the same thing.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45798
diff changeset
52 def test_same_as_binascii_crc32(self):
2893c0129fe5 Add a test to make sure zlib.crc32 and binascii.crc32 return the same thing.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45798
diff changeset
53 foo = 'abcdefghijklmnop'
2893c0129fe5 Add a test to make sure zlib.crc32 and binascii.crc32 return the same thing.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45798
diff changeset
54 self.assertEqual(binascii.crc32(foo), zlib.crc32(foo))
2893c0129fe5 Add a test to make sure zlib.crc32 and binascii.crc32 return the same thing.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45798
diff changeset
55 self.assertEqual(binascii.crc32('spam'), zlib.crc32('spam'))
2893c0129fe5 Add a test to make sure zlib.crc32 and binascii.crc32 return the same thing.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45798
diff changeset
56
46063
ce45fd3a7a3f A stab in the dark attempt to fix the alpha/tru64 buildbot problem and add more
Gregory P. Smith <greg@mad-scientist.com>
parents: 45886
diff changeset
57 def test_negative_crc_iv_input(self):
ce45fd3a7a3f A stab in the dark attempt to fix the alpha/tru64 buildbot problem and add more
Gregory P. Smith <greg@mad-scientist.com>
parents: 45886
diff changeset
58 # The range of valid input values for the crc state should be
ce45fd3a7a3f A stab in the dark attempt to fix the alpha/tru64 buildbot problem and add more
Gregory P. Smith <greg@mad-scientist.com>
parents: 45886
diff changeset
59 # -2**31 through 2**32-1 to allow inputs artifically constrained
ce45fd3a7a3f A stab in the dark attempt to fix the alpha/tru64 buildbot problem and add more
Gregory P. Smith <greg@mad-scientist.com>
parents: 45886
diff changeset
60 # to a signed 32-bit integer.
ce45fd3a7a3f A stab in the dark attempt to fix the alpha/tru64 buildbot problem and add more
Gregory P. Smith <greg@mad-scientist.com>
parents: 45886
diff changeset
61 self.assertEqual(zlib.crc32('ham', -1), zlib.crc32('ham', 0xffffffffL))
ce45fd3a7a3f A stab in the dark attempt to fix the alpha/tru64 buildbot problem and add more
Gregory P. Smith <greg@mad-scientist.com>
parents: 45886
diff changeset
62 self.assertEqual(zlib.crc32('spam', -3141593),
ce45fd3a7a3f A stab in the dark attempt to fix the alpha/tru64 buildbot problem and add more
Gregory P. Smith <greg@mad-scientist.com>
parents: 45886
diff changeset
63 zlib.crc32('spam', 0xffd01027L))
ce45fd3a7a3f A stab in the dark attempt to fix the alpha/tru64 buildbot problem and add more
Gregory P. Smith <greg@mad-scientist.com>
parents: 45886
diff changeset
64 self.assertEqual(zlib.crc32('spam', -(2**31)),
ce45fd3a7a3f A stab in the dark attempt to fix the alpha/tru64 buildbot problem and add more
Gregory P. Smith <greg@mad-scientist.com>
parents: 45886
diff changeset
65 zlib.crc32('spam', (2**31)))
27653
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
66
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
67
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
68 class ExceptionTestCase(unittest.TestCase):
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
69 # make sure we generate some expected errors
43570
a5a4b786bb21 test_bigbits was not testing what it seemed to.
Armin Rigo <arigo@tunes.org>
parents: 41513
diff changeset
70 def test_badlevel(self):
a5a4b786bb21 test_bigbits was not testing what it seemed to.
Armin Rigo <arigo@tunes.org>
parents: 41513
diff changeset
71 # specifying compression level out of range causes an error
a5a4b786bb21 test_bigbits was not testing what it seemed to.
Armin Rigo <arigo@tunes.org>
parents: 41513
diff changeset
72 # (but -1 is Z_DEFAULT_COMPRESSION and apparently the zlib
a5a4b786bb21 test_bigbits was not testing what it seemed to.
Armin Rigo <arigo@tunes.org>
parents: 41513
diff changeset
73 # accepts 0 too)
a5a4b786bb21 test_bigbits was not testing what it seemed to.
Armin Rigo <arigo@tunes.org>
parents: 41513
diff changeset
74 self.assertRaises(zlib.error, zlib.compress, 'ERROR', 10)
27653
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
75
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
76 def test_badcompressobj(self):
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
77 # verify failure on building compress object with bad params
32262
36798f1c8e9a Remove lots of magic constants.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 30256
diff changeset
78 self.assertRaises(ValueError, zlib.compressobj, 1, zlib.DEFLATED, 0)
43570
a5a4b786bb21 test_bigbits was not testing what it seemed to.
Armin Rigo <arigo@tunes.org>
parents: 41513
diff changeset
79 # specifying total bits too large causes an error
a5a4b786bb21 test_bigbits was not testing what it seemed to.
Armin Rigo <arigo@tunes.org>
parents: 41513
diff changeset
80 self.assertRaises(ValueError,
a5a4b786bb21 test_bigbits was not testing what it seemed to.
Armin Rigo <arigo@tunes.org>
parents: 41513
diff changeset
81 zlib.compressobj, 1, zlib.DEFLATED, zlib.MAX_WBITS + 1)
6237
03b8793f76c2 Many more tests, including tests of many optional arguments.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6071
diff changeset
82
27653
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
83 def test_baddecompressobj(self):
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
84 # verify failure on building decompress object with bad params
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
85 self.assertRaises(ValueError, zlib.decompressobj, 0)
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
86
46363
27c7139a98ef Fix zlib crash from zlib.decompressobj().flush(val) when val was not positive.
Gregory P. Smith <greg@mad-scientist.com>
parents: 46063
diff changeset
87 def test_decompressobj_badflush(self):
27c7139a98ef Fix zlib crash from zlib.decompressobj().flush(val) when val was not positive.
Gregory P. Smith <greg@mad-scientist.com>
parents: 46063
diff changeset
88 # verify failure on calling decompressobj.flush with bad params
27c7139a98ef Fix zlib crash from zlib.decompressobj().flush(val) when val was not positive.
Gregory P. Smith <greg@mad-scientist.com>
parents: 46063
diff changeset
89 self.assertRaises(ValueError, zlib.decompressobj().flush, 0)
27c7139a98ef Fix zlib crash from zlib.decompressobj().flush(val) when val was not positive.
Gregory P. Smith <greg@mad-scientist.com>
parents: 46063
diff changeset
90 self.assertRaises(ValueError, zlib.decompressobj().flush, -1)
27c7139a98ef Fix zlib crash from zlib.decompressobj().flush(val) when val was not positive.
Gregory P. Smith <greg@mad-scientist.com>
parents: 46063
diff changeset
91
27653
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
92
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
93
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
94 class CompressTestCase(unittest.TestCase):
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
95 # Test compression in one go (whole message compression)
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
96 def test_speech(self):
32268
8e5958c93c88 Remove a number of tests that differ only in input data size. It seems
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 32262
diff changeset
97 x = zlib.compress(HAMLET_SCENE)
8e5958c93c88 Remove a number of tests that differ only in input data size. It seems
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 32262
diff changeset
98 self.assertEqual(zlib.decompress(x), HAMLET_SCENE)
6237
03b8793f76c2 Many more tests, including tests of many optional arguments.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6071
diff changeset
99
27653
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
100 def test_speech128(self):
32268
8e5958c93c88 Remove a number of tests that differ only in input data size. It seems
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 32262
diff changeset
101 # compress more data
8e5958c93c88 Remove a number of tests that differ only in input data size. It seems
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 32262
diff changeset
102 data = HAMLET_SCENE * 128
27653
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
103 x = zlib.compress(data)
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
104 self.assertEqual(zlib.decompress(x), data)
6069
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
105
27653
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
106
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
107
6069
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
108
27653
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
109 class CompressObjectTestCase(unittest.TestCase):
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
110 # Test compression object
32268
8e5958c93c88 Remove a number of tests that differ only in input data size. It seems
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 32262
diff changeset
111 def test_pair(self):
8e5958c93c88 Remove a number of tests that differ only in input data size. It seems
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 32262
diff changeset
112 # straightforward compress/decompress objects
8e5958c93c88 Remove a number of tests that differ only in input data size. It seems
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 32262
diff changeset
113 data = HAMLET_SCENE * 128
32262
36798f1c8e9a Remove lots of magic constants.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 30256
diff changeset
114 co = zlib.compressobj()
27653
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
115 x1 = co.compress(data)
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
116 x2 = co.flush()
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
117 self.assertRaises(zlib.error, co.flush) # second flush should not work
32262
36798f1c8e9a Remove lots of magic constants.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 30256
diff changeset
118 dco = zlib.decompressobj()
27653
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
119 y1 = dco.decompress(x1 + x2)
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
120 y2 = dco.flush()
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
121 self.assertEqual(data, y1 + y2)
23169
713ff889d094 Fix SF #544995 (zlib crash on second flush call)
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20547
diff changeset
122
32262
36798f1c8e9a Remove lots of magic constants.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 30256
diff changeset
123 def test_compressoptions(self):
36798f1c8e9a Remove lots of magic constants.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 30256
diff changeset
124 # specify lots of options to compressobj()
36798f1c8e9a Remove lots of magic constants.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 30256
diff changeset
125 level = 2
36798f1c8e9a Remove lots of magic constants.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 30256
diff changeset
126 method = zlib.DEFLATED
36798f1c8e9a Remove lots of magic constants.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 30256
diff changeset
127 wbits = -12
36798f1c8e9a Remove lots of magic constants.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 30256
diff changeset
128 memlevel = 9
36798f1c8e9a Remove lots of magic constants.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 30256
diff changeset
129 strategy = zlib.Z_FILTERED
36798f1c8e9a Remove lots of magic constants.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 30256
diff changeset
130 co = zlib.compressobj(level, method, wbits, memlevel, strategy)
32268
8e5958c93c88 Remove a number of tests that differ only in input data size. It seems
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 32262
diff changeset
131 x1 = co.compress(HAMLET_SCENE)
32262
36798f1c8e9a Remove lots of magic constants.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 30256
diff changeset
132 x2 = co.flush()
36798f1c8e9a Remove lots of magic constants.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 30256
diff changeset
133 dco = zlib.decompressobj(wbits)
36798f1c8e9a Remove lots of magic constants.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 30256
diff changeset
134 y1 = dco.decompress(x1 + x2)
36798f1c8e9a Remove lots of magic constants.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 30256
diff changeset
135 y2 = dco.flush()
32268
8e5958c93c88 Remove a number of tests that differ only in input data size. It seems
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 32262
diff changeset
136 self.assertEqual(HAMLET_SCENE, y1 + y2)
32262
36798f1c8e9a Remove lots of magic constants.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 30256
diff changeset
137
27653
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
138 def test_compressincremental(self):
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
139 # compress object in steps, decompress object as one-shot
32268
8e5958c93c88 Remove a number of tests that differ only in input data size. It seems
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 32262
diff changeset
140 data = HAMLET_SCENE * 128
32262
36798f1c8e9a Remove lots of magic constants.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 30256
diff changeset
141 co = zlib.compressobj()
27653
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
142 bufs = []
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
143 for i in range(0, len(data), 256):
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
144 bufs.append(co.compress(data[i:i+256]))
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
145 bufs.append(co.flush())
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
146 combuf = ''.join(bufs)
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
147
32262
36798f1c8e9a Remove lots of magic constants.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 30256
diff changeset
148 dco = zlib.decompressobj()
27653
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
149 y1 = dco.decompress(''.join(bufs))
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
150 y2 = dco.flush()
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
151 self.assertEqual(data, y1 + y2)
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
152
32268
8e5958c93c88 Remove a number of tests that differ only in input data size. It seems
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 32262
diff changeset
153 def test_decompinc(self, flush=False, source=None, cx=256, dcx=64):
27653
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
154 # compress object in steps, decompress object in steps
32268
8e5958c93c88 Remove a number of tests that differ only in input data size. It seems
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 32262
diff changeset
155 source = source or HAMLET_SCENE
8e5958c93c88 Remove a number of tests that differ only in input data size. It seems
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 32262
diff changeset
156 data = source * 128
32262
36798f1c8e9a Remove lots of magic constants.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 30256
diff changeset
157 co = zlib.compressobj()
27653
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
158 bufs = []
32268
8e5958c93c88 Remove a number of tests that differ only in input data size. It seems
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 32262
diff changeset
159 for i in range(0, len(data), cx):
8e5958c93c88 Remove a number of tests that differ only in input data size. It seems
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 32262
diff changeset
160 bufs.append(co.compress(data[i:i+cx]))
27653
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
161 bufs.append(co.flush())
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
162 combuf = ''.join(bufs)
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
163
32262
36798f1c8e9a Remove lots of magic constants.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 30256
diff changeset
164 self.assertEqual(data, zlib.decompress(combuf))
6237
03b8793f76c2 Many more tests, including tests of many optional arguments.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6071
diff changeset
165
32262
36798f1c8e9a Remove lots of magic constants.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 30256
diff changeset
166 dco = zlib.decompressobj()
27653
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
167 bufs = []
32268
8e5958c93c88 Remove a number of tests that differ only in input data size. It seems
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 32262
diff changeset
168 for i in range(0, len(combuf), dcx):
8e5958c93c88 Remove a number of tests that differ only in input data size. It seems
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 32262
diff changeset
169 bufs.append(dco.decompress(combuf[i:i+dcx]))
27653
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
170 self.assertEqual('', dco.unconsumed_tail, ########
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
171 "(A) uct should be '': not %d long" %
32268
8e5958c93c88 Remove a number of tests that differ only in input data size. It seems
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 32262
diff changeset
172 len(dco.unconsumed_tail))
8e5958c93c88 Remove a number of tests that differ only in input data size. It seems
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 32262
diff changeset
173 if flush:
8e5958c93c88 Remove a number of tests that differ only in input data size. It seems
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 32262
diff changeset
174 bufs.append(dco.flush())
8e5958c93c88 Remove a number of tests that differ only in input data size. It seems
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 32262
diff changeset
175 else:
8e5958c93c88 Remove a number of tests that differ only in input data size. It seems
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 32262
diff changeset
176 while True:
8e5958c93c88 Remove a number of tests that differ only in input data size. It seems
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 32262
diff changeset
177 chunk = dco.decompress('')
8e5958c93c88 Remove a number of tests that differ only in input data size. It seems
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 32262
diff changeset
178 if chunk:
8e5958c93c88 Remove a number of tests that differ only in input data size. It seems
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 32262
diff changeset
179 bufs.append(chunk)
8e5958c93c88 Remove a number of tests that differ only in input data size. It seems
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 32262
diff changeset
180 else:
8e5958c93c88 Remove a number of tests that differ only in input data size. It seems
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 32262
diff changeset
181 break
27653
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
182 self.assertEqual('', dco.unconsumed_tail, ########
32268
8e5958c93c88 Remove a number of tests that differ only in input data size. It seems
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 32262
diff changeset
183 "(B) uct should be '': not %d long" %
8e5958c93c88 Remove a number of tests that differ only in input data size. It seems
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 32262
diff changeset
184 len(dco.unconsumed_tail))
27653
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
185 self.assertEqual(data, ''.join(bufs))
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
186 # Failure means: "decompressobj with init options failed"
6237
03b8793f76c2 Many more tests, including tests of many optional arguments.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6071
diff changeset
187
32268
8e5958c93c88 Remove a number of tests that differ only in input data size. It seems
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 32262
diff changeset
188 def test_decompincflush(self):
8e5958c93c88 Remove a number of tests that differ only in input data size. It seems
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 32262
diff changeset
189 self.test_decompinc(flush=True)
6237
03b8793f76c2 Many more tests, including tests of many optional arguments.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 6071
diff changeset
190
32268
8e5958c93c88 Remove a number of tests that differ only in input data size. It seems
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 32262
diff changeset
191 def test_decompimax(self, source=None, cx=256, dcx=64):
8e5958c93c88 Remove a number of tests that differ only in input data size. It seems
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 32262
diff changeset
192 # compress in steps, decompress in length-restricted steps
8e5958c93c88 Remove a number of tests that differ only in input data size. It seems
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 32262
diff changeset
193 source = source or HAMLET_SCENE
8e5958c93c88 Remove a number of tests that differ only in input data size. It seems
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 32262
diff changeset
194 # Check a decompression object with max_length specified
8e5958c93c88 Remove a number of tests that differ only in input data size. It seems
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 32262
diff changeset
195 data = source * 128
8e5958c93c88 Remove a number of tests that differ only in input data size. It seems
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 32262
diff changeset
196 co = zlib.compressobj()
8e5958c93c88 Remove a number of tests that differ only in input data size. It seems
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 32262
diff changeset
197 bufs = []
8e5958c93c88 Remove a number of tests that differ only in input data size. It seems
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 32262
diff changeset
198 for i in range(0, len(data), cx):
8e5958c93c88 Remove a number of tests that differ only in input data size. It seems
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 32262
diff changeset
199 bufs.append(co.compress(data[i:i+cx]))
8e5958c93c88 Remove a number of tests that differ only in input data size. It seems
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 32262
diff changeset
200 bufs.append(co.flush())
8e5958c93c88 Remove a number of tests that differ only in input data size. It seems
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 32262
diff changeset
201 combuf = ''.join(bufs)
8e5958c93c88 Remove a number of tests that differ only in input data size. It seems
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 32262
diff changeset
202 self.assertEqual(data, zlib.decompress(combuf),
8e5958c93c88 Remove a number of tests that differ only in input data size. It seems
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 32262
diff changeset
203 'compressed data failure')
6069
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
204
32268
8e5958c93c88 Remove a number of tests that differ only in input data size. It seems
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 32262
diff changeset
205 dco = zlib.decompressobj()
8e5958c93c88 Remove a number of tests that differ only in input data size. It seems
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 32262
diff changeset
206 bufs = []
8e5958c93c88 Remove a number of tests that differ only in input data size. It seems
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 32262
diff changeset
207 cb = combuf
8e5958c93c88 Remove a number of tests that differ only in input data size. It seems
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 32262
diff changeset
208 while cb:
8e5958c93c88 Remove a number of tests that differ only in input data size. It seems
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 32262
diff changeset
209 #max_length = 1 + len(cb)//10
8e5958c93c88 Remove a number of tests that differ only in input data size. It seems
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 32262
diff changeset
210 chunk = dco.decompress(cb, dcx)
8e5958c93c88 Remove a number of tests that differ only in input data size. It seems
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 32262
diff changeset
211 self.failIf(len(chunk) > dcx,
8e5958c93c88 Remove a number of tests that differ only in input data size. It seems
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 32262
diff changeset
212 'chunk too big (%d>%d)' % (len(chunk), dcx))
8e5958c93c88 Remove a number of tests that differ only in input data size. It seems
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 32262
diff changeset
213 bufs.append(chunk)
8e5958c93c88 Remove a number of tests that differ only in input data size. It seems
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 32262
diff changeset
214 cb = dco.unconsumed_tail
8e5958c93c88 Remove a number of tests that differ only in input data size. It seems
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 32262
diff changeset
215 bufs.append(dco.flush())
8e5958c93c88 Remove a number of tests that differ only in input data size. It seems
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 32262
diff changeset
216 self.assertEqual(data, ''.join(bufs), 'Wrong data retrieved')
20466
60e12f83d2d2 [ #403753 ] zlib decompress; uncontrollable memory usage
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19131
diff changeset
217
32268
8e5958c93c88 Remove a number of tests that differ only in input data size. It seems
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 32262
diff changeset
218 def test_decompressmaxlen(self, flush=False):
27653
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
219 # Check a decompression object with max_length specified
32268
8e5958c93c88 Remove a number of tests that differ only in input data size. It seems
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 32262
diff changeset
220 data = HAMLET_SCENE * 128
32262
36798f1c8e9a Remove lots of magic constants.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 30256
diff changeset
221 co = zlib.compressobj()
27653
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
222 bufs = []
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
223 for i in range(0, len(data), 256):
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
224 bufs.append(co.compress(data[i:i+256]))
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
225 bufs.append(co.flush())
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
226 combuf = ''.join(bufs)
32262
36798f1c8e9a Remove lots of magic constants.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 30256
diff changeset
227 self.assertEqual(data, zlib.decompress(combuf),
27653
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
228 'compressed data failure')
20547
a12ccdbce8da Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 20466
diff changeset
229
32262
36798f1c8e9a Remove lots of magic constants.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 30256
diff changeset
230 dco = zlib.decompressobj()
27653
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
231 bufs = []
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
232 cb = combuf
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
233 while cb:
28087
465405a159c1 Use floor division (//).
Guido van Rossum <guido@python.org>
parents: 27963
diff changeset
234 max_length = 1 + len(cb)//10
27653
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
235 chunk = dco.decompress(cb, max_length)
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
236 self.failIf(len(chunk) > max_length,
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
237 'chunk too big (%d>%d)' % (len(chunk),max_length))
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
238 bufs.append(chunk)
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
239 cb = dco.unconsumed_tail
32268
8e5958c93c88 Remove a number of tests that differ only in input data size. It seems
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 32262
diff changeset
240 if flush:
8e5958c93c88 Remove a number of tests that differ only in input data size. It seems
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 32262
diff changeset
241 bufs.append(dco.flush())
8e5958c93c88 Remove a number of tests that differ only in input data size. It seems
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 32262
diff changeset
242 else:
8e5958c93c88 Remove a number of tests that differ only in input data size. It seems
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 32262
diff changeset
243 while chunk:
8e5958c93c88 Remove a number of tests that differ only in input data size. It seems
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 32262
diff changeset
244 chunk = dco.decompress('', max_length)
8e5958c93c88 Remove a number of tests that differ only in input data size. It seems
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 32262
diff changeset
245 self.failIf(len(chunk) > max_length,
8e5958c93c88 Remove a number of tests that differ only in input data size. It seems
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 32262
diff changeset
246 'chunk too big (%d>%d)' % (len(chunk),max_length))
8e5958c93c88 Remove a number of tests that differ only in input data size. It seems
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 32262
diff changeset
247 bufs.append(chunk)
27653
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
248 self.assertEqual(data, ''.join(bufs), 'Wrong data retrieved')
20466
60e12f83d2d2 [ #403753 ] zlib decompress; uncontrollable memory usage
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19131
diff changeset
249
32268
8e5958c93c88 Remove a number of tests that differ only in input data size. It seems
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 32262
diff changeset
250 def test_decompressmaxlenflush(self):
8e5958c93c88 Remove a number of tests that differ only in input data size. It seems
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 32262
diff changeset
251 self.test_decompressmaxlen(flush=True)
27653
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
252
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
253 def test_maxlenmisc(self):
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
254 # Misc tests of max_length
32262
36798f1c8e9a Remove lots of magic constants.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 30256
diff changeset
255 dco = zlib.decompressobj()
27653
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
256 self.assertRaises(ValueError, dco.decompress, "", -1)
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
257 self.assertEqual('', dco.unconsumed_tail)
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
258
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
259 def test_flushes(self):
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
260 # Test flush() with the various options, using all the
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
261 # different levels in order to provide more variations.
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
262 sync_opt = ['Z_NO_FLUSH', 'Z_SYNC_FLUSH', 'Z_FULL_FLUSH']
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
263 sync_opt = [getattr(zlib, opt) for opt in sync_opt
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
264 if hasattr(zlib, opt)]
32268
8e5958c93c88 Remove a number of tests that differ only in input data size. It seems
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 32262
diff changeset
265 data = HAMLET_SCENE * 8
10439
9ce1474abddd Added simple test for the flush() method of compression objects, trying the
Andrew M. Kuchling <amk@amk.ca>
parents: 8139
diff changeset
266
27653
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
267 for sync in sync_opt:
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
268 for level in range(10):
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
269 obj = zlib.compressobj( level )
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
270 a = obj.compress( data[:3000] )
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
271 b = obj.flush( sync )
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
272 c = obj.compress( data[3000:] )
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
273 d = obj.flush()
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
274 self.assertEqual(zlib.decompress(''.join([a,b,c,d])),
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
275 data, ("Decompress failed: flush "
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
276 "mode=%i, level=%i") % (sync, level))
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
277 del obj
16719
89c126887df0 Add test case from bug #124981: zlib decompress of sync-flushed data
Andrew M. Kuchling <amk@amk.ca>
parents: 16600
diff changeset
278
27653
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
279 def test_odd_flush(self):
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
280 # Test for odd flushing bugs noted in 2.0, and hopefully fixed in 2.1
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
281 import random
16719
89c126887df0 Add test case from bug #124981: zlib decompress of sync-flushed data
Andrew M. Kuchling <amk@amk.ca>
parents: 16600
diff changeset
282
27653
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
283 if hasattr(zlib, 'Z_SYNC_FLUSH'):
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
284 # Testing on 17K of "random" data
16719
89c126887df0 Add test case from bug #124981: zlib decompress of sync-flushed data
Andrew M. Kuchling <amk@amk.ca>
parents: 16600
diff changeset
285
27653
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
286 # Create compressor and decompressor objects
32268
8e5958c93c88 Remove a number of tests that differ only in input data size. It seems
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 32262
diff changeset
287 co = zlib.compressobj(zlib.Z_BEST_COMPRESSION)
27653
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
288 dco = zlib.decompressobj()
19131
67698d689203 Patch #441091 from Finn Bock: the more advanced flush options are not
Andrew M. Kuchling <amk@amk.ca>
parents: 16723
diff changeset
289
27653
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
290 # Try 17K of data
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
291 # generate random data stream
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
292 try:
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
293 # In 2.3 and later, WichmannHill is the RNG of the bug report
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
294 gen = random.WichmannHill()
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
295 except AttributeError:
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
296 try:
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
297 # 2.2 called it Random
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
298 gen = random.Random()
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
299 except AttributeError:
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
300 # others might simply have a single RNG
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
301 gen = random
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
302 gen.seed(1)
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
303 data = genblock(1, 17 * 1024, generator=gen)
16719
89c126887df0 Add test case from bug #124981: zlib decompress of sync-flushed data
Andrew M. Kuchling <amk@amk.ca>
parents: 16600
diff changeset
304
27653
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
305 # compress, sync-flush, and decompress
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
306 first = co.compress(data)
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
307 second = co.flush(zlib.Z_SYNC_FLUSH)
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
308 expanded = dco.decompress(first + second)
16723
59f1b57bc909 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 16719
diff changeset
309
27653
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
310 # if decompressed data is different from the input data, choke.
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
311 self.assertEqual(expanded, data, "17K random source doesn't match")
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
312
34264
b2ecda4dd70f [Bug #1083110] calling .flush() on decompress objects causes a segfault due to an uninitialized pointer: fixes the problem and adds a test case
Andrew M. Kuchling <amk@amk.ca>
parents: 32268
diff changeset
313 def test_empty_flush(self):
b2ecda4dd70f [Bug #1083110] calling .flush() on decompress objects causes a segfault due to an uninitialized pointer: fixes the problem and adds a test case
Andrew M. Kuchling <amk@amk.ca>
parents: 32268
diff changeset
314 # Test that calling .flush() on unused objects works.
b2ecda4dd70f [Bug #1083110] calling .flush() on decompress objects causes a segfault due to an uninitialized pointer: fixes the problem and adds a test case
Andrew M. Kuchling <amk@amk.ca>
parents: 32268
diff changeset
315 # (Bug #1083110 -- calling .flush() on decompress objects
b2ecda4dd70f [Bug #1083110] calling .flush() on decompress objects causes a segfault due to an uninitialized pointer: fixes the problem and adds a test case
Andrew M. Kuchling <amk@amk.ca>
parents: 32268
diff changeset
316 # caused a core dump.)
b2ecda4dd70f [Bug #1083110] calling .flush() on decompress objects causes a segfault due to an uninitialized pointer: fixes the problem and adds a test case
Andrew M. Kuchling <amk@amk.ca>
parents: 32268
diff changeset
317
b2ecda4dd70f [Bug #1083110] calling .flush() on decompress objects causes a segfault due to an uninitialized pointer: fixes the problem and adds a test case
Andrew M. Kuchling <amk@amk.ca>
parents: 32268
diff changeset
318 co = zlib.compressobj(zlib.Z_BEST_COMPRESSION)
b2ecda4dd70f [Bug #1083110] calling .flush() on decompress objects causes a segfault due to an uninitialized pointer: fixes the problem and adds a test case
Andrew M. Kuchling <amk@amk.ca>
parents: 32268
diff changeset
319 self.failUnless(co.flush()) # Returns a zlib header
b2ecda4dd70f [Bug #1083110] calling .flush() on decompress objects causes a segfault due to an uninitialized pointer: fixes the problem and adds a test case
Andrew M. Kuchling <amk@amk.ca>
parents: 32268
diff changeset
320 dco = zlib.decompressobj()
b2ecda4dd70f [Bug #1083110] calling .flush() on decompress objects causes a segfault due to an uninitialized pointer: fixes the problem and adds a test case
Andrew M. Kuchling <amk@amk.ca>
parents: 32268
diff changeset
321 self.assertEqual(dco.flush(), "") # Returns nothing
35003
52fed2d0641a whitespace normalisation
Anthony Baxter <anthonybaxter@gmail.com>
parents: 34264
diff changeset
322
38771
618d088d9668 Patch #1503046, Conditional compilation of zlib.(de)compressobj.copy
Neal Norwitz <nnorwitz@gmail.com>
parents: 38190
diff changeset
323 if hasattr(zlib.compressobj(), "copy"):
618d088d9668 Patch #1503046, Conditional compilation of zlib.(de)compressobj.copy
Neal Norwitz <nnorwitz@gmail.com>
parents: 38190
diff changeset
324 def test_compresscopy(self):
618d088d9668 Patch #1503046, Conditional compilation of zlib.(de)compressobj.copy
Neal Norwitz <nnorwitz@gmail.com>
parents: 38190
diff changeset
325 # Test copying a compression object
618d088d9668 Patch #1503046, Conditional compilation of zlib.(de)compressobj.copy
Neal Norwitz <nnorwitz@gmail.com>
parents: 38190
diff changeset
326 data0 = HAMLET_SCENE
618d088d9668 Patch #1503046, Conditional compilation of zlib.(de)compressobj.copy
Neal Norwitz <nnorwitz@gmail.com>
parents: 38190
diff changeset
327 data1 = HAMLET_SCENE.swapcase()
618d088d9668 Patch #1503046, Conditional compilation of zlib.(de)compressobj.copy
Neal Norwitz <nnorwitz@gmail.com>
parents: 38190
diff changeset
328 c0 = zlib.compressobj(zlib.Z_BEST_COMPRESSION)
618d088d9668 Patch #1503046, Conditional compilation of zlib.(de)compressobj.copy
Neal Norwitz <nnorwitz@gmail.com>
parents: 38190
diff changeset
329 bufs0 = []
618d088d9668 Patch #1503046, Conditional compilation of zlib.(de)compressobj.copy
Neal Norwitz <nnorwitz@gmail.com>
parents: 38190
diff changeset
330 bufs0.append(c0.compress(data0))
38190
d6d879af36d3 Patch #1435422: zlib's compress and decompress objects now have a
Georg Brandl <georg@python.org>
parents: 35914
diff changeset
331
38771
618d088d9668 Patch #1503046, Conditional compilation of zlib.(de)compressobj.copy
Neal Norwitz <nnorwitz@gmail.com>
parents: 38190
diff changeset
332 c1 = c0.copy()
618d088d9668 Patch #1503046, Conditional compilation of zlib.(de)compressobj.copy
Neal Norwitz <nnorwitz@gmail.com>
parents: 38190
diff changeset
333 bufs1 = bufs0[:]
38190
d6d879af36d3 Patch #1435422: zlib's compress and decompress objects now have a
Georg Brandl <georg@python.org>
parents: 35914
diff changeset
334
38771
618d088d9668 Patch #1503046, Conditional compilation of zlib.(de)compressobj.copy
Neal Norwitz <nnorwitz@gmail.com>
parents: 38190
diff changeset
335 bufs0.append(c0.compress(data0))
618d088d9668 Patch #1503046, Conditional compilation of zlib.(de)compressobj.copy
Neal Norwitz <nnorwitz@gmail.com>
parents: 38190
diff changeset
336 bufs0.append(c0.flush())
618d088d9668 Patch #1503046, Conditional compilation of zlib.(de)compressobj.copy
Neal Norwitz <nnorwitz@gmail.com>
parents: 38190
diff changeset
337 s0 = ''.join(bufs0)
38190
d6d879af36d3 Patch #1435422: zlib's compress and decompress objects now have a
Georg Brandl <georg@python.org>
parents: 35914
diff changeset
338
38771
618d088d9668 Patch #1503046, Conditional compilation of zlib.(de)compressobj.copy
Neal Norwitz <nnorwitz@gmail.com>
parents: 38190
diff changeset
339 bufs1.append(c1.compress(data1))
618d088d9668 Patch #1503046, Conditional compilation of zlib.(de)compressobj.copy
Neal Norwitz <nnorwitz@gmail.com>
parents: 38190
diff changeset
340 bufs1.append(c1.flush())
618d088d9668 Patch #1503046, Conditional compilation of zlib.(de)compressobj.copy
Neal Norwitz <nnorwitz@gmail.com>
parents: 38190
diff changeset
341 s1 = ''.join(bufs1)
38190
d6d879af36d3 Patch #1435422: zlib's compress and decompress objects now have a
Georg Brandl <georg@python.org>
parents: 35914
diff changeset
342
38771
618d088d9668 Patch #1503046, Conditional compilation of zlib.(de)compressobj.copy
Neal Norwitz <nnorwitz@gmail.com>
parents: 38190
diff changeset
343 self.assertEqual(zlib.decompress(s0),data0+data0)
618d088d9668 Patch #1503046, Conditional compilation of zlib.(de)compressobj.copy
Neal Norwitz <nnorwitz@gmail.com>
parents: 38190
diff changeset
344 self.assertEqual(zlib.decompress(s1),data0+data1)
38190
d6d879af36d3 Patch #1435422: zlib's compress and decompress objects now have a
Georg Brandl <georg@python.org>
parents: 35914
diff changeset
345
38771
618d088d9668 Patch #1503046, Conditional compilation of zlib.(de)compressobj.copy
Neal Norwitz <nnorwitz@gmail.com>
parents: 38190
diff changeset
346 def test_badcompresscopy(self):
618d088d9668 Patch #1503046, Conditional compilation of zlib.(de)compressobj.copy
Neal Norwitz <nnorwitz@gmail.com>
parents: 38190
diff changeset
347 # Test copying a compression object in an inconsistent state
618d088d9668 Patch #1503046, Conditional compilation of zlib.(de)compressobj.copy
Neal Norwitz <nnorwitz@gmail.com>
parents: 38190
diff changeset
348 c = zlib.compressobj()
618d088d9668 Patch #1503046, Conditional compilation of zlib.(de)compressobj.copy
Neal Norwitz <nnorwitz@gmail.com>
parents: 38190
diff changeset
349 c.compress(HAMLET_SCENE)
618d088d9668 Patch #1503046, Conditional compilation of zlib.(de)compressobj.copy
Neal Norwitz <nnorwitz@gmail.com>
parents: 38190
diff changeset
350 c.flush()
618d088d9668 Patch #1503046, Conditional compilation of zlib.(de)compressobj.copy
Neal Norwitz <nnorwitz@gmail.com>
parents: 38190
diff changeset
351 self.assertRaises(ValueError, c.copy)
38190
d6d879af36d3 Patch #1435422: zlib's compress and decompress objects now have a
Georg Brandl <georg@python.org>
parents: 35914
diff changeset
352
38771
618d088d9668 Patch #1503046, Conditional compilation of zlib.(de)compressobj.copy
Neal Norwitz <nnorwitz@gmail.com>
parents: 38190
diff changeset
353 if hasattr(zlib.decompressobj(), "copy"):
618d088d9668 Patch #1503046, Conditional compilation of zlib.(de)compressobj.copy
Neal Norwitz <nnorwitz@gmail.com>
parents: 38190
diff changeset
354 def test_decompresscopy(self):
618d088d9668 Patch #1503046, Conditional compilation of zlib.(de)compressobj.copy
Neal Norwitz <nnorwitz@gmail.com>
parents: 38190
diff changeset
355 # Test copying a decompression object
618d088d9668 Patch #1503046, Conditional compilation of zlib.(de)compressobj.copy
Neal Norwitz <nnorwitz@gmail.com>
parents: 38190
diff changeset
356 data = HAMLET_SCENE
618d088d9668 Patch #1503046, Conditional compilation of zlib.(de)compressobj.copy
Neal Norwitz <nnorwitz@gmail.com>
parents: 38190
diff changeset
357 comp = zlib.compress(data)
38190
d6d879af36d3 Patch #1435422: zlib's compress and decompress objects now have a
Georg Brandl <georg@python.org>
parents: 35914
diff changeset
358
38771
618d088d9668 Patch #1503046, Conditional compilation of zlib.(de)compressobj.copy
Neal Norwitz <nnorwitz@gmail.com>
parents: 38190
diff changeset
359 d0 = zlib.decompressobj()
618d088d9668 Patch #1503046, Conditional compilation of zlib.(de)compressobj.copy
Neal Norwitz <nnorwitz@gmail.com>
parents: 38190
diff changeset
360 bufs0 = []
618d088d9668 Patch #1503046, Conditional compilation of zlib.(de)compressobj.copy
Neal Norwitz <nnorwitz@gmail.com>
parents: 38190
diff changeset
361 bufs0.append(d0.decompress(comp[:32]))
38190
d6d879af36d3 Patch #1435422: zlib's compress and decompress objects now have a
Georg Brandl <georg@python.org>
parents: 35914
diff changeset
362
38771
618d088d9668 Patch #1503046, Conditional compilation of zlib.(de)compressobj.copy
Neal Norwitz <nnorwitz@gmail.com>
parents: 38190
diff changeset
363 d1 = d0.copy()
618d088d9668 Patch #1503046, Conditional compilation of zlib.(de)compressobj.copy
Neal Norwitz <nnorwitz@gmail.com>
parents: 38190
diff changeset
364 bufs1 = bufs0[:]
38190
d6d879af36d3 Patch #1435422: zlib's compress and decompress objects now have a
Georg Brandl <georg@python.org>
parents: 35914
diff changeset
365
38771
618d088d9668 Patch #1503046, Conditional compilation of zlib.(de)compressobj.copy
Neal Norwitz <nnorwitz@gmail.com>
parents: 38190
diff changeset
366 bufs0.append(d0.decompress(comp[32:]))
618d088d9668 Patch #1503046, Conditional compilation of zlib.(de)compressobj.copy
Neal Norwitz <nnorwitz@gmail.com>
parents: 38190
diff changeset
367 s0 = ''.join(bufs0)
38190
d6d879af36d3 Patch #1435422: zlib's compress and decompress objects now have a
Georg Brandl <georg@python.org>
parents: 35914
diff changeset
368
38771
618d088d9668 Patch #1503046, Conditional compilation of zlib.(de)compressobj.copy
Neal Norwitz <nnorwitz@gmail.com>
parents: 38190
diff changeset
369 bufs1.append(d1.decompress(comp[32:]))
618d088d9668 Patch #1503046, Conditional compilation of zlib.(de)compressobj.copy
Neal Norwitz <nnorwitz@gmail.com>
parents: 38190
diff changeset
370 s1 = ''.join(bufs1)
38190
d6d879af36d3 Patch #1435422: zlib's compress and decompress objects now have a
Georg Brandl <georg@python.org>
parents: 35914
diff changeset
371
38771
618d088d9668 Patch #1503046, Conditional compilation of zlib.(de)compressobj.copy
Neal Norwitz <nnorwitz@gmail.com>
parents: 38190
diff changeset
372 self.assertEqual(s0,s1)
618d088d9668 Patch #1503046, Conditional compilation of zlib.(de)compressobj.copy
Neal Norwitz <nnorwitz@gmail.com>
parents: 38190
diff changeset
373 self.assertEqual(s0,data)
38190
d6d879af36d3 Patch #1435422: zlib's compress and decompress objects now have a
Georg Brandl <georg@python.org>
parents: 35914
diff changeset
374
38771
618d088d9668 Patch #1503046, Conditional compilation of zlib.(de)compressobj.copy
Neal Norwitz <nnorwitz@gmail.com>
parents: 38190
diff changeset
375 def test_baddecompresscopy(self):
618d088d9668 Patch #1503046, Conditional compilation of zlib.(de)compressobj.copy
Neal Norwitz <nnorwitz@gmail.com>
parents: 38190
diff changeset
376 # Test copying a compression object in an inconsistent state
618d088d9668 Patch #1503046, Conditional compilation of zlib.(de)compressobj.copy
Neal Norwitz <nnorwitz@gmail.com>
parents: 38190
diff changeset
377 data = zlib.compress(HAMLET_SCENE)
618d088d9668 Patch #1503046, Conditional compilation of zlib.(de)compressobj.copy
Neal Norwitz <nnorwitz@gmail.com>
parents: 38190
diff changeset
378 d = zlib.decompressobj()
618d088d9668 Patch #1503046, Conditional compilation of zlib.(de)compressobj.copy
Neal Norwitz <nnorwitz@gmail.com>
parents: 38190
diff changeset
379 d.decompress(data)
618d088d9668 Patch #1503046, Conditional compilation of zlib.(de)compressobj.copy
Neal Norwitz <nnorwitz@gmail.com>
parents: 38190
diff changeset
380 d.flush()
618d088d9668 Patch #1503046, Conditional compilation of zlib.(de)compressobj.copy
Neal Norwitz <nnorwitz@gmail.com>
parents: 38190
diff changeset
381 self.assertRaises(ValueError, d.copy)
16719
89c126887df0 Add test case from bug #124981: zlib decompress of sync-flushed data
Andrew M. Kuchling <amk@amk.ca>
parents: 16600
diff changeset
382
27653
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
383 def genblock(seed, length, step=1024, generator=random):
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
384 """length-byte stream of random data from a seed (in step-byte blocks)."""
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
385 if seed is not None:
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
386 generator.seed(seed)
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
387 randint = generator.randint
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
388 if length < step or step < 2:
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
389 step = length
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
390 blocks = []
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
391 for i in range(0, length, step):
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
392 blocks.append(''.join([chr(randint(0,255))
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
393 for x in range(step)]))
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
394 return ''.join(blocks)[:length]
6069
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
395
27653
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
396
6069
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
397
27653
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
398 def choose_lines(source, number, seed=None, generator=random):
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
399 """Return a list of number lines randomly chosen from the source"""
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
400 if seed is not None:
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
401 generator.seed(seed)
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
402 sources = source.split('\n')
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
403 return [generator.choice(sources) for n in range(number)]
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
404
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
405
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
406
32268
8e5958c93c88 Remove a number of tests that differ only in input data size. It seems
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 32262
diff changeset
407 HAMLET_SCENE = """
15305
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 13050
diff changeset
408 LAERTES
6069
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
409
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
410 O, fear me not.
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
411 I stay too long: but here my father comes.
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
412
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
413 Enter POLONIUS
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
414
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
415 A double blessing is a double grace,
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
416 Occasion smiles upon a second leave.
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
417
15305
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 13050
diff changeset
418 LORD POLONIUS
6069
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
419
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
420 Yet here, Laertes! aboard, aboard, for shame!
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
421 The wind sits in the shoulder of your sail,
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
422 And you are stay'd for. There; my blessing with thee!
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
423 And these few precepts in thy memory
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
424 See thou character. Give thy thoughts no tongue,
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
425 Nor any unproportioned thought his act.
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
426 Be thou familiar, but by no means vulgar.
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
427 Those friends thou hast, and their adoption tried,
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
428 Grapple them to thy soul with hoops of steel;
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
429 But do not dull thy palm with entertainment
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
430 Of each new-hatch'd, unfledged comrade. Beware
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
431 Of entrance to a quarrel, but being in,
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
432 Bear't that the opposed may beware of thee.
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
433 Give every man thy ear, but few thy voice;
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
434 Take each man's censure, but reserve thy judgment.
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
435 Costly thy habit as thy purse can buy,
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
436 But not express'd in fancy; rich, not gaudy;
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
437 For the apparel oft proclaims the man,
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
438 And they in France of the best rank and station
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
439 Are of a most select and generous chief in that.
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
440 Neither a borrower nor a lender be;
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
441 For loan oft loses both itself and friend,
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
442 And borrowing dulls the edge of husbandry.
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
443 This above all: to thine ownself be true,
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
444 And it must follow, as the night the day,
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
445 Thou canst not then be false to any man.
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
446 Farewell: my blessing season this in thee!
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
447
15305
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 13050
diff changeset
448 LAERTES
6069
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
449
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
450 Most humbly do I take my leave, my lord.
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
451
15305
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 13050
diff changeset
452 LORD POLONIUS
6069
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
453
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
454 The time invites you; go; your servants tend.
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
455
15305
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 13050
diff changeset
456 LAERTES
6069
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
457
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
458 Farewell, Ophelia; and remember well
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
459 What I have said to you.
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
460
15305
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 13050
diff changeset
461 OPHELIA
6069
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
462
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
463 'Tis in my memory lock'd,
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
464 And you yourself shall keep the key of it.
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
465
15305
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 13050
diff changeset
466 LAERTES
6069
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
467
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
468 Farewell.
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
469 """
27653
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
470
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
471
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
472 def test_main():
28917
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28087
diff changeset
473 test_support.run_unittest(
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28087
diff changeset
474 ChecksumTestCase,
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28087
diff changeset
475 ExceptionTestCase,
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28087
diff changeset
476 CompressTestCase,
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28087
diff changeset
477 CompressObjectTestCase
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28087
diff changeset
478 )
27653
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
479
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
480 if __name__ == "__main__":
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
481 test_main()