diff options
author | Brian Gianforcaro <bgianf@serenityos.org> | 2021-05-06 01:19:30 -0700 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-05-06 17:54:28 +0200 |
commit | 67322b0702836807e29265e86556ebf43bb9d510 (patch) | |
tree | 86d2e2099ecc377cf11ddcf9106c500969b1afbb /Tests/AK/TestNeverDestroyed.cpp | |
parent | fd0dbd1ebfbcbc29d46393061daa49dc7390caa7 (diff) | |
download | serenity-67322b0702836807e29265e86556ebf43bb9d510.zip |
Tests: Move AK tests to Tests/AK
Diffstat (limited to 'Tests/AK/TestNeverDestroyed.cpp')
-rw-r--r-- | Tests/AK/TestNeverDestroyed.cpp | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/Tests/AK/TestNeverDestroyed.cpp b/Tests/AK/TestNeverDestroyed.cpp new file mode 100644 index 0000000000..7b72d6bae3 --- /dev/null +++ b/Tests/AK/TestNeverDestroyed.cpp @@ -0,0 +1,73 @@ +/* + * Copyright (c) 2020, the SerenityOS developers. + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include <LibTest/TestCase.h> + +#include <AK/NeverDestroyed.h> +#include <AK/StdLibExtras.h> + +struct Counter { + Counter() = default; + + ~Counter() { ++num_destroys; } + + Counter(const Counter&) + { + ++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 {}; + AK::NeverDestroyed<Counter> n { move(c) }; + + EXPECT_EQ(0, n->num_copies); + EXPECT_EQ(1, n->num_moves); +} + +TEST_CASE(should_not_destroy) +{ + Counter* c = nullptr; + { + AK::NeverDestroyed<Counter> n {}; + c = &n.get(); + } + EXPECT_EQ(0, c->num_destroys); +} + +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); +} |