AK: Add roundtrip tests for BitStream

This commit is contained in:
Martin Janiczek 2023-09-30 01:39:58 +02:00 committed by Tim Schumacher
parent c802971f93
commit a00b4a5c1f
Notes: sideshowbarker 2024-07-17 05:05:51 +09:00

View file

@ -8,6 +8,8 @@
#include <AK/MemoryStream.h>
#include <LibTest/TestCase.h>
using namespace Test::Randomized;
// Note: This does not do any checks on the internal representation, it just ensures that the behavior of the input and output streams match.
TEST_CASE(little_endian_bit_stream_input_output_match)
{
@ -129,3 +131,90 @@ TEST_CASE(big_endian_bit_stream_input_output_match)
EXPECT_EQ(0b1101001000100001u, result);
}
}
RANDOMIZED_TEST_CASE(roundtrip_u8_little_endian)
{
GEN(n, Gen::unsigned_int(NumericLimits<u8>::max()));
auto memory_stream = make<AllocatingMemoryStream>();
LittleEndianOutputBitStream sut_write { MaybeOwned<Stream>(*memory_stream) };
LittleEndianInputBitStream sut_read { MaybeOwned<Stream>(*memory_stream) };
MUST(sut_write.write_bits(n, 8));
MUST(sut_write.flush_buffer_to_stream());
auto result = MUST(sut_read.read_bits<u64>(8));
EXPECT_EQ(result, n);
}
RANDOMIZED_TEST_CASE(roundtrip_u16_little_endian)
{
GEN(n, Gen::unsigned_int(NumericLimits<u16>::max()));
auto memory_stream = make<AllocatingMemoryStream>();
LittleEndianOutputBitStream sut_write { MaybeOwned<Stream>(*memory_stream) };
LittleEndianInputBitStream sut_read { MaybeOwned<Stream>(*memory_stream) };
MUST(sut_write.write_bits(n, 16));
MUST(sut_write.flush_buffer_to_stream());
auto result = MUST(sut_read.read_bits<u64>(16));
EXPECT_EQ(result, n);
}
RANDOMIZED_TEST_CASE(roundtrip_u32_little_endian)
{
GEN(n, Gen::unsigned_int(NumericLimits<u32>::max()));
auto memory_stream = make<AllocatingMemoryStream>();
LittleEndianOutputBitStream sut_write { MaybeOwned<Stream>(*memory_stream) };
LittleEndianInputBitStream sut_read { MaybeOwned<Stream>(*memory_stream) };
MUST(sut_write.write_bits(n, 32));
MUST(sut_write.flush_buffer_to_stream());
auto result = MUST(sut_read.read_bits<u64>(32));
EXPECT_EQ(result, n);
}
RANDOMIZED_TEST_CASE(roundtrip_u8_big_endian)
{
GEN(n, Gen::unsigned_int(NumericLimits<u8>::max()));
auto memory_stream = make<AllocatingMemoryStream>();
BigEndianOutputBitStream sut_write { MaybeOwned<Stream>(*memory_stream) };
BigEndianInputBitStream sut_read { MaybeOwned<Stream>(*memory_stream) };
MUST(sut_write.write_bits(n, 8));
auto result = MUST(sut_read.read_bits<u64>(8));
EXPECT_EQ(result, n);
}
RANDOMIZED_TEST_CASE(roundtrip_u16_big_endian)
{
GEN(n, Gen::unsigned_int(NumericLimits<u16>::max()));
auto memory_stream = make<AllocatingMemoryStream>();
BigEndianOutputBitStream sut_write { MaybeOwned<Stream>(*memory_stream) };
BigEndianInputBitStream sut_read { MaybeOwned<Stream>(*memory_stream) };
MUST(sut_write.write_bits(n, 16));
auto result = MUST(sut_read.read_bits<u64>(16));
EXPECT_EQ(result, n);
}
RANDOMIZED_TEST_CASE(roundtrip_u32_big_endian)
{
GEN(n, Gen::unsigned_int(NumericLimits<u32>::max()));
auto memory_stream = make<AllocatingMemoryStream>();
BigEndianOutputBitStream sut_write { MaybeOwned<Stream>(*memory_stream) };
BigEndianInputBitStream sut_read { MaybeOwned<Stream>(*memory_stream) };
MUST(sut_write.write_bits(n, 32));
auto result = MUST(sut_read.read_bits<u64>(32));
EXPECT_EQ(result, n);
}