diff --git a/Tests/LibCore/CMakeLists.txt b/Tests/LibCore/CMakeLists.txt index 1e3f4b50973..c83c2292655 100644 --- a/Tests/LibCore/CMakeLists.txt +++ b/Tests/LibCore/CMakeLists.txt @@ -13,4 +13,4 @@ endforeach() # NOTE: Required because of the LocalServer tests target_link_libraries(TestLibCoreStream LibThreading) -install(FILES long_lines.txt 10kb.txt DESTINATION usr/Tests/LibCore) +install(FILES long_lines.txt 10kb.txt small.txt DESTINATION usr/Tests/LibCore) diff --git a/Tests/LibCore/TestLibCoreStream.cpp b/Tests/LibCore/TestLibCoreStream.cpp index 2f1276b49c1..bef25c9176d 100644 --- a/Tests/LibCore/TestLibCoreStream.cpp +++ b/Tests/LibCore/TestLibCoreStream.cpp @@ -380,7 +380,7 @@ TEST_CASE(local_socket_write) // Buffered stream tests -TEST_CASE(buffered_file_read) +TEST_CASE(buffered_long_file_read) { auto maybe_file = Core::Stream::File::open("/usr/Tests/LibCore/long_lines.txt", Core::Stream::OpenMode::Read); EXPECT(!maybe_file.is_error()); @@ -402,6 +402,34 @@ TEST_CASE(buffered_file_read) EXPECT_EQ(maybe_after_seek_nread.value(), 3985ul); // 4095 - 110 } +TEST_CASE(buffered_small_file_read) +{ + auto maybe_file = Core::Stream::File::open("/usr/Tests/LibCore/small.txt", Core::Stream::OpenMode::Read); + EXPECT(!maybe_file.is_error()); + auto maybe_buffered_file = Core::Stream::BufferedFile::create(maybe_file.release_value()); + EXPECT(!maybe_buffered_file.is_error()); + auto file = maybe_buffered_file.release_value(); + + static constexpr StringView expected_lines[] { + "Well"sv, + "hello"sv, + "friends!"sv, + ":^)"sv + }; + + // Testing that we don't read out of bounds when the entire file fits into the buffer + auto buffer = ByteBuffer::create_uninitialized(4096).release_value(); + for (auto const& line : expected_lines) { + VERIFY(file.can_read_line().release_value()); + auto maybe_nread = file.read_line(buffer); + EXPECT(!maybe_nread.is_error()); + EXPECT_EQ(maybe_nread.value(), line.length()); + EXPECT_EQ(StringView(buffer.span().trim(maybe_nread.value())), line); + } + EXPECT(!file.can_read_line().is_error()); + EXPECT(!file.can_read_line().value()); +} + constexpr auto buffered_sent_data = "Well hello friends!\n:^)\nThis shouldn't be present. :^("sv; constexpr auto first_line = "Well hello friends!"sv; constexpr auto second_line = ":^)"sv; diff --git a/Tests/LibCore/small.txt b/Tests/LibCore/small.txt new file mode 100644 index 00000000000..de8d8a34709 --- /dev/null +++ b/Tests/LibCore/small.txt @@ -0,0 +1,4 @@ +Well +hello +friends! +:^)