[issue5804] Add an 'offset' argument to zlib.decompress - Code Review (original) (raw)
OLD
NEW
1 import unittest
1 import unittest
2 from test import test_support
2 from test import test_support
3 import binascii
3 import binascii
4 import random
4 import random
5
5
6 zlib = test_support.import_module('zlib')
6 zlib = test_support.import_module('zlib')
7
7
8
8
9 class ChecksumTestCase(unittest.TestCase):
9 class ChecksumTestCase(unittest.TestCase):
10 # checksum test cases
10 # checksum test cases
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading...
96 def test_speech(self):
96 def test_speech(self):
97 x = zlib.compress(HAMLET_SCENE)
97 x = zlib.compress(HAMLET_SCENE)
98 self.assertEqual(zlib.decompress(x), HAMLET_SCENE)
98 self.assertEqual(zlib.decompress(x), HAMLET_SCENE)
99
99
100 def test_speech128(self):
100 def test_speech128(self):
101 # compress more data
101 # compress more data
102 data = HAMLET_SCENE * 128
102 data = HAMLET_SCENE * 128
103 x = zlib.compress(data)
103 x = zlib.compress(data)
104 self.assertEqual(zlib.decompress(x), data)
104 self.assertEqual(zlib.decompress(x), data)
105
105
106
106 def test_offset(self):
107 # test the "tail" feature of decompression
108 a = HAMLET_SCENE
109 b = a.swapcase()*2
110 c = zlib.compress(a)+zlib.compress(b)
111 ········
112 aa, offset = zlib.decompress(c, offset=0)
113 bb, offset = zlib.decompress(c, offset=offset)
114 self.assertEqual(a, aa)
115 self.assertEqual(b, bb)
116 self.assertEqual(offset, len(c))
107
117
108
118
109 class CompressObjectTestCase(unittest.TestCase):
119 class CompressObjectTestCase(unittest.TestCase):
110 # Test compression object
120 # Test compression object
111 def test_pair(self):
121 def test_pair(self):
112 # straightforward compress/decompress objects
122 # straightforward compress/decompress objects
113 data = HAMLET_SCENE * 128
123 data = HAMLET_SCENE * 128
114 co = zlib.compressobj()
124 co = zlib.compressobj()
115 x1 = co.compress(data)
125 x1 = co.compress(data)
116 x2 = co.flush()
126 x2 = co.flush()
(...skipping 256 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading...
373 self.assertEqual(s0,data)
383 self.assertEqual(s0,data)
374
384
375 def test_baddecompresscopy(self):
385 def test_baddecompresscopy(self):
376 # Test copying a compression object in an inconsistent state
386 # Test copying a compression object in an inconsistent state
377 data = zlib.compress(HAMLET_SCENE)
387 data = zlib.compress(HAMLET_SCENE)
378 d = zlib.decompressobj()
388 d = zlib.decompressobj()
379 d.decompress(data)
389 d.decompress(data)
380 d.flush()
390 d.flush()
381 self.assertRaises(ValueError, d.copy)
391 self.assertRaises(ValueError, d.copy)
382
392
393 def test_unused_data(self):
394 #test any unused data
395 data = HAMLET_SCENE
396 unused = data.swapcase()*2
397 x = zlib.compress(data) + unused
398 dco = zlib.decompressobj()
399 y = dco.decompress(x) + dco.flush()
400 self.assertEqual(y, data)
401 self.assertEqual(dco.unused_data, unused)
402
383 def genblock(seed, length, step=1024, generator=random):
403 def genblock(seed, length, step=1024, generator=random):
384 """length-byte stream of random data from a seed (in step-byte blocks)."""
404 """length-byte stream of random data from a seed (in step-byte blocks)."""
385 if seed is not None:
405 if seed is not None:
386 generator.seed(seed)
406 generator.seed(seed)
387 randint = generator.randint
407 randint = generator.randint
388 if length < step or step < 2:
408 if length < step or step < 2:
389 step = length
409 step = length
390 blocks = []
410 blocks = []
391 for i in range(0, length, step):
411 for i in range(0, length, step):
392 blocks.append(''.join([chr(randint(0,255))
412 blocks.append(''.join([chr(randint(0,255))
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading...
472 def test_main():
492 def test_main():
473 test_support.run_unittest(
493 test_support.run_unittest(
474 ChecksumTestCase,
494 ChecksumTestCase,
475 ExceptionTestCase,
495 ExceptionTestCase,
476 CompressTestCase,
496 CompressTestCase,
477 CompressObjectTestCase
497 CompressObjectTestCase
478 )
498 )
479
499
480 if __name__ == "__main__":
500 if __name__ == "__main__":
481 test_main()
501 test_main()
OLD
NEW