2020-11-22 00:23:17 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, the SerenityOS developers.
|
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-11-22 00:23:17 +00:00
|
|
|
*/
|
|
|
|
|
2021-04-25 05:53:23 +00:00
|
|
|
#include <LibTest/TestCase.h>
|
2020-11-22 00:23:17 +00:00
|
|
|
|
|
|
|
#include <AK/NeverDestroyed.h>
|
|
|
|
#include <AK/StdLibExtras.h>
|
|
|
|
|
|
|
|
struct Counter {
|
|
|
|
Counter() = default;
|
|
|
|
|
|
|
|
~Counter() { ++num_destroys; }
|
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
Counter(Counter const&)
|
2020-11-22 00:23:17 +00:00
|
|
|
{
|
|
|
|
++num_copies;
|
|
|
|
}
|
|
|
|
|
|
|
|
Counter(Counter&&) { ++num_moves; }
|
|
|
|
|
|
|
|
int num_copies {};
|
|
|
|
int num_moves {};
|
|
|
|
int num_destroys {};
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST_CASE(should_construct_by_copy)
|
|
|
|
{
|
|
|
|
Counter c {};
|
|
|
|
AK::NeverDestroyed<Counter> n { c };
|
|
|
|
|
|
|
|
EXPECT_EQ(1, n->num_copies);
|
|
|
|
EXPECT_EQ(0, n->num_moves);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE(should_construct_by_move)
|
|
|
|
{
|
|
|
|
Counter c {};
|
2021-03-17 15:30:02 +00:00
|
|
|
AK::NeverDestroyed<Counter> n { move(c) };
|
2020-11-22 00:23:17 +00:00
|
|
|
|
|
|
|
EXPECT_EQ(0, n->num_copies);
|
|
|
|
EXPECT_EQ(1, n->num_moves);
|
|
|
|
}
|
|
|
|
|
2024-06-05 21:52:33 +00:00
|
|
|
struct DestructorChecker {
|
|
|
|
DestructorChecker(bool& destroyed)
|
|
|
|
: m_destroyed(destroyed)
|
2020-11-22 00:23:17 +00:00
|
|
|
{
|
|
|
|
}
|
2024-06-05 21:52:33 +00:00
|
|
|
|
|
|
|
~DestructorChecker()
|
|
|
|
{
|
|
|
|
m_destroyed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool& m_destroyed;
|
|
|
|
};
|
2020-11-22 00:23:17 +00:00
|
|
|
|
2021-05-12 11:51:59 +00:00
|
|
|
TEST_CASE(should_not_destroy)
|
|
|
|
{
|
2024-06-05 21:52:33 +00:00
|
|
|
bool destroyed = false;
|
|
|
|
{
|
|
|
|
AK::NeverDestroyed<DestructorChecker> n(destroyed);
|
|
|
|
}
|
|
|
|
EXPECT(!destroyed);
|
2021-05-12 11:51:59 +00:00
|
|
|
}
|
|
|
|
|
2020-11-22 00:23:17 +00:00
|
|
|
TEST_CASE(should_provide_dereference_operator)
|
|
|
|
{
|
|
|
|
AK::NeverDestroyed<Counter> n {};
|
|
|
|
EXPECT_EQ(0, n->num_destroys);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE(should_provide_indirection_operator)
|
|
|
|
{
|
|
|
|
AK::NeverDestroyed<Counter> n {};
|
|
|
|
EXPECT_EQ(0, (*n).num_destroys);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE(should_provide_basic_getter)
|
|
|
|
{
|
|
|
|
AK::NeverDestroyed<Counter> n {};
|
|
|
|
EXPECT_EQ(0, n.get().num_destroys);
|
|
|
|
}
|