Skip to content

Commit 45b4db3

Browse files
authored
Add more tests (#94)
* test Z_NEED_DICT error * test large input sizes * test reading one byte at a time * fix test for 1.6
1 parent 5c61dc6 commit 45b4db3

3 files changed

Lines changed: 103 additions & 2 deletions

File tree

test/Project.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,6 @@ CodecZlib = "944b1d66-785c-5afd-91f1-9de20f533193"
44
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
55
TestsForCodecPackages = "c2e61002-3542-480d-8b3c-5f05cc4f8554"
66
TranscodingStreams = "3bb67fe8-82b1-5028-8e26-92a6c54297fa"
7+
8+
[sources]
9+
CodecZlib = {path = ".."}

test/big-mem-tests.jl

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# This file contains tests that require a large amount of memory (at least 25 GB)
2+
# and take a long time to run. The tests are designed to check the
3+
# compression and decompression functionality of the package
4+
# with very large inputs. These tests are not run with CI
5+
6+
using Test
7+
using CodecZlib
8+
9+
# Enable this when https://github.com/JuliaIO/CodecZlib.jl/issues/88 is fixed.
10+
# @testset "memory leak" begin
11+
# function foo()
12+
# for i in 1:1000000
13+
# c = transcode(GzipCompressor(), zeros(UInt8,16))
14+
# u = transcode(GzipDecompressor(), c)
15+
# end
16+
# end
17+
# foo()
18+
# end
19+
20+
@testset "Big Memory Tests" begin
21+
Sys.WORD_SIZE == 64 || error("tests require 64 bit word size")
22+
@info "compressing zeros"
23+
for n in (2^32 - 1, 2^32, 2^32 +1)
24+
@info "compressing"
25+
local c = transcode(GzipCompressor, zeros(UInt8, n))
26+
@info "decompressing"
27+
local u = transcode(GzipDecompressor, c)
28+
c = nothing
29+
all_zero = all(iszero, u)
30+
len_n = length(u) == n
31+
@test all_zero && len_n
32+
end
33+
34+
@info "compressing random"
35+
for n in (2^32 - 1, 2^32, 2^32 +1)
36+
local u = rand(UInt8, n)
37+
@info "compressing"
38+
local c = transcode(GzipCompressor, u)
39+
@info "decompressing"
40+
local u2 = transcode(GzipDecompressor, c)
41+
c = nothing
42+
are_equal = u == u2
43+
@test are_equal
44+
end
45+
46+
@info "decompressing huge concatenation"
47+
uncompressed = rand(UInt8, 2^20)
48+
@info "compressing"
49+
compressed = transcode(GzipCompressor, uncompressed)
50+
total_compressed = UInt8[]
51+
sizehint!(total_compressed, length(compressed)*2^12)
52+
total_uncompressed = UInt8[]
53+
sizehint!(total_uncompressed, length(uncompressed)*2^12)
54+
for i in 1:2^12
55+
append!(total_uncompressed, uncompressed)
56+
append!(total_compressed, compressed)
57+
end
58+
@test length(total_compressed) > 2^32
59+
@info "decompressing"
60+
@test total_uncompressed == transcode(GzipDecompressor, total_compressed)
61+
end

test/runtests.jl

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,19 @@ Aqua.test_all(CodecZlib)
2020

2121
const 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
@@ -211,8 +224,7 @@ end
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
@@ -233,6 +245,20 @@ end
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
236262
end
237263

238264
# Test APIs of TranscodingStreams.jl using the gzip compressor/decompressor.
@@ -329,6 +355,17 @@ end
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
332369
end
333370
@testset "error printing" begin
334371
@test sprint(Base.showerror, ZlibError("test error message")) ==

0 commit comments

Comments
 (0)