summaryrefslogtreecommitdiff
path: root/Tests/LibCore
diff options
context:
space:
mode:
authorTim Schumacher <timschumi@gmx.de>2022-12-26 14:32:01 +0100
committerAndreas Kling <kling@serenityos.org>2023-01-10 10:28:26 +0100
commitb4b80b7ec69ce2ed2b9ec68da9776ced89bc595f (patch)
tree3776ee1f56ed22f1f9b5977de4a06607b59ac72a /Tests/LibCore
parent0bdbe27d6b3ed8c303e15ad7b3cee17bd9096423 (diff)
downloadserenity-b4b80b7ec69ce2ed2b9ec68da9776ced89bc595f.zip
LibCore: Add `{Big,Little}EndianOutputBitStream`
Also add some tests that ensure that the input and output streams match each other, because I can't wrap my head around what the internal representation looks like.
Diffstat (limited to 'Tests/LibCore')
-rw-r--r--Tests/LibCore/TestLibCoreStream.cpp115
1 files changed, 115 insertions, 0 deletions
diff --git a/Tests/LibCore/TestLibCoreStream.cpp b/Tests/LibCore/TestLibCoreStream.cpp
index 945302791a..f5c7b66044 100644
--- a/Tests/LibCore/TestLibCoreStream.cpp
+++ b/Tests/LibCore/TestLibCoreStream.cpp
@@ -6,6 +6,7 @@
#include <AK/Format.h>
#include <AK/String.h>
+#include <LibCore/BitStream.h>
#include <LibCore/EventLoop.h>
#include <LibCore/LocalServer.h>
#include <LibCore/MemoryStream.h>
@@ -559,3 +560,117 @@ TEST_CASE(allocating_memory_stream_10kb)
offset += file_span.size();
}
}
+
+// Bit stream tests
+
+// 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)
+{
+ auto memory_stream = make<Core::Stream::AllocatingMemoryStream>();
+
+ // 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.
+ auto bit_write_stream = MUST(Core::Stream::LittleEndianOutputBitStream::construct(Core::Stream::Handle<Core::Stream::Stream>(*memory_stream)));
+ auto bit_read_stream = MUST(Core::Stream::LittleEndianInputBitStream::construct(Core::Stream::Handle<Core::Stream::Stream>(*memory_stream)));
+
+ // 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));
+ EXPECT_EQ(0b1111u, result);
+ result = MUST(bit_read_stream->read_bits(4));
+ 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));
+ EXPECT_EQ(0b0000u, result);
+ result = MUST(bit_read_stream->read_bits(4));
+ 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));
+ EXPECT_EQ(0b1000u, result);
+ result = MUST(bit_read_stream->read_bits(4));
+ 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));
+ EXPECT_EQ(0b1000u, result);
+ result = MUST(bit_read_stream->read_bits(4));
+ 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));
+ 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)
+{
+ auto memory_stream = make<Core::Stream::AllocatingMemoryStream>();
+
+ // 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.
+ auto bit_write_stream = MUST(Core::Stream::BigEndianOutputBitStream::construct(Core::Stream::Handle<Core::Stream::Stream>(*memory_stream)));
+ auto bit_read_stream = MUST(Core::Stream::BigEndianInputBitStream::construct(Core::Stream::Handle<Core::Stream::Stream>(*memory_stream)));
+
+ // 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));
+ EXPECT_EQ(0b1111u, result);
+ result = MUST(bit_read_stream->read_bits(4));
+ 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));
+ EXPECT_EQ(0b0000u, result);
+ result = MUST(bit_read_stream->read_bits(4));
+ 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));
+ EXPECT_EQ(0b1000u, result);
+ result = MUST(bit_read_stream->read_bits(4));
+ 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));
+ EXPECT_EQ(0b1000u, result);
+ result = MUST(bit_read_stream->read_bits(4));
+ 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));
+ EXPECT_EQ(0b1101001000100001u, result);
+ }
+}