From fe9c30f40423557610b8015dd9a068502df80159 Mon Sep 17 00:00:00 2001 From: Shauren Date: Wed, 13 Mar 2024 17:04:26 +0100 Subject: Core/Utils: unique_trackable_ptr improvements * Added comparison operators * Added type casting helper functions (cherry picked from commit f690b693386ef44754fa4528f3c565d563ad9468) --- tests/common/UniqueTrackablePtr.cpp | 66 ++++++++++++++++++++++++++++++++++++- 1 file changed, 65 insertions(+), 1 deletion(-) (limited to 'tests') diff --git a/tests/common/UniqueTrackablePtr.cpp b/tests/common/UniqueTrackablePtr.cpp index 11fdb226ba5..3ab4cee1a55 100644 --- a/tests/common/UniqueTrackablePtr.cpp +++ b/tests/common/UniqueTrackablePtr.cpp @@ -23,7 +23,7 @@ struct TestObj { TestObj(bool* deleted = nullptr) : Deleted(deleted) { } - ~TestObj() + virtual ~TestObj() { if (Deleted) *Deleted = true; @@ -32,6 +32,21 @@ struct TestObj bool* Deleted = nullptr; }; +struct TestObj2 +{ + virtual ~TestObj2() = default; + + int a = 5; +}; + +struct TestObj3 : public TestObj2, public TestObj +{ +}; + +struct TestObj4 : public TestObj +{ +}; + TEST_CASE("Trinity::unique_trackable_ptr frees memory", "[UniqueTrackablePtr]") { bool deleted = false; @@ -88,3 +103,52 @@ TEST_CASE("Trinity::unique_weak_ptr", "[UniqueTrackablePtr]") REQUIRE(!!weakRef.lock()); } } + +TEST_CASE("Trinity::unique_strong_ref_ptr type casts", "[UniqueTrackablePtr]") +{ + Trinity::unique_trackable_ptr ptr = Trinity::make_unique_trackable(); + + Trinity::unique_weak_ptr weak = ptr; + + Trinity::unique_strong_ref_ptr temp = weak.lock(); + REQUIRE(temp != nullptr); + + SECTION("static_pointer_cast") + { + Trinity::unique_strong_ref_ptr testObj2 = Trinity::static_pointer_cast(temp); + + REQUIRE(testObj2.get() == static_cast(ptr.get())); + + // sanity check that we didn't accidentally setup inheritance of TestObjs incorrectly + REQUIRE(testObj2.get() != reinterpret_cast(ptr.get())); + + REQUIRE(testObj2 == Trinity::static_pointer_cast(weak).lock()); + } + + SECTION("reinterpret_pointer_cast") + { + Trinity::unique_strong_ref_ptr testObj2 = Trinity::reinterpret_pointer_cast(temp); + + REQUIRE(testObj2.get() == reinterpret_cast(ptr.get())); + + REQUIRE(testObj2 == Trinity::reinterpret_pointer_cast(weak).lock()); + } + + SECTION("succeeding dynamic_pointer_cast") + { + Trinity::unique_strong_ref_ptr testObj2 = Trinity::dynamic_pointer_cast(temp); + + REQUIRE(testObj2.get() == dynamic_cast(ptr.get())); + + REQUIRE(testObj2 == Trinity::dynamic_pointer_cast(weak).lock()); + } + + SECTION("failing dynamic_pointer_cast") + { + Trinity::unique_strong_ref_ptr testObj2 = Trinity::dynamic_pointer_cast(temp); + + REQUIRE(testObj2 == nullptr); + + REQUIRE(testObj2 == Trinity::dynamic_pointer_cast(weak).lock()); + } +} -- cgit v1.2.3