2023-01-25 19:19:05 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2021, sin-ack <sin-ack@protonmail.com>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <AK/MemoryStream.h>
|
|
|
|
#include <AK/String.h>
|
|
|
|
#include <LibTest/TestCase.h>
|
|
|
|
|
|
|
|
TEST_CASE(allocating_memory_stream_empty)
|
|
|
|
{
|
|
|
|
AllocatingMemoryStream stream;
|
|
|
|
|
|
|
|
EXPECT_EQ(stream.used_buffer_size(), 0ul);
|
|
|
|
|
|
|
|
{
|
|
|
|
Array<u8, 32> array;
|
2023-02-24 21:38:01 +00:00
|
|
|
auto read_bytes = MUST(stream.read_some(array));
|
2023-01-25 19:19:05 +00:00
|
|
|
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;
|
2023-03-01 14:37:45 +00:00
|
|
|
MUST(stream.write_until_depleted("Well Hello Friends! :^)"sv.bytes()));
|
2023-01-25 19:19:05 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
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.
|
2023-05-06 14:53:22 +00:00
|
|
|
// So we have to assume some things about the behavior of this class:
|
2023-01-25 19:19:05 +00:00
|
|
|
// - 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.
|
|
|
|
|
2023-05-29 09:00:25 +00:00
|
|
|
// First, fill exactly one chunk (in groups of 16 bytes).
|
|
|
|
for (size_t i = 0; i < AllocatingMemoryStream::CHUNK_SIZE / 16; ++i)
|
2023-03-01 14:37:45 +00:00
|
|
|
MUST(stream.write_until_depleted("AAAAAAAAAAAAAAAA"sv.bytes()));
|
2023-01-25 19:19:05 +00:00
|
|
|
|
|
|
|
// Then discard it all.
|
2023-05-29 09:00:25 +00:00
|
|
|
MUST(stream.discard(AllocatingMemoryStream::CHUNK_SIZE));
|
2023-01-25 19:19:05 +00:00
|
|
|
// Now we can write into this chunk again, knowing that it's initialized to all 'A's.
|
2023-03-01 14:37:45 +00:00
|
|
|
MUST(stream.write_until_depleted("Well Hello Friends! :^)"sv.bytes()));
|
2023-01-25 19:19:05 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
auto offset = MUST(stream.offset_of("A"sv.bytes()));
|
|
|
|
EXPECT(!offset.has_value());
|
|
|
|
}
|
|
|
|
}
|