Tests: Add tests for Optional's conditionally trivial functions

This commit is contained in:
Daniel Bertalan 2021-07-03 12:14:36 +02:00 committed by Ali Mohammad Pur
parent 39dd13fd17
commit 2ef28602ba
Notes: sideshowbarker 2024-07-18 10:31:02 +09:00

View file

@ -1,5 +1,6 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2021, Daniel Bertalan <dani@danielbertalan.dev>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -96,3 +97,95 @@ TEST_CASE(comparison_with_numeric_types)
EXPECT_EQ(opt1, 7u);
EXPECT_NE(opt1, -2);
}
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)
{
}
CopyChecker(const CopyChecker& other)
: 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)
{
}
MoveChecker(const MoveChecker& other)
: m_was_move_constructed(other.m_was_move_constructed)
{
EXPECT(false);
};
MoveChecker(MoveChecker&& other)
: m_was_move_constructed(other.m_was_move_constructed)
{
m_was_move_constructed = true;
};
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
}