@@ -20,6 +20,19 @@ Aqua.test_all(CodecZlib)
2020
2121const testdir = @__DIR__
2222
23+ # decompress one byte at a time
24+ function decompress_bytes (decoder, data:: Vector{UInt8} ):: Vector{UInt8}
25+ io = IOBuffer ()
26+ s = decoder (io; bufsize= 1 )
27+ for i in eachindex (data)
28+ write (s, data[i])
29+ flush (s)
30+ end
31+ write (s, TranscodingStreams. TOKEN_END)
32+ flush (s)
33+ take! (io)
34+ end
35+
2336@testset " Gzip Codec" begin
2437 codec = GzipCompressor ()
2538 @test codec isa GzipCompressor
211224 @test codec isa DeflateCompressor
212225 @test occursin (r" ^(CodecZlib\. )?DeflateCompressor\( level=-1, windowbits=-\d +\) $" , sprint (show, codec))
213226 @test CodecZlib. initialize (codec) === nothing
214- # FIXME : This test fails.
215- # @test CodecZlib.finalize(codec) === nothing
227+ @test CodecZlib. finalize (codec) === nothing
216228
217229 codec = DeflateDecompressor ()
218230 @test codec isa DeflateDecompressor
233245 @test_throws ArgumentError DeflateCompressor (level= 10 )
234246 @test_throws ArgumentError DeflateCompressor (windowbits= 16 )
235247 @test_throws ArgumentError DeflateDecompressor (windowbits= 16 )
248+
249+ # Test decoding byte by byte
250+ # Exercise Deflate distances and lengths
251+ for len in [10 , 100 , 200 , 257 , 258 , 259 ]
252+ thing = rand (UInt8, len)
253+ d = UInt8[]
254+ for dist in [0 : 258 ; 1000 : 1030 ; 2000 : 1000 : 33000 ;]
255+ append! (d, thing)
256+ append! (d, rand (0x00 : 0x0f , dist))
257+ end
258+ c = transcode (DeflateCompressor, d)
259+ @test transcode (DeflateDecompressor, c) == d
260+ @test decompress_bytes (DeflateDecompressorStream, c) == d
261+ end
236262end
237263
238264# Test APIs of TranscodingStreams.jl using the gzip compressor/decompressor.
329355 local compressed = transcode (ZlibCompressor, uncompressed)
330356 compressed[70 ] ⊻= 0x01
331357 @test_throws ZlibError transcode (ZlibDecompressor, compressed)
358+ # Z_NEED_DICT error
359+ try
360+ transcode (
361+ ZlibDecompressor,
362+ UInt8[0x78 , 0xbb , 0x00 , 0x00 , 0x00 , 0x01 , 0x03 , 0x00 , 0x00 , 0x00 , 0x00 , 0x01 ],
363+ )
364+ @test false
365+ catch e
366+ @test e isa ZlibError
367+ @test endswith (e. msg, " (code: $(CodecZlib. Z_NEED_DICT) )" )
368+ end
332369end
333370@testset " error printing" begin
334371 @test sprint (Base. showerror, ZlibError (" test error message" )) ==
0 commit comments