mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 02:07:35 +00:00
AK: Add roundtrip tests for BitStream
This commit is contained in:
parent
c802971f93
commit
a00b4a5c1f
1 changed files with 89 additions and 0 deletions
|
@ -8,6 +8,8 @@
|
||||||
#include <AK/MemoryStream.h>
|
#include <AK/MemoryStream.h>
|
||||||
#include <LibTest/TestCase.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.
|
// 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)
|
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);
|
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);
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue