diff options
| author | Stefan Boberg <[email protected]> | 2021-05-11 13:05:39 +0200 |
|---|---|---|
| committer | Stefan Boberg <[email protected]> | 2021-05-11 13:05:39 +0200 |
| commit | f8d9ac5d13dd37b8b57af0478e77ba1e75c813aa (patch) | |
| tree | 1daf7621e110d48acd5e12e3073ce48ef0dd11b2 /zencore/refcount.cpp | |
| download | zen-f8d9ac5d13dd37b8b57af0478e77ba1e75c813aa.tar.xz zen-f8d9ac5d13dd37b8b57af0478e77ba1e75c813aa.zip | |
Adding zenservice code
Diffstat (limited to 'zencore/refcount.cpp')
| -rw-r--r-- | zencore/refcount.cpp | 96 |
1 files changed, 96 insertions, 0 deletions
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 <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 |