@@ -74,25 +74,12 @@ def test_baddecompressobj(self):
7474class CompressTestCase (unittest .TestCase ):
7575 # Test compression in one go (whole message compression)
7676 def test_speech (self ):
77- # decompress(compress(data)) better be data
78- x = zlib .compress (hamlet_scene )
79- self .assertEqual (zlib .decompress (x ), hamlet_scene )
80-
81- def test_speech8 (self ):
82- # decompress(compress(data)) better be data -- more compression chances
83- data = hamlet_scene * 8
84- x = zlib .compress (data )
85- self .assertEqual (zlib .decompress (x ), data )
86-
87- def test_speech16 (self ):
88- # decompress(compress(data)) better be data -- more compression chances
89- data = hamlet_scene * 16
90- x = zlib .compress (data )
91- self .assertEqual (zlib .decompress (x ), data )
77+ x = zlib .compress (HAMLET_SCENE )
78+ self .assertEqual (zlib .decompress (x ), HAMLET_SCENE )
9279
9380 def test_speech128 (self ):
94- # decompress( compress(data)) better be data -- more compression chances
95- data = hamlet_scene * 8 * 16
81+ # compress more data
82+ data = HAMLET_SCENE * 128
9683 x = zlib .compress (data )
9784 self .assertEqual (zlib .decompress (x ), data )
9885
@@ -101,22 +88,10 @@ def test_speech128(self):
10188
10289class CompressObjectTestCase (unittest .TestCase ):
10390 # Test compression object
104- def test_pairsmall (self ):
105- # use compress object in straightforward manner, decompress w/ object
106- data = hamlet_scene
107- co = zlib .compressobj ()
108- x1 = co .compress (data )
109- x2 = co .flush ()
110- self .assertRaises (zlib .error , co .flush ) # second flush should not work
111- dco = zlib .decompressobj ()
112- y1 = dco .decompress (x1 + x2 )
113- y2 = dco .flush ()
114- self .assertEqual (data , y1 + y2 )
115-
11691 def test_pair (self ):
117- # straightforward compress/decompress objects, more compression
118- data = hamlet_scene * 8 * 16
119- co = zlib .compressobj (zlib . Z_BEST_COMPRESSION , zlib . DEFLATED )
92+ # straightforward compress/decompress objects
93+ data = HAMLET_SCENE * 128
94+ co = zlib .compressobj ()
12095 x1 = co .compress (data )
12196 x2 = co .flush ()
12297 self .assertRaises (zlib .error , co .flush ) # second flush should not work
@@ -133,16 +108,16 @@ def test_compressoptions(self):
133108 memlevel = 9
134109 strategy = zlib .Z_FILTERED
135110 co = zlib .compressobj (level , method , wbits , memlevel , strategy )
136- x1 = co .compress (hamlet_scene )
111+ x1 = co .compress (HAMLET_SCENE )
137112 x2 = co .flush ()
138113 dco = zlib .decompressobj (wbits )
139114 y1 = dco .decompress (x1 + x2 )
140115 y2 = dco .flush ()
141- self .assertEqual (hamlet_scene , y1 + y2 )
116+ self .assertEqual (HAMLET_SCENE , y1 + y2 )
142117
143118 def test_compressincremental (self ):
144119 # compress object in steps, decompress object as one-shot
145- data = hamlet_scene * 8 * 16
120+ data = HAMLET_SCENE * 128
146121 co = zlib .compressobj ()
147122 bufs = []
148123 for i in range (0 , len (data ), 256 ):
@@ -155,114 +130,53 @@ def test_compressincremental(self):
155130 y2 = dco .flush ()
156131 self .assertEqual (data , y1 + y2 )
157132
158- def test_decompressincremental (self ):
133+ def test_decompinc (self , flush = False , source = None , cx = 256 , dcx = 64 ):
159134 # compress object in steps, decompress object in steps
160- data = hamlet_scene * 8 * 16
135+ source = source or HAMLET_SCENE
136+ data = source * 128
161137 co = zlib .compressobj ()
162138 bufs = []
163- for i in range (0 , len (data ), 256 ):
164- bufs .append (co .compress (data [i :i + 256 ]))
139+ for i in range (0 , len (data ), cx ):
140+ bufs .append (co .compress (data [i :i + cx ]))
165141 bufs .append (co .flush ())
166142 combuf = '' .join (bufs )
167143
168144 self .assertEqual (data , zlib .decompress (combuf ))
169145
170146 dco = zlib .decompressobj ()
171147 bufs = []
172- for i in range (0 , len (combuf ), 128 ):
173- bufs .append (dco .decompress (combuf [i :i + 128 ]))
148+ for i in range (0 , len (combuf ), dcx ):
149+ bufs .append (dco .decompress (combuf [i :i + dcx ]))
174150 self .assertEqual ('' , dco .unconsumed_tail , ########
175151 "(A) uct should be '': not %d long" %
176- len (dco .unconsumed_tail ))
177- bufs .append (dco .flush ())
152+ len (dco .unconsumed_tail ))
153+ if flush :
154+ bufs .append (dco .flush ())
155+ else :
156+ while True :
157+ chunk = dco .decompress ('' )
158+ if chunk :
159+ bufs .append (chunk )
160+ else :
161+ break
178162 self .assertEqual ('' , dco .unconsumed_tail , ########
179- "(B) uct should be '': not %d long" %
180- len (dco .unconsumed_tail ))
163+ "(B) uct should be '': not %d long" %
164+ len (dco .unconsumed_tail ))
181165 self .assertEqual (data , '' .join (bufs ))
182166 # Failure means: "decompressobj with init options failed"
183167
184- def test_decompinc (self ,sizes = [128 ],flush = True ,source = None ,cx = 256 ,dcx = 64 ):
185- # compress object in steps, decompress object in steps, loop sizes
186- source = source or hamlet_scene
187- for reps in sizes :
188- data = source * reps
189- co = zlib .compressobj ()
190- bufs = []
191- for i in range (0 , len (data ), cx ):
192- bufs .append (co .compress (data [i :i + cx ]))
193- bufs .append (co .flush ())
194- combuf = '' .join (bufs )
195-
196- self .assertEqual (data , zlib .decompress (combuf ))
168+ def test_decompincflush (self ):
169+ self .test_decompinc (flush = True )
197170
198- dco = zlib .decompressobj ()
199- bufs = []
200- for i in range (0 , len (combuf ), dcx ):
201- bufs .append (dco .decompress (combuf [i :i + dcx ]))
202- self .assertEqual ('' , dco .unconsumed_tail , ########
203- "(A) uct should be '': not %d long" %
204- len (dco .unconsumed_tail ))
205- if flush :
206- bufs .append (dco .flush ())
207- else :
208- while True :
209- chunk = dco .decompress ('' )
210- if chunk :
211- bufs .append (chunk )
212- else :
213- break
214- self .assertEqual ('' , dco .unconsumed_tail , ########
215- "(B) uct should be '': not %d long" %
216- len (dco .unconsumed_tail ))
217- self .assertEqual (data , '' .join (bufs ))
218- # Failure means: "decompressobj with init options failed"
219-
220- def test_decompimax (self ,sizes = [128 ],flush = True ,source = None ,cx = 256 ,dcx = 64 ):
221- # compress in steps, decompress in length-restricted steps, loop sizes
222- source = source or hamlet_scene
223- for reps in sizes :
224- # Check a decompression object with max_length specified
225- data = source * reps
226- co = zlib .compressobj ()
227- bufs = []
228- for i in range (0 , len (data ), cx ):
229- bufs .append (co .compress (data [i :i + cx ]))
230- bufs .append (co .flush ())
231- combuf = '' .join (bufs )
232- self .assertEqual (data , zlib .decompress (combuf ),
233- 'compressed data failure' )
234-
235- dco = zlib .decompressobj ()
236- bufs = []
237- cb = combuf
238- while cb :
239- #max_length = 1 + len(cb)//10
240- chunk = dco .decompress (cb , dcx )
241- self .failIf (len (chunk ) > dcx ,
242- 'chunk too big (%d>%d)' % (len (chunk ), dcx ))
243- bufs .append (chunk )
244- cb = dco .unconsumed_tail
245- if flush :
246- bufs .append (dco .flush ())
247- else :
248- while True :
249- chunk = dco .decompress ('' , dcx )
250- self .failIf (len (chunk ) > dcx ,
251- 'chunk too big in tail (%d>%d)' % (len (chunk ), dcx ))
252- if chunk :
253- bufs .append (chunk )
254- else :
255- break
256- self .assertEqual (len (data ), len ('' .join (bufs )))
257- self .assertEqual (data , '' .join (bufs ), 'Wrong data retrieved' )
258-
259- def test_decompressmaxlen (self ):
171+ def test_decompimax (self , source = None , cx = 256 , dcx = 64 ):
172+ # compress in steps, decompress in length-restricted steps
173+ source = source or HAMLET_SCENE
260174 # Check a decompression object with max_length specified
261- data = hamlet_scene * 8 * 16
175+ data = source * 128
262176 co = zlib .compressobj ()
263177 bufs = []
264- for i in range (0 , len (data ), 256 ):
265- bufs .append (co .compress (data [i :i + 256 ]))
178+ for i in range (0 , len (data ), cx ):
179+ bufs .append (co .compress (data [i :i + cx ]))
266180 bufs .append (co .flush ())
267181 combuf = '' .join (bufs )
268182 self .assertEqual (data , zlib .decompress (combuf ),
@@ -272,28 +186,26 @@ def test_decompressmaxlen(self):
272186 bufs = []
273187 cb = combuf
274188 while cb :
275- max_length = 1 + len (cb )// 10
276- chunk = dco .decompress (cb , max_length )
277- self .failIf (len (chunk ) > max_length ,
278- 'chunk too big (%d>%d)' % (len (chunk ),max_length ))
189+ # max_length = 1 + len(cb)//10
190+ chunk = dco .decompress (cb , dcx )
191+ self .failIf (len (chunk ) > dcx ,
192+ 'chunk too big (%d>%d)' % (len (chunk ), dcx ))
279193 bufs .append (chunk )
280194 cb = dco .unconsumed_tail
281195 bufs .append (dco .flush ())
282- self .assertEqual (len (data ), len ('' .join (bufs )))
283196 self .assertEqual (data , '' .join (bufs ), 'Wrong data retrieved' )
284197
285- def test_decompressmaxlenflushless (self ):
286- # identical to test_decompressmaxlen except flush is replaced
287- # with an equivalent. This works and other fails on (eg) 2.2.2
288- data = hamlet_scene * 8 * 16
198+ def test_decompressmaxlen (self , flush = False ):
199+ # Check a decompression object with max_length specified
200+ data = HAMLET_SCENE * 128
289201 co = zlib .compressobj ()
290202 bufs = []
291203 for i in range (0 , len (data ), 256 ):
292204 bufs .append (co .compress (data [i :i + 256 ]))
293205 bufs .append (co .flush ())
294206 combuf = '' .join (bufs )
295207 self .assertEqual (data , zlib .decompress (combuf ),
296- 'compressed data mismatch ' )
208+ 'compressed data failure ' )
297209
298210 dco = zlib .decompressobj ()
299211 bufs = []
@@ -305,16 +217,19 @@ def test_decompressmaxlenflushless(self):
305217 'chunk too big (%d>%d)' % (len (chunk ),max_length ))
306218 bufs .append (chunk )
307219 cb = dco .unconsumed_tail
308-
309- # bufs.append(dco.flush())
310- while len ( chunk ) :
311- chunk = dco . decompress ( '' , max_length )
312- self . failIf ( len ( chunk ) > max_length ,
313- 'chunk too big (%d>%d)' % ( len (chunk ), max_length ))
314- bufs . append ( chunk )
315-
220+ if flush :
221+ bufs .append (dco .flush ())
222+ else :
223+ while chunk :
224+ chunk = dco . decompress ( '' , max_length )
225+ self . failIf ( len (chunk ) > max_length ,
226+ 'chunk too big (%d>%d)' % ( len ( chunk ), max_length ) )
227+ bufs . append ( chunk )
316228 self .assertEqual (data , '' .join (bufs ), 'Wrong data retrieved' )
317229
230+ def test_decompressmaxlenflush (self ):
231+ self .test_decompressmaxlen (flush = True )
232+
318233 def test_maxlenmisc (self ):
319234 # Misc tests of max_length
320235 dco = zlib .decompressobj ()
@@ -327,7 +242,7 @@ def test_flushes(self):
327242 sync_opt = ['Z_NO_FLUSH' , 'Z_SYNC_FLUSH' , 'Z_FULL_FLUSH' ]
328243 sync_opt = [getattr (zlib , opt ) for opt in sync_opt
329244 if hasattr (zlib , opt )]
330- data = hamlet_scene * 8
245+ data = HAMLET_SCENE * 8
331246
332247 for sync in sync_opt :
333248 for level in range (10 ):
@@ -349,7 +264,7 @@ def test_odd_flush(self):
349264 # Testing on 17K of "random" data
350265
351266 # Create compressor and decompressor objects
352- co = zlib .compressobj (9 )
267+ co = zlib .compressobj (zlib . Z_BEST_COMPRESSION )
353268 dco = zlib .decompressobj ()
354269
355270 # Try 17K of data
@@ -375,23 +290,6 @@ def test_odd_flush(self):
375290 # if decompressed data is different from the input data, choke.
376291 self .assertEqual (expanded , data , "17K random source doesn't match" )
377292
378- def test_manydecompinc (self ):
379- # Run incremental decompress test for a large range of sizes
380- self .test_decompinc (sizes = [1 << n for n in range (8 )],
381- flush = True , cx = 32 , dcx = 4 )
382-
383- def test_manydecompimax (self ):
384- # Run incremental decompress maxlen test for a large range of sizes
385- # avoid the flush bug
386- self .test_decompimax (sizes = [1 << n for n in range (8 )],
387- flush = False , cx = 32 , dcx = 4 )
388-
389- def test_manydecompimaxflush (self ):
390- # Run incremental decompress maxlen test for a large range of sizes
391- # avoid the flush bug
392- self .test_decompimax (sizes = [1 << n for n in range (8 )],
393- flush = True , cx = 32 , dcx = 4 )
394-
395293
396294def genblock (seed , length , step = 1024 , generator = random ):
397295 """length-byte stream of random data from a seed (in step-byte blocks)."""
@@ -417,7 +315,7 @@ def choose_lines(source, number, seed=None, generator=random):
417315
418316
419317
420- hamlet_scene = """
318+ HAMLET_SCENE = """
421319LAERTES
422320
423321 O, fear me not.
0 commit comments