from test
import test_support
# print test_support.TESTFN
# This was in the original. Avoid non-repeatable sources.
# Left here (unused) in case something wants to be done with it.
t
= imp
.find_module('test_zlib')
class ChecksumTestCase(unittest
.TestCase
):
def test_crc32start(self
):
self
.assertEqual(zlib
.crc32(""), zlib
.crc32("", 0))
def test_crc32empty(self
):
self
.assertEqual(zlib
.crc32("", 0), 0)
self
.assertEqual(zlib
.crc32("", 1), 1)
self
.assertEqual(zlib
.crc32("", 432), 432)
def test_adler32start(self
):
self
.assertEqual(zlib
.adler32(""), zlib
.adler32("", 1))
def test_adler32empty(self
):
self
.assertEqual(zlib
.adler32("", 0), 0)
self
.assertEqual(zlib
.adler32("", 1), 1)
self
.assertEqual(zlib
.adler32("", 432), 432)
def assertEqual32(self
, seen
, expected
):
# 32-bit values masked -- checksums on 32- vs 64- bit machines
# This is important if bit 31 (0x08000000L) is set.
self
.assertEqual(seen
& 0x0FFFFFFFFL
, expected
& 0x0FFFFFFFFL
)
self
.assertEqual32(zlib
.crc32("penguin", 0), 0x0e5c1a120L
)
self
.assertEqual32(zlib
.crc32("penguin", 1), 0x43b6aa94)
self
.assertEqual32(zlib
.adler32("penguin", 0), 0x0bcf02f6)
self
.assertEqual32(zlib
.adler32("penguin", 1), 0x0bd602f7)
self
.assertEqual(zlib
.crc32("penguin"), zlib
.crc32("penguin", 0))
self
.assertEqual(zlib
.adler32("penguin"),zlib
.adler32("penguin",1))
class ExceptionTestCase(unittest
.TestCase
):
# make sure we generate some expected errors
# specifying total bits too large causes an error
self
.assertRaises(zlib
.error
,
zlib
.compress
, 'ERROR', zlib
.MAX_WBITS
+ 1)
def test_badcompressobj(self
):
# verify failure on building compress object with bad params
self
.assertRaises(ValueError, zlib
.compressobj
, 1, zlib
.DEFLATED
, 0)
def test_baddecompressobj(self
):
# verify failure on building decompress object with bad params
self
.assertRaises(ValueError, zlib
.decompressobj
, 0)
class CompressTestCase(unittest
.TestCase
):
# Test compression in one go (whole message compression)
x
= zlib
.compress(HAMLET_SCENE
)
self
.assertEqual(zlib
.decompress(x
), HAMLET_SCENE
)
def test_speech128(self
):
data
= HAMLET_SCENE
* 128
self
.assertEqual(zlib
.decompress(x
), data
)
class CompressObjectTestCase(unittest
.TestCase
):
# Test compression object
# straightforward compress/decompress objects
data
= HAMLET_SCENE
* 128
self
.assertRaises(zlib
.error
, co
.flush
) # second flush should not work
dco
= zlib
.decompressobj()
y1
= dco
.decompress(x1
+ x2
)
self
.assertEqual(data
, y1
+ y2
)
def test_compressoptions(self
):
# specify lots of options to compressobj()
strategy
= zlib
.Z_FILTERED
co
= zlib
.compressobj(level
, method
, wbits
, memlevel
, strategy
)
x1
= co
.compress(HAMLET_SCENE
)
dco
= zlib
.decompressobj(wbits
)
y1
= dco
.decompress(x1
+ x2
)
self
.assertEqual(HAMLET_SCENE
, y1
+ y2
)
def test_compressincremental(self
):
# compress object in steps, decompress object as one-shot
data
= HAMLET_SCENE
* 128
for i
in range(0, len(data
), 256):
bufs
.append(co
.compress(data
[i
:i
+256]))
dco
= zlib
.decompressobj()
y1
= dco
.decompress(''.join(bufs
))
self
.assertEqual(data
, y1
+ y2
)
def test_decompinc(self
, flush
=False, source
=None, cx
=256, dcx
=64):
# compress object in steps, decompress object in steps
source
= source
or HAMLET_SCENE
for i
in range(0, len(data
), cx
):
bufs
.append(co
.compress(data
[i
:i
+cx
]))
self
.assertEqual(data
, zlib
.decompress(combuf
))
dco
= zlib
.decompressobj()
for i
in range(0, len(combuf
), dcx
):
bufs
.append(dco
.decompress(combuf
[i
:i
+dcx
]))
self
.assertEqual('', dco
.unconsumed_tail
, ########
"(A) uct should be '': not %d long" %
len(dco
.unconsumed_tail
))
chunk
= dco
.decompress('')
self
.assertEqual('', dco
.unconsumed_tail
, ########
"(B) uct should be '': not %d long" %
len(dco
.unconsumed_tail
))
self
.assertEqual(data
, ''.join(bufs
))
# Failure means: "decompressobj with init options failed"
def test_decompincflush(self
):
self
.test_decompinc(flush
=True)
def test_decompimax(self
, source
=None, cx
=256, dcx
=64):
# compress in steps, decompress in length-restricted steps
source
= source
or HAMLET_SCENE
# Check a decompression object with max_length specified
for i
in range(0, len(data
), cx
):
bufs
.append(co
.compress(data
[i
:i
+cx
]))
self
.assertEqual(data
, zlib
.decompress(combuf
),
'compressed data failure')
dco
= zlib
.decompressobj()
#max_length = 1 + len(cb)//10
chunk
= dco
.decompress(cb
, dcx
)
self
.failIf(len(chunk
) > dcx
,
'chunk too big (%d>%d)' % (len(chunk
), dcx
))
self
.assertEqual(data
, ''.join(bufs
), 'Wrong data retrieved')
def test_decompressmaxlen(self
, flush
=False):
# Check a decompression object with max_length specified
data
= HAMLET_SCENE
* 128
for i
in range(0, len(data
), 256):
bufs
.append(co
.compress(data
[i
:i
+256]))
self
.assertEqual(data
, zlib
.decompress(combuf
),
'compressed data failure')
dco
= zlib
.decompressobj()
max_length
= 1 + len(cb
)//10
chunk
= dco
.decompress(cb
, max_length
)
self
.failIf(len(chunk
) > max_length
,
'chunk too big (%d>%d)' % (len(chunk
),max_length
))
chunk
= dco
.decompress('', max_length
)
self
.failIf(len(chunk
) > max_length
,
'chunk too big (%d>%d)' % (len(chunk
),max_length
))
self
.assertEqual(data
, ''.join(bufs
), 'Wrong data retrieved')
def test_decompressmaxlenflush(self
):
self
.test_decompressmaxlen(flush
=True)
def test_maxlenmisc(self
):
# Misc tests of max_length
dco
= zlib
.decompressobj()
self
.assertRaises(ValueError, dco
.decompress
, "", -1)
self
.assertEqual('', dco
.unconsumed_tail
)
# Test flush() with the various options, using all the
# different levels in order to provide more variations.
sync_opt
= ['Z_NO_FLUSH', 'Z_SYNC_FLUSH', 'Z_FULL_FLUSH']
sync_opt
= [getattr(zlib
, opt
) for opt
in sync_opt
obj
= zlib
.compressobj( level
)
a
= obj
.compress( data
[:3000] )
c
= obj
.compress( data
[3000:] )
self
.assertEqual(zlib
.decompress(''.join([a
,b
,c
,d
])),
data
, ("Decompress failed: flush "
"mode=%i, level=%i") % (sync
, level
))
def test_odd_flush(self
):
# Test for odd flushing bugs noted in 2.0, and hopefully fixed in 2.1
if hasattr(zlib
, 'Z_SYNC_FLUSH'):
# Testing on 17K of "random" data
# Create compressor and decompressor objects
co
= zlib
.compressobj(zlib
.Z_BEST_COMPRESSION
)
dco
= zlib
.decompressobj()
# generate random data stream
# In 2.3 and later, WichmannHill is the RNG of the bug report
gen
= random
.WichmannHill()
# others might simply have a single RNG
data
= genblock(1, 17 * 1024, generator
=gen
)
# compress, sync-flush, and decompress
first
= co
.compress(data
)
second
= co
.flush(zlib
.Z_SYNC_FLUSH
)
expanded
= dco
.decompress(first
+ second
)
# if decompressed data is different from the input data, choke.
self
.assertEqual(expanded
, data
, "17K random source doesn't match")
def test_empty_flush(self
):
# Test that calling .flush() on unused objects works.
# (Bug #1083110 -- calling .flush() on decompress objects
co
= zlib
.compressobj(zlib
.Z_BEST_COMPRESSION
)
self
.failUnless(co
.flush()) # Returns a zlib header
dco
= zlib
.decompressobj()
self
.assertEqual(dco
.flush(), "") # Returns nothing
def genblock(seed
, length
, step
=1024, generator
=random
):
"""length-byte stream of random data from a seed (in step-byte blocks)."""
randint
= generator
.randint
if length
< step
or step
< 2:
for i
in range(0, length
, step
):
blocks
.append(''.join([chr(randint(0,255))
return ''.join(blocks
)[:length
]
def choose_lines(source
, number
, seed
=None, generator
=random
):
"""Return a list of number lines randomly chosen from the source"""
sources
= source
.split('\n')
return [generator
.choice(sources
) for n
in range(number
)]
I stay too long: but here my father comes.
A double blessing is a double grace,
Occasion smiles upon a second leave.
Yet here, Laertes! aboard, aboard, for shame!
The wind sits in the shoulder of your sail,
And you are stay'd for. There; my blessing with thee!
And these few precepts in thy memory
See thou character. Give thy thoughts no tongue,
Nor any unproportioned thought his act.
Be thou familiar, but by no means vulgar.
Those friends thou hast, and their adoption tried,
Grapple them to thy soul with hoops of steel;
But do not dull thy palm with entertainment
Of each new-hatch'd, unfledged comrade. Beware
Of entrance to a quarrel, but being in,
Bear't that the opposed may beware of thee.
Give every man thy ear, but few thy voice;
Take each man's censure, but reserve thy judgment.
Costly thy habit as thy purse can buy,
But not express'd in fancy; rich, not gaudy;
For the apparel oft proclaims the man,
And they in France of the best rank and station
Are of a most select and generous chief in that.
Neither a borrower nor a lender be;
For loan oft loses both itself and friend,
And borrowing dulls the edge of husbandry.
This above all: to thine ownself be true,
And it must follow, as the night the day,
Thou canst not then be false to any man.
Farewell: my blessing season this in thee!
Most humbly do I take my leave, my lord.
The time invites you; go; your servants tend.
Farewell, Ophelia; and remember well
'Tis in my memory lock'd,
And you yourself shall keep the key of it.
test_support
.run_unittest(
if __name__
== "__main__":
if not tests
: tests
= 'o'
if 'k' in tests
: testcases
.append(ChecksumTestCase
)
if 'x' in tests
: testcases
.append(ExceptionTestCase
)
if 'c' in tests
: testcases
.append(CompressTestCase
)
if 'o' in tests
: testcases
.append(CompressObjectTestCase
)
test_support
.run_unittest(*testcases
)
sys
.path
.insert(1, '/Py23Src/python/dist/src/Lib/test')
ts
, ut
= tz
.test_support
, tz
.unittest
su
.addTest(ut
.makeSuite(tz
.CompressTestCase
))