diff options
author | Tim Schumacher <timschumi@gmx.de> | 2023-05-03 18:36:53 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2023-05-17 09:08:53 +0200 |
commit | e8067ac2ee2e08c3c8d19e386003be175492cec7 (patch) | |
tree | ffd9868c017d547e59da9972df6a3913afe3d78f | |
parent | 85a54cc796b27e3ca7d25ce1fdaa1d3f73a9f759 (diff) | |
download | serenity-e8067ac2ee2e08c3c8d19e386003be175492cec7.zip |
Tests: Add an LZMA roundtrip compression test
-rw-r--r-- | Tests/LibCompress/TestLzma.cpp | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/Tests/LibCompress/TestLzma.cpp b/Tests/LibCompress/TestLzma.cpp index 13e161c542..58e9d5decb 100644 --- a/Tests/LibCompress/TestLzma.cpp +++ b/Tests/LibCompress/TestLzma.cpp @@ -31,6 +31,50 @@ TEST_CASE(repetition_length_beyond_distance) EXPECT_EQ("ABABABA"sv.bytes(), buffer.span()); } +TEST_CASE(compress_decompress_roundtrip_with_known_size) +{ + auto const uncompressed = "Well hello friends, this is a simple text file :)"sv.bytes(); + + auto stream = MUST(try_make<AllocatingMemoryStream>()); + + Compress::LzmaCompressorOptions const compressor_options { + .literal_context_bits = 3, + .literal_position_bits = 0, + .position_bits = 2, + .dictionary_size = 4 * KiB, + .uncompressed_size = uncompressed.size(), + }; + auto compressor = TRY_OR_FAIL(Compress::LzmaCompressor::create_container(MaybeOwned<Stream> { *stream }, compressor_options)); + TRY_OR_FAIL(compressor->write_until_depleted(uncompressed)); + + auto decompressor = TRY_OR_FAIL(Compress::LzmaDecompressor::create_from_container(MaybeOwned<Stream> { *stream })); + auto result = TRY_OR_FAIL(decompressor->read_until_eof()); + + EXPECT_EQ(uncompressed, result.span()); +} + +TEST_CASE(compress_decompress_roundtrip_with_unknown_size) +{ + auto const uncompressed = "Well hello friends, this is a simple text file :)"sv.bytes(); + + auto stream = MUST(try_make<AllocatingMemoryStream>()); + + Compress::LzmaCompressorOptions const compressor_options { + .literal_context_bits = 3, + .literal_position_bits = 0, + .position_bits = 2, + .dictionary_size = 4 * KiB, + }; + auto compressor = TRY_OR_FAIL(Compress::LzmaCompressor::create_container(MaybeOwned<Stream> { *stream }, compressor_options)); + TRY_OR_FAIL(compressor->write_until_depleted(uncompressed)); + TRY_OR_FAIL(compressor->flush()); + + auto decompressor = TRY_OR_FAIL(Compress::LzmaDecompressor::create_from_container(MaybeOwned<Stream> { *stream })); + auto result = TRY_OR_FAIL(decompressor->read_until_eof()); + + EXPECT_EQ(uncompressed, result.span()); +} + // The following tests are based on test files from the LZMA specification, which has been placed in the public domain. // LZMA Specification Draft (2015): https://www.7-zip.org/a/lzma-specification.7z |