1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
/*
* 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>
#ifdef __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 };
};
#ifdef __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);
}
|