2021-05-09 03:54:22 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2021, Ali Mohammad Pur <mpfard@serenity.org>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <LibTest/TestSuite.h>
|
|
|
|
|
|
|
|
#include <AK/Tuple.h>
|
|
|
|
|
|
|
|
TEST_CASE(basic)
|
|
|
|
{
|
2022-12-04 18:02:33 +00:00
|
|
|
Tuple<int, DeprecatedString> value { 1, "foo" };
|
2021-05-09 03:54:22 +00:00
|
|
|
EXPECT_EQ(value.get<int>(), 1);
|
2022-12-04 18:02:33 +00:00
|
|
|
EXPECT_EQ(value.get<DeprecatedString>(), "foo");
|
2021-05-09 03:54:22 +00:00
|
|
|
EXPECT_EQ(value.get<0>(), 1);
|
|
|
|
EXPECT_EQ(value.get<1>(), "foo");
|
|
|
|
|
|
|
|
// Move assignment
|
|
|
|
value = { 2, "bar" };
|
|
|
|
EXPECT_EQ(value.get<int>(), 2);
|
2022-12-04 18:02:33 +00:00
|
|
|
EXPECT_EQ(value.get<DeprecatedString>(), "bar");
|
2021-05-09 03:54:22 +00:00
|
|
|
EXPECT_EQ(value.get<0>(), 2);
|
|
|
|
EXPECT_EQ(value.get<1>(), "bar");
|
|
|
|
|
|
|
|
// Copy ctor
|
|
|
|
auto other_value { value };
|
|
|
|
EXPECT_EQ(other_value.get<int>(), 2);
|
2022-12-04 18:02:33 +00:00
|
|
|
EXPECT_EQ(other_value.get<DeprecatedString>(), "bar");
|
2021-05-09 03:54:22 +00:00
|
|
|
EXPECT_EQ(other_value.get<0>(), 2);
|
|
|
|
EXPECT_EQ(other_value.get<1>(), "bar");
|
|
|
|
|
|
|
|
// Move ctor
|
|
|
|
auto moved_to_value { move(value) };
|
|
|
|
EXPECT_EQ(moved_to_value.get<int>(), 2);
|
2022-12-04 18:02:33 +00:00
|
|
|
EXPECT_EQ(moved_to_value.get<DeprecatedString>(), "bar");
|
2021-05-09 03:54:22 +00:00
|
|
|
EXPECT_EQ(moved_to_value.get<0>(), 2);
|
|
|
|
EXPECT_EQ(moved_to_value.get<1>(), "bar");
|
|
|
|
|
|
|
|
// Copy assignment
|
|
|
|
value = moved_to_value;
|
|
|
|
EXPECT_EQ(moved_to_value.get<int>(), 2);
|
2022-12-04 18:02:33 +00:00
|
|
|
EXPECT_EQ(moved_to_value.get<DeprecatedString>(), "bar");
|
2021-05-09 03:54:22 +00:00
|
|
|
EXPECT_EQ(moved_to_value.get<0>(), 2);
|
|
|
|
EXPECT_EQ(moved_to_value.get<1>(), "bar");
|
|
|
|
EXPECT_EQ(value.get<int>(), 2);
|
2022-12-04 18:02:33 +00:00
|
|
|
EXPECT_EQ(value.get<DeprecatedString>(), "bar");
|
2021-05-09 03:54:22 +00:00
|
|
|
EXPECT_EQ(value.get<0>(), 2);
|
|
|
|
EXPECT_EQ(value.get<1>(), "bar");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE(no_copy)
|
|
|
|
{
|
|
|
|
struct NoCopy {
|
|
|
|
AK_MAKE_NONCOPYABLE(NoCopy);
|
2023-06-16 14:13:41 +00:00
|
|
|
AK_MAKE_DEFAULT_MOVABLE(NoCopy);
|
2021-05-09 03:54:22 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
NoCopy() = default;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Deleted copy ctor should not cause an issue so long as the value isn't copied.
|
|
|
|
Tuple<NoCopy, int, int> value { {}, 1, 2 };
|
|
|
|
auto foo = move(value);
|
|
|
|
EXPECT_EQ(foo.get<1>(), 1);
|
|
|
|
EXPECT_EQ(foo.get<2>(), 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE(apply)
|
|
|
|
{
|
2022-12-04 18:02:33 +00:00
|
|
|
Tuple<int, int, DeprecatedString> args { 1, 2, "foo" };
|
2021-05-09 03:54:22 +00:00
|
|
|
|
|
|
|
// With copy
|
|
|
|
{
|
|
|
|
bool was_called = false;
|
2022-12-04 18:02:33 +00:00
|
|
|
args.apply_as_args([&](int a, int b, DeprecatedString c) {
|
2021-05-09 03:54:22 +00:00
|
|
|
was_called = true;
|
|
|
|
EXPECT_EQ(a, 1);
|
|
|
|
EXPECT_EQ(b, 2);
|
|
|
|
EXPECT_EQ(c, "foo");
|
|
|
|
});
|
|
|
|
EXPECT(was_called);
|
|
|
|
}
|
|
|
|
|
|
|
|
// With reference
|
|
|
|
{
|
|
|
|
bool was_called = false;
|
2022-12-04 18:02:33 +00:00
|
|
|
args.apply_as_args([&](int& a, int& b, DeprecatedString& c) {
|
2021-05-09 03:54:22 +00:00
|
|
|
was_called = true;
|
|
|
|
EXPECT_EQ(a, 1);
|
|
|
|
EXPECT_EQ(b, 2);
|
|
|
|
EXPECT_EQ(c, "foo");
|
|
|
|
});
|
|
|
|
EXPECT(was_called);
|
|
|
|
}
|
|
|
|
|
|
|
|
// With const reference, taken from a const tuple
|
|
|
|
{
|
|
|
|
bool was_called = false;
|
2022-04-01 17:58:27 +00:00
|
|
|
auto const& args_ref = args;
|
2022-12-04 18:02:33 +00:00
|
|
|
args_ref.apply_as_args([&](int const& a, int const& b, DeprecatedString const& c) {
|
2021-05-09 03:54:22 +00:00
|
|
|
was_called = true;
|
|
|
|
EXPECT_EQ(a, 1);
|
|
|
|
EXPECT_EQ(b, 2);
|
|
|
|
EXPECT_EQ(c, "foo");
|
|
|
|
});
|
|
|
|
EXPECT(was_called);
|
|
|
|
}
|
|
|
|
}
|