Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add basic Supposition.jl tests #190

Merged
merged 4 commits into from
Mar 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@
/Manifest.toml
/docs/build/
/docs/Manifest.toml
/fuzz/test/
/fuzz/Manifest.toml
8 changes: 8 additions & 0 deletions fuzz/Project.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[deps]
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Supposition = "5a0628fe-1738-4658-9b6d-0b7605a9755b"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
TranscodingStreams = "3bb67fe8-82b1-5028-8e26-92a6c54297fa"

[compat]
Supposition = "0.3"
110 changes: 110 additions & 0 deletions fuzz/fuzz.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
# Basic Supposition.jl tests to ensure nested streams can be read and written to.

using Supposition: Data, @composed, @check, event!, produce!

include("../test/codecdoubleframe.jl")

const TS_kwarg = @composed (
bufsize=Data.Integers(1, 2^10),
stop_on_end=Data.Booleans(),
sharedbuf=Data.Booleans(),
) -> (
if sharedbuf
# default sharedbuf
(;bufsize, stop_on_end)
else
# sharedbuf = false
(;bufsize, stop_on_end, sharedbuf)
end
)

const datas = Data.Vectors(Data.Integers{UInt8}())
const noopcodecs = Data.Vectors(Data.Just(Noop); max_size=3)

function codecwrap(child)
map(child) do x
DataType[
DoubleFrameEncoder;
x;
DoubleFrameDecoder;
]
end | map(Data.Vectors(child; min_size=2, max_size=2)) do x
reduce(vcat, x)
end
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)

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

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 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 function read_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)
if stream isa Base.GenericIOBuffer
take!(stream)
else
write(stream, TranscodingStreams.TOKEN_END)
flush(stream)
take_all(stream.stream)
end
end

const write_codecs_kws = map(reverse, read_codecs_kws)

@check 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 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
take_all(stream) == data
end
20 changes: 20 additions & 0 deletions test/codecdoubleframe.jl
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,26 @@ DoubleFrameDecoderStream(stream::IO; kwargs...) = TranscodingStream(DoubleFrameD
@test String(take!(sink)) == "[ ][ aabbcc ][ ddee ]"
end

@testset "stop_on_end=true in nested streams" begin
s1 = DoubleFrameDecoderStream(DoubleFrameEncoderStream(
DoubleFrameDecoderStream(
DoubleFrameEncoderStream(IOBuffer(b""));
stop_on_end=true,
)
))
@test_broken read(s1) == b""
@test_broken eof(s1)

s2 = NoopStream(
DoubleFrameDecoderStream(
DoubleFrameEncoderStream(IOBuffer(b""));
stop_on_end=true,
)
)
@test read(s2) == b""
@test_broken eof(s2)
end

test_roundtrip_read(DoubleFrameEncoderStream, DoubleFrameDecoderStream)
test_roundtrip_write(DoubleFrameEncoderStream, DoubleFrameDecoderStream)
test_roundtrip_lines(DoubleFrameEncoderStream, DoubleFrameDecoderStream)
Expand Down
Loading