diff options
author | Tim Schumacher <timschumi@gmx.de> | 2023-05-29 11:14:08 +0200 |
---|---|---|
committer | Jelle Raaijmakers <jelle@gmta.nl> | 2023-05-29 13:30:46 +0200 |
commit | 52aab509142b71a9fd0c4accb7ea25d94619d52f (patch) | |
tree | 6a9fc9ac2c4f7d41bc89852dfc887858af19a333 /Tests | |
parent | 9a7ae52b310ada54f8c0821bab8974eab211c2a0 (diff) | |
download | serenity-52aab509142b71a9fd0c4accb7ea25d94619d52f.zip |
AK: Handle empty trailing chunks in `AllocatingMemoryStream::offset_of`
Diffstat (limited to 'Tests')
-rw-r--r-- | Tests/AK/TestMemoryStream.cpp | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/Tests/AK/TestMemoryStream.cpp b/Tests/AK/TestMemoryStream.cpp index 097c92e4f2..073be8fd32 100644 --- a/Tests/AK/TestMemoryStream.cpp +++ b/Tests/AK/TestMemoryStream.cpp @@ -90,3 +90,36 @@ TEST_CASE(allocating_memory_stream_offset_of_oob) EXPECT(!offset.has_value()); } } + +TEST_CASE(allocating_memory_stream_offset_of_after_chunk_reorder) +{ + AllocatingMemoryStream stream; + + // First, fill exactly one chunk (in groups of 16 bytes). This chunk will be reordered. + for (size_t i = 0; i < AllocatingMemoryStream::CHUNK_SIZE / 16; ++i) + MUST(stream.write_until_depleted("AAAAAAAAAAAAAAAA"sv.bytes())); + + // Append a few additional bytes to create a second chunk. + MUST(stream.write_until_depleted("BCDEFGHIJKLMNOPQ"sv.bytes())); + + // Read back the first chunk, which should reorder it to the end of the list. + // The chunk that we wrote to the second time is now the first one. + MUST(stream.discard(AllocatingMemoryStream::CHUNK_SIZE)); + + { + auto offset = MUST(stream.offset_of("A"sv.bytes())); + EXPECT(!offset.has_value()); + } + + { + auto offset = MUST(stream.offset_of("B"sv.bytes())); + EXPECT(offset.has_value()); + EXPECT_EQ(offset.value(), 0ul); + } + + { + auto offset = MUST(stream.offset_of("Q"sv.bytes())); + EXPECT(offset.has_value()); + EXPECT_EQ(offset.value(), 15ul); + } +} |