summaryrefslogtreecommitdiff
path: root/Tests
diff options
context:
space:
mode:
authorAndrew Kaster <akaster@serenityos.org>2023-02-19 18:34:29 -0700
committerLinus Groh <mail@linusgroh.de>2023-02-21 10:57:44 +0100
commit0ea697ace57fbc1de834402077d6f0bde1b82f43 (patch)
treea9c57033c72076a1dede71c288cf0adc859d3462 /Tests
parent7ac7a73758f7060bc19b48b973a496bd8486dbae (diff)
downloadserenity-0ea697ace57fbc1de834402077d6f0bde1b82f43.zip
AK: Add String::from_stream method
The caller is responsible for determining how long the string is that they want to read.
Diffstat (limited to 'Tests')
-rw-r--r--Tests/AK/TestString.cpp49
1 files changed, 49 insertions, 0 deletions
diff --git a/Tests/AK/TestString.cpp b/Tests/AK/TestString.cpp
index ae48664b83..59668da04f 100644
--- a/Tests/AK/TestString.cpp
+++ b/Tests/AK/TestString.cpp
@@ -10,6 +10,7 @@
#include <LibTest/TestCase.h>
+#include <AK/MemoryStream.h>
#include <AK/StringBuilder.h>
#include <AK/Try.h>
#include <AK/Utf8View.h>
@@ -67,6 +68,54 @@ TEST_CASE(long_strings)
EXPECT_EQ(string.bytes_as_string_view(), "abcdefgh"sv);
}
+TEST_CASE(long_streams)
+{
+ {
+ u8 bytes[64] = {};
+ constexpr auto test_view = "Well, hello friends"sv;
+ FixedMemoryStream stream(Bytes { bytes, sizeof(bytes) });
+ MUST(stream.write(test_view.bytes()));
+ MUST(stream.seek(0));
+
+ auto string = MUST(String::from_stream(stream, test_view.length()));
+
+ EXPECT_EQ(string.is_short_string(), false);
+ EXPECT_EQ(string.bytes().size(), 19u);
+ EXPECT_EQ(string.bytes_as_string_view(), test_view);
+ }
+
+ {
+ AllocatingMemoryStream stream;
+ MUST(stream.write(("abc"sv).bytes()));
+
+ auto string = MUST(String::from_stream(stream, 3u));
+
+ EXPECT_EQ(string.is_short_string(), true);
+ EXPECT_EQ(string.bytes().size(), 3u);
+ EXPECT_EQ(string.bytes_as_string_view(), "abc"sv);
+ }
+
+ {
+ AllocatingMemoryStream stream;
+ MUST(stream.write(("0123456789"sv).bytes()));
+
+ auto string = MUST(String::from_stream(stream, 9u));
+
+ EXPECT_EQ(string.is_short_string(), false);
+ EXPECT_EQ(string.bytes().size(), 9u);
+ EXPECT_EQ(string.bytes_as_string_view(), "012345678"sv);
+ }
+
+ {
+ AllocatingMemoryStream stream;
+ MUST(stream.write_value(0xffffffff));
+ MUST(stream.write_value(0xffffffff));
+ MUST(stream.write_value(0xffffffff));
+ auto error_or_string = String::from_stream(stream, stream.used_buffer_size());
+ EXPECT_EQ(error_or_string.is_error(), true);
+ }
+}
+
TEST_CASE(from_code_points)
{
for (u32 code_point = 0; code_point < 0x80; ++code_point) {