ladybird/Tests/AK/TestWeakPtr.cpp
Nico Weber 2af028132a AK+Everywhere: Add AK_COMPILER_{GCC,CLANG} and use them most places
Doesn't use them in libc headers so that those don't have to pull in
AK/Platform.h.

AK_COMPILER_GCC is set _only_ for gcc, not for clang too. (__GNUC__ is
defined in clang builds as well.) Using AK_COMPILER_GCC simplifies
things some.

AK_COMPILER_CLANG isn't as much of a win, other than that it's
consistent with AK_COMPILER_GCC.
2022-10-04 23:35:07 +01:00

66 lines
1.6 KiB
C++

/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibTest/TestCase.h>
#include <AK/String.h>
#include <AK/WeakPtr.h>
#include <AK/Weakable.h>
#if defined(AK_COMPILER_CLANG)
# pragma clang diagnostic push
# pragma clang diagnostic ignored "-Wunused-private-field"
#endif
class SimpleWeakable : public Weakable<SimpleWeakable>
, public RefCounted<SimpleWeakable> {
public:
SimpleWeakable() = default;
private:
int m_member { 123 };
};
#if defined(AK_COMPILER_CLANG)
# pragma clang diagnostic pop
#endif
TEST_CASE(basic_weak)
{
WeakPtr<SimpleWeakable> weak1;
WeakPtr<SimpleWeakable> weak2;
{
auto simple = adopt_ref(*new SimpleWeakable);
weak1 = simple;
weak2 = simple;
EXPECT_EQ(weak1.is_null(), false);
EXPECT_EQ(weak2.is_null(), false);
EXPECT_EQ(weak1.strong_ref().ptr(), simple.ptr());
EXPECT_EQ(weak1.strong_ref().ptr(), weak2.strong_ref().ptr());
}
EXPECT_EQ(weak1.is_null(), true);
EXPECT_EQ(weak1.strong_ref().ptr(), nullptr);
EXPECT_EQ(weak1.strong_ref().ptr(), weak2.strong_ref().ptr());
}
TEST_CASE(weakptr_move)
{
WeakPtr<SimpleWeakable> weak1;
WeakPtr<SimpleWeakable> weak2;
{
auto simple = adopt_ref(*new SimpleWeakable);
weak1 = simple;
weak2 = move(weak1);
EXPECT_EQ(weak1.is_null(), true);
EXPECT_EQ(weak2.is_null(), false);
EXPECT_EQ(weak2.strong_ref().ptr(), simple.ptr());
}
EXPECT_EQ(weak2.is_null(), true);
}