summaryrefslogtreecommitdiff
path: root/AK/Tests/TestStringView.cpp
diff options
context:
space:
mode:
authorAndrew Kaster <andrewdkaster@gmail.com>2020-05-02 12:21:20 -0600
committerAndreas Kling <kling@serenityos.org>2020-05-04 09:39:05 +0200
commit3eb3c2477fbacff9bbeac771c1d2ecc4ddec03e7 (patch)
tree745d36ca934faf16728a623bef42b20a0ea22b13 /AK/Tests/TestStringView.cpp
parent94c28552c6e29cebd83bf48084262c1003f7baab (diff)
downloadserenity-3eb3c2477fbacff9bbeac771c1d2ecc4ddec03e7.zip
AK: Add StringView::find_first/last_of
These methods search from the beginning or end of a string for the first character in the input StringView and returns the position in the string of the first match. Note that this is not a substring match. Each comes with single char overloads for efficiency.
Diffstat (limited to 'AK/Tests/TestStringView.cpp')
-rw-r--r--AK/Tests/TestStringView.cpp43
1 files changed, 43 insertions, 0 deletions
diff --git a/AK/Tests/TestStringView.cpp b/AK/Tests/TestStringView.cpp
index 53f2704b46..e26fa65f63 100644
--- a/AK/Tests/TestStringView.cpp
+++ b/AK/Tests/TestStringView.cpp
@@ -111,4 +111,47 @@ TEST_CASE(lines)
EXPECT_EQ(test_string_vector.at(2).is_empty(), true);
}
+TEST_CASE(find_first_of)
+{
+ String test_string = "aabbcc_xy_ccbbaa";
+ StringView test_string_view = test_string.view();
+
+ EXPECT_EQ(test_string_view.find_first_of('b').has_value(), true);
+ EXPECT_EQ(test_string_view.find_first_of('b').value(), 2U);
+
+ EXPECT_EQ(test_string_view.find_first_of('_').has_value(), true);
+ EXPECT_EQ(test_string_view.find_first_of('_').value(), 6U);
+
+ EXPECT_EQ(test_string_view.find_first_of("bc").has_value(), true);
+ EXPECT_EQ(test_string_view.find_first_of("bc").value(), 2U);
+
+ EXPECT_EQ(test_string_view.find_first_of("yx").has_value(), true);
+ EXPECT_EQ(test_string_view.find_first_of("yx").value(), 7U);
+
+ EXPECT_EQ(test_string_view.find_first_of('n').has_value(), false);
+ EXPECT_EQ(test_string_view.find_first_of("defg").has_value(), false);
+}
+
+TEST_CASE(find_last_of)
+{
+ String test_string = "aabbcc_xy_ccbbaa";
+ StringView test_string_view = test_string.view();
+
+ EXPECT_EQ(test_string_view.find_last_of('b').has_value(), true);
+ EXPECT_EQ(test_string_view.find_last_of('b').value(), 13U);
+
+ EXPECT_EQ(test_string_view.find_last_of('_').has_value(), true);
+ EXPECT_EQ(test_string_view.find_last_of('_').value(), 9U);
+
+ EXPECT_EQ(test_string_view.find_last_of("bc").has_value(), true);
+ EXPECT_EQ(test_string_view.find_last_of("bc").value(), 13U);
+
+ EXPECT_EQ(test_string_view.find_last_of("yx").has_value(), true);
+ EXPECT_EQ(test_string_view.find_last_of("yx").value(), 8U);
+
+ EXPECT_EQ(test_string_view.find_last_of('3').has_value(), false);
+ EXPECT_EQ(test_string_view.find_last_of("fghi").has_value(), false);
+}
+
+
TEST_MAIN(StringView)