diff --git a/test/cpp-utils/pointer/unique_ref_test.cpp b/test/cpp-utils/pointer/unique_ref_test.cpp index be30cfa9..a3419de6 100644 --- a/test/cpp-utils/pointer/unique_ref_test.cpp +++ b/test/cpp-utils/pointer/unique_ref_test.cpp @@ -629,20 +629,26 @@ TEST_F(UniqueRefTest, givenUniqueRefWithDefaultDeleter_whenDestructed_thenCallsD EXPECT_TRUE(wasDestructed); } -TEST_F(UniqueRefTest, givenUniqueRefWithDefaultDeleter_whenMoveConstructed_thenDoesntCallDefaultDeleter) { +TEST_F(UniqueRefTest, givenUniqueRefWithDefaultDeleter_whenMoveConstructed_thenCallsDefaultDeleterAfterSecondDestructed) { bool wasDestructed = false; auto obj = make_unique_ref(&wasDestructed); - auto obj2 = std::move(obj); - EXPECT_FALSE(wasDestructed); + { + unique_ref obj2 = std::move(obj); + EXPECT_FALSE(wasDestructed); + } + EXPECT_TRUE(wasDestructed); } -TEST_F(UniqueRefTest, givenUniqueRefWithDefaultDeleter_whenMoveAssigned_thenDoesntCallDefaultDeleter) { +TEST_F(UniqueRefTest, givenUniqueRefWithDefaultDeleter_whenMoveAssigned_thenCallDefaultDeleterAfterSecondDestructed) { bool dummy = false; bool wasDestructed = false; - auto obj = make_unique_ref(&wasDestructed); - auto obj2 = make_unique_ref(&dummy); - obj2 = std::move(obj); - EXPECT_FALSE(wasDestructed); + unique_ref obj = make_unique_ref(&wasDestructed); + { + unique_ref obj2 = make_unique_ref(&dummy); + obj2 = std::move(obj); + EXPECT_FALSE(wasDestructed); + } + EXPECT_TRUE(wasDestructed); } TEST_F(UniqueRefTest, givenUniqueRefWithDefaultDeleter_whenDestructCalled_thenCallsDefaultDeleter) { @@ -670,20 +676,26 @@ TEST_F(UniqueRefTest, givenUniqueRefWithCustomDefaultConstructibleDeleter_whenDe EXPECT_TRUE(wasDestructed); } -TEST_F(UniqueRefTest, givenUniqueRefWithCustomDefaultConstructibleDeleter_whenMoveConstructed_thenDoesntCallCustomDeleter) { +TEST_F(UniqueRefTest, givenUniqueRefWithCustomDefaultConstructibleDeleter_whenMoveConstructed_thenCallsCustomDeleterAfterSecondDestructed) { bool wasDestructed = false; - auto obj = nullcheck(std::unique_ptr(&wasDestructed)).value(); - auto obj2 = std::move(obj); - EXPECT_FALSE(wasDestructed); + unique_ref obj = nullcheck(std::unique_ptr(&wasDestructed)).value(); + { + unique_ref obj2 = std::move(obj); + EXPECT_FALSE(wasDestructed); + } + EXPECT_TRUE(wasDestructed); } -TEST_F(UniqueRefTest, givenUniqueRefWithCustomDefaultConstructibleDeleter_whenMoveAssigned_thenDoesntCallCustomDeleter) { +TEST_F(UniqueRefTest, givenUniqueRefWithCustomDefaultConstructibleDeleter_whenMoveAssigned_thenCallsCustomDeleterAfterSecondDestructed) { bool dummy = false; bool wasDestructed = false; - auto obj = nullcheck(std::unique_ptr(&wasDestructed)).value(); - auto obj2 = nullcheck(std::unique_ptr(&dummy)).value(); - obj2 = std::move(obj); - EXPECT_FALSE(wasDestructed); + unique_ref obj = nullcheck(std::unique_ptr(&wasDestructed)).value(); + { + unique_ref obj2 = nullcheck(std::unique_ptr(&dummy)).value(); + obj2 = std::move(obj); + EXPECT_FALSE(wasDestructed); + } + EXPECT_TRUE(wasDestructed); } TEST_F(UniqueRefTest, givenUniqueRefWithCustomDefaultConstructibleDeleter_whenDestructCalled_thenCallsCustomDeleter) { @@ -714,20 +726,26 @@ TEST_F(UniqueRefTest, givenUniqueRefWithCustomDeleterInstance_whenDestructed_the EXPECT_EQ(4, value); } -TEST_F(UniqueRefTest, givenUniqueRefWithCustomDeleterInstance_whenMoveConstructed_thenDoesntCallCustomDeleterInstance) { +TEST_F(UniqueRefTest, givenUniqueRefWithCustomDeleterInstance_whenMoveConstructed_thenCallsCustomDeleterInstanceAfterSecondDestructed) { int value = 0; - auto obj = nullcheck(std::unique_ptr(&value, SetToDeleter(4))).value(); - auto obj2 = std::move(obj); - EXPECT_EQ(0, value); + unique_ref obj = nullcheck(std::unique_ptr(&value, SetToDeleter(4))).value(); + { + unique_ref obj2 = std::move(obj); + EXPECT_EQ(0, value); + } + EXPECT_EQ(4, value); } -TEST_F(UniqueRefTest, givenUniqueRefWithCustomDeleterInstance_whenMoveAssigned_thenDoesntCallCustomDeleterInstance) { +TEST_F(UniqueRefTest, givenUniqueRefWithCustomDeleterInstance_whenMoveAssigned_thenCallsCustomDeleterInstanceAfterSecondDestructed) { 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); + unique_ref obj = nullcheck(std::unique_ptr(&value, SetToDeleter(4))).value(); + { + unique_ref obj2 = nullcheck(std::unique_ptr(&dummy, SetToDeleter(0))).value(); + obj2 = std::move(obj); + EXPECT_EQ(0, value); + } + EXPECT_EQ(4, value); } TEST_F(UniqueRefTest, givenUniqueRefWithCustomDeleterInstance_whenDestructCalled_thenCallsCustomDeleterInstance) { @@ -738,7 +756,7 @@ TEST_F(UniqueRefTest, givenUniqueRefWithCustomDeleterInstance_whenDestructCalled EXPECT_FALSE(obj.isValid()); } -TEST_F(UniqueRefTest, givenUniquePtrWithCustomDeleterInstance_whenMovedToUniqueRef_thenHasSameDeleterInstance) { +TEST_F(UniqueRefTest, givenUniquePtrWithCustomDeleterInstance_whenMovedToUniquePtr_thenHasSameDeleterInstance) { int dummy = 0; SetToDeleter deleter(4); auto ptr = std::unique_ptr(&dummy, deleter); @@ -762,3 +780,14 @@ TEST_F(UniqueRefTest, givenUniqueRefWithCustomDeleterInstance_whenMoveAssigning_ ref2 = std::move(ref); EXPECT_EQ(4, ref2.get_deleter().value_); } + +TEST_F(UniqueRefTest, AllowsMoveConstructingToUniqueRefOfConst) { + unique_ref a = make_unique_ref(3); + unique_ref b = std::move(a); +} + +TEST_F(UniqueRefTest, AllowsMoveAssigningToUniqueRefOfConst) { + unique_ref a = make_unique_ref(3); + unique_ref b = make_unique_ref(10); + b = std::move(a); +}