diff --git a/test/cpp-utils/pointer/unique_ref_test.cpp b/test/cpp-utils/pointer/unique_ref_test.cpp index 1300ee67..16604e7e 100644 --- a/test/cpp-utils/pointer/unique_ref_test.cpp +++ b/test/cpp-utils/pointer/unique_ref_test.cpp @@ -544,7 +544,7 @@ struct SetToTrueDeleter final { }; } -TEST_F(UniqueRefTest, givenUniqueRefWithCustomDeleter_whenDestructed_thenCallsCustomDeleter) { +TEST_F(UniqueRefTest, givenUniqueRefWithCustomDefaultConstructibleDeleter_whenDestructed_thenCallsCustomDeleter) { bool wasDestructed = false; { auto obj = nullcheck(std::unique_ptr(&wasDestructed)).value(); @@ -553,14 +553,14 @@ TEST_F(UniqueRefTest, givenUniqueRefWithCustomDeleter_whenDestructed_thenCallsCu EXPECT_TRUE(wasDestructed); } -TEST_F(UniqueRefTest, givenUniqueRefWithCustomDeleter_whenMoveConstructed_thenDoesntCallCustomDeleter) { +TEST_F(UniqueRefTest, givenUniqueRefWithCustomDefaultConstructibleDeleter_whenMoveConstructed_thenDoesntCallCustomDeleter) { bool wasDestructed = false; auto obj = nullcheck(std::unique_ptr(&wasDestructed)).value(); auto obj2 = std::move(obj); EXPECT_FALSE(wasDestructed); } -TEST_F(UniqueRefTest, givenUniqueRefWithCustomDeleter_whenMoveAssigned_thenDoesntCallCustomDeleter) { +TEST_F(UniqueRefTest, givenUniqueRefWithCustomDefaultConstructibleDeleter_whenMoveAssigned_thenDoesntCallCustomDeleter) { bool dummy = false; bool wasDestructed = false; auto obj = nullcheck(std::unique_ptr(&wasDestructed)).value(); @@ -569,7 +569,7 @@ TEST_F(UniqueRefTest, givenUniqueRefWithCustomDeleter_whenMoveAssigned_thenDoesn EXPECT_FALSE(wasDestructed); } -TEST_F(UniqueRefTest, givenUniqueRefWithCustomDeleter_whenDestructCalled_thenCallsCustomDeleter) { +TEST_F(UniqueRefTest, givenUniqueRefWithCustomDefaultConstructibleDeleter_whenDestructCalled_thenCallsCustomDeleter) { bool wasDestructed = false; auto obj = nullcheck(std::unique_ptr(&wasDestructed)).value(); destruct(std::move(obj)); @@ -577,6 +577,75 @@ TEST_F(UniqueRefTest, givenUniqueRefWithCustomDeleter_whenDestructCalled_thenCal EXPECT_FALSE(obj.isValid()); } +namespace { +struct SetToDeleter final { + SetToDeleter(int value): value_(value) {} + int value_; + + void operator()(int* ptr) { + *ptr = value_; + } +}; +} + +TEST_F(UniqueRefTest, givenUniqueRefWithCustomDeleterInstance_whenDestructed_thenCallsCustomDeleterInstance) { + int value = 0; + { + auto obj = nullcheck(std::unique_ptr(&value, SetToDeleter(4))).value(); + EXPECT_EQ(0, value); + } + EXPECT_EQ(4, value); +} + +TEST_F(UniqueRefTest, givenUniqueRefWithCustomDeleterInstance_whenMoveConstructed_thenDoesntCallCustomDeleterInstance) { + int value = 0; + auto obj = nullcheck(std::unique_ptr(&value, SetToDeleter(4))).value(); + auto obj2 = std::move(obj); + EXPECT_EQ(0, value); +} + +TEST_F(UniqueRefTest, givenUniqueRefWithCustomDeleterInstance_whenMoveAssigned_thenDoesntCallCustomDeleterInstance) { + int dummy = 0; + int value = 0; + auto obj = nullcheck(std::unique_ptr(&value, SetToDeleter(4))).value(); + auto obj2 = nullcheck(std::unique_ptr(&dummy, SetToDeleter(0))).value(); + obj2 = std::move(obj); + EXPECT_EQ(0, value); +} + +TEST_F(UniqueRefTest, givenUniqueRefWithCustomDeleterInstance_whenDestructCalled_thenCallsCustomDeleterInstance) { + int value = 0; + auto obj = nullcheck(std::unique_ptr(&value, SetToDeleter(4))).value(); + destruct(std::move(obj)); + EXPECT_EQ(4, value); + EXPECT_FALSE(obj.isValid()); +} + +TEST_F(UniqueRefTest, givenUniquePtrWithCustomDeleterInstance_whenMovedToUniqueRef_thenHasSameDeleterInstance) { + int dummy = 0; + SetToDeleter deleter(4); + auto ptr = std::unique_ptr(&dummy, deleter); + auto ref = nullcheck(std::move(ptr)).value(); + EXPECT_EQ(4, ref.get_deleter().value_); +} + +TEST_F(UniqueRefTest, givenUniqueRefWithCustomDeleterInstance_whenMoveConstructing_thenHasSameDeleterInstance) { + int dummy = 0; + SetToDeleter deleter(4); + auto ref = nullcheck(std::unique_ptr(&dummy, deleter)).value(); + unique_ref ref2 = std::move(ref); + EXPECT_EQ(4, ref2.get_deleter().value_); +} + +TEST_F(UniqueRefTest, givenUniqueRefWithCustomDeleterInstance_whenMoveAssigning_thenHasSameDeleterInstance) { + int dummy = 0; + SetToDeleter deleter(4); + auto ref = nullcheck(std::unique_ptr(&dummy, deleter)).value(); + auto ref2 = nullcheck(std::unique_ptr(&dummy, SetToDeleter(0))).value(); + ref2 = std::move(ref); + EXPECT_EQ(4, ref2.get_deleter().value_); +} + TEST_F(UniqueRefTest, givenUniqueRefToChildClass_whenMoveConstructedToBaseClass_thenWorksAsExpected) { unique_ref child = make_unique_ref(3); unique_ref base = std::move(child);