aboutsummaryrefslogtreecommitdiff
path: root/src/zencore/refcount.cpp
blob: 674b154e0e02f63253cd167c7653627ddc45285a (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
// Copyright Epic Games, Inc. All Rights Reserved.

#include <zenbase/refcount.h>

#include <zencore/testing.h>

#include <functional>

namespace zen {

//////////////////////////////////////////////////////////////////////////
//
// Testing related code follows...
//

#if ZEN_WITH_TESTS

struct TestRefClass : public RefCounted
{
	~TestRefClass()
	{
		if (OnDestroy)
			OnDestroy();
	}

	using RefCounted::RefCount;

	std::function<void()> OnDestroy;
};

void
refcount_forcelink()
{
}

TEST_SUITE_BEGIN("core.refcount");

TEST_CASE("Ref")
{
	Ref<TestRefClass> RefA;
	RefA = new TestRefClass;

	bool IsDestroyed = false;
	RefA->OnDestroy	 = [&] { IsDestroyed = true; };

	CHECK(IsDestroyed == false);
	CHECK(RefA->RefCount() == 1);

	Ref<TestRefClass> RefB;
	RefB = RefA;

	CHECK(IsDestroyed == false);
	CHECK(RefA->RefCount() == 2);

	Ref<TestRefClass> RefC;
	RefB = RefC;

	CHECK(IsDestroyed == false);
	CHECK(RefA->RefCount() == 1);
	RefA = RefC;

	CHECK(IsDestroyed == true);
}

TEST_SUITE_END();

#endif

}  // namespace zen