Skip to content

Commit

Permalink
Add test for reusing encoder after finalize
Browse files Browse the repository at this point in the history
  • Loading branch information
nhz2 committed Dec 4, 2024
1 parent ad1f984 commit 353b02a
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/TestsForCodecPackages/Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "TestsForCodecPackages"
uuid = "c2e61002-3542-480d-8b3c-5f05cc4f8554"
authors = ["nhz2 <nhz2@cornell.edu>"]
version = "0.1.0"
version = "0.1.1"

[deps]
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Expand Down
33 changes: 32 additions & 1 deletion lib/TestsForCodecPackages/src/TestsForCodecPackages.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ export
test_roundtrip_seekstart,
test_roundtrip_fileio,
test_chunked_read,
test_chunked_write
test_chunked_write,
test_reuse_encoder

function test_roundtrip_read(encoder, decoder)
seed!(TEST_RANDOM_SEED)
Expand Down Expand Up @@ -157,4 +158,34 @@ function test_chunked_write(Encoder, Decoder)
finalize(encoder)
end

function test_reuse_encoder(Encoder, Decoder)
seed!(TEST_RANDOM_SEED)
compressor = Encoder()
x = rand(UInt8, 1000)
TranscodingStreams.initialize(compressor)
ret1 = transcode(compressor, x)
TranscodingStreams.finalize(compressor)

# compress again using the same compressor
TranscodingStreams.initialize(compressor)
ret2 = transcode(compressor, x)
ret3 = transcode(compressor, x)
TranscodingStreams.finalize(compressor)

Test.@test transcode(Decoder, ret1) == x
Test.@test transcode(Decoder, ret2) == x
Test.@test transcode(Decoder, ret3) == x
Test.@test ret1 == ret2
Test.@test ret1 == ret3

decompressor = Decoder()
TranscodingStreams.initialize(decompressor)
Test.@test transcode(decompressor, ret1) == x
TranscodingStreams.finalize(decompressor)

TranscodingStreams.initialize(decompressor)
Test.@test transcode(decompressor, ret1) == x
TranscodingStreams.finalize(decompressor)
end

end # module TestsForCodecPackages
4 changes: 4 additions & 0 deletions test/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,7 @@ Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
TestsForCodecPackages = "c2e61002-3542-480d-8b3c-5f05cc4f8554"
TranscodingStreams = "3bb67fe8-82b1-5028-8e26-92a6c54297fa"

[sources]
TranscodingStreams = {path = ".."}
TestsForCodecPackages = {path = "../lib/TestsForCodecPackages"}
4 changes: 3 additions & 1 deletion test/codecdoubleframe.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ using TestsForCodecPackages:
test_roundtrip_seekstart,
test_roundtrip_fileio,
test_chunked_read,
test_chunked_write
test_chunked_write,
test_reuse_encoder

# An insane codec for testing the codec APIs.
struct DoubleFrameEncoder <: TranscodingStreams.Codec
Expand Down Expand Up @@ -461,4 +462,5 @@ DoubleFrameDecoderStream(stream::IO; kwargs...) = TranscodingStream(DoubleFrameD
test_roundtrip_fileio(DoubleFrameEncoder, DoubleFrameDecoder)
test_chunked_read(DoubleFrameEncoder, DoubleFrameDecoder)
test_chunked_write(DoubleFrameEncoder, DoubleFrameDecoder)
test_reuse_encoder(DoubleFrameEncoder, DoubleFrameDecoder)
end

0 comments on commit 353b02a

Please sign in to comment.