Skip to content

Commit

Permalink
Add missing ready_to_read! to unread (#228)
Browse files Browse the repository at this point in the history
  • Loading branch information
nhz2 authored Jun 24, 2024
1 parent 2e7f5fc commit 0875466
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 1 deletion.
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

0 comments on commit 0875466

Please sign in to comment.