From f8d9ac5d13dd37b8b57af0478e77ba1e75c813aa Mon Sep 17 00:00:00 2001 From: Stefan Boberg Date: Tue, 11 May 2021 13:05:39 +0200 Subject: Adding zenservice code --- zencore/refcount.cpp | 96 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 zencore/refcount.cpp (limited to 'zencore/refcount.cpp') diff --git a/zencore/refcount.cpp b/zencore/refcount.cpp new file mode 100644 index 000000000..943635552 --- /dev/null +++ b/zencore/refcount.cpp @@ -0,0 +1,96 @@ +// Copyright Epic Games, Inc. All Rights Reserved. + +#include + +#include +#include + +namespace zen { + +////////////////////////////////////////////////////////////////////////// +// +// Testing related code follows... +// + +struct TestRefClass : public RefCounted +{ + ~TestRefClass() + { + if (OnDestroy) + OnDestroy(); + } + + using RefCounted::RefCount; + + std::function OnDestroy; +}; + +void +refcount_forcelink() +{ +} + +TEST_CASE("RefPtr") +{ + RefPtr Ref; + Ref = new TestRefClass; + + bool IsDestroyed = false; + Ref->OnDestroy = [&] { IsDestroyed = true; }; + + CHECK(IsDestroyed == false); + CHECK(Ref->RefCount() == 1); + + RefPtr Ref2; + Ref2 = Ref; + + CHECK(IsDestroyed == false); + CHECK(Ref->RefCount() == 2); + + RefPtr 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 Ref{&StackRefClass}; + + CHECK(IsDestroyed == false); + CHECK(StackRefClass.RefCount() == 2); + + RefPtr Ref2; + Ref2 = Ref; + + CHECK(IsDestroyed == false); + CHECK(StackRefClass.RefCount() == 3); + + RefPtr Ref3; + Ref2 = Ref3; + + CHECK(IsDestroyed == false); + CHECK(StackRefClass.RefCount() == 2); + + Ref = Ref3; + CHECK(IsDestroyed == false); + CHECK(StackRefClass.RefCount() == 1); + } + + CHECK(IsDestroyed == true); +} + +} // namespace zen -- cgit v1.2.3