serenity/Tests/AK/TestBitStream.cpp

122 lines
4.9 KiB
C++
Raw Normal View History

2023-01-25 19:06:16 +00:00
/*
* Copyright (c) 2023, Tim Schumacher <timschumi@gmx.de>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/BitStream.h>
2023-01-25 19:19:05 +00:00
#include <AK/MemoryStream.h>
2023-01-25 19:06:16 +00:00
#include <LibTest/TestCase.h>
// 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)
{
2023-01-25 19:19:05 +00:00
auto memory_stream = make<AllocatingMemoryStream>();
2023-01-25 19:06:16 +00:00
// Note: The bit stream only ever reads from/writes to the underlying stream in one byte chunks,
// so testing with sizes that will not trigger a write will yield unexpected results.
LittleEndianOutputBitStream bit_write_stream { MaybeOwned<AK::Stream>(*memory_stream) };
LittleEndianInputBitStream bit_read_stream { MaybeOwned<AK::Stream>(*memory_stream) };
2023-01-25 19:06:16 +00:00
// Test two mirrored chunks of a fully mirrored pattern to check that we are not dropping bits.
{
MUST(bit_write_stream.write_bits(0b1111u, 4));
MUST(bit_write_stream.write_bits(0b1111u, 4));
auto result = MUST(bit_read_stream.read_bits(4));
2023-01-25 19:06:16 +00:00
EXPECT_EQ(0b1111u, result);
result = MUST(bit_read_stream.read_bits(4));
2023-01-25 19:06:16 +00:00
EXPECT_EQ(0b1111u, result);
}
{
MUST(bit_write_stream.write_bits(0b0000u, 4));
MUST(bit_write_stream.write_bits(0b0000u, 4));
auto result = MUST(bit_read_stream.read_bits(4));
2023-01-25 19:06:16 +00:00
EXPECT_EQ(0b0000u, result);
result = MUST(bit_read_stream.read_bits(4));
2023-01-25 19:06:16 +00:00
EXPECT_EQ(0b0000u, result);
}
// Test two mirrored chunks of a non-mirrored pattern to check that we are writing bits within a pattern in the correct order.
{
MUST(bit_write_stream.write_bits(0b1000u, 4));
MUST(bit_write_stream.write_bits(0b1000u, 4));
auto result = MUST(bit_read_stream.read_bits(4));
2023-01-25 19:06:16 +00:00
EXPECT_EQ(0b1000u, result);
result = MUST(bit_read_stream.read_bits(4));
2023-01-25 19:06:16 +00:00
EXPECT_EQ(0b1000u, result);
}
// Test two different chunks to check that we are not confusing their order.
{
MUST(bit_write_stream.write_bits(0b1000u, 4));
MUST(bit_write_stream.write_bits(0b0100u, 4));
auto result = MUST(bit_read_stream.read_bits(4));
2023-01-25 19:06:16 +00:00
EXPECT_EQ(0b1000u, result);
result = MUST(bit_read_stream.read_bits(4));
2023-01-25 19:06:16 +00:00
EXPECT_EQ(0b0100u, result);
}
// Test a pattern that spans multiple bytes.
{
MUST(bit_write_stream.write_bits(0b1101001000100001u, 16));
auto result = MUST(bit_read_stream.read_bits(16));
2023-01-25 19:06:16 +00:00
EXPECT_EQ(0b1101001000100001u, result);
}
}
// 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(big_endian_bit_stream_input_output_match)
{
2023-01-25 19:19:05 +00:00
auto memory_stream = make<AllocatingMemoryStream>();
2023-01-25 19:06:16 +00:00
// Note: The bit stream only ever reads from/writes to the underlying stream in one byte chunks,
// so testing with sizes that will not trigger a write will yield unexpected results.
BigEndianOutputBitStream bit_write_stream { MaybeOwned<AK::Stream>(*memory_stream) };
BigEndianInputBitStream bit_read_stream { MaybeOwned<AK::Stream>(*memory_stream) };
2023-01-25 19:06:16 +00:00
// Test two mirrored chunks of a fully mirrored pattern to check that we are not dropping bits.
{
MUST(bit_write_stream.write_bits(0b1111u, 4));
MUST(bit_write_stream.write_bits(0b1111u, 4));
auto result = MUST(bit_read_stream.read_bits(4));
2023-01-25 19:06:16 +00:00
EXPECT_EQ(0b1111u, result);
result = MUST(bit_read_stream.read_bits(4));
2023-01-25 19:06:16 +00:00
EXPECT_EQ(0b1111u, result);
}
{
MUST(bit_write_stream.write_bits(0b0000u, 4));
MUST(bit_write_stream.write_bits(0b0000u, 4));
auto result = MUST(bit_read_stream.read_bits(4));
2023-01-25 19:06:16 +00:00
EXPECT_EQ(0b0000u, result);
result = MUST(bit_read_stream.read_bits(4));
2023-01-25 19:06:16 +00:00
EXPECT_EQ(0b0000u, result);
}
// Test two mirrored chunks of a non-mirrored pattern to check that we are writing bits within a pattern in the correct order.
{
MUST(bit_write_stream.write_bits(0b1000u, 4));
MUST(bit_write_stream.write_bits(0b1000u, 4));
auto result = MUST(bit_read_stream.read_bits(4));
2023-01-25 19:06:16 +00:00
EXPECT_EQ(0b1000u, result);
result = MUST(bit_read_stream.read_bits(4));
2023-01-25 19:06:16 +00:00
EXPECT_EQ(0b1000u, result);
}
// Test two different chunks to check that we are not confusing their order.
{
MUST(bit_write_stream.write_bits(0b1000u, 4));
MUST(bit_write_stream.write_bits(0b0100u, 4));
auto result = MUST(bit_read_stream.read_bits(4));
2023-01-25 19:06:16 +00:00
EXPECT_EQ(0b1000u, result);
result = MUST(bit_read_stream.read_bits(4));
2023-01-25 19:06:16 +00:00
EXPECT_EQ(0b0100u, result);
}
// Test a pattern that spans multiple bytes.
{
MUST(bit_write_stream.write_bits(0b1101001000100001u, 16));
auto result = MUST(bit_read_stream.read_bits(16));
2023-01-25 19:06:16 +00:00
EXPECT_EQ(0b1101001000100001u, result);
}
}