summaryrefslogtreecommitdiff
path: root/Tests
diff options
context:
space:
mode:
authorAli Mohammad Pur <ali.mpfard@gmail.com>2022-04-03 15:36:34 +0430
committerAndreas Kling <kling@serenityos.org>2022-04-04 12:48:31 +0200
commit221ecf17d34e5d82ca5ab74a10c66c0c9763fe3f (patch)
tree42c31f64afa7eae16bc86b2561ab4300364b317a /Tests
parent8f1ba8db4f5ff5765ee7b316106ee1f9fefea006 (diff)
downloadserenity-221ecf17d34e5d82ca5ab74a10c66c0c9763fe3f.zip
AK: Allow Optional<T&> to exist
This implements Optional<T&> as a T*, whose presence has been missing since the early days of Optional. As a lot of find_foo() APIs return an Optional<T> which imposes a pointless copy on the underlying value, and can sometimes be very misleading, with this change, those APIs can return Optional<T&>.
Diffstat (limited to 'Tests')
-rw-r--r--Tests/AK/TestOptional.cpp58
1 files changed, 58 insertions, 0 deletions
diff --git a/Tests/AK/TestOptional.cpp b/Tests/AK/TestOptional.cpp
index e96c12a23c..723d3dd74b 100644
--- a/Tests/AK/TestOptional.cpp
+++ b/Tests/AK/TestOptional.cpp
@@ -211,3 +211,61 @@ TEST_CASE(test_copy_ctor_and_dtor_called)
static_assert(!IsDestructible<Optional<NonDestructible>>);
#endif
}
+
+TEST_CASE(basic_optional_reference)
+{
+ Optional<int&> x;
+ EXPECT_EQ(x.has_value(), false);
+ int a = 3;
+ x = a;
+ EXPECT_EQ(x.has_value(), true);
+ EXPECT_EQ(x.value(), 3);
+ EXPECT_EQ(&x.value(), &a);
+
+ Optional<int const&> y;
+ EXPECT_EQ(y.has_value(), false);
+ int b = 3;
+ y = b;
+ EXPECT_EQ(y.has_value(), true);
+ EXPECT_EQ(y.value(), 3);
+ EXPECT_EQ(&y.value(), &b);
+ static_assert(IsConst<RemoveReference<decltype(y.value())>>);
+}
+
+TEST_CASE(move_optional_reference)
+{
+ Optional<int&> x;
+ EXPECT_EQ(x.has_value(), false);
+ int b = 3;
+ x = b;
+ EXPECT_EQ(x.has_value(), true);
+ EXPECT_EQ(x.value(), 3);
+
+ Optional<int&> y;
+ y = move(x);
+ EXPECT_EQ(y.has_value(), true);
+ EXPECT_EQ(y.value(), 3);
+ EXPECT_EQ(x.has_value(), false);
+}
+
+TEST_CASE(short_notation_reference)
+{
+ StringView test = "foo";
+ Optional<StringView&> value = test;
+
+ EXPECT_EQ(value->length(), 3u);
+ EXPECT_EQ(*value, "foo");
+}
+
+TEST_CASE(comparison_reference)
+{
+ StringView test = "foo";
+ Optional<StringView&> opt0;
+ Optional<StringView const&> opt1 = test;
+ Optional<String> opt2 = "foo";
+ Optional<StringView> opt3 = "bar";
+
+ EXPECT_NE(opt0, opt1);
+ EXPECT_EQ(opt1, opt2);
+ EXPECT_NE(opt1, opt3);
+}