aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorShauren <shauren.trinity@gmail.com>2024-03-13 17:04:26 +0100
committerShauren <shauren.trinity@gmail.com>2024-03-13 22:13:02 +0100
commite3ecd87a76bac891c233d16c1354376ab470ca8e (patch)
treeac3c73f8146ecf3397b1a907b1e18dc8cbb15944 /tests
parent9ebf232d7e7dd5df4f635b2573b03c21b277225b (diff)
Core/Utils: unique_trackable_ptr improvements
* Added comparison operators * Added type casting helper functions (cherry picked from commit f690b693386ef44754fa4528f3c565d563ad9468)
Diffstat (limited to 'tests')
-rw-r--r--tests/common/UniqueTrackablePtr.cpp66
1 files changed, 65 insertions, 1 deletions
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<TestObj> ptr = Trinity::make_unique_trackable<TestObj3>();
+
+ Trinity::unique_weak_ptr<TestObj> weak = ptr;
+
+ Trinity::unique_strong_ref_ptr<TestObj> temp = weak.lock();
+ REQUIRE(temp != nullptr);
+
+ SECTION("static_pointer_cast")
+ {
+ Trinity::unique_strong_ref_ptr<TestObj3> testObj2 = Trinity::static_pointer_cast<TestObj3>(temp);
+
+ REQUIRE(testObj2.get() == static_cast<TestObj3*>(ptr.get()));
+
+ // sanity check that we didn't accidentally setup inheritance of TestObjs incorrectly
+ REQUIRE(testObj2.get() != reinterpret_cast<TestObj3*>(ptr.get()));
+
+ REQUIRE(testObj2 == Trinity::static_pointer_cast<TestObj3>(weak).lock());
+ }
+
+ SECTION("reinterpret_pointer_cast")
+ {
+ Trinity::unique_strong_ref_ptr<TestObj3> testObj2 = Trinity::reinterpret_pointer_cast<TestObj3>(temp);
+
+ REQUIRE(testObj2.get() == reinterpret_cast<TestObj3*>(ptr.get()));
+
+ REQUIRE(testObj2 == Trinity::reinterpret_pointer_cast<TestObj3>(weak).lock());
+ }
+
+ SECTION("succeeding dynamic_pointer_cast")
+ {
+ Trinity::unique_strong_ref_ptr<TestObj3> testObj2 = Trinity::dynamic_pointer_cast<TestObj3>(temp);
+
+ REQUIRE(testObj2.get() == dynamic_cast<TestObj3*>(ptr.get()));
+
+ REQUIRE(testObj2 == Trinity::dynamic_pointer_cast<TestObj3>(weak).lock());
+ }
+
+ SECTION("failing dynamic_pointer_cast")
+ {
+ Trinity::unique_strong_ref_ptr<TestObj4> testObj2 = Trinity::dynamic_pointer_cast<TestObj4>(temp);
+
+ REQUIRE(testObj2 == nullptr);
+
+ REQUIRE(testObj2 == Trinity::dynamic_pointer_cast<TestObj4>(weak).lock());
+ }
+}