|
4 | 4 | * SPDX-License-Identifier: BSD-2-Clause
|
5 | 5 | */
|
6 | 6 |
|
| 7 | +#include <AK/BufferedStream.h> |
7 | 8 | #include <AK/MemoryStream.h>
|
8 | 9 | #include <AK/String.h>
|
9 | 10 | #include <LibTest/TestCase.h>
|
@@ -270,3 +271,30 @@ TEST_CASE(fixed_memory_read_in_place)
|
270 | 271 | EXPECT_EQ(characters_again, some_words.bytes());
|
271 | 272 | EXPECT(mutable_stream.is_eof());
|
272 | 273 | }
|
| 274 | + |
| 275 | +TEST_CASE(buffered_memory_stream_read_line) |
| 276 | +{ |
| 277 | + auto array = Array<u8, 32> {}; |
| 278 | + |
| 279 | + // First line: 8 bytes, second line: 24 bytes |
| 280 | + array.fill('A'); |
| 281 | + array[7] = '\n'; |
| 282 | + array[31] = '\n'; |
| 283 | + |
| 284 | + auto memory_stream = make<FixedMemoryStream>(array.span(), FixedMemoryStream::Mode::ReadOnly); |
| 285 | + |
| 286 | + // Buffer for buffered seekable is larger than the stream, so stream goes EOF immediately on read |
| 287 | + auto buffered_stream = TRY_OR_FAIL(InputBufferedSeekable<FixedMemoryStream>::create(move(memory_stream), 64)); |
| 288 | + |
| 289 | + // Buffer is only 16 bytes, first read succeeds, second fails |
| 290 | + auto buffer = TRY_OR_FAIL(ByteBuffer::create_zeroed(16)); |
| 291 | + |
| 292 | + auto read_bytes = TRY_OR_FAIL(buffered_stream->read_line(buffer)); |
| 293 | + |
| 294 | + EXPECT_EQ(read_bytes, "AAAAAAA"sv); |
| 295 | + |
| 296 | + auto read_or_error = buffered_stream->read_line(buffer); |
| 297 | + |
| 298 | + EXPECT(read_or_error.is_error()); |
| 299 | + EXPECT_EQ(read_or_error.error().code(), EMSGSIZE); |
| 300 | +} |
0 commit comments