LibCompress: Add unit tests for CanonicalCode.

This commit is contained in:
asynts 2020-09-10 14:31:54 +02:00 committed by Andreas Kling
parent 5c9c0082a1
commit 3c03ce0c80
Notes: sideshowbarker 2024-07-19 02:46:11 +09:00

View file

@ -27,6 +27,7 @@
#include <AK/TestSuite.h>
#include <AK/Array.h>
#include <AK/MemoryStream.h>
#include <LibCompress/Deflate.h>
#include <LibCompress/Gzip.h>
#include <LibCompress/Zlib.h>
@ -44,6 +45,48 @@ static bool compare(ReadonlyBytes lhs, ReadonlyBytes rhs)
return true;
}
TEST_CASE(canonical_code_simple)
{
const Array<u8, 32> 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<u8, 6> input {
0x00, 0x42, 0x84, 0xa9, 0xb0, 0x15
};
const Array<u32, 9> 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<u8, 6> code {
0x03, 0x02, 0x03, 0x03, 0x02, 0x03
};
const Array<u8, 4> input {
0xa1, 0xf3, 0xa1, 0xf3
};
const Array<u32, 12> 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<u8, 28> compressed {