blob: 94363555240f352ca113bec580a0509248e7625d (
plain) (
blame)
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
// Copyright Epic Games, Inc. All Rights Reserved.
#include <zencore/refcount.h>
#include <doctest/doctest.h>
#include <functional>
namespace zen {
//////////////////////////////////////////////////////////////////////////
//
// Testing related code follows...
//
struct TestRefClass : public RefCounted
{
~TestRefClass()
{
if (OnDestroy)
OnDestroy();
}
using RefCounted::RefCount;
std::function<void()> OnDestroy;
};
void
refcount_forcelink()
{
}
TEST_CASE("RefPtr")
{
RefPtr<TestRefClass> Ref;
Ref = new TestRefClass;
bool IsDestroyed = false;
Ref->OnDestroy = [&] { IsDestroyed = true; };
CHECK(IsDestroyed == false);
CHECK(Ref->RefCount() == 1);
RefPtr<TestRefClass> Ref2;
Ref2 = Ref;
CHECK(IsDestroyed == false);
CHECK(Ref->RefCount() == 2);
RefPtr<TestRefClass> Ref3;
Ref2 = Ref3;
CHECK(IsDestroyed == false);
CHECK(Ref->RefCount() == 1);
Ref = Ref3;
CHECK(IsDestroyed == true);
}
TEST_CASE("RefPtr on Stack allocated object")
{
bool IsDestroyed = false;
{
TestRefClass StackRefClass;
StackRefClass.OnDestroy = [&] { IsDestroyed = true; };
CHECK(StackRefClass.RefCount() == 1); // Stack allocated objects should have +1 ref
RefPtr<TestRefClass> Ref{&StackRefClass};
CHECK(IsDestroyed == false);
CHECK(StackRefClass.RefCount() == 2);
RefPtr<TestRefClass> Ref2;
Ref2 = Ref;
CHECK(IsDestroyed == false);
CHECK(StackRefClass.RefCount() == 3);
RefPtr<TestRefClass> Ref3;
Ref2 = Ref3;
CHECK(IsDestroyed == false);
CHECK(StackRefClass.RefCount() == 2);
Ref = Ref3;
CHECK(IsDestroyed == false);
CHECK(StackRefClass.RefCount() == 1);
}
CHECK(IsDestroyed == true);
}
} // namespace zen
|