Skip to content

Commit

Permalink
Add test_roundtrip_seekstart function to help test that seekstart
Browse files Browse the repository at this point in the history
… resets the state of the codec (#218)
  • Loading branch information
nhz2 authored May 30, 2024
1 parent 7893d74 commit 9f342c6
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 0 deletions.
18 changes: 18 additions & 0 deletions ext/TestExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,24 @@ function TranscodingStreams.test_roundtrip_lines(encoder, decoder)
Test.@test hash(lines) == hash(readlines(decoder(buf)))
end

function TranscodingStreams.test_roundtrip_seekstart(encoder, decoder)
seed!(TEST_RANDOM_SEED)
for n in vcat(0:30, sort!(rand(500:100_000, 30))), alpha in (0x00:0xff, 0x00:0x0f)
data = rand(alpha, n)
file = IOBuffer(data)
stream = decoder(encoder(file))
for m in vcat(0:min(n,20), rand(0:n, 10))
Test.@test read(stream, m) == @view(data[1:m])
seekstart(stream)
end
seekstart(stream)
Test.@test read(stream) == data
seekstart(stream)
Test.@test read(stream) == data
close(stream)
end
end

function TranscodingStreams.test_roundtrip_fileio(Encoder, Decoder)
data = b"""
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla sit amet tempus felis. Etiam molestie urna placerat iaculis pellentesque. Maecenas porttitor et dolor vitae posuere. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc eget nibh quam. Nullam aliquet interdum fringilla. Duis facilisis, lectus in consectetur varius, lorem sem tempor diam, nec auctor tellus nibh sit amet sapien. In ex nunc, elementum eget facilisis ut, luctus eu orci. Sed sapien urna, accumsan et elit non, auctor pretium massa. Phasellus consectetur nisi suscipit blandit aliquam. Nulla facilisi. Mauris pellentesque sem sit amet mi vestibulum eleifend. Nulla faucibus orci ac lorem efficitur, et blandit orci interdum. Aenean posuere ultrices ex sed rhoncus. Donec malesuada mollis sem, sed varius nunc sodales sed. Curabitur lobortis non justo non tristique.
Expand Down
1 change: 1 addition & 0 deletions src/TranscodingStreams.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ function test_roundtrip_read end
function test_roundtrip_write end
function test_roundtrip_transcode end
function test_roundtrip_lines end
function test_roundtrip_seekstart end
function test_roundtrip_fileio end
function test_chunked_read end
function test_chunked_write end
Expand Down
2 changes: 2 additions & 0 deletions test/codecdoubleframe.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ using TranscodingStreams:
test_roundtrip_read,
test_roundtrip_write,
test_roundtrip_lines,
test_roundtrip_seekstart,
test_roundtrip_transcode,
test_roundtrip_fileio,
test_chunked_read,
Expand Down Expand Up @@ -305,6 +306,7 @@ DoubleFrameDecoderStream(stream::IO; kwargs...) = TranscodingStream(DoubleFrameD
test_roundtrip_read(DoubleFrameEncoderStream, DoubleFrameDecoderStream)
test_roundtrip_write(DoubleFrameEncoderStream, DoubleFrameDecoderStream)
test_roundtrip_lines(DoubleFrameEncoderStream, DoubleFrameDecoderStream)
test_roundtrip_seekstart(DoubleFrameEncoderStream, DoubleFrameDecoderStream)
test_roundtrip_transcode(DoubleFrameEncoder, DoubleFrameDecoder)
test_roundtrip_fileio(DoubleFrameEncoder, DoubleFrameDecoder)
test_chunked_read(DoubleFrameEncoder, DoubleFrameDecoder)
Expand Down
1 change: 1 addition & 0 deletions test/codecnoop.jl
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ using FillArrays: Zeros
TranscodingStreams.test_roundtrip_read(NoopStream, NoopStream)
TranscodingStreams.test_roundtrip_write(NoopStream, NoopStream)
TranscodingStreams.test_roundtrip_lines(NoopStream, NoopStream)
TranscodingStreams.test_roundtrip_seekstart(NoopStream, NoopStream)

@testset "switch write => read" begin
stream = NoopStream(IOBuffer(collect(b"foobar"), read=true, write=true))
Expand Down

0 comments on commit 9f342c6

Please sign in to comment.