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 missing ready_to_read! to unread #228

Merged
merged 1 commit into from
Jun 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: 1 addition & 1 deletion src/buffer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ end

# Insert data to the current buffer.
# `data` must not alias `buf`
function insertdata!(buf::Buffer, data::Union{AbstractArray{UInt8}, Memory})
function insertdata!(buf::Buffer, data::Union{AbstractVector{UInt8}, Memory})
nbytes = Int(length(data))
makemargin!(buf, nbytes)
datapos = if iszero(buf.markpos)
Expand Down
1 change: 1 addition & 0 deletions src/stream.jl
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,7 @@ inserted.
`data` must not alias any internal buffers in `stream`
"""
function unread(stream::TranscodingStream, data::AbstractVector{UInt8})
ready_to_read!(stream)
insertdata!(stream.buffer1, data)
return nothing
end
Expand Down
16 changes: 16 additions & 0 deletions test/codecdoubleframe.jl
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,22 @@ DoubleFrameDecoderStream(stream::IO; kwargs...) = TranscodingStream(DoubleFrameD
close(stream)
end

@testset "unread" begin
stream = DoubleFrameDecoderStream(IOBuffer("[ ffoooobbaarr ]"))
@test position(stream) == 0
@test read(stream, 3) == b"foo"
@test position(stream) == 3
@test read(stream, 3) == b"bar"
@test position(stream) == 6
@test TranscodingStreams.unread(stream, b"baz") === nothing
@test position(stream) == 3
@test read(stream, 3) == b"baz"
@test position(stream) == 6
@test eof(stream)
@test position(stream) == 6
close(stream)
end

test_roundtrip_read(DoubleFrameEncoderStream, DoubleFrameDecoderStream)
test_roundtrip_write(DoubleFrameEncoderStream, DoubleFrameDecoderStream)
test_roundtrip_lines(DoubleFrameEncoderStream, DoubleFrameDecoderStream)
Expand Down
24 changes: 24 additions & 0 deletions test/codecnoop.jl
Original file line number Diff line number Diff line change
Expand Up @@ -325,13 +325,20 @@ using FillArrays: Zeros
@testset "unread" begin
stream = NoopStream(IOBuffer(""))
@test TranscodingStreams.unread(stream, b"foo") === nothing
@test position(stream) == -3
@test read(stream, 3) == b"foo"
@test position(stream) == 0
@test eof(stream)
close(stream)

stream = NoopStream(IOBuffer("foo"))
@test read(stream, 3) == b"foo"
@test position(stream) == 3
@test TranscodingStreams.unread(stream, b"bar") === nothing
@test position(stream) == 0
@test read(stream, 3) == b"bar"
@test position(stream) == 3
@test eof(stream)
close(stream)

stream = NoopStream(IOBuffer("foobar"))
Expand All @@ -351,24 +358,36 @@ using FillArrays: Zeros
close(stream)

stream = NoopStream(IOBuffer("foobar"))
@test position(stream) == 0
@test read(stream, 3) == b"foo"
@test position(stream) == 3
@test read(stream, 3) == b"bar"
@test position(stream) == 6
@test TranscodingStreams.unread(stream, b"baz") === nothing
@test position(stream) == 3
@test read(stream, 3) == b"baz"
@test position(stream) == 6
@test eof(stream)
@test position(stream) == 6
close(stream)

for bufsize in (1, 2, 3, 4, 100)
for n in (1, 100)
stream = NoopStream(IOBuffer("foo"^n*"bar"^n); bufsize)
@test mark(stream) == 0
@test position(stream) == 0
@test read(stream, 3n) == codeunits("foo"^n)
@test read(stream, 3n) == codeunits("bar"^n)
@test position(stream) == 6n
TranscodingStreams.unread(stream, codeunits("baz"^n))
@test position(stream) == 3n
@test reset(stream) == 0
@test position(stream) == 0
@test read(stream, 3n) == codeunits("foo"^n)
@test read(stream, 3n) == codeunits("baz"^n)
@test position(stream) == 6n
@test eof(stream)
@test position(stream) == 6n
close(stream)
end
end
Expand Down Expand Up @@ -414,6 +433,11 @@ using FillArrays: Zeros
end
@test read(stream, 3) == b"bar"
close(stream)

stream = NoopStream(IOBuffer())
write(stream, b"foo")
@test_throws ArgumentError TranscodingStreams.unread(stream, b"bar")
close(stream)
end

stream = NoopStream(IOBuffer(""))
Expand Down
Loading