Skip to content

Commit aa46ccd

Browse files
authored
Document and test windowbits option (#95)
1 parent 45b4db3 commit aa46ccd

File tree

3 files changed

+51
-25
lines changed

3 files changed

+51
-25
lines changed

src/compression.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ Create a gzip compression codec.
2424
2525
Arguments
2626
---------
27-
- `level`: compression level (-1..9)
28-
- `windowbits`: size of history buffer (9..15)
27+
- `level` (-1..9): compression level. 1 gives best speed, 9 gives best compression, 0 gives no compression at all (the input data is simply copied a block at a time). -1 requests a default compromise between speed and compression (currently equivalent to level 6).
28+
- `windowbits` (9..15): size of history buffer is `2^windowbits`.
2929
3030
!!! warning
3131
`serialize` and `deepcopy` will not work with this codec due to stored raw pointers.
@@ -72,8 +72,8 @@ Create a zlib compression codec.
7272
7373
Arguments
7474
---------
75-
- `level`: compression level (-1..9)
76-
- `windowbits`: size of history buffer (9..15)
75+
- `level` (-1..9): compression level. 1 gives best speed, 9 gives best compression, 0 gives no compression at all (the input data is simply copied a block at a time). -1 requests a default compromise between speed and compression (currently equivalent to level 6).
76+
- `windowbits` (9..15): size of history buffer is `2^windowbits`.
7777
7878
!!! warning
7979
`serialize` and `deepcopy` will not work with this codec due to stored raw pointers.
@@ -120,8 +120,8 @@ Create a deflate compression codec.
120120
121121
Arguments
122122
---------
123-
- `level`: compression level (-1..9)
124-
- `windowbits`: size of history buffer (9..15)
123+
- `level` (-1..9): compression level. 1 gives best speed, 9 gives best compression, 0 gives no compression at all (the input data is simply copied a block at a time). -1 requests a default compromise between speed and compression (currently equivalent to level 6).
124+
- `windowbits` (9..15): size of history buffer is `2^windowbits`.
125125
126126
!!! warning
127127
`serialize` and `deepcopy` will not work with this codec due to stored raw pointers.

src/decompression.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ If `gziponly` is `false`, this codec can decompress the zlib format as well.
2525
2626
Arguments
2727
---------
28-
- `windowbits`: size of history buffer (8..15)
28+
- `windowbits` (8..15): Changing `windowbits` from its default of 15 will prevent decoding data using a history buffer larger than `2^windowbits`.
2929
- `gziponly`: flag to inactivate data format detection
3030
3131
!!! warning
@@ -69,7 +69,7 @@ Create a zlib decompression codec.
6969
7070
Arguments
7171
---------
72-
- `windowbits`: size of history buffer (8..15)
72+
- `windowbits` (8..15): Changing `windowbits` from its default of 15 will prevent decoding data using a history buffer larger than `2^windowbits`.
7373
7474
!!! warning
7575
`serialize` and `deepcopy` will not work with this codec due to stored raw pointers.
@@ -112,7 +112,7 @@ Create a deflate decompression codec.
112112
113113
Arguments
114114
---------
115-
- `windowbits`: size of history buffer (8..15)
115+
- `windowbits` (8..15): Changing `windowbits` from its default of 15 will prevent decoding data using a history buffer larger than `2^windowbits`.
116116
117117
!!! warning
118118
`serialize` and `deepcopy` will not work with this codec due to stored raw pointers.

test/runtests.jl

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,17 @@ function decompress_bytes(decoder, data::Vector{UInt8})::Vector{UInt8}
3333
take!(io)
3434
end
3535

36+
# generate random data to test compression
37+
function generate_data()
38+
thing = rand(UInt8, 100)
39+
d = UInt8[]
40+
for dist in [0:258; 400:200:2000; 2000:1000:33000;]
41+
append!(d, thing)
42+
append!(d, rand(0x00:0x0f, dist))
43+
end
44+
d
45+
end
46+
3647
@testset "Gzip Codec" begin
3748
codec = GzipCompressor()
3849
@test codec isa GzipCompressor
@@ -133,8 +144,6 @@ end
133144
test_reuse_encoder(GzipCompressor, GzipDecompressor)
134145

135146
@test_throws ArgumentError GzipCompressor(level=10)
136-
@test_throws ArgumentError GzipCompressor(windowbits=16)
137-
@test_throws ArgumentError GzipDecompressor(windowbits=16)
138147
end
139148

140149
@testset "Zlib Codec" begin
@@ -215,8 +224,6 @@ end
215224
test_reuse_encoder(ZlibCompressor, ZlibDecompressor)
216225

217226
@test_throws ArgumentError ZlibCompressor(level=10)
218-
@test_throws ArgumentError ZlibCompressor(windowbits=16)
219-
@test_throws ArgumentError ZlibDecompressor(windowbits=16)
220227
end
221228

222229
@testset "Deflate Codec" begin
@@ -243,21 +250,40 @@ end
243250
@test DeflateDecompressorStream <: TranscodingStream
244251

245252
@test_throws ArgumentError DeflateCompressor(level=10)
246-
@test_throws ArgumentError DeflateCompressor(windowbits=16)
247-
@test_throws ArgumentError DeflateDecompressor(windowbits=16)
248253

249254
# 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))
255+
d = generate_data()
256+
c = transcode(DeflateCompressor, d)
257+
@test transcode(DeflateDecompressor, c) == d
258+
@test decompress_bytes(DeflateDecompressorStream, c) == d
259+
end
260+
261+
@testset "roundtrip windowbits" begin
262+
d = generate_data()
263+
for (encoder, decoder) in [
264+
(GzipCompressorStream, GzipDecompressorStream),
265+
(ZlibCompressorStream, ZlibDecompressorStream),
266+
(DeflateCompressorStream, DeflateDecompressorStream),
267+
]
268+
for compression_windowbits in 9:15
269+
for decompression_windowbits in 8:15
270+
c = read(encoder(IOBuffer(d); windowbits=compression_windowbits, level=9))
271+
if compression_windowbits decompression_windowbits
272+
@test d == read(decoder(IOBuffer(c); windowbits=decompression_windowbits))
273+
else
274+
try
275+
u = read(decoder(IOBuffer(c); windowbits=decompression_windowbits))
276+
@test u == d
277+
catch e
278+
@test e isa ZlibError
279+
end
280+
end
281+
end
257282
end
258-
c = transcode(DeflateCompressor, d)
259-
@test transcode(DeflateDecompressor, c) == d
260-
@test decompress_bytes(DeflateDecompressorStream, c) == d
283+
@test_throws ArgumentError encoder(IOBuffer(d); windowbits=8)
284+
@test_throws ArgumentError decoder(IOBuffer(d); windowbits=7)
285+
@test_throws ArgumentError encoder(IOBuffer(d); windowbits=16)
286+
@test_throws ArgumentError decoder(IOBuffer(d); windowbits=16)
261287
end
262288
end
263289

0 commit comments

Comments
 (0)