annotate Lib/test/test_zlib.py @ 79675:c0a2294de787 2.7

Issue #16199: Backport 2to3 typo fix from 3.2.
author Chris Jerdonek <chris.jerdonek@gmail.com>
date Thu, 11 Oct 2012 16:15:46 -0700
parents 24543f7a87ce
children 1c54def5947c
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
69800
f43213129ba8 Issue #10276: test_zlib checks that inputs of 2 GB are handled correctly by
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68256
diff changeset
2 from test.test_support import TESTFN, run_unittest, import_module, unlink, requires
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
69834
7f3cab59ef3e Issue #11277: test_zlib tests a buffer of 1 GB on 32 bits
Victor Stinner <victor.stinner@haypocalc.com>
parents: 69824
diff changeset
5 from test.test_support import precisionbigmemtest, _1G, _4G
69800
f43213129ba8 Issue #10276: test_zlib checks that inputs of 2 GB are handled correctly by
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68256
diff changeset
6 import sys
27653
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
7
69800
f43213129ba8 Issue #10276: test_zlib checks that inputs of 2 GB are handled correctly by
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68256
diff changeset
8 try:
f43213129ba8 Issue #10276: test_zlib checks that inputs of 2 GB are handled correctly by
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68256
diff changeset
9 import mmap
f43213129ba8 Issue #10276: test_zlib checks that inputs of 2 GB are handled correctly by
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68256
diff changeset
10 except ImportError:
f43213129ba8 Issue #10276: test_zlib checks that inputs of 2 GB are handled correctly by
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68256
diff changeset
11 mmap = None
f43213129ba8 Issue #10276: test_zlib checks that inputs of 2 GB are handled correctly by
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68256
diff changeset
12
f43213129ba8 Issue #10276: test_zlib checks that inputs of 2 GB are handled correctly by
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68256
diff changeset
13 zlib = import_module('zlib')
52491
f4c5e3a64be2 Change more tests to use import_module for the modules that
R. David Murray <rdmurray@bitdance.com>
parents: 46363
diff changeset
14
6069
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
15
27653
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
16 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
17 # 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
18 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
19 self.assertEqual(zlib.crc32(""), zlib.crc32("", 0))
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52491
diff changeset
20 self.assertTrue(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
21
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
22 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
23 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
24 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
25 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
26
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
27 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
28 self.assertEqual(zlib.adler32(""), zlib.adler32("", 1))
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52491
diff changeset
29 self.assertTrue(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
30
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
31 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
32 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
33 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
34 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
35
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
36 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
37 # 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
38 # 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
39 self.assertEqual(seen & 0x0FFFFFFFFL, expected & 0x0FFFFFFFFL)
6069
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
40
27653
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
41 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
42 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
43 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
44 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
45 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
46
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
47 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
48 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
49
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
50 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
51 """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
52 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
53 # 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
54 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
55 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
56 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
57 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
58
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
59 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
60 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
61 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
62 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
63
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
64 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
65 # 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
66 # -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
67 # 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
68 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
69 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
70 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
71 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
72 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
73
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
74
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
75 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
76 # 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
77 def test_badlevel(self):
a5a4b786bb21 test_bigbits was not testing what it seemed to.
Armin Rigo <arigo@tunes.org>
parents: 41513
diff changeset
78 # 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
79 # (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
80 # accepts 0 too)
a5a4b786bb21 test_bigbits was not testing what it seemed to.
Armin Rigo <arigo@tunes.org>
parents: 41513
diff changeset
81 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
82
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_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
84 # 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
85 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
86 # 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
87 self.assertRaises(ValueError,
a5a4b786bb21 test_bigbits was not testing what it seemed to.
Armin Rigo <arigo@tunes.org>
parents: 41513
diff changeset
88 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
89
27653
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
90 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
91 # verify failure on building decompress object with bad params
60407
3ce4aac20935 Merged revisions 79848 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 46363
diff changeset
92 self.assertRaises(ValueError, zlib.decompressobj, -1)
27653
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
93
46364
0498ebb6c41b Merge r62235 from trunk.
Gregory P. Smith <greg@mad-scientist.com>
parents: 38771
diff changeset
94 def test_decompressobj_badflush(self):
0498ebb6c41b Merge r62235 from trunk.
Gregory P. Smith <greg@mad-scientist.com>
parents: 38771
diff changeset
95 # verify failure on calling decompressobj.flush with bad params
0498ebb6c41b Merge r62235 from trunk.
Gregory P. Smith <greg@mad-scientist.com>
parents: 38771
diff changeset
96 self.assertRaises(ValueError, zlib.decompressobj().flush, 0)
0498ebb6c41b Merge r62235 from trunk.
Gregory P. Smith <greg@mad-scientist.com>
parents: 38771
diff changeset
97 self.assertRaises(ValueError, zlib.decompressobj().flush, -1)
0498ebb6c41b Merge r62235 from trunk.
Gregory P. Smith <greg@mad-scientist.com>
parents: 38771
diff changeset
98
27653
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
99
61369
f18981904507 Merged revisions 80926 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60407
diff changeset
100 class BaseCompressTestCase(object):
f18981904507 Merged revisions 80926 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60407
diff changeset
101 def check_big_compress_buffer(self, size, compress_func):
f18981904507 Merged revisions 80926 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60407
diff changeset
102 _1M = 1024 * 1024
f18981904507 Merged revisions 80926 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60407
diff changeset
103 fmt = "%%0%dx" % (2 * _1M)
f18981904507 Merged revisions 80926 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60407
diff changeset
104 # Generate 10MB worth of random, and expand it by repeating it.
f18981904507 Merged revisions 80926 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60407
diff changeset
105 # The assumption is that zlib's memory is not big enough to exploit
f18981904507 Merged revisions 80926 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60407
diff changeset
106 # such spread out redundancy.
f18981904507 Merged revisions 80926 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60407
diff changeset
107 data = ''.join([binascii.a2b_hex(fmt % random.getrandbits(8 * _1M))
f18981904507 Merged revisions 80926 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60407
diff changeset
108 for i in range(10)])
f18981904507 Merged revisions 80926 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60407
diff changeset
109 data = data * (size // len(data) + 1)
f18981904507 Merged revisions 80926 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60407
diff changeset
110 try:
f18981904507 Merged revisions 80926 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60407
diff changeset
111 compress_func(data)
f18981904507 Merged revisions 80926 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60407
diff changeset
112 finally:
f18981904507 Merged revisions 80926 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60407
diff changeset
113 # Release memory
f18981904507 Merged revisions 80926 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60407
diff changeset
114 data = None
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
61369
f18981904507 Merged revisions 80926 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60407
diff changeset
116 def check_big_decompress_buffer(self, size, decompress_func):
f18981904507 Merged revisions 80926 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60407
diff changeset
117 data = 'x' * size
f18981904507 Merged revisions 80926 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60407
diff changeset
118 try:
f18981904507 Merged revisions 80926 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60407
diff changeset
119 compressed = zlib.compress(data, 1)
f18981904507 Merged revisions 80926 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60407
diff changeset
120 finally:
f18981904507 Merged revisions 80926 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60407
diff changeset
121 # Release memory
f18981904507 Merged revisions 80926 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60407
diff changeset
122 data = None
f18981904507 Merged revisions 80926 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60407
diff changeset
123 data = decompress_func(compressed)
f18981904507 Merged revisions 80926 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60407
diff changeset
124 # Sanity check
f18981904507 Merged revisions 80926 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60407
diff changeset
125 try:
f18981904507 Merged revisions 80926 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60407
diff changeset
126 self.assertEqual(len(data), size)
f18981904507 Merged revisions 80926 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60407
diff changeset
127 self.assertEqual(len(data.strip('x')), 0)
f18981904507 Merged revisions 80926 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60407
diff changeset
128 finally:
f18981904507 Merged revisions 80926 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60407
diff changeset
129 data = None
f18981904507 Merged revisions 80926 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60407
diff changeset
130
f18981904507 Merged revisions 80926 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60407
diff changeset
131
f18981904507 Merged revisions 80926 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60407
diff changeset
132 class CompressTestCase(BaseCompressTestCase, unittest.TestCase):
27653
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
133 # 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
134 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
135 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
136 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
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_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
139 # 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
140 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
141 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
142 self.assertEqual(zlib.decompress(x), data)
6069
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
143
61522
2a70c80349b0 Merged revisions 81098 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61518
diff changeset
144 def test_incomplete_stream(self):
2a70c80349b0 Merged revisions 81098 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61518
diff changeset
145 # An useful error message is given
2a70c80349b0 Merged revisions 81098 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61518
diff changeset
146 x = zlib.compress(HAMLET_SCENE)
61521
3a8ca67ae431 Issue #8681: Make the zlib module's error messages more informative when
Antoine Pitrou <solipsis@pitrou.net>
parents: 61517
diff changeset
147 self.assertRaisesRegexp(zlib.error,
3a8ca67ae431 Issue #8681: Make the zlib module's error messages more informative when
Antoine Pitrou <solipsis@pitrou.net>
parents: 61517
diff changeset
148 "Error -5 while decompressing data: incomplete or truncated stream",
3a8ca67ae431 Issue #8681: Make the zlib module's error messages more informative when
Antoine Pitrou <solipsis@pitrou.net>
parents: 61517
diff changeset
149 zlib.decompress, x[:-1])
61522
2a70c80349b0 Merged revisions 81098 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61518
diff changeset
150
61369
f18981904507 Merged revisions 80926 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60407
diff changeset
151 # Memory use of the following functions takes into account overallocation
f18981904507 Merged revisions 80926 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60407
diff changeset
152
f18981904507 Merged revisions 80926 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60407
diff changeset
153 @precisionbigmemtest(size=_1G + 1024 * 1024, memuse=3)
f18981904507 Merged revisions 80926 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60407
diff changeset
154 def test_big_compress_buffer(self, size):
f18981904507 Merged revisions 80926 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60407
diff changeset
155 compress = lambda s: zlib.compress(s, 1)
f18981904507 Merged revisions 80926 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60407
diff changeset
156 self.check_big_compress_buffer(size, compress)
f18981904507 Merged revisions 80926 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60407
diff changeset
157
f18981904507 Merged revisions 80926 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60407
diff changeset
158 @precisionbigmemtest(size=_1G + 1024 * 1024, memuse=2)
f18981904507 Merged revisions 80926 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60407
diff changeset
159 def test_big_decompress_buffer(self, size):
f18981904507 Merged revisions 80926 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60407
diff changeset
160 self.check_big_decompress_buffer(size, zlib.decompress)
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
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
162
61369
f18981904507 Merged revisions 80926 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60407
diff changeset
163 class CompressObjectTestCase(BaseCompressTestCase, unittest.TestCase):
27653
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
164 # 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
165 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
166 # 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
167 data = HAMLET_SCENE * 128
32262
36798f1c8e9a Remove lots of magic constants.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 30256
diff changeset
168 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
169 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
170 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
171 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
172 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
173 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
174 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
175 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
176
32262
36798f1c8e9a Remove lots of magic constants.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 30256
diff changeset
177 def test_compressoptions(self):
36798f1c8e9a Remove lots of magic constants.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 30256
diff changeset
178 # specify lots of options to compressobj()
36798f1c8e9a Remove lots of magic constants.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 30256
diff changeset
179 level = 2
36798f1c8e9a Remove lots of magic constants.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 30256
diff changeset
180 method = zlib.DEFLATED
36798f1c8e9a Remove lots of magic constants.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 30256
diff changeset
181 wbits = -12
36798f1c8e9a Remove lots of magic constants.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 30256
diff changeset
182 memlevel = 9
36798f1c8e9a Remove lots of magic constants.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 30256
diff changeset
183 strategy = zlib.Z_FILTERED
36798f1c8e9a Remove lots of magic constants.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 30256
diff changeset
184 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
185 x1 = co.compress(HAMLET_SCENE)
32262
36798f1c8e9a Remove lots of magic constants.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 30256
diff changeset
186 x2 = co.flush()
36798f1c8e9a Remove lots of magic constants.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 30256
diff changeset
187 dco = zlib.decompressobj(wbits)
36798f1c8e9a Remove lots of magic constants.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 30256
diff changeset
188 y1 = dco.decompress(x1 + x2)
36798f1c8e9a Remove lots of magic constants.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 30256
diff changeset
189 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
190 self.assertEqual(HAMLET_SCENE, y1 + y2)
32262
36798f1c8e9a Remove lots of magic constants.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 30256
diff changeset
191
27653
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
192 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
193 # 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
194 data = HAMLET_SCENE * 128
32262
36798f1c8e9a Remove lots of magic constants.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 30256
diff changeset
195 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
196 bufs = []
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
197 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
198 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
199 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
200 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
201
32262
36798f1c8e9a Remove lots of magic constants.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 30256
diff changeset
202 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
203 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
204 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
205 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
206
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
207 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
208 # 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
209 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
210 data = source * 128
32262
36798f1c8e9a Remove lots of magic constants.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 30256
diff changeset
211 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
212 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
213 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
214 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
215 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
216 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
217
32262
36798f1c8e9a Remove lots of magic constants.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 30256
diff changeset
218 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
219
32262
36798f1c8e9a Remove lots of magic constants.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 30256
diff changeset
220 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
221 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
222 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
223 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
224 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
225 "(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
226 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
227 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
228 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
229 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
230 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
231 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
232 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
233 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
234 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
235 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
236 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
237 "(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
238 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
239 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
240 # 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
241
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
242 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
243 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
244
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
245 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
246 # 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
247 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
248 # 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
249 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
250 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
251 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
252 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
253 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
254 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
255 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
256 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
257 'compressed data failure')
6069
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
258
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
259 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
260 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
261 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
262 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
263 #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
264 chunk = dco.decompress(cb, dcx)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52491
diff changeset
265 self.assertFalse(len(chunk) > dcx,
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
266 '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
267 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
268 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
269 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
270 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
271
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
272 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
273 # 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
274 data = HAMLET_SCENE * 128
32262
36798f1c8e9a Remove lots of magic constants.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 30256
diff changeset
275 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
276 bufs = []
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
277 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
278 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
279 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
280 combuf = ''.join(bufs)
32262
36798f1c8e9a Remove lots of magic constants.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 30256
diff changeset
281 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
282 'compressed data failure')
20547
a12ccdbce8da Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 20466
diff changeset
283
32262
36798f1c8e9a Remove lots of magic constants.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 30256
diff changeset
284 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
285 bufs = []
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
286 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
287 while cb:
28087
465405a159c1 Use floor division (//).
Guido van Rossum <guido@python.org>
parents: 27963
diff changeset
288 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
289 chunk = dco.decompress(cb, max_length)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52491
diff changeset
290 self.assertFalse(len(chunk) > max_length,
27653
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
291 '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
292 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
293 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
294 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
295 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
296 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
297 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
298 chunk = dco.decompress('', max_length)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52491
diff changeset
299 self.assertFalse(len(chunk) > max_length,
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
300 '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
301 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
302 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
303
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
304 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
305 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
306
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
307 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
308 # Misc tests of max_length
32262
36798f1c8e9a Remove lots of magic constants.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 30256
diff changeset
309 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
310 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
311 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
312
70102
24543f7a87ce Issue #12050: zlib.decompressobj().decompress() now clears the unconsumed_tail
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 69886
diff changeset
313 def test_clear_unconsumed_tail(self):
24543f7a87ce Issue #12050: zlib.decompressobj().decompress() now clears the unconsumed_tail
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 69886
diff changeset
314 # Issue #12050: calling decompress() without providing max_length
24543f7a87ce Issue #12050: zlib.decompressobj().decompress() now clears the unconsumed_tail
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 69886
diff changeset
315 # should clear the unconsumed_tail attribute.
24543f7a87ce Issue #12050: zlib.decompressobj().decompress() now clears the unconsumed_tail
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 69886
diff changeset
316 cdata = "x\x9cKLJ\x06\x00\x02M\x01" # "abc"
24543f7a87ce Issue #12050: zlib.decompressobj().decompress() now clears the unconsumed_tail
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 69886
diff changeset
317 dco = zlib.decompressobj()
24543f7a87ce Issue #12050: zlib.decompressobj().decompress() now clears the unconsumed_tail
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 69886
diff changeset
318 ddata = dco.decompress(cdata, 1)
24543f7a87ce Issue #12050: zlib.decompressobj().decompress() now clears the unconsumed_tail
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 69886
diff changeset
319 ddata += dco.decompress(dco.unconsumed_tail)
24543f7a87ce Issue #12050: zlib.decompressobj().decompress() now clears the unconsumed_tail
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 69886
diff changeset
320 self.assertEqual(dco.unconsumed_tail, "")
24543f7a87ce Issue #12050: zlib.decompressobj().decompress() now clears the unconsumed_tail
Nadeem Vawda <nadeem.vawda@gmail.com>
parents: 69886
diff changeset
321
27653
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
322 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
323 # 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
324 # 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
325 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
326 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
327 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
328 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
329
27653
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
330 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
331 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
332 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
333 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
334 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
335 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
336 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
337 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
338 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
339 "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
340 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
341
27653
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
342 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
343 # 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
344 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
345
27653
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
346 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
347 # 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
348
27653
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
349 # 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
350 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
351 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
352
27653
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
353 # 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
354 # 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
355 try:
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
356 # 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
357 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
358 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
359 try:
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
360 # 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
361 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
362 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
363 # 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
364 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
365 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
366 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
367
27653
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
368 # 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
369 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
370 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
371 expanded = dco.decompress(first + second)
16723
59f1b57bc909 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 16719
diff changeset
372
27653
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
373 # 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
374 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
375
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
376 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
377 # 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
378 # (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
379 # 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
380
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
381 co = zlib.compressobj(zlib.Z_BEST_COMPRESSION)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52491
diff changeset
382 self.assertTrue(co.flush()) # Returns a zlib header
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
383 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
384 self.assertEqual(dco.flush(), "") # Returns nothing
35003
52fed2d0641a whitespace normalisation
Anthony Baxter <anthonybaxter@gmail.com>
parents: 34264
diff changeset
385
61518
af2f84e66253 Merged revisions 81094 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61369
diff changeset
386 def test_decompress_incomplete_stream(self):
af2f84e66253 Merged revisions 81094 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61369
diff changeset
387 # This is 'foo', deflated
af2f84e66253 Merged revisions 81094 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61369
diff changeset
388 x = 'x\x9cK\xcb\xcf\x07\x00\x02\x82\x01E'
af2f84e66253 Merged revisions 81094 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61369
diff changeset
389 # For the record
af2f84e66253 Merged revisions 81094 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61369
diff changeset
390 self.assertEqual(zlib.decompress(x), 'foo')
af2f84e66253 Merged revisions 81094 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61369
diff changeset
391 self.assertRaises(zlib.error, zlib.decompress, x[:-5])
af2f84e66253 Merged revisions 81094 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61369
diff changeset
392 # Omitting the stream end works with decompressor objects
af2f84e66253 Merged revisions 81094 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61369
diff changeset
393 # (see issue #8672).
af2f84e66253 Merged revisions 81094 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61369
diff changeset
394 dco = zlib.decompressobj()
af2f84e66253 Merged revisions 81094 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61369
diff changeset
395 y = dco.decompress(x[:-5])
af2f84e66253 Merged revisions 81094 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61369
diff changeset
396 y += dco.flush()
af2f84e66253 Merged revisions 81094 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61369
diff changeset
397 self.assertEqual(y, 'foo')
af2f84e66253 Merged revisions 81094 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61369
diff changeset
398
38771
618d088d9668 Patch #1503046, Conditional compilation of zlib.(de)compressobj.copy
Neal Norwitz <nnorwitz@gmail.com>
parents: 38190
diff changeset
399 if hasattr(zlib.compressobj(), "copy"):
618d088d9668 Patch #1503046, Conditional compilation of zlib.(de)compressobj.copy
Neal Norwitz <nnorwitz@gmail.com>
parents: 38190
diff changeset
400 def test_compresscopy(self):
618d088d9668 Patch #1503046, Conditional compilation of zlib.(de)compressobj.copy
Neal Norwitz <nnorwitz@gmail.com>
parents: 38190
diff changeset
401 # Test copying a compression object
618d088d9668 Patch #1503046, Conditional compilation of zlib.(de)compressobj.copy
Neal Norwitz <nnorwitz@gmail.com>
parents: 38190
diff changeset
402 data0 = HAMLET_SCENE
618d088d9668 Patch #1503046, Conditional compilation of zlib.(de)compressobj.copy
Neal Norwitz <nnorwitz@gmail.com>
parents: 38190
diff changeset
403 data1 = HAMLET_SCENE.swapcase()
618d088d9668 Patch #1503046, Conditional compilation of zlib.(de)compressobj.copy
Neal Norwitz <nnorwitz@gmail.com>
parents: 38190
diff changeset
404 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
405 bufs0 = []
618d088d9668 Patch #1503046, Conditional compilation of zlib.(de)compressobj.copy
Neal Norwitz <nnorwitz@gmail.com>
parents: 38190
diff changeset
406 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
407
38771
618d088d9668 Patch #1503046, Conditional compilation of zlib.(de)compressobj.copy
Neal Norwitz <nnorwitz@gmail.com>
parents: 38190
diff changeset
408 c1 = c0.copy()
618d088d9668 Patch #1503046, Conditional compilation of zlib.(de)compressobj.copy
Neal Norwitz <nnorwitz@gmail.com>
parents: 38190
diff changeset
409 bufs1 = bufs0[:]
38190
d6d879af36d3 Patch #1435422: zlib's compress and decompress objects now have a
Georg Brandl <georg@python.org>
parents: 35914
diff changeset
410
38771
618d088d9668 Patch #1503046, Conditional compilation of zlib.(de)compressobj.copy
Neal Norwitz <nnorwitz@gmail.com>
parents: 38190
diff changeset
411 bufs0.append(c0.compress(data0))
618d088d9668 Patch #1503046, Conditional compilation of zlib.(de)compressobj.copy
Neal Norwitz <nnorwitz@gmail.com>
parents: 38190
diff changeset
412 bufs0.append(c0.flush())
618d088d9668 Patch #1503046, Conditional compilation of zlib.(de)compressobj.copy
Neal Norwitz <nnorwitz@gmail.com>
parents: 38190
diff changeset
413 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
414
38771
618d088d9668 Patch #1503046, Conditional compilation of zlib.(de)compressobj.copy
Neal Norwitz <nnorwitz@gmail.com>
parents: 38190
diff changeset
415 bufs1.append(c1.compress(data1))
618d088d9668 Patch #1503046, Conditional compilation of zlib.(de)compressobj.copy
Neal Norwitz <nnorwitz@gmail.com>
parents: 38190
diff changeset
416 bufs1.append(c1.flush())
618d088d9668 Patch #1503046, Conditional compilation of zlib.(de)compressobj.copy
Neal Norwitz <nnorwitz@gmail.com>
parents: 38190
diff changeset
417 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
418
38771
618d088d9668 Patch #1503046, Conditional compilation of zlib.(de)compressobj.copy
Neal Norwitz <nnorwitz@gmail.com>
parents: 38190
diff changeset
419 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
420 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
421
38771
618d088d9668 Patch #1503046, Conditional compilation of zlib.(de)compressobj.copy
Neal Norwitz <nnorwitz@gmail.com>
parents: 38190
diff changeset
422 def test_badcompresscopy(self):
618d088d9668 Patch #1503046, Conditional compilation of zlib.(de)compressobj.copy
Neal Norwitz <nnorwitz@gmail.com>
parents: 38190
diff changeset
423 # 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
424 c = zlib.compressobj()
618d088d9668 Patch #1503046, Conditional compilation of zlib.(de)compressobj.copy
Neal Norwitz <nnorwitz@gmail.com>
parents: 38190
diff changeset
425 c.compress(HAMLET_SCENE)
618d088d9668 Patch #1503046, Conditional compilation of zlib.(de)compressobj.copy
Neal Norwitz <nnorwitz@gmail.com>
parents: 38190
diff changeset
426 c.flush()
618d088d9668 Patch #1503046, Conditional compilation of zlib.(de)compressobj.copy
Neal Norwitz <nnorwitz@gmail.com>
parents: 38190
diff changeset
427 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
428
38771
618d088d9668 Patch #1503046, Conditional compilation of zlib.(de)compressobj.copy
Neal Norwitz <nnorwitz@gmail.com>
parents: 38190
diff changeset
429 if hasattr(zlib.decompressobj(), "copy"):
618d088d9668 Patch #1503046, Conditional compilation of zlib.(de)compressobj.copy
Neal Norwitz <nnorwitz@gmail.com>
parents: 38190
diff changeset
430 def test_decompresscopy(self):
618d088d9668 Patch #1503046, Conditional compilation of zlib.(de)compressobj.copy
Neal Norwitz <nnorwitz@gmail.com>
parents: 38190
diff changeset
431 # Test copying a decompression object
618d088d9668 Patch #1503046, Conditional compilation of zlib.(de)compressobj.copy
Neal Norwitz <nnorwitz@gmail.com>
parents: 38190
diff changeset
432 data = HAMLET_SCENE
618d088d9668 Patch #1503046, Conditional compilation of zlib.(de)compressobj.copy
Neal Norwitz <nnorwitz@gmail.com>
parents: 38190
diff changeset
433 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
434
38771
618d088d9668 Patch #1503046, Conditional compilation of zlib.(de)compressobj.copy
Neal Norwitz <nnorwitz@gmail.com>
parents: 38190
diff changeset
435 d0 = zlib.decompressobj()
618d088d9668 Patch #1503046, Conditional compilation of zlib.(de)compressobj.copy
Neal Norwitz <nnorwitz@gmail.com>
parents: 38190
diff changeset
436 bufs0 = []
618d088d9668 Patch #1503046, Conditional compilation of zlib.(de)compressobj.copy
Neal Norwitz <nnorwitz@gmail.com>
parents: 38190
diff changeset
437 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
438
38771
618d088d9668 Patch #1503046, Conditional compilation of zlib.(de)compressobj.copy
Neal Norwitz <nnorwitz@gmail.com>
parents: 38190
diff changeset
439 d1 = d0.copy()
618d088d9668 Patch #1503046, Conditional compilation of zlib.(de)compressobj.copy
Neal Norwitz <nnorwitz@gmail.com>
parents: 38190
diff changeset
440 bufs1 = bufs0[:]
38190
d6d879af36d3 Patch #1435422: zlib's compress and decompress objects now have a
Georg Brandl <georg@python.org>
parents: 35914
diff changeset
441
38771
618d088d9668 Patch #1503046, Conditional compilation of zlib.(de)compressobj.copy
Neal Norwitz <nnorwitz@gmail.com>
parents: 38190
diff changeset
442 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
443 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
444
38771
618d088d9668 Patch #1503046, Conditional compilation of zlib.(de)compressobj.copy
Neal Norwitz <nnorwitz@gmail.com>
parents: 38190
diff changeset
445 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
446 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
447
38771
618d088d9668 Patch #1503046, Conditional compilation of zlib.(de)compressobj.copy
Neal Norwitz <nnorwitz@gmail.com>
parents: 38190
diff changeset
448 self.assertEqual(s0,s1)
618d088d9668 Patch #1503046, Conditional compilation of zlib.(de)compressobj.copy
Neal Norwitz <nnorwitz@gmail.com>
parents: 38190
diff changeset
449 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
450
38771
618d088d9668 Patch #1503046, Conditional compilation of zlib.(de)compressobj.copy
Neal Norwitz <nnorwitz@gmail.com>
parents: 38190
diff changeset
451 def test_baddecompresscopy(self):
618d088d9668 Patch #1503046, Conditional compilation of zlib.(de)compressobj.copy
Neal Norwitz <nnorwitz@gmail.com>
parents: 38190
diff changeset
452 # 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
453 data = zlib.compress(HAMLET_SCENE)
618d088d9668 Patch #1503046, Conditional compilation of zlib.(de)compressobj.copy
Neal Norwitz <nnorwitz@gmail.com>
parents: 38190
diff changeset
454 d = zlib.decompressobj()
618d088d9668 Patch #1503046, Conditional compilation of zlib.(de)compressobj.copy
Neal Norwitz <nnorwitz@gmail.com>
parents: 38190
diff changeset
455 d.decompress(data)
618d088d9668 Patch #1503046, Conditional compilation of zlib.(de)compressobj.copy
Neal Norwitz <nnorwitz@gmail.com>
parents: 38190
diff changeset
456 d.flush()
618d088d9668 Patch #1503046, Conditional compilation of zlib.(de)compressobj.copy
Neal Norwitz <nnorwitz@gmail.com>
parents: 38190
diff changeset
457 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
458
61369
f18981904507 Merged revisions 80926 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60407
diff changeset
459 # Memory use of the following functions takes into account overallocation
f18981904507 Merged revisions 80926 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60407
diff changeset
460
f18981904507 Merged revisions 80926 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60407
diff changeset
461 @precisionbigmemtest(size=_1G + 1024 * 1024, memuse=3)
f18981904507 Merged revisions 80926 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60407
diff changeset
462 def test_big_compress_buffer(self, size):
f18981904507 Merged revisions 80926 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60407
diff changeset
463 c = zlib.compressobj(1)
f18981904507 Merged revisions 80926 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60407
diff changeset
464 compress = lambda s: c.compress(s) + c.flush()
f18981904507 Merged revisions 80926 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60407
diff changeset
465 self.check_big_compress_buffer(size, compress)
f18981904507 Merged revisions 80926 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60407
diff changeset
466
f18981904507 Merged revisions 80926 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60407
diff changeset
467 @precisionbigmemtest(size=_1G + 1024 * 1024, memuse=2)
f18981904507 Merged revisions 80926 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60407
diff changeset
468 def test_big_decompress_buffer(self, size):
f18981904507 Merged revisions 80926 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60407
diff changeset
469 d = zlib.decompressobj()
f18981904507 Merged revisions 80926 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60407
diff changeset
470 decompress = lambda s: d.decompress(s) + d.flush()
f18981904507 Merged revisions 80926 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60407
diff changeset
471 self.check_big_decompress_buffer(size, decompress)
f18981904507 Merged revisions 80926 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60407
diff changeset
472
f18981904507 Merged revisions 80926 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60407
diff changeset
473
27653
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
474 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
475 """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
476 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
477 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
478 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
479 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
480 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
481 blocks = []
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
482 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
483 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
484 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
485 return ''.join(blocks)[:length]
6069
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
486
27653
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
487
6069
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
488
27653
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
489 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
490 """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
491 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
492 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
493 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
494 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
495
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
496
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
497
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
498 HAMLET_SCENE = """
15305
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 13050
diff changeset
499 LAERTES
6069
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
500
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
501 O, fear me not.
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
502 I stay too long: but here my father comes.
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
503
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
504 Enter POLONIUS
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
505
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
506 A double blessing is a double grace,
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
507 Occasion smiles upon a second leave.
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
508
15305
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 13050
diff changeset
509 LORD POLONIUS
6069
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
510
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
511 Yet here, Laertes! aboard, aboard, for shame!
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
512 The wind sits in the shoulder of your sail,
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
513 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
514 And these few precepts in thy memory
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
515 See thou character. Give thy thoughts no tongue,
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
516 Nor any unproportioned thought his act.
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
517 Be thou familiar, but by no means vulgar.
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
518 Those friends thou hast, and their adoption tried,
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
519 Grapple them to thy soul with hoops of steel;
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
520 But do not dull thy palm with entertainment
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
521 Of each new-hatch'd, unfledged comrade. Beware
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
522 Of entrance to a quarrel, but being in,
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
523 Bear't that the opposed may beware of thee.
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
524 Give every man thy ear, but few thy voice;
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
525 Take each man's censure, but reserve thy judgment.
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
526 Costly thy habit as thy purse can buy,
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
527 But not express'd in fancy; rich, not gaudy;
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
528 For the apparel oft proclaims the man,
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
529 And they in France of the best rank and station
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
530 Are of a most select and generous chief in that.
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
531 Neither a borrower nor a lender be;
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
532 For loan oft loses both itself and friend,
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
533 And borrowing dulls the edge of husbandry.
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
534 This above all: to thine ownself be true,
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
535 And it must follow, as the night the day,
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
536 Thou canst not then be false to any man.
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
537 Farewell: my blessing season this in thee!
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
538
15305
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 13050
diff changeset
539 LAERTES
6069
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
540
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
541 Most humbly do I take my leave, my lord.
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
542
15305
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 13050
diff changeset
543 LORD POLONIUS
6069
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
544
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
545 The time invites you; go; your servants tend.
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
546
15305
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 13050
diff changeset
547 LAERTES
6069
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
548
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
549 Farewell, Ophelia; and remember well
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
550 What I have said to you.
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
551
15305
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 13050
diff changeset
552 OPHELIA
6069
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
553
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
554 'Tis in my memory lock'd,
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
555 And you yourself shall keep the key of it.
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
556
15305
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 13050
diff changeset
557 LAERTES
6069
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
558
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
559 Farewell.
e0f249993a4f test the zlib module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
560 """
27653
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
561
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
562
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
563 def test_main():
69800
f43213129ba8 Issue #10276: test_zlib checks that inputs of 2 GB are handled correctly by
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68256
diff changeset
564 run_unittest(
28917
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28087
diff changeset
565 ChecksumTestCase,
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28087
diff changeset
566 ExceptionTestCase,
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28087
diff changeset
567 CompressTestCase,
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28087
diff changeset
568 CompressObjectTestCase
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28087
diff changeset
569 )
27653
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
570
106a44a4b975 - Thanks to Scott David Daniels, a subtle bug in how the zlib
Guido van Rossum <guido@python.org>
parents: 24758
diff changeset
571 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
572 test_main()