summaryrefslogtreecommitdiff
path: root/Tests
diff options
context:
space:
mode:
authorTimothy Flynn <trflynn89@pm.me>2023-01-21 12:34:01 -0500
committerLinus Groh <mail@linusgroh.de>2023-01-22 01:03:13 +0000
commit12c8bc3e856300b7a42c2027df042ee22f21077f (patch)
tree5aa34c89c1fb163a1912d09346874bdcfdfc7e6d /Tests
parent8aca8e82cbaefc92f7622e8f793c95aa7d07411f (diff)
downloadserenity-12c8bc3e856300b7a42c2027df042ee22f21077f.zip
AK: Add a String factory to create a string from a single code point
Diffstat (limited to 'Tests')
-rw-r--r--Tests/AK/TestString.cpp25
1 files changed, 24 insertions, 1 deletions
diff --git a/Tests/AK/TestString.cpp b/Tests/AK/TestString.cpp
index c5785c7dab..f90b7bf393 100644
--- a/Tests/AK/TestString.cpp
+++ b/Tests/AK/TestString.cpp
@@ -4,9 +4,12 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
+// This is included first on purpose. We specifically do not want LibTest to override VERIFY here so
+// that we can actually test that some String factory methods cause a crash with invalid input.
+#include <AK/String.h>
+
#include <LibTest/TestCase.h>
-#include <AK/String.h>
#include <AK/StringBuilder.h>
#include <AK/Try.h>
#include <AK/Utf8View.h>
@@ -64,6 +67,26 @@ TEST_CASE(long_strings)
EXPECT_EQ(string.bytes_as_string_view(), "abcdefgh"sv);
}
+TEST_CASE(from_code_points)
+{
+ for (u32 code_point = 0; code_point < 0x80; ++code_point) {
+ auto string = String::from_code_point(code_point);
+
+ auto ch = static_cast<char>(code_point);
+ StringView view { &ch, 1 };
+
+ EXPECT_EQ(string, view);
+ }
+
+ auto string = String::from_code_point(0x10ffff);
+ EXPECT_EQ(string, "\xF4\x8F\xBF\xBF"sv);
+
+ EXPECT_CRASH("Creating a string from an invalid code point", [] {
+ String::from_code_point(0xffffffff);
+ return Test::Crash::Failure::DidNotCrash;
+ });
+}
+
TEST_CASE(substring)
{
auto superstring = MUST(String::from_utf8("Hello I am a long string"sv));