Skip to content

Commit

Permalink
test reading one byte at a time
Browse files Browse the repository at this point in the history
  • Loading branch information
nhz2 committed Jul 25, 2024
1 parent 38055a8 commit 3679c7c
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
6 changes: 6 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ include("tests_from_inflate.jl")
end
@test decompress(zlib_compress(d)) == d
@test decompress(p7zip_compress(d)) == d
@test decompress_bytes(zlib_compress(d)) == d
@test decompress_bytes(p7zip_compress(d)) == d
@test de64compress(p7zip_64compress(d)) == d
@test de64compress_bytes(p7zip_64compress(d)) == d
@test_throws ErrorException de64compress(p7zip_64compress(d)[begin:end-1])

for n in 65536-1000:65536+1000
Expand All @@ -34,7 +37,10 @@ include("tests_from_inflate.jl")
d = zeros(UInt8, n)
@test decompress(zlib_compress(d)) == d
@test decompress(p7zip_compress(d)) == d
@test decompress_bytes(zlib_compress(d)) == d
@test decompress_bytes(p7zip_compress(d)) == d
@test de64compress(p7zip_64compress(d)) == d
@test de64compress_bytes(p7zip_64compress(d)) == d
@test_throws ErrorException de64compress(p7zip_64compress(d)[begin:end-1])
end
end
Expand Down
12 changes: 12 additions & 0 deletions test/tests_from_inflate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ long_string = join(fill(medium_string, 1000), short_string)
d = collect(codeunits(s))
@test decompress(zlib_compress(d)) == d
@test decompress(p7zip_compress(d)) == d
@test decompress_bytes(zlib_compress(d)) == d
@test decompress_bytes(p7zip_compress(d)) == d
@test de64compress(p7zip_64compress(d)) == d
@test de64compress_bytes(p7zip_64compress(d)) == d
end
end

Expand All @@ -45,7 +48,10 @@ end
d = rand(UInt8, n)
@test decompress(zlib_compress(d)) == d
@test decompress(p7zip_compress(d)) == d
@test decompress_bytes(zlib_compress(d)) == d
@test decompress_bytes(p7zip_compress(d)) == d
@test de64compress(p7zip_64compress(d)) == d
@test de64compress_bytes(p7zip_64compress(d)) == d
end
end

Expand All @@ -55,7 +61,10 @@ end
d = rand(UInt8, n) .& 0x0f
@test decompress(zlib_compress(d)) == d
@test decompress(p7zip_compress(d)) == d
@test decompress_bytes(zlib_compress(d)) == d
@test decompress_bytes(p7zip_compress(d)) == d
@test de64compress(p7zip_64compress(d)) == d
@test de64compress_bytes(p7zip_64compress(d)) == d
end
end

Expand All @@ -65,6 +74,7 @@ empty_deflate = [0x03, 0x00]
@testset "Empty messages" begin
@test decompress(empty_deflate) == UInt8[]
@test de64compress(empty_deflate) == UInt8[]
@test de64compress_bytes(empty_deflate) == UInt8[]
end

@testset "Deflate corruption" begin
Expand All @@ -74,7 +84,9 @@ end
0xdb, 0x48, 0xf2, 0x3f] # incomplete code table
for d in [d1, d2, d3]
@test_throws ErrorException decompress(d)
@test_throws ErrorException decompress_bytes(d)
@test_throws ErrorException de64compress(d)
@test_throws ErrorException de64compress_bytes(d)
end
end

Expand Down
29 changes: 29 additions & 0 deletions test/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,35 @@ function de64compress(data::Vector{UInt8})::Vector{UInt8}
transcode(Deflate64Decompressor, data)
end

# decompress one byte at a time
function decompress_bytes(data::Vector{UInt8})::Vector{UInt8}
io = IOBuffer()
s = DeflateDecompressorStream(io; bufsize=1)
for i in eachindex(data)
write(s, data[i])
flush(s)
end
write(s, TranscodingStreams.TOKEN_END)
flush(s)
take!(io)
end

function de64compress(data::Vector{UInt8})::Vector{UInt8}
transcode(Deflate64Decompressor, data)
end

function de64compress_bytes(data::Vector{UInt8})::Vector{UInt8}
io = IOBuffer()
s = Deflate64DecompressorStream(io; bufsize=1)
for i in eachindex(data)
write(s, data[i])
flush(s)
end
write(s, TranscodingStreams.TOKEN_END)
flush(s)
take!(io)
end

function checkcrc32_zipfile(zipfile::String)
data = read(zipfile)
r = ZipReader(data)
Expand Down

0 comments on commit 3679c7c

Please sign in to comment.