From 3c03ce0c80dc0979947ec67700b0a7c071a8f5bd Mon Sep 17 00:00:00 2001 From: asynts Date: Thu, 10 Sep 2020 14:31:54 +0200 Subject: [PATCH] LibCompress: Add unit tests for CanonicalCode. --- Userland/test-compress.cpp | 43 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/Userland/test-compress.cpp b/Userland/test-compress.cpp index 432f439739..08150f2cec 100644 --- a/Userland/test-compress.cpp +++ b/Userland/test-compress.cpp @@ -27,6 +27,7 @@ #include #include +#include #include #include #include @@ -44,6 +45,48 @@ static bool compare(ReadonlyBytes lhs, ReadonlyBytes rhs) return true; } +TEST_CASE(canonical_code_simple) +{ + const Array code { + 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, + 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, + 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05 + }; + const Array input { + 0x00, 0x42, 0x84, 0xa9, 0xb0, 0x15 + }; + const Array output { + 0x00, 0x01, 0x01, 0x02, 0x03, 0x05, 0x08, 0x0d, 0x15 + }; + + const auto huffman = Compress::CanonicalCode::from_bytes(code).value(); + auto memory_stream = InputMemoryStream { input }; + auto bit_stream = InputBitStream { memory_stream }; + + for (size_t idx = 0; idx < 9; ++idx) + EXPECT_EQ(huffman.read_symbol(bit_stream), output[idx]); +} + +TEST_CASE(canonical_code_complex) +{ + const Array code { + 0x03, 0x02, 0x03, 0x03, 0x02, 0x03 + }; + const Array input { + 0xa1, 0xf3, 0xa1, 0xf3 + }; + const Array output { + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05 + }; + + const auto huffman = Compress::CanonicalCode::from_bytes(code).value(); + auto memory_stream = InputMemoryStream { input }; + auto bit_stream = InputBitStream { memory_stream }; + + for (size_t idx = 0; idx < 12; ++idx) + EXPECT_EQ(huffman.read_symbol(bit_stream), output[idx]); +} + TEST_CASE(deflate_decompress_compressed_block) { const Array compressed {