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