summaryrefslogtreecommitdiff
path: root/Tests/LibCore/TestLibCoreStream.cpp
diff options
context:
space:
mode:
authorTim Schumacher <timschumi@gmx.de>2023-01-16 16:46:18 +0100
committerJelle Raaijmakers <jelle@gmta.nl>2023-01-17 21:56:56 +0100
commitadd2e2c076d493885f9facbeb32cadc492743315 (patch)
tree2299873b42725d333448fad849cd6530ec28e14a /Tests/LibCore/TestLibCoreStream.cpp
parentbdf991fe763b17bc4ac7014483f782664c2fdad1 (diff)
downloadserenity-add2e2c076d493885f9facbeb32cadc492743315.zip
LibCore: Do short forward seeks by discarding bytes from the buffer
This saves us an actual seek and rereading already stored buffer data in cases where the seek is entirely covered by the currently buffered data. This is especially important since we implement `discard` using `seek` for seekable streams.
Diffstat (limited to 'Tests/LibCore/TestLibCoreStream.cpp')
-rw-r--r--Tests/LibCore/TestLibCoreStream.cpp19
1 files changed, 17 insertions, 2 deletions
diff --git a/Tests/LibCore/TestLibCoreStream.cpp b/Tests/LibCore/TestLibCoreStream.cpp
index 48fb655ec8..bf256814cc 100644
--- a/Tests/LibCore/TestLibCoreStream.cpp
+++ b/Tests/LibCore/TestLibCoreStream.cpp
@@ -470,8 +470,9 @@ TEST_CASE(buffered_small_file_read)
TEST_CASE(buffered_file_tell_and_seek)
{
+ // We choose a buffer size of 12 bytes to cover half of the input file.
auto file = Core::Stream::File::open("/usr/Tests/LibCore/small.txt"sv, Core::Stream::OpenMode::Read).release_value();
- auto buffered_file = Core::Stream::BufferedFile::create(move(file)).release_value();
+ auto buffered_file = Core::Stream::BufferedFile::create(move(file), 12).release_value();
// Initial state.
{
@@ -529,13 +530,27 @@ TEST_CASE(buffered_file_tell_and_seek)
EXPECT_EQ(current_offset, 0);
}
- // Read the first character.
+ // Read the first character. This should prime the buffer if it hasn't happened already.
{
auto character = buffered_file->read_value<char>().release_value();
EXPECT_EQ(character, 'W');
auto current_offset = buffered_file->tell().release_value();
EXPECT_EQ(current_offset, 1);
}
+
+ // Seek beyond the buffer size, which should invalidate the buffer.
+ {
+ auto current_offset = buffered_file->seek(12, Core::Stream::SeekMode::SetPosition).release_value();
+ EXPECT_EQ(current_offset, 12);
+ }
+
+ // Ensure that we still read the correct contents from the new offset with a (presumably) freshly filled buffer.
+ {
+ auto character = buffered_file->read_value<char>().release_value();
+ EXPECT_EQ(character, 'r');
+ auto current_offset = buffered_file->tell().release_value();
+ EXPECT_EQ(current_offset, 13);
+ }
}
constexpr auto buffered_sent_data = "Well hello friends!\n:^)\nThis shouldn't be present. :^("sv;