Skip to content

Commit

Permalink
apply some suggestions and widen tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nhz2 committed Mar 24, 2024
1 parent f5858a2 commit 9c95f19
Showing 1 changed file with 62 additions and 53 deletions.
115 changes: 62 additions & 53 deletions fuzz/fuzz.jl
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
# Basic Supposition.jl tests to ensure nested streams can be read and written to.

include("../test/codecdoubleframe.jl")
using Supposition: Data, @composed, @check, event!
using Supposition: Data, @composed, @check, event!, produce!

datas = Data.Vectors(Data.Integers{UInt8}())
include("../test/codecdoubleframe.jl")

# Possible kwargs for TranscodingStream constructor
TS_kwarg = @composed (
const TS_kwarg = @composed (
bufsize=Data.Integers(1, 2^10),
stop_on_end=Data.Booleans(),
sharedbuf=Data.Booleans(),
Expand All @@ -20,58 +18,61 @@ TS_kwarg = @composed (
end
)

# Possible NoopStream wrapper
noop_wrapper = @composed (
kw=TS_kwarg,
) -> (function noop_wrapper(io)
event!("noop", kw)
NoopStream(io; kw...)
end)
const datas = Data.Vectors(Data.Integers{UInt8}())
const noopcodecs = Data.Vectors(Data.Just(Noop); max_size=3)

# Possible Encoder Decoder wrapper
dec_enc_wrapper = @composed (
kw_enc=TS_kwarg,
kw_dec=TS_kwarg,
) -> (function r_enc_dec_wrapper(io)
event!("encoder", kw_enc)
event!("decoder", kw_dec)
DoubleFrameDecoderStream(DoubleFrameEncoderStream(io; kw_enc...); kw_dec...)
end)
enc_dec_wrapper = @composed (
kw_enc=TS_kwarg,
kw_dec=TS_kwarg,
) -> (function w_enc_dec_wrapper(io)
event!("decoder", kw_dec)
event!("encoder", kw_enc)
DoubleFrameEncoderStream(DoubleFrameDecoderStream(io; kw_dec...); kw_enc...)
end)
function codecwrap(child)
map(child) do cs
DataType[
DoubleFrameEncoder;
cs;
DoubleFrameDecoder;
]
end | map(child) do cs
DataType[
cs;
produce!(child);
]
end
end

# Possible deeply nested wrappers
read_wrapper = @composed (
w = Data.Vectors(noop_wrapper | dec_enc_wrapper; max_size=5)
) -> (function read_wrapper(io)
event!("wrapping IOBuffer with:", nothing)
((identity, w...))(io)
end)
write_wrapper = @composed (
w = Data.Vectors(noop_wrapper | enc_dec_wrapper; max_size=5)
) -> (function write_wrapper(io)
event!("wrapping IOBuffer with:", nothing)
((identity, w...))(io)
end)
# possible vector of codec types that when read from
# should be equivalent to a Noop.
# Every encoder is balanced with a decoder.
const codecs = Data.Recursive(noopcodecs, codecwrap; max_layers=4)

@check max_examples=100000 function read_data(w=read_wrapper, data=datas)
stream = w(IOBuffer(data))
read(stream) == data || return false
eof(stream)
function prepare_kws(codecs)
# res will be a nicely printed summary of the layers of the stream.
res = []
for codec in codecs
kw = Data.produce!(TS_kwarg)
push!(res, (codec, kw))
end
res
end
@check max_examples=100000 function read_byte_data(w=read_wrapper, data=datas)
stream = w(IOBuffer(data))
for i in 1:length(data)

const read_codecs_kws = map(prepare_kws, codecs)

function wrap_stream(codecs_kws, io::IO)::IO
event!("IOBuffer:", nothing)
foldl(codecs_kws; init=io) do stream, (codec, kw)
event!("codec:", (codec, kw))
TranscodingStream(codec(), stream; kw...)
end
end

@check db=false function read_byte_data(kws=read_codecs_kws, data=datas)
stream = wrap_stream(kws, IOBuffer(data))
for i in eachindex(data)
read(stream, UInt8) == data[i] || return false
end
eof(stream)
end
@check db=false function read_byte_data(kws=read_codecs_kws, data=datas)
stream = wrap_stream(kws, IOBuffer(data))
read(stream) == data || return false
eof(stream)
end

# flush all nested streams and return final data
function take_all(stream)
Expand All @@ -84,13 +85,21 @@ function take_all(stream)
end
end

@check max_examples=100000 function write_data(w=write_wrapper, data=datas)
stream = w(IOBuffer())
const write_codecs_kws = map(reverse, read_codecs_kws)

@check db=false function write_data(
kws=write_codecs_kws,
data=datas,
)
stream = wrap_stream(kws, IOBuffer())
write(stream, data) == length(data) || return false
take_all(stream) == data
end
@check max_examples=100000 function write_byte_data(w=write_wrapper, data=datas)
stream = w(IOBuffer())
@check db=false function write_byte_data(
kws=write_codecs_kws,
data=datas,
)
stream = wrap_stream(kws, IOBuffer())
for i in 1:length(data)
write(stream, data[i]) == 1 || return false
end
Expand Down

0 comments on commit 9c95f19

Please sign in to comment.