mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-28 18:40:29 +00:00
LibCore: Fix relative seeking in IODevice
The recently introduced read buffer in IODevice broke relative seeking. The amount of data in the buffer wouldn't get taken into account.
This commit is contained in:
parent
b818d4c7e3
commit
cdaa179eeb
Notes:
sideshowbarker
2024-07-17 23:21:29 +09:00
Author: https://github.com/standardexe Commit: https://github.com/SerenityOS/serenity/commit/cdaa179eeb2 Pull-request: https://github.com/SerenityOS/serenity/pull/11130
4 changed files with 35 additions and 1 deletions
1
Tests/LibCore/10kb.txt
Normal file
1
Tests/LibCore/10kb.txt
Normal file
File diff suppressed because one or more lines are too long
|
@ -9,4 +9,4 @@ foreach(source IN LISTS TEST_SOURCES)
|
|||
serenity_test("${source}" LibCore)
|
||||
endforeach()
|
||||
|
||||
install(FILES long_lines.txt DESTINATION usr/Tests/LibCore)
|
||||
install(FILES long_lines.txt 10kb.txt DESTINATION usr/Tests/LibCore)
|
||||
|
|
|
@ -44,3 +44,35 @@ TEST_CASE(file_readline)
|
|||
EXPECT_EQ(inputData[i], outputData[i]);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE(file_get_read_position)
|
||||
{
|
||||
const String path = "10kb.txt";
|
||||
auto file = Core::File::open(path, Core::OpenMode::ReadOnly).release_value();
|
||||
|
||||
const size_t step_size = 98;
|
||||
for (size_t i = 0; i < 10240 - step_size; i += step_size) {
|
||||
auto read_buffer = file->read(step_size);
|
||||
EXPECT_EQ(read_buffer.size(), step_size);
|
||||
|
||||
for (size_t j = 0; j < read_buffer.size(); j++) {
|
||||
EXPECT_EQ(static_cast<u32>(read_buffer[j] - '0'), (i + j) % 10);
|
||||
}
|
||||
|
||||
off_t offset = 0;
|
||||
VERIFY(file->seek(0, Core::SeekMode::FromCurrentPosition, &offset));
|
||||
EXPECT_EQ(offset, static_cast<off_t>(i + step_size));
|
||||
}
|
||||
|
||||
{
|
||||
off_t offset = 0;
|
||||
VERIFY(file->seek(0, Core::SeekMode::FromEndPosition, &offset));
|
||||
EXPECT_EQ(offset, 10240);
|
||||
}
|
||||
|
||||
{
|
||||
off_t offset = 0;
|
||||
VERIFY(file->seek(0, Core::SeekMode::SetPosition, &offset));
|
||||
EXPECT_EQ(offset, 0);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -245,6 +245,7 @@ bool IODevice::seek(i64 offset, SeekMode mode, off_t* pos)
|
|||
break;
|
||||
case SeekMode::FromCurrentPosition:
|
||||
m = SEEK_CUR;
|
||||
offset -= m_buffered_data.size();
|
||||
break;
|
||||
case SeekMode::FromEndPosition:
|
||||
m = SEEK_END;
|
||||
|
|
Loading…
Reference in a new issue