diff options
author | Tim Schumacher <timschumi@gmx.de> | 2023-01-25 20:06:16 +0100 |
---|---|---|
committer | Andrew Kaster <andrewdkaster@gmail.com> | 2023-01-29 19:16:44 -0700 |
commit | 2470dd3bb5f437658ec2f95ff3e7f0bb2e0b5448 (patch) | |
tree | 647f8b75f2248efbb8d732098e0cad3c2fe0f037 /Tests | |
parent | 94f139c1117e2870c45df27085b004723b5b86ca (diff) | |
download | serenity-2470dd3bb5f437658ec2f95ff3e7f0bb2e0b5448.zip |
AK: Move bit streams from `LibCore`
Diffstat (limited to 'Tests')
-rw-r--r-- | Tests/AK/CMakeLists.txt | 1 | ||||
-rw-r--r-- | Tests/AK/TestBitStream.cpp | 121 | ||||
-rw-r--r-- | Tests/LibCompress/TestDeflate.cpp | 6 | ||||
-rw-r--r-- | Tests/LibCore/TestLibCoreStream.cpp | 115 |
4 files changed, 125 insertions, 118 deletions
diff --git a/Tests/AK/CMakeLists.txt b/Tests/AK/CMakeLists.txt index 588997d53b..fae14c43d6 100644 --- a/Tests/AK/CMakeLists.txt +++ b/Tests/AK/CMakeLists.txt @@ -10,6 +10,7 @@ set(AK_TEST_SOURCES TestBinarySearch.cpp TestBitCast.cpp TestBitmap.cpp + TestBitStream.cpp TestBuiltinWrappers.cpp TestByteBuffer.cpp TestCharacterTypes.cpp diff --git a/Tests/AK/TestBitStream.cpp b/Tests/AK/TestBitStream.cpp new file mode 100644 index 0000000000..0316ae863e --- /dev/null +++ b/Tests/AK/TestBitStream.cpp @@ -0,0 +1,121 @@ +/* + * Copyright (c) 2023, Tim Schumacher <timschumi@gmx.de> + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include <AK/BitStream.h> +#include <LibCore/MemoryStream.h> +#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) +{ + 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(LittleEndianOutputBitStream::construct(MaybeOwned<AK::Stream>(*memory_stream))); + auto bit_read_stream = MUST(LittleEndianInputBitStream::construct(MaybeOwned<AK::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(BigEndianOutputBitStream::construct(MaybeOwned<AK::Stream>(*memory_stream))); + auto bit_read_stream = MUST(BigEndianInputBitStream::construct(MaybeOwned<AK::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); + } +} diff --git a/Tests/LibCompress/TestDeflate.cpp b/Tests/LibCompress/TestDeflate.cpp index 56fd34b733..bdeeabd9b0 100644 --- a/Tests/LibCompress/TestDeflate.cpp +++ b/Tests/LibCompress/TestDeflate.cpp @@ -7,9 +7,9 @@ #include <LibTest/TestCase.h> #include <AK/Array.h> +#include <AK/BitStream.h> #include <AK/Random.h> #include <LibCompress/Deflate.h> -#include <LibCore/BitStream.h> #include <LibCore/MemoryStream.h> #include <cstring> @@ -29,7 +29,7 @@ TEST_CASE(canonical_code_simple) auto const huffman = Compress::CanonicalCode::from_bytes(code).value(); auto memory_stream = MUST(Core::Stream::FixedMemoryStream::construct(input)); - auto bit_stream = MUST(Core::Stream::LittleEndianInputBitStream::construct(move(memory_stream))); + auto bit_stream = MUST(LittleEndianInputBitStream::construct(move(memory_stream))); for (size_t idx = 0; idx < 9; ++idx) EXPECT_EQ(MUST(huffman.read_symbol(*bit_stream)), output[idx]); @@ -49,7 +49,7 @@ TEST_CASE(canonical_code_complex) auto const huffman = Compress::CanonicalCode::from_bytes(code).value(); auto memory_stream = MUST(Core::Stream::FixedMemoryStream::construct(input)); - auto bit_stream = MUST(Core::Stream::LittleEndianInputBitStream::construct(move(memory_stream))); + auto bit_stream = MUST(LittleEndianInputBitStream::construct(move(memory_stream))); for (size_t idx = 0; idx < 12; ++idx) EXPECT_EQ(MUST(huffman.read_symbol(*bit_stream)), output[idx]); diff --git a/Tests/LibCore/TestLibCoreStream.cpp b/Tests/LibCore/TestLibCoreStream.cpp index 0549a35518..c7c87aff85 100644 --- a/Tests/LibCore/TestLibCoreStream.cpp +++ b/Tests/LibCore/TestLibCoreStream.cpp @@ -7,7 +7,6 @@ #include <AK/Format.h> #include <AK/MaybeOwned.h> #include <AK/String.h> -#include <LibCore/BitStream.h> #include <LibCore/EventLoop.h> #include <LibCore/LocalServer.h> #include <LibCore/MemoryStream.h> @@ -717,117 +716,3 @@ 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(MaybeOwned<AK::Stream>(*memory_stream))); - auto bit_read_stream = MUST(Core::Stream::LittleEndianInputBitStream::construct(MaybeOwned<AK::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(MaybeOwned<AK::Stream>(*memory_stream))); - auto bit_read_stream = MUST(Core::Stream::BigEndianInputBitStream::construct(MaybeOwned<AK::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); - } -} |