2020-01-18 11:38:21 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
2021-07-03 13:14:36 +03:00
|
|
|
* Copyright (c) 2021, Daniel Bertalan <dani@danielbertalan.dev>
|
2020-01-18 11:38:21 +03:00
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 11:38:21 +03:00
|
|
|
*/
|
|
|
|
|
2021-04-25 08:53:23 +03:00
|
|
|
#include <LibTest/TestCase.h>
|
2019-08-05 22:47:36 +03:00
|
|
|
|
2023-12-16 17:19:34 +03:00
|
|
|
#include <AK/ByteString.h>
|
2019-08-05 22:47:36 +03:00
|
|
|
#include <AK/Optional.h>
|
2021-11-10 13:05:21 +03:00
|
|
|
#include <AK/Vector.h>
|
2019-08-05 22:47:36 +03:00
|
|
|
|
|
|
|
TEST_CASE(basic_optional)
|
|
|
|
{
|
|
|
|
Optional<int> x;
|
|
|
|
EXPECT_EQ(x.has_value(), false);
|
|
|
|
x = 3;
|
|
|
|
EXPECT_EQ(x.has_value(), true);
|
|
|
|
EXPECT_EQ(x.value(), 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE(move_optional)
|
|
|
|
{
|
|
|
|
Optional<int> x;
|
|
|
|
EXPECT_EQ(x.has_value(), false);
|
|
|
|
x = 3;
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2021-09-04 01:44:10 +03:00
|
|
|
TEST_CASE(optional_rvalue_ref_qualified_getters)
|
|
|
|
{
|
|
|
|
struct DontCopyMe {
|
|
|
|
DontCopyMe() { }
|
|
|
|
~DontCopyMe() = default;
|
|
|
|
DontCopyMe(DontCopyMe&&) = default;
|
|
|
|
DontCopyMe& operator=(DontCopyMe&&) = default;
|
|
|
|
DontCopyMe(DontCopyMe const&) = delete;
|
|
|
|
DontCopyMe& operator=(DontCopyMe const&) = delete;
|
|
|
|
|
|
|
|
int x { 13 };
|
|
|
|
};
|
|
|
|
|
|
|
|
auto make_an_optional = []() -> Optional<DontCopyMe> {
|
|
|
|
return DontCopyMe {};
|
|
|
|
};
|
|
|
|
|
|
|
|
EXPECT_EQ(make_an_optional().value().x, 13);
|
|
|
|
EXPECT_EQ(make_an_optional().value_or(DontCopyMe {}).x, 13);
|
|
|
|
}
|
|
|
|
|
2019-08-05 23:27:47 +03:00
|
|
|
TEST_CASE(optional_leak_1)
|
|
|
|
{
|
|
|
|
struct Structure {
|
2023-12-16 17:19:34 +03:00
|
|
|
Optional<ByteString> str;
|
2019-08-05 23:27:47 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
// This used to leak, it does not anymore.
|
|
|
|
Vector<Structure> vec;
|
|
|
|
vec.append({ "foo" });
|
|
|
|
EXPECT_EQ(vec[0].str.has_value(), true);
|
|
|
|
EXPECT_EQ(vec[0].str.value(), "foo");
|
|
|
|
}
|
|
|
|
|
2020-12-30 23:16:37 +03:00
|
|
|
TEST_CASE(short_notation)
|
|
|
|
{
|
2022-07-11 20:32:29 +03:00
|
|
|
Optional<StringView> value = "foo"sv;
|
2020-12-30 23:16:37 +03:00
|
|
|
|
|
|
|
EXPECT_EQ(value->length(), 3u);
|
|
|
|
EXPECT_EQ(*value, "foo");
|
|
|
|
}
|
2021-06-01 11:12:53 +03:00
|
|
|
|
|
|
|
TEST_CASE(comparison_without_values)
|
|
|
|
{
|
|
|
|
Optional<StringView> opt0;
|
|
|
|
Optional<StringView> opt1;
|
2023-12-16 17:19:34 +03:00
|
|
|
Optional<ByteString> opt2;
|
2021-06-01 11:12:53 +03:00
|
|
|
EXPECT_EQ(opt0, opt1);
|
|
|
|
EXPECT_EQ(opt0, opt2);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE(comparison_with_values)
|
|
|
|
{
|
|
|
|
Optional<StringView> opt0;
|
2022-07-11 20:32:29 +03:00
|
|
|
Optional<StringView> opt1 = "foo"sv;
|
2023-12-16 17:19:34 +03:00
|
|
|
Optional<ByteString> opt2 = "foo"sv;
|
2022-07-11 20:32:29 +03:00
|
|
|
Optional<StringView> opt3 = "bar"sv;
|
2021-06-01 11:12:53 +03:00
|
|
|
EXPECT_NE(opt0, opt1);
|
|
|
|
EXPECT_EQ(opt1, opt2);
|
|
|
|
EXPECT_NE(opt1, opt3);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE(comparison_to_underlying_types)
|
|
|
|
{
|
2023-12-16 17:19:34 +03:00
|
|
|
Optional<ByteString> opt0;
|
|
|
|
EXPECT_NE(opt0, ByteString());
|
2021-06-01 11:12:53 +03:00
|
|
|
EXPECT_NE(opt0, "foo");
|
|
|
|
|
2022-07-11 20:32:29 +03:00
|
|
|
Optional<StringView> opt1 = "foo"sv;
|
2021-06-01 11:12:53 +03:00
|
|
|
EXPECT_EQ(opt1, "foo");
|
|
|
|
EXPECT_NE(opt1, "bar");
|
2023-12-16 17:19:34 +03:00
|
|
|
EXPECT_EQ(opt1, ByteString("foo"));
|
2021-06-01 11:12:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE(comparison_with_numeric_types)
|
|
|
|
{
|
|
|
|
Optional<u8> opt0;
|
|
|
|
EXPECT_NE(opt0, 0);
|
|
|
|
Optional<u8> opt1 = 7;
|
|
|
|
EXPECT_EQ(opt1, 7);
|
|
|
|
EXPECT_EQ(opt1, 7.0);
|
|
|
|
EXPECT_EQ(opt1, 7u);
|
|
|
|
EXPECT_NE(opt1, -2);
|
|
|
|
}
|
2021-07-03 13:14:36 +03:00
|
|
|
|
|
|
|
TEST_CASE(test_copy_ctor_and_dtor_called)
|
|
|
|
{
|
|
|
|
#ifdef AK_HAVE_CONDITIONALLY_TRIVIAL
|
|
|
|
static_assert(IsTriviallyDestructible<Optional<u8>>);
|
|
|
|
static_assert(IsTriviallyCopyable<Optional<u8>>);
|
|
|
|
static_assert(IsTriviallyCopyConstructible<Optional<u8>>);
|
|
|
|
static_assert(IsTriviallyCopyAssignable<Optional<u8>>);
|
|
|
|
// These can't be trivial as we have to clear the original object.
|
|
|
|
static_assert(!IsTriviallyMoveConstructible<Optional<u8>>);
|
|
|
|
static_assert(!IsTriviallyMoveAssignable<Optional<u8>>);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
struct DestructionChecker {
|
|
|
|
explicit DestructionChecker(bool& was_destroyed)
|
|
|
|
: m_was_destroyed(was_destroyed)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~DestructionChecker()
|
|
|
|
{
|
|
|
|
m_was_destroyed = true;
|
|
|
|
}
|
|
|
|
bool& m_was_destroyed;
|
|
|
|
};
|
|
|
|
|
|
|
|
static_assert(!IsTriviallyDestructible<Optional<DestructionChecker>>);
|
|
|
|
|
|
|
|
bool was_destroyed = false;
|
|
|
|
{
|
|
|
|
Optional<DestructionChecker> test_optional = DestructionChecker { was_destroyed };
|
|
|
|
}
|
|
|
|
EXPECT(was_destroyed);
|
|
|
|
|
|
|
|
struct CopyChecker {
|
|
|
|
explicit CopyChecker(bool& was_copy_constructed)
|
|
|
|
: m_was_copy_constructed(was_copy_constructed)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
CopyChecker(CopyChecker const& other)
|
2021-07-03 13:14:36 +03:00
|
|
|
: m_was_copy_constructed(other.m_was_copy_constructed)
|
|
|
|
{
|
|
|
|
m_was_copy_constructed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool& m_was_copy_constructed;
|
|
|
|
};
|
|
|
|
|
|
|
|
static_assert(IsCopyConstructible<Optional<CopyChecker>>);
|
|
|
|
static_assert(!IsTriviallyCopyConstructible<Optional<CopyChecker>>);
|
|
|
|
|
|
|
|
bool was_copy_constructed = false;
|
|
|
|
Optional<CopyChecker> copy1 = CopyChecker { was_copy_constructed };
|
|
|
|
Optional<CopyChecker> copy2 = copy1;
|
|
|
|
EXPECT(was_copy_constructed);
|
|
|
|
|
|
|
|
struct MoveChecker {
|
|
|
|
explicit MoveChecker(bool& was_move_constructed)
|
|
|
|
: m_was_move_constructed(was_move_constructed)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
MoveChecker(MoveChecker const& other)
|
2021-07-03 13:14:36 +03:00
|
|
|
: m_was_move_constructed(other.m_was_move_constructed)
|
|
|
|
{
|
|
|
|
EXPECT(false);
|
2023-07-08 05:48:11 +03:00
|
|
|
}
|
2021-07-03 13:14:36 +03:00
|
|
|
|
|
|
|
MoveChecker(MoveChecker&& other)
|
|
|
|
: m_was_move_constructed(other.m_was_move_constructed)
|
|
|
|
{
|
|
|
|
m_was_move_constructed = true;
|
2023-07-08 05:48:11 +03:00
|
|
|
}
|
2021-07-03 13:14:36 +03:00
|
|
|
|
|
|
|
bool& m_was_move_constructed;
|
|
|
|
};
|
|
|
|
static_assert(IsMoveConstructible<Optional<MoveChecker>>);
|
|
|
|
static_assert(!IsTriviallyMoveConstructible<Optional<MoveChecker>>);
|
|
|
|
|
|
|
|
bool was_moved = false;
|
|
|
|
Optional<MoveChecker> move1 = MoveChecker { was_moved };
|
|
|
|
Optional<MoveChecker> move2 = move(move1);
|
|
|
|
EXPECT(was_moved);
|
|
|
|
|
|
|
|
#ifdef AK_HAVE_CONDITIONALLY_TRIVIAL
|
|
|
|
struct NonDestructible {
|
|
|
|
~NonDestructible() = delete;
|
|
|
|
};
|
|
|
|
static_assert(!IsDestructible<Optional<NonDestructible>>);
|
|
|
|
#endif
|
|
|
|
}
|
2022-04-03 14:06:34 +03:00
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2022-07-11 20:32:29 +03:00
|
|
|
StringView test = "foo"sv;
|
2022-04-03 14:06:34 +03:00
|
|
|
Optional<StringView&> value = test;
|
|
|
|
|
|
|
|
EXPECT_EQ(value->length(), 3u);
|
|
|
|
EXPECT_EQ(*value, "foo");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE(comparison_reference)
|
|
|
|
{
|
2022-07-11 20:32:29 +03:00
|
|
|
StringView test = "foo"sv;
|
2022-04-03 14:06:34 +03:00
|
|
|
Optional<StringView&> opt0;
|
|
|
|
Optional<StringView const&> opt1 = test;
|
2023-12-16 17:19:34 +03:00
|
|
|
Optional<ByteString> opt2 = "foo"sv;
|
2022-07-11 20:32:29 +03:00
|
|
|
Optional<StringView> opt3 = "bar"sv;
|
2022-04-03 14:06:34 +03:00
|
|
|
|
|
|
|
EXPECT_NE(opt0, opt1);
|
|
|
|
EXPECT_EQ(opt1, opt2);
|
|
|
|
EXPECT_NE(opt1, opt3);
|
|
|
|
}
|