diff options
author | Tim Schumacher <timschumi@gmx.de> | 2023-01-25 20:19:05 +0100 |
---|---|---|
committer | Andrew Kaster <andrewdkaster@gmail.com> | 2023-01-29 19:16:44 -0700 |
commit | 093cf428a3d7473f429a589f1573d9ac1c41d762 (patch) | |
tree | 7314dbfc855b44470a78756e1e41f43bdd56d083 /Tests | |
parent | 11550f582ba99d317717ef76fef23118fa226ee1 (diff) | |
download | serenity-093cf428a3d7473f429a589f1573d9ac1c41d762.zip |
AK: Move memory streams from `LibCore`
Diffstat (limited to 'Tests')
-rw-r--r-- | Tests/AK/CMakeLists.txt | 1 | ||||
-rw-r--r-- | Tests/AK/TestBitStream.cpp | 6 | ||||
-rw-r--r-- | Tests/AK/TestMemoryStream.cpp | 94 | ||||
-rw-r--r-- | Tests/LibCompress/TestDeflate.cpp | 6 | ||||
-rw-r--r-- | Tests/LibCore/TestLibCoreStream.cpp | 87 | ||||
-rw-r--r-- | Tests/LibWasm/test-wasm.cpp | 4 |
6 files changed, 103 insertions, 95 deletions
diff --git a/Tests/AK/CMakeLists.txt b/Tests/AK/CMakeLists.txt index fae14c43d6..cab724ee36 100644 --- a/Tests/AK/CMakeLists.txt +++ b/Tests/AK/CMakeLists.txt @@ -50,6 +50,7 @@ set(AK_TEST_SOURCES TestLexicalPath.cpp TestMACAddress.cpp TestMemory.cpp + TestMemoryStream.cpp TestNeverDestroyed.cpp TestNonnullRefPtr.cpp TestNumberFormat.cpp diff --git a/Tests/AK/TestBitStream.cpp b/Tests/AK/TestBitStream.cpp index 0316ae863e..b04b11d39e 100644 --- a/Tests/AK/TestBitStream.cpp +++ b/Tests/AK/TestBitStream.cpp @@ -5,13 +5,13 @@ */ #include <AK/BitStream.h> -#include <LibCore/MemoryStream.h> +#include <AK/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>(); + auto memory_stream = make<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. @@ -67,7 +67,7 @@ TEST_CASE(little_endian_bit_stream_input_output_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(big_endian_bit_stream_input_output_match) { - auto memory_stream = make<Core::Stream::AllocatingMemoryStream>(); + auto memory_stream = make<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. diff --git a/Tests/AK/TestMemoryStream.cpp b/Tests/AK/TestMemoryStream.cpp new file mode 100644 index 0000000000..48f74a0848 --- /dev/null +++ b/Tests/AK/TestMemoryStream.cpp @@ -0,0 +1,94 @@ +/* + * Copyright (c) 2021, sin-ack <sin-ack@protonmail.com> + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include <AK/MemoryStream.h> +#include <AK/String.h> +#include <LibCore/Stream.h> +#include <LibTest/TestCase.h> + +TEST_CASE(allocating_memory_stream_empty) +{ + AllocatingMemoryStream stream; + + EXPECT_EQ(stream.used_buffer_size(), 0ul); + + { + Array<u8, 32> array; + auto read_bytes = MUST(stream.read(array)); + EXPECT_EQ(read_bytes.size(), 0ul); + } + + { + auto offset = MUST(stream.offset_of("test"sv.bytes())); + EXPECT(!offset.has_value()); + } +} + +TEST_CASE(allocating_memory_stream_offset_of) +{ + AllocatingMemoryStream stream; + MUST(stream.write_entire_buffer("Well Hello Friends! :^)"sv.bytes())); + + { + auto offset = MUST(stream.offset_of(" "sv.bytes())); + EXPECT(offset.has_value()); + EXPECT_EQ(offset.value(), 4ul); + } + + { + auto offset = MUST(stream.offset_of("W"sv.bytes())); + EXPECT(offset.has_value()); + EXPECT_EQ(offset.value(), 0ul); + } + + { + auto offset = MUST(stream.offset_of(")"sv.bytes())); + EXPECT(offset.has_value()); + EXPECT_EQ(offset.value(), 22ul); + } + + { + auto offset = MUST(stream.offset_of("-"sv.bytes())); + EXPECT(!offset.has_value()); + } + + MUST(stream.discard(1)); + + { + auto offset = MUST(stream.offset_of("W"sv.bytes())); + EXPECT(!offset.has_value()); + } + + { + auto offset = MUST(stream.offset_of("e"sv.bytes())); + EXPECT(offset.has_value()); + EXPECT_EQ(offset.value(), 0ul); + } +} + +TEST_CASE(allocating_memory_stream_offset_of_oob) +{ + AllocatingMemoryStream stream; + // NOTE: This test is to make sure that offset_of() doesn't read past the end of the "initialized" data. + // So we have to assume some things about the behaviour of this class: + // - The chunk size is 4096 bytes. + // - A chunk is moved to the end when it's fully read from + // - A free chunk is used as-is, no new ones are allocated if one exists. + + // First, fill exactly one chunk. + for (size_t i = 0; i < 256; ++i) + MUST(stream.write_entire_buffer("AAAAAAAAAAAAAAAA"sv.bytes())); + + // Then discard it all. + MUST(stream.discard(4096)); + // Now we can write into this chunk again, knowing that it's initialized to all 'A's. + MUST(stream.write_entire_buffer("Well Hello Friends! :^)"sv.bytes())); + + { + auto offset = MUST(stream.offset_of("A"sv.bytes())); + EXPECT(!offset.has_value()); + } +} diff --git a/Tests/LibCompress/TestDeflate.cpp b/Tests/LibCompress/TestDeflate.cpp index bdeeabd9b0..9cb2ec9460 100644 --- a/Tests/LibCompress/TestDeflate.cpp +++ b/Tests/LibCompress/TestDeflate.cpp @@ -8,9 +8,9 @@ #include <AK/Array.h> #include <AK/BitStream.h> +#include <AK/MemoryStream.h> #include <AK/Random.h> #include <LibCompress/Deflate.h> -#include <LibCore/MemoryStream.h> #include <cstring> TEST_CASE(canonical_code_simple) @@ -28,7 +28,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 memory_stream = MUST(FixedMemoryStream::construct(input)); auto bit_stream = MUST(LittleEndianInputBitStream::construct(move(memory_stream))); for (size_t idx = 0; idx < 9; ++idx) @@ -48,7 +48,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 memory_stream = MUST(FixedMemoryStream::construct(input)); auto bit_stream = MUST(LittleEndianInputBitStream::construct(move(memory_stream))); for (size_t idx = 0; idx < 12; ++idx) diff --git a/Tests/LibCore/TestLibCoreStream.cpp b/Tests/LibCore/TestLibCoreStream.cpp index 500d3b5a18..fe448f2698 100644 --- a/Tests/LibCore/TestLibCoreStream.cpp +++ b/Tests/LibCore/TestLibCoreStream.cpp @@ -9,7 +9,6 @@ #include <AK/String.h> #include <LibCore/EventLoop.h> #include <LibCore/LocalServer.h> -#include <LibCore/MemoryStream.h> #include <LibCore/Stream.h> #include <LibCore/TCPServer.h> #include <LibCore/Timer.h> @@ -594,89 +593,3 @@ TEST_CASE(buffered_tcp_socket_read) auto second_received_line = maybe_second_received_line.value(); EXPECT_EQ(second_received_line, second_line); } - -// Allocating memory stream tests - -TEST_CASE(allocating_memory_stream_empty) -{ - Core::Stream::AllocatingMemoryStream stream; - - EXPECT_EQ(stream.used_buffer_size(), 0ul); - - { - Array<u8, 32> array; - auto read_bytes = MUST(stream.read(array)); - EXPECT_EQ(read_bytes.size(), 0ul); - } - - { - auto offset = MUST(stream.offset_of("test"sv.bytes())); - EXPECT(!offset.has_value()); - } -} - -TEST_CASE(allocating_memory_stream_offset_of) -{ - Core::Stream::AllocatingMemoryStream stream; - MUST(stream.write_entire_buffer("Well Hello Friends! :^)"sv.bytes())); - - { - auto offset = MUST(stream.offset_of(" "sv.bytes())); - EXPECT(offset.has_value()); - EXPECT_EQ(offset.value(), 4ul); - } - - { - auto offset = MUST(stream.offset_of("W"sv.bytes())); - EXPECT(offset.has_value()); - EXPECT_EQ(offset.value(), 0ul); - } - - { - auto offset = MUST(stream.offset_of(")"sv.bytes())); - EXPECT(offset.has_value()); - EXPECT_EQ(offset.value(), 22ul); - } - - { - auto offset = MUST(stream.offset_of("-"sv.bytes())); - EXPECT(!offset.has_value()); - } - - MUST(stream.discard(1)); - - { - auto offset = MUST(stream.offset_of("W"sv.bytes())); - EXPECT(!offset.has_value()); - } - - { - auto offset = MUST(stream.offset_of("e"sv.bytes())); - EXPECT(offset.has_value()); - EXPECT_EQ(offset.value(), 0ul); - } -} - -TEST_CASE(allocating_memory_stream_offset_of_oob) -{ - Core::Stream::AllocatingMemoryStream stream; - // NOTE: This test is to make sure that offset_of() doesn't read past the end of the "initialized" data. - // So we have to assume some things about the behaviour of this class: - // - The chunk size is 4096 bytes. - // - A chunk is moved to the end when it's fully read from - // - A free chunk is used as-is, no new ones are allocated if one exists. - - // First, fill exactly one chunk. - for (size_t i = 0; i < 256; ++i) - MUST(stream.write_entire_buffer("AAAAAAAAAAAAAAAA"sv.bytes())); - - // Then discard it all. - MUST(stream.discard(4096)); - // Now we can write into this chunk again, knowing that it's initialized to all 'A's. - MUST(stream.write_entire_buffer("Well Hello Friends! :^)"sv.bytes())); - - { - auto offset = MUST(stream.offset_of("A"sv.bytes())); - EXPECT(!offset.has_value()); - } -} diff --git a/Tests/LibWasm/test-wasm.cpp b/Tests/LibWasm/test-wasm.cpp index e203c6486f..917d32b94c 100644 --- a/Tests/LibWasm/test-wasm.cpp +++ b/Tests/LibWasm/test-wasm.cpp @@ -4,7 +4,7 @@ * SPDX-License-Identifier: BSD-2-Clause */ -#include <LibCore/MemoryStream.h> +#include <AK/MemoryStream.h> #include <LibCore/Stream.h> #include <LibTest/JavaScriptTestRunner.h> #include <LibWasm/AbstractMachine/BytecodeInterpreter.h> @@ -106,7 +106,7 @@ TESTJS_GLOBAL_FUNCTION(parse_webassembly_module, parseWebAssemblyModule) if (!is<JS::Uint8Array>(object)) return vm.throw_completion<JS::TypeError>("Expected a Uint8Array argument to parse_webassembly_module"); auto& array = static_cast<JS::Uint8Array&>(*object); - auto stream = Core::Stream::FixedMemoryStream::construct(array.data()).release_value_but_fixme_should_propagate_errors(); + auto stream = FixedMemoryStream::construct(array.data()).release_value_but_fixme_should_propagate_errors(); auto result = Wasm::Module::parse(*stream); if (result.is_error()) return vm.throw_completion<JS::SyntaxError>(Wasm::parse_error_to_deprecated_string(result.error())); |