<s> import unittest <EOL> from test import test_support <EOL> import zlib <EOL> import random <EOL> def getbuf ( ) : <EOL> import imp <EOL> try : <EOL> t = imp . find_module ( '<STR_LIT>' ) <EOL> file = t [ <NUM_LIT:0> ] <EOL> except ImportError : <EOL> file = open ( __file__ ) <EOL> buf = file . read ( ) * <NUM_LIT:8> <EOL> file . close ( ) <EOL> return buf <EOL> class ChecksumTestCase ( unittest . TestCase ) : <EOL> def test_crc32start ( self ) : <EOL> self . assertEqual ( zlib . crc32 ( "<STR_LIT>" ) , zlib . crc32 ( "<STR_LIT>" , <NUM_LIT:0> ) ) <EOL> self . assert_ ( zlib . crc32 ( "<STR_LIT:abc>" , <NUM_LIT> ) ) <EOL> def test_crc32empty ( self ) : <EOL> self . assertEqual ( zlib . crc32 ( "<STR_LIT>" , <NUM_LIT:0> ) , <NUM_LIT:0> ) <EOL> self . assertEqual ( zlib . crc32 ( "<STR_LIT>" , <NUM_LIT:1> ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( zlib . crc32 ( "<STR_LIT>" , <NUM_LIT> ) , <NUM_LIT> ) <EOL> def test_adler32start ( self ) : <EOL> self . assertEqual ( zlib . adler32 ( "<STR_LIT>" ) , zlib . adler32 ( "<STR_LIT>" , <NUM_LIT:1> ) ) <EOL> self . assert_ ( zlib . adler32 ( "<STR_LIT:abc>" , <NUM_LIT> ) ) <EOL> def test_adler32empty ( self ) : <EOL> self . assertEqual ( zlib . adler32 ( "<STR_LIT>" , <NUM_LIT:0> ) , <NUM_LIT:0> ) <EOL> self . assertEqual ( zlib . adler32 ( "<STR_LIT>" , <NUM_LIT:1> ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( zlib . adler32 ( "<STR_LIT>" , <NUM_LIT> ) , <NUM_LIT> ) <EOL> def assertEqual32 ( self , seen , expected ) : <EOL> self . assertEqual ( seen & <NUM_LIT> L , expected & <NUM_LIT> L ) <EOL> def test_penguins ( self ) : <EOL> self . assertEqual32 ( zlib . crc32 ( "<STR_LIT>" , <NUM_LIT:0> ) , <NUM_LIT> L ) <EOL> self . assertEqual32 ( zlib . crc32 ( "<STR_LIT>" , <NUM_LIT:1> ) , <NUM_LIT> ) <EOL> self . assertEqual32 ( zlib . adler32 ( "<STR_LIT>" , <NUM_LIT:0> ) , <NUM_LIT> ) <EOL> self . assertEqual32 ( zlib . adler32 ( "<STR_LIT>" , <NUM_LIT:1> ) , <NUM_LIT> ) <EOL> self . assertEqual ( zlib . crc32 ( "<STR_LIT>" ) , zlib . crc32 ( "<STR_LIT>" , <NUM_LIT:0> ) ) <EOL> self . assertEqual ( zlib . adler32 ( "<STR_LIT>" ) , zlib . adler32 ( "<STR_LIT>" , <NUM_LIT:1> ) ) <EOL> class ExceptionTestCase ( unittest . TestCase ) : <EOL> def test_bigbits ( self ) : <EOL> self . assertRaises ( zlib . error , <EOL> zlib . compress , '<STR_LIT>' , zlib . MAX_WBITS + <NUM_LIT:1> ) <EOL> def test_badcompressobj ( self ) : <EOL> self . assertRaises ( ValueError , zlib . compressobj , <NUM_LIT:1> , zlib . DEFLATED , <NUM_LIT:0> ) <EOL> def test_baddecompressobj ( self ) : <EOL> self . assertRaises ( ValueError , zlib . decompressobj , <NUM_LIT:0> ) <EOL> class CompressTestCase ( unittest . TestCase ) : <EOL> def test_speech ( self ) : <EOL> x = zlib . compress ( HAMLET_SCENE ) <EOL> self . assertEqual ( zlib . decompress ( x ) , HAMLET_SCENE ) <EOL> def test_speech128 ( self ) : <EOL> data = HAMLET_SCENE * <NUM_LIT> <EOL> x = zlib . compress ( data ) <EOL> self . assertEqual ( zlib . decompress ( x ) , data ) <EOL> class CompressObjectTestCase ( unittest . TestCase ) : <EOL> def test_pair ( self ) : <EOL> data = HAMLET_SCENE * <NUM_LIT> <EOL> co = zlib . compressobj ( ) <EOL> x1 = co . compress ( data ) <EOL> x2 = co . flush ( ) <EOL> self . assertRaises ( zlib . error , co . flush ) <EOL> dco = zlib . decompressobj ( ) <EOL> y1 = dco . decompress ( x1 + x2 ) <EOL> y2 = dco . flush ( ) <EOL> self . assertEqual ( data , y1 + y2 ) <EOL> def test_compressoptions ( self ) : <EOL> level = <NUM_LIT:2> <EOL> method = zlib . DEFLATED <EOL> wbits = - <NUM_LIT:12> <EOL> memlevel = <NUM_LIT:9> <EOL> strategy = zlib . Z_FILTERED <EOL> co = zlib . compressobj ( level , method , wbits , memlevel , strategy ) <EOL> x1 = co . compress ( HAMLET_SCENE ) <EOL> x2 = co . flush ( ) <EOL> dco = zlib . decompressobj ( wbits ) <EOL> y1 = dco . decompress ( x1 + x2 ) <EOL> y2 = dco . flush ( ) <EOL> self . assertEqual ( HAMLET_SCENE , y1 + y2 ) <EOL> def test_compressincremental ( self ) : <EOL> data = HAMLET_SCENE * <NUM_LIT> <EOL> co = zlib . compressobj ( ) <EOL> bufs = [ ] <EOL> for i in range ( <NUM_LIT:0> , len ( data ) , <NUM_LIT> ) : <EOL> bufs . append ( co . compress ( data [ i : i + <NUM_LIT> ] ) ) <EOL> bufs . append ( co . flush ( ) ) <EOL> combuf = '<STR_LIT>' . join ( bufs ) <EOL> dco = zlib . decompressobj ( ) <EOL> y1 = dco . decompress ( '<STR_LIT>' . join ( bufs ) ) <EOL> y2 = dco . flush ( ) <EOL> self . assertEqual ( data , y1 + y2 ) <EOL> def test_decompinc ( self , flush = False , source = None , cx = <NUM_LIT> , dcx = <NUM_LIT:64> ) : <EOL> source = source or HAMLET_SCENE <EOL> data = source * <NUM_LIT> <EOL> co = zlib . compressobj ( ) <EOL> bufs = [ ] <EOL> for i in range ( <NUM_LIT:0> , len ( data ) , cx ) : <EOL> bufs . append ( co . compress ( data [ i : i + cx ] ) ) <EOL> bufs . append ( co . flush ( ) ) <EOL> combuf = '<STR_LIT>' . join ( bufs ) <EOL> self . assertEqual ( data , zlib . decompress ( combuf ) ) <EOL> dco = zlib . decompressobj ( ) <EOL> bufs = [ ] <EOL> for i in range ( <NUM_LIT:0> , len ( combuf ) , dcx ) : <EOL> bufs . append ( dco . decompress ( combuf [ i : i + dcx ] ) ) <EOL> self . assertEqual ( '<STR_LIT>' , dco . unconsumed_tail , <EOL> "<STR_LIT>" % <EOL> len ( dco . unconsumed_tail ) ) <EOL> if flush : <EOL> bufs . append ( dco . flush ( ) ) <EOL> else : <EOL> while True : <EOL> chunk = dco . decompress ( '<STR_LIT>' ) <EOL> if chunk : <EOL> bufs . append ( chunk ) <EOL> else : <EOL> break <EOL> self . assertEqual ( '<STR_LIT>' , dco . unconsumed_tail , <EOL> "<STR_LIT>" % <EOL> len ( dco . unconsumed_tail ) ) <EOL> self . assertEqual ( data , '<STR_LIT>' . join ( bufs ) ) <EOL> def test_decompincflush ( self ) : <EOL> self . test_decompinc ( flush = True ) <EOL> def test_decompimax ( self , source = None , cx = <NUM_LIT> , dcx = <NUM_LIT:64> ) : <EOL> source = source or HAMLET_SCENE <EOL> data = source * <NUM_LIT> <EOL> co = zlib . compressobj ( ) <EOL> bufs = [ ] <EOL> for i in range ( <NUM_LIT:0> , len ( data ) , cx ) : <EOL> bufs . append ( co . compress ( data [ i : i + cx ] ) ) <EOL> bufs . append ( co . flush ( ) ) <EOL> combuf = '<STR_LIT>' . join ( bufs ) <EOL> self . assertEqual ( data , zlib . decompress ( combuf ) , <EOL> '<STR_LIT>' ) <EOL> dco = zlib . decompressobj ( ) <EOL> bufs = [ ] <EOL> cb = combuf <EOL> while cb : <EOL> chunk = dco . decompress ( cb , dcx ) <EOL> self . failIf ( len ( chunk ) > dcx , <EOL> '<STR_LIT>' % ( len ( chunk ) , dcx ) ) <EOL> bufs . append ( chunk ) <EOL> cb = dco . unconsumed_tail <EOL> bufs . append ( dco . flush ( ) ) <EOL> self . assertEqual ( data , '<STR_LIT>' . join ( bufs ) , '<STR_LIT>' ) <EOL> def test_decompressmaxlen ( self , flush = False ) : <EOL> data = HAMLET_SCENE * <NUM_LIT> <EOL> co = zlib . compressobj ( ) <EOL> bufs = [ ] <EOL> for i in range ( <NUM_LIT:0> , len ( data ) , <NUM_LIT> ) : <EOL> bufs . append ( co . compress ( data [ i : i + <NUM_LIT> ] ) ) <EOL> bufs . append ( co . flush ( ) ) <EOL> combuf = '<STR_LIT>' . join ( bufs ) <EOL> self . assertEqual ( data , zlib . decompress ( combuf ) , <EOL> '<STR_LIT>' ) <EOL> dco = zlib . decompressobj ( ) <EOL> bufs = [ ] <EOL> cb = combuf <EOL> while cb : <EOL> max_length = <NUM_LIT:1> + len ( cb ) // <NUM_LIT:10> <EOL> chunk = dco . decompress ( cb , max_length ) <EOL> self . failIf ( len ( chunk ) > max_length , <EOL> '<STR_LIT>' % ( len ( chunk ) , max_length ) ) <EOL> bufs . append ( chunk ) <EOL> cb = dco . unconsumed_tail <EOL> if flush : <EOL> bufs . append ( dco . flush ( ) ) <EOL> else : <EOL> while chunk : <EOL> chunk = dco . decompress ( '<STR_LIT>' , max_length ) <EOL> self . failIf ( len ( chunk ) > max_length , <EOL> '<STR_LIT>' % ( len ( chunk ) , max_length ) ) <EOL> bufs . append ( chunk ) <EOL> self . assertEqual ( data , '<STR_LIT>' . join ( bufs ) , '<STR_LIT>' ) <EOL> def test_decompressmaxlenflush ( self ) : <EOL> self . test_decompressmaxlen ( flush = True ) <EOL> def test_maxlenmisc ( self ) : <EOL> dco = zlib . decompressobj ( ) <EOL> self . assertRaises ( ValueError , dco . decompress , "<STR_LIT>" , - <NUM_LIT:1> ) <EOL> self . assertEqual ( '<STR_LIT>' , dco . unconsumed_tail ) <EOL> def test_flushes ( self ) : <EOL> sync_opt = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> sync_opt = [ getattr ( zlib , opt ) for opt in sync_opt <EOL> if hasattr ( zlib , opt ) ] <EOL> data = HAMLET_SCENE * <NUM_LIT:8> <EOL> for sync in sync_opt : <EOL> for level in range ( <NUM_LIT:10> ) : <EOL> obj = zlib . compressobj ( level ) <EOL> a = obj . compress ( data [ : <NUM_LIT> ] ) <EOL> b = obj . flush ( sync ) <EOL> c = obj . compress ( data [ <NUM_LIT> : ] ) <EOL> d = obj . flush ( ) <EOL> self . assertEqual ( zlib . decompress ( '<STR_LIT>' . join ( [ a , b , c , d ] ) ) , <EOL> data , ( "<STR_LIT>" <EOL> "<STR_LIT>" ) % ( sync , level ) ) <EOL> del obj <EOL> def test_odd_flush ( self ) : <EOL> import random <EOL> if hasattr ( zlib , '<STR_LIT>' ) : <EOL> co = zlib . compressobj ( zlib . Z_BEST_COMPRESSION ) <EOL> dco = zlib . decompressobj ( ) <EOL> try : <EOL> gen = random . WichmannHill ( ) <EOL> except AttributeError : <EOL> try : <EOL> gen = random . Random ( ) <EOL> except AttributeError : <EOL> gen = random <EOL> gen . seed ( <NUM_LIT:1> ) <EOL> data = genblock ( <NUM_LIT:1> , <NUM_LIT> * <NUM_LIT> , generator = gen ) <EOL> first = co . compress ( data ) <EOL> second = co . flush ( zlib . Z_SYNC_FLUSH ) <EOL> expanded = dco . decompress ( first + second ) <EOL> self . assertEqual ( expanded , data , "<STR_LIT>" ) <EOL> def test_empty_flush ( self ) : <EOL> co = zlib . compressobj ( zlib . Z_BEST_COMPRESSION ) <EOL> self . failUnless ( co . flush ( ) ) <EOL> dco = zlib . decompressobj ( ) <EOL> self . assertEqual ( dco . flush ( ) , "<STR_LIT>" ) <EOL> if hasattr ( zlib . compressobj ( ) , "<STR_LIT>" ) : <EOL> def test_compresscopy ( self ) : <EOL> data0 = HAMLET_SCENE <EOL> data1 = HAMLET_SCENE . swapcase ( ) <EOL> c0 = zlib . compressobj ( zlib . Z_BEST_COMPRESSION ) <EOL> bufs0 = [ ] <EOL> bufs0 . append ( c0 . compress ( data0 ) ) <EOL> c1 = c0 . copy ( ) <EOL> bufs1 = bufs0 [ : ] <EOL> bufs0 . append ( c0 . compress ( data0 ) ) <EOL> bufs0 . append ( c0 . flush ( ) ) <EOL> s0 = '<STR_LIT>' . join ( bufs0 ) <EOL> bufs1 . append ( c1 . compress ( data1 ) ) <EOL> bufs1 . append ( c1 . flush ( ) ) <EOL> s1 = '<STR_LIT>' . join ( bufs1 ) <EOL> self . assertEqual ( zlib . decompress ( s0 ) , data0 + data0 ) <EOL> self . assertEqual ( zlib . decompress ( s1 ) , data0 + data1 ) <EOL> def test_badcompresscopy ( self ) : <EOL> c = zlib . compressobj ( ) <EOL> c . compress ( HAMLET_SCENE ) <EOL> c . flush ( ) <EOL> self . assertRaises ( ValueError , c . copy ) <EOL> if hasattr ( zlib . decompressobj ( ) , "<STR_LIT>" ) : <EOL> def test_decompresscopy ( self ) : <EOL> data = HAMLET_SCENE <EOL> comp = zlib . compress ( data ) <EOL> d0 = zlib . decompressobj ( ) <EOL> bufs0 = [ ] <EOL> bufs0 . append ( d0 . decompress ( comp [ : <NUM_LIT:32> ] ) ) <EOL> d1 = d0 . copy ( ) <EOL> bufs1 = bufs0 [ : ] <EOL> bufs0 . append ( d0 . decompress ( comp [ <NUM_LIT:32> : ] ) ) <EOL> s0 = '<STR_LIT>' . join ( bufs0 ) <EOL> bufs1 . append ( d1 . decompress ( comp [ <NUM_LIT:32> : ] ) ) <EOL> s1 = '<STR_LIT>' . join ( bufs1 ) <EOL> self . assertEqual ( s0 , s1 ) <EOL> self . assertEqual ( s0 , data ) <EOL> def test_baddecompresscopy ( self ) : <EOL> data = zlib . compress ( HAMLET_SCENE ) <EOL> d = zlib . decompressobj ( ) <EOL> d . decompress ( data ) <EOL> d . flush ( ) <EOL> self . assertRaises ( ValueError , d . copy ) <EOL> def genblock ( seed , length , step = <NUM_LIT> , generator = random ) : <EOL> """<STR_LIT>""" <EOL> if seed is not None : <EOL> generator . seed ( seed ) <EOL> randint = generator . randint <EOL> if length < step or step < <NUM_LIT:2> : <EOL> step = length <EOL> blocks = [ ] <EOL> for i in range ( <NUM_LIT:0> , length , step ) : <EOL> blocks . append ( '<STR_LIT>' . join ( [ chr ( randint ( <NUM_LIT:0> , <NUM_LIT:255> ) ) <EOL> for x in range ( step ) ] ) ) <EOL> return '<STR_LIT>' . join ( blocks ) [ : length ] <EOL> def choose_lines ( source , number , seed = None , generator = random ) : <EOL> """<STR_LIT>""" <EOL> if seed is not None : <EOL> generator . seed ( seed ) <EOL> sources = source . split ( '<STR_LIT:\n>' ) <EOL> return [ generator . choice ( sources ) for n in range ( number ) ] <EOL> HAMLET_SCENE = """<STR_LIT>""" <EOL> def test_main ( ) : <EOL> test_support . run_unittest ( <EOL> ChecksumTestCase , <EOL> ExceptionTestCase , <EOL> CompressTestCase , <EOL> CompressObjectTestCase <EOL> ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> test_main ( ) <EOL> def test ( tests = '<STR_LIT>' ) : <EOL> if not tests : tests = '<STR_LIT:o>' <EOL> testcases = [ ] <EOL> if '<STR_LIT:k>' in tests : testcases . append ( ChecksumTestCase ) <EOL> if '<STR_LIT:x>' in tests : testcases . append ( ExceptionTestCase ) <EOL> if '<STR_LIT:c>' in tests : testcases . append ( CompressTestCase ) <EOL> if '<STR_LIT:o>' in tests : testcases . append ( CompressObjectTestCase ) <EOL> test_support . run_unittest ( * testcases ) <EOL> if False : <EOL> import sys <EOL> sys . path . insert ( <NUM_LIT:1> , '<STR_LIT>' ) <EOL> import test_zlib as tz <EOL> ts , ut = tz . test_support , tz . unittest <EOL> su = ut . TestSuite ( ) <EOL> su . addTest ( ut . makeSuite ( tz . CompressTestCase ) ) <EOL> ts . run_suite ( su ) </s>
