1 #include <c10/util/intrusive_ptr.h> 3 #include <gtest/gtest.h> 6 #include <unordered_map> 7 #include <unordered_set> 11 using c10::make_intrusive;
15 #pragma GCC diagnostic ignored "-Wpragmas" 16 #pragma GCC diagnostic ignored "-Wunknown-warning-option" 17 #pragma GCC diagnostic ignored "-Wself-move" 24 SomeClass1Parameter(
int param_) : param(param_) {}
29 SomeClass2Parameters(
int param1_,
int param2_)
30 : param1(param1_), param2(param2_) {}
34 using SomeClass = SomeClass0Parameters;
36 SomeBaseClass(
int v_) : v(v_) {}
39 struct SomeChildClass : SomeBaseClass {
40 SomeChildClass(
int v) : SomeBaseClass(v) {}
45 DestructableMock(
bool* resourcesReleased,
bool* wasDestructed)
46 : resourcesReleased_(resourcesReleased), wasDestructed_(wasDestructed) {}
48 ~DestructableMock()
override {
49 *wasDestructed_ =
true;
52 void release_resources()
override {
53 *resourcesReleased_ =
true;
57 bool* resourcesReleased_;
61 class ChildDestructableMock final :
public DestructableMock {
63 ChildDestructableMock(
bool* resourcesReleased,
bool* wasDestructed)
64 : DestructableMock(resourcesReleased, wasDestructed) {}
66 class NullType1 final {
67 static SomeClass singleton_;
69 static constexpr SomeClass* singleton() {
73 SomeClass NullType1::singleton_;
74 class NullType2 final {
75 static SomeClass singleton_;
77 static constexpr SomeClass* singleton() {
81 SomeClass NullType2::singleton_;
82 static_assert(NullType1::singleton() != NullType2::singleton(),
"");
87 "intrusive_ptr<T>::element_type is wrong");
89 TEST(MakeIntrusiveTest, ClassWith0Parameters) {
91 make_intrusive<SomeClass0Parameters>();
93 EXPECT_EQ(var.get(),
dynamic_cast<SomeClass0Parameters*
>(var.get()));
96 TEST(MakeIntrusiveTest, ClassWith1Parameter) {
98 make_intrusive<SomeClass1Parameter>(5);
99 EXPECT_EQ(5, var->param);
102 TEST(MakeIntrusiveTest, ClassWith2Parameters) {
104 make_intrusive<SomeClass2Parameters>(7, 2);
105 EXPECT_EQ(7, var->param1);
106 EXPECT_EQ(2, var->param2);
109 TEST(MakeIntrusiveTest, TypeIsAutoDeductible) {
110 auto var2 = make_intrusive<SomeClass0Parameters>();
111 auto var3 = make_intrusive<SomeClass1Parameter>(2);
112 auto var4 = make_intrusive<SomeClass2Parameters>(2, 3);
115 TEST(MakeIntrusiveTest, CanAssignToBaseClassPtr) {
117 EXPECT_EQ(3, var->v);
120 TEST(IntrusivePtrTargetTest, whenAllocatedOnStack_thenDoesntCrash) {
124 TEST(IntrusivePtrTest, givenValidPtr_whenCallingGet_thenReturnsObject) {
126 make_intrusive<SomeClass1Parameter>(5);
127 EXPECT_EQ(5, obj.get()->param);
130 TEST(IntrusivePtrTest, givenValidPtr_whenCallingConstGet_thenReturnsObject) {
132 make_intrusive<SomeClass1Parameter>(5);
133 EXPECT_EQ(5, obj.get()->param);
136 TEST(IntrusivePtrTest, givenInvalidPtr_whenCallingGet_thenReturnsNullptr) {
138 EXPECT_EQ(
nullptr, obj.get());
141 TEST(IntrusivePtrTest, givenValidPtr_whenDereferencing_thenReturnsObject) {
143 make_intrusive<SomeClass1Parameter>(5);
144 EXPECT_EQ(5, (*obj).param);
147 TEST(IntrusivePtrTest, givenValidPtr_whenConstDereferencing_thenReturnsObject) {
149 make_intrusive<SomeClass1Parameter>(5);
150 EXPECT_EQ(5, (*obj).param);
153 TEST(IntrusivePtrTest, givenValidPtr_whenArrowDereferencing_thenReturnsObject) {
155 make_intrusive<SomeClass1Parameter>(3);
156 EXPECT_EQ(3, obj->param);
161 givenValidPtr_whenConstArrowDereferencing_thenReturnsObject) {
163 make_intrusive<SomeClass1Parameter>(3);
164 EXPECT_EQ(3, obj->param);
167 TEST(IntrusivePtrTest, givenValidPtr_whenMoveAssigning_thenPointsToSameObject) {
170 SomeClass* obj1ptr = obj1.get();
171 obj2 = std::move(obj1);
172 EXPECT_EQ(obj1ptr, obj2.get());
175 TEST(IntrusivePtrTest, givenValidPtr_whenMoveAssigning_thenOldInstanceInvalid) {
178 obj2 = std::move(obj1);
179 EXPECT_FALSE(obj1.defined());
184 givenValidPtr_whenMoveAssigningToSelf_thenPointsToSameObject) {
186 SomeClass* obj1ptr = obj1.get();
187 obj1 = std::move(obj1);
188 EXPECT_EQ(obj1ptr, obj1.get());
191 TEST(IntrusivePtrTest, givenValidPtr_whenMoveAssigningToSelf_thenStaysValid) {
193 obj1 = std::move(obj1);
194 EXPECT_TRUE(obj1.defined());
199 givenInvalidPtr_whenMoveAssigningToSelf_thenStaysInvalid) {
201 obj1 = std::move(obj1);
202 EXPECT_FALSE(obj1.defined());
207 givenInvalidPtr_whenMoveAssigning_thenNewInstanceIsValid) {
210 SomeClass* obj1ptr = obj1.get();
211 obj2 = std::move(obj1);
212 EXPECT_TRUE(obj2.defined());
217 givenInvalidPtr_whenMoveAssigning_thenPointsToSameObject) {
220 SomeClass* obj1ptr = obj1.get();
221 obj2 = std::move(obj1);
222 EXPECT_EQ(obj1ptr, obj2.get());
227 givenValidPtr_whenMoveAssigningFromInvalidPtr_thenNewInstanceIsInvalid) {
230 EXPECT_TRUE(obj2.defined());
231 obj2 = std::move(obj1);
232 EXPECT_FALSE(obj2.defined());
237 givenValidPtr_whenMoveAssigningToBaseClass_thenPointsToSameObject) {
240 SomeBaseClass* obj1ptr = obj1.get();
241 obj2 = std::move(obj1);
242 EXPECT_EQ(obj1ptr, obj2.get());
243 EXPECT_EQ(1, obj2->v);
248 givenValidPtr_whenMoveAssigningToBaseClass_thenOldInstanceInvalid) {
251 obj2 = std::move(obj1);
252 EXPECT_FALSE(obj1.defined());
257 givenInvalidPtr_whenMoveAssigningToBaseClass_thenNewInstanceIsValid) {
260 SomeBaseClass* obj1ptr = obj1.get();
261 obj2 = std::move(obj1);
262 EXPECT_TRUE(obj2.defined());
267 givenInvalidPtr_whenMoveAssigningToBaseClass_thenPointsToSameObject) {
270 SomeBaseClass* obj1ptr = obj1.get();
271 obj2 = std::move(obj1);
272 EXPECT_EQ(obj1ptr, obj2.get());
273 EXPECT_EQ(5, obj2->v);
278 givenInvalidPtr_whenMoveAssigningInvalidPtrToBaseClass_thenNewInstanceIsValid) {
281 EXPECT_TRUE(obj2.defined());
282 obj2 = std::move(obj1);
283 EXPECT_FALSE(obj2.defined());
288 givenNullPtr_whenMoveAssigningToDifferentNullptr_thenHasNewNullptr) {
291 obj2 = std::move(obj1);
292 EXPECT_NE(NullType1::singleton(), NullType2::singleton());
293 EXPECT_EQ(NullType1::singleton(), obj1.get());
294 EXPECT_EQ(NullType2::singleton(), obj2.get());
295 EXPECT_FALSE(obj1.defined());
296 EXPECT_FALSE(obj2.defined());
299 TEST(IntrusivePtrTest, givenValidPtr_whenCopyAssigning_thenPointsToSameObject) {
302 SomeClass* obj1ptr = obj1.get();
304 EXPECT_EQ(obj1ptr, obj2.get());
307 TEST(IntrusivePtrTest, givenValidPtr_whenCopyAssigning_thenOldInstanceValid) {
311 EXPECT_TRUE(obj1.defined());
316 givenValidPtr_whenCopyAssigningToSelf_thenPointsToSameObject) {
318 SomeClass* obj1ptr = obj1.get();
320 EXPECT_EQ(obj1ptr, obj1.get());
323 TEST(IntrusivePtrTest, givenValidPtr_whenCopyAssigningToSelf_thenStaysValid) {
326 EXPECT_TRUE(obj1.defined());
331 givenInvalidPtr_whenCopyAssigningToSelf_thenStaysInvalid) {
334 EXPECT_FALSE(obj1.defined());
339 givenInvalidPtr_whenCopyAssigning_thenNewInstanceIsValid) {
342 SomeClass* obj1ptr = obj1.get();
344 EXPECT_TRUE(obj2.defined());
349 givenValidPtr_whenCopyAssigningToBaseClass_thenPointsToSameObject) {
353 EXPECT_EQ(3, base->v);
358 givenValidPtr_whenCopyAssigningToBaseClass_thenOldInstanceInvalid) {
362 EXPECT_TRUE(obj1.defined());
367 givenInvalidPtr_whenCopyAssigningToBaseClass_thenNewInstanceIsValid) {
370 SomeBaseClass* obj1ptr = obj1.get();
372 EXPECT_TRUE(obj2.defined());
377 givenInvalidPtr_whenCopyAssigningToBaseClass_thenPointsToSameObject) {
380 SomeBaseClass* obj1ptr = obj1.get();
382 EXPECT_EQ(obj1ptr, obj2.get());
383 EXPECT_EQ(5, obj2->v);
388 givenPtr_whenCopyAssigningInvalidPtrToBaseClass_thenNewInstanceIsInvalid) {
391 EXPECT_TRUE(obj2.defined());
393 EXPECT_FALSE(obj2.defined());
398 givenNullPtr_whenCopyAssigningToDifferentNullptr_thenHasNewNullptr) {
402 EXPECT_NE(NullType1::singleton(), NullType2::singleton());
403 EXPECT_EQ(NullType1::singleton(), obj1.get());
404 EXPECT_EQ(NullType2::singleton(), obj2.get());
405 EXPECT_FALSE(obj1.defined());
406 EXPECT_FALSE(obj2.defined());
409 TEST(IntrusivePtrTest, givenPtr_whenMoveConstructing_thenPointsToSameObject) {
411 SomeClass* obj1ptr = obj1.get();
413 EXPECT_EQ(obj1ptr, obj2.get());
416 TEST(IntrusivePtrTest, givenPtr_whenMoveConstructing_thenOldInstanceInvalid) {
419 EXPECT_FALSE(obj1.defined());
422 TEST(IntrusivePtrTest, givenPtr_whenMoveConstructing_thenNewInstanceValid) {
425 EXPECT_TRUE(obj2.defined());
430 givenPtr_whenMoveConstructingFromInvalidPtr_thenNewInstanceInvalid) {
433 EXPECT_FALSE(obj2.defined());
438 givenPtr_whenMoveConstructingToBaseClass_thenPointsToSameObject) {
440 SomeBaseClass* objptr = child.get();
442 EXPECT_EQ(3, base->v);
443 EXPECT_EQ(objptr, base.get());
448 givenPtr_whenMoveConstructingToBaseClass_thenOldInstanceInvalid) {
451 EXPECT_FALSE(child.defined());
456 givenPtr_whenMoveConstructingToBaseClass_thenNewInstanceValid) {
459 EXPECT_TRUE(obj2.defined());
464 givenPtr_whenMoveConstructingToBaseClassFromInvalidPtr_thenNewInstanceInvalid) {
467 EXPECT_FALSE(obj2.defined());
472 givenNullPtr_whenMoveConstructingToDifferentNullptr_thenHasNewNullptr) {
475 EXPECT_NE(NullType1::singleton(), NullType2::singleton());
476 EXPECT_EQ(NullType1::singleton(), obj1.get());
477 EXPECT_EQ(NullType2::singleton(), obj2.get());
478 EXPECT_FALSE(obj1.defined());
479 EXPECT_FALSE(obj2.defined());
482 TEST(IntrusivePtrTest, givenPtr_whenCopyConstructing_thenPointsToSameObject) {
484 SomeClass* obj1ptr = obj1.get();
486 EXPECT_EQ(obj1ptr, obj2.get());
487 EXPECT_TRUE(obj1.defined());
490 TEST(IntrusivePtrTest, givenPtr_whenCopyConstructing_thenOldInstanceValid) {
493 EXPECT_TRUE(obj1.defined());
496 TEST(IntrusivePtrTest, givenPtr_whenCopyConstructing_thenNewInstanceValid) {
499 EXPECT_TRUE(obj2.defined());
504 givenPtr_whenCopyConstructingFromInvalidPtr_thenNewInstanceInvalid) {
507 EXPECT_FALSE(obj2.defined());
512 givenPtr_whenCopyConstructingToBaseClass_thenPointsToSameObject) {
514 SomeBaseClass* objptr = child.get();
516 EXPECT_EQ(3, base->v);
517 EXPECT_EQ(objptr, base.get());
522 givenPtr_whenCopyConstructingToBaseClass_thenOldInstanceInvalid) {
525 EXPECT_TRUE(child.defined());
530 givenPtr_whenCopyConstructingToBaseClass_thenNewInstanceInvalid) {
533 EXPECT_TRUE(base.defined());
538 givenPtr_whenCopyConstructingToBaseClassFromInvalidPtr_thenNewInstanceInvalid) {
541 EXPECT_FALSE(obj2.defined());
546 givenNullPtr_whenCopyConstructingToDifferentNullptr_thenHasNewNullptr) {
549 EXPECT_NE(NullType1::singleton(), NullType2::singleton());
550 EXPECT_EQ(NullType1::singleton(), obj1.get());
551 EXPECT_EQ(NullType2::singleton(), obj2.get());
552 EXPECT_FALSE(obj1.defined());
553 EXPECT_FALSE(obj2.defined());
556 TEST(IntrusivePtrTest, SwapFunction) {
559 SomeClass* obj1ptr = obj1.get();
560 SomeClass* obj2ptr = obj2.get();
562 EXPECT_EQ(obj2ptr, obj1.get());
563 EXPECT_EQ(obj1ptr, obj2.get());
566 TEST(IntrusivePtrTest, SwapMethod) {
569 SomeClass* obj1ptr = obj1.get();
570 SomeClass* obj2ptr = obj2.get();
572 EXPECT_EQ(obj2ptr, obj1.get());
573 EXPECT_EQ(obj1ptr, obj2.get());
576 TEST(IntrusivePtrTest, SwapFunctionFromInvalid) {
579 SomeClass* obj2ptr = obj2.get();
581 EXPECT_EQ(obj2ptr, obj1.get());
582 EXPECT_TRUE(obj1.defined());
583 EXPECT_FALSE(obj2.defined());
586 TEST(IntrusivePtrTest, SwapMethodFromInvalid) {
589 SomeClass* obj2ptr = obj2.get();
591 EXPECT_EQ(obj2ptr, obj1.get());
592 EXPECT_TRUE(obj1.defined());
593 EXPECT_FALSE(obj2.defined());
596 TEST(IntrusivePtrTest, SwapFunctionWithInvalid) {
599 SomeClass* obj1ptr = obj1.get();
601 EXPECT_FALSE(obj1.defined());
602 EXPECT_TRUE(obj2.defined());
603 EXPECT_EQ(obj1ptr, obj2.get());
606 TEST(IntrusivePtrTest, SwapMethodWithInvalid) {
609 SomeClass* obj1ptr = obj1.get();
611 EXPECT_FALSE(obj1.defined());
612 EXPECT_TRUE(obj2.defined());
613 EXPECT_EQ(obj1ptr, obj2.get());
616 TEST(IntrusivePtrTest, SwapFunctionInvalidWithInvalid) {
620 EXPECT_FALSE(obj1.defined());
621 EXPECT_FALSE(obj2.defined());
624 TEST(IntrusivePtrTest, SwapMethodInvalidWithInvalid) {
628 EXPECT_FALSE(obj1.defined());
629 EXPECT_FALSE(obj2.defined());
632 TEST(IntrusivePtrTest, CanBePutInContainer) {
633 std::vector<intrusive_ptr<SomeClass1Parameter>> vec;
634 vec.push_back(make_intrusive<SomeClass1Parameter>(5));
635 EXPECT_EQ(5, vec[0]->param);
638 TEST(IntrusivePtrTest, CanBePutInSet) {
639 std::set<intrusive_ptr<SomeClass1Parameter>>
set;
640 set.insert(make_intrusive<SomeClass1Parameter>(5));
641 EXPECT_EQ(5, (*
set.begin())->param);
644 TEST(IntrusivePtrTest, CanBePutInUnorderedSet) {
645 std::unordered_set<intrusive_ptr<SomeClass1Parameter>>
set;
646 set.insert(make_intrusive<SomeClass1Parameter>(5));
647 EXPECT_EQ(5, (*
set.begin())->param);
650 TEST(IntrusivePtrTest, CanBePutInMap) {
653 intrusive_ptr<SomeClass1Parameter>>
655 map.insert(std::make_pair(
656 make_intrusive<SomeClass1Parameter>(5),
657 make_intrusive<SomeClass1Parameter>(3)));
658 EXPECT_EQ(5, map.begin()->first->param);
659 EXPECT_EQ(3, map.begin()->second->param);
662 TEST(IntrusivePtrTest, CanBePutInUnorderedMap) {
664 intrusive_ptr<SomeClass1Parameter>,
665 intrusive_ptr<SomeClass1Parameter>>
667 map.insert(std::make_pair(
668 make_intrusive<SomeClass1Parameter>(3),
669 make_intrusive<SomeClass1Parameter>(5)));
670 EXPECT_EQ(3, map.begin()->first->param);
671 EXPECT_EQ(5, map.begin()->second->param);
674 TEST(IntrusivePtrTest, Equality_AfterCopyConstructor) {
677 EXPECT_TRUE(var1 == var2);
678 EXPECT_FALSE(var1 != var2);
681 TEST(IntrusivePtrTest, Equality_AfterCopyAssignment) {
685 EXPECT_TRUE(var1 == var2);
686 EXPECT_FALSE(var1 != var2);
689 TEST(IntrusivePtrTest, Equality_Nullptr) {
692 EXPECT_TRUE(var1 == var2);
693 EXPECT_FALSE(var1 != var2);
696 TEST(IntrusivePtrTest, Nonequality) {
699 EXPECT_TRUE(var1 != var2);
700 EXPECT_FALSE(var1 == var2);
703 TEST(IntrusivePtrTest, Nonequality_NullptrLeft) {
706 EXPECT_TRUE(var1 != var2);
707 EXPECT_FALSE(var1 == var2);
710 TEST(IntrusivePtrTest, Nonequality_NullptrRight) {
713 EXPECT_TRUE(var1 != var2);
714 EXPECT_FALSE(var1 == var2);
717 TEST(IntrusivePtrTest, HashIsDifferent) {
725 TEST(IntrusivePtrTest, HashIsDifferent_ValidAndInvalid) {
733 TEST(IntrusivePtrTest, HashIsSame_AfterCopyConstructor) {
741 TEST(IntrusivePtrTest, HashIsSame_AfterCopyAssignment) {
750 TEST(IntrusivePtrTest, HashIsSame_BothNullptr) {
758 TEST(IntrusivePtrTest, OneIsLess) {
766 TEST(IntrusivePtrTest, NullptrIsLess1) {
772 TEST(IntrusivePtrTest, NullptrIsLess2) {
778 TEST(IntrusivePtrTest, NullptrIsNotLessThanNullptr) {
784 TEST(IntrusivePtrTest, givenPtr_whenCallingReset_thenIsInvalid) {
785 auto obj = make_intrusive<SomeClass>();
786 EXPECT_TRUE(obj.defined());
788 EXPECT_FALSE(obj.defined());
791 TEST(IntrusivePtrTest, givenPtr_whenCallingReset_thenHoldsNullptr) {
792 auto obj = make_intrusive<SomeClass>();
793 EXPECT_NE(
nullptr, obj.get());
795 EXPECT_EQ(
nullptr, obj.get());
798 TEST(IntrusivePtrTest, givenPtr_whenDestructed_thenDestructsObject) {
799 bool resourcesReleased =
false;
800 bool wasDestructed =
false;
802 auto obj = make_intrusive<DestructableMock>(&resourcesReleased, &wasDestructed);
803 EXPECT_FALSE(resourcesReleased);
804 EXPECT_FALSE(wasDestructed);
806 EXPECT_TRUE(resourcesReleased);
807 EXPECT_TRUE(wasDestructed);
812 givenPtr_whenMoveConstructed_thenDestructsObjectAfterSecondDestructed) {
813 bool resourcesReleased =
false;
814 bool wasDestructed =
false;
815 auto obj = make_intrusive<DestructableMock>(&resourcesReleased, &wasDestructed);
817 auto obj2 = std::move(obj);
818 EXPECT_FALSE(resourcesReleased);
819 EXPECT_FALSE(wasDestructed);
821 EXPECT_TRUE(resourcesReleased);
822 EXPECT_TRUE(wasDestructed);
827 givenPtr_whenMoveConstructedToBaseClass_thenDestructsObjectAfterSecondDestructed) {
828 bool resourcesReleased =
false;
829 bool wasDestructed =
false;
830 auto obj = make_intrusive<ChildDestructableMock>(&resourcesReleased, &wasDestructed);
833 EXPECT_FALSE(resourcesReleased);
834 EXPECT_FALSE(wasDestructed);
836 EXPECT_TRUE(resourcesReleased);
837 EXPECT_TRUE(wasDestructed);
840 TEST(IntrusivePtrTest, givenPtr_whenMoveAssigned_thenDestructsOldObject) {
842 bool resourcesReleased =
false;
843 bool wasDestructed =
false;
844 auto obj = make_intrusive<DestructableMock>(&dummy, &dummy);
846 auto obj2 = make_intrusive<DestructableMock>(&resourcesReleased, &wasDestructed);
847 EXPECT_FALSE(resourcesReleased);
848 EXPECT_FALSE(wasDestructed);
849 obj2 = std::move(obj);
850 EXPECT_TRUE(resourcesReleased);
851 EXPECT_TRUE(wasDestructed);
857 givenPtr_whenMoveAssignedToBaseClass_thenDestructsOldObject) {
859 bool resourcesReleased =
false;
860 bool wasDestructed =
false;
861 auto obj = make_intrusive<ChildDestructableMock>(&dummy, &dummy);
863 auto obj2 = make_intrusive<DestructableMock>(&resourcesReleased, &wasDestructed);
864 EXPECT_FALSE(resourcesReleased);
865 EXPECT_FALSE(wasDestructed);
866 obj2 = std::move(obj);
867 EXPECT_TRUE(resourcesReleased);
868 EXPECT_TRUE(wasDestructed);
874 givenPtrWithCopy_whenMoveAssigned_thenDestructsOldObjectAfterCopyIsDestructed) {
876 bool resourcesReleased =
false;
877 bool wasDestructed =
false;
878 auto obj = make_intrusive<DestructableMock>(&dummy, &dummy);
880 auto obj2 = make_intrusive<DestructableMock>(&resourcesReleased, &wasDestructed);
883 EXPECT_FALSE(resourcesReleased);
884 EXPECT_FALSE(wasDestructed);
885 obj2 = std::move(obj);
886 EXPECT_FALSE(resourcesReleased);
887 EXPECT_FALSE(wasDestructed);
889 EXPECT_TRUE(resourcesReleased);
890 EXPECT_TRUE(wasDestructed);
896 givenPtrWithBaseClassCopy_whenMoveAssigned_thenDestructsOldObjectAfterCopyIsDestructed) {
898 bool resourcesReleased =
false;
899 bool wasDestructed =
false;
900 auto obj = make_intrusive<ChildDestructableMock>(&dummy, &dummy);
903 make_intrusive<ChildDestructableMock>(&resourcesReleased, &wasDestructed);
906 EXPECT_FALSE(resourcesReleased);
907 EXPECT_FALSE(wasDestructed);
908 obj2 = std::move(obj);
909 EXPECT_FALSE(resourcesReleased);
910 EXPECT_FALSE(wasDestructed);
912 EXPECT_TRUE(resourcesReleased);
913 EXPECT_TRUE(wasDestructed);
919 givenPtrWithCopy_whenMoveAssignedToBaseClass_thenDestructsOldObjectAfterCopyIsDestructed) {
921 bool resourcesReleased =
false;
922 bool wasDestructed =
false;
923 auto obj = make_intrusive<ChildDestructableMock>(&dummy, &dummy);
925 auto obj2 = make_intrusive<DestructableMock>(&resourcesReleased, &wasDestructed);
928 EXPECT_FALSE(resourcesReleased);
929 EXPECT_FALSE(wasDestructed);
930 obj2 = std::move(obj);
931 EXPECT_FALSE(resourcesReleased);
932 EXPECT_FALSE(wasDestructed);
934 EXPECT_TRUE(resourcesReleased);
935 EXPECT_TRUE(wasDestructed);
941 givenPtr_whenMoveAssigned_thenDestructsObjectAfterSecondDestructed) {
943 bool resourcesReleased =
false;
944 bool wasDestructed =
false;
945 auto obj = make_intrusive<DestructableMock>(&resourcesReleased, &wasDestructed);
947 auto obj2 = make_intrusive<DestructableMock>(&dummy, &dummy);
948 obj2 = std::move(obj);
949 EXPECT_FALSE(resourcesReleased);
950 EXPECT_FALSE(wasDestructed);
952 EXPECT_TRUE(resourcesReleased);
953 EXPECT_TRUE(wasDestructed);
958 givenPtr_whenMoveAssignedToBaseClass_thenDestructsObjectAfterSecondDestructed) {
960 bool resourcesReleased =
false;
961 bool wasDestructed =
false;
962 auto obj = make_intrusive<ChildDestructableMock>(&resourcesReleased, &wasDestructed);
964 auto obj2 = make_intrusive<DestructableMock>(&dummy, &dummy);
965 obj2 = std::move(obj);
966 EXPECT_FALSE(resourcesReleased);
967 EXPECT_FALSE(wasDestructed);
969 EXPECT_TRUE(resourcesReleased);
970 EXPECT_TRUE(wasDestructed);
975 givenPtr_whenCopyConstructedAndDestructed_thenDestructsObjectAfterLastDestruction) {
976 bool resourcesReleased =
false;
977 bool wasDestructed =
false;
979 auto obj = make_intrusive<DestructableMock>(&resourcesReleased, &wasDestructed);
982 EXPECT_FALSE(resourcesReleased);
983 EXPECT_FALSE(wasDestructed);
985 EXPECT_FALSE(resourcesReleased);
986 EXPECT_FALSE(wasDestructed);
988 EXPECT_TRUE(resourcesReleased);
989 EXPECT_TRUE(wasDestructed);
994 givenPtr_whenCopyConstructedToBaseClassAndDestructed_thenDestructsObjectAfterLastDestruction) {
995 bool resourcesReleased =
false;
996 bool wasDestructed =
false;
998 auto obj = make_intrusive<ChildDestructableMock>(&resourcesReleased, &wasDestructed);
1001 EXPECT_FALSE(resourcesReleased);
1002 EXPECT_FALSE(wasDestructed);
1004 EXPECT_FALSE(resourcesReleased);
1005 EXPECT_FALSE(wasDestructed);
1007 EXPECT_TRUE(resourcesReleased);
1008 EXPECT_TRUE(wasDestructed);
1013 givenPtr_whenCopyConstructedAndOriginalDestructed_thenDestructsObjectAfterLastDestruction) {
1014 bool resourcesReleased =
false;
1015 bool wasDestructed =
false;
1017 auto obj = make_intrusive<DestructableMock>(&resourcesReleased, &wasDestructed);
1020 EXPECT_FALSE(resourcesReleased);
1021 EXPECT_FALSE(wasDestructed);
1023 EXPECT_TRUE(resourcesReleased);
1024 EXPECT_TRUE(wasDestructed);
1029 givenPtr_whenCopyConstructedToBaseClassAndOriginalDestructed_thenDestructsObjectAfterLastDestruction) {
1030 bool resourcesReleased =
false;
1031 bool wasDestructed =
false;
1033 auto obj = make_intrusive<ChildDestructableMock>(&resourcesReleased, &wasDestructed);
1036 EXPECT_FALSE(resourcesReleased);
1037 EXPECT_FALSE(wasDestructed);
1039 EXPECT_TRUE(resourcesReleased);
1040 EXPECT_TRUE(wasDestructed);
1045 givenPtr_whenCopyAssignedAndDestructed_thenDestructsObjectAfterLastDestruction) {
1046 bool resourcesReleased =
false;
1047 bool wasDestructed =
false;
1050 auto obj = make_intrusive<DestructableMock>(&resourcesReleased, &wasDestructed);
1053 make_intrusive<DestructableMock>(&dummy, &dummy);
1055 EXPECT_FALSE(resourcesReleased);
1056 EXPECT_FALSE(wasDestructed);
1058 EXPECT_FALSE(resourcesReleased);
1059 EXPECT_FALSE(wasDestructed);
1061 EXPECT_TRUE(resourcesReleased);
1062 EXPECT_TRUE(wasDestructed);
1067 givenPtr_whenCopyAssignedToBaseClassAndDestructed_thenDestructsObjectAfterLastDestruction) {
1068 bool resourcesReleased =
false;
1069 bool wasDestructed =
false;
1072 auto obj = make_intrusive<ChildDestructableMock>(&resourcesReleased, &wasDestructed);
1075 make_intrusive<DestructableMock>(&dummy, &dummy);
1077 EXPECT_FALSE(resourcesReleased);
1078 EXPECT_FALSE(wasDestructed);
1080 EXPECT_FALSE(resourcesReleased);
1081 EXPECT_FALSE(wasDestructed);
1083 EXPECT_TRUE(resourcesReleased);
1084 EXPECT_TRUE(wasDestructed);
1089 givenPtr_whenCopyAssignedAndOriginalDestructed_thenDestructsObjectAfterLastDestruction) {
1090 bool resourcesReleased =
false;
1091 bool wasDestructed =
false;
1094 auto copy = make_intrusive<DestructableMock>(&dummy, &dummy);
1096 auto obj = make_intrusive<DestructableMock>(&resourcesReleased, &wasDestructed);
1098 EXPECT_FALSE(resourcesReleased);
1099 EXPECT_FALSE(wasDestructed);
1101 EXPECT_FALSE(resourcesReleased);
1102 EXPECT_FALSE(wasDestructed);
1104 EXPECT_TRUE(resourcesReleased);
1105 EXPECT_TRUE(wasDestructed);
1110 givenPtr_whenCopyAssignedToBaseClassAndOriginalDestructed_thenDestructsObjectAfterLastDestruction) {
1111 bool wasDestructed =
false;
1112 bool resourcesReleased =
false;
1115 auto copy = make_intrusive<DestructableMock>(&dummy, &dummy);
1118 make_intrusive<ChildDestructableMock>(&resourcesReleased, &wasDestructed);
1120 EXPECT_FALSE(resourcesReleased);
1121 EXPECT_FALSE(wasDestructed);
1123 EXPECT_FALSE(resourcesReleased);
1124 EXPECT_FALSE(wasDestructed);
1126 EXPECT_TRUE(resourcesReleased);
1127 EXPECT_TRUE(wasDestructed);
1130 TEST(IntrusivePtrTest, givenPtr_whenCopyAssigned_thenDestructsOldObject) {
1132 bool resourcesReleased =
false;
1133 bool wasDestructed =
false;
1134 auto obj = make_intrusive<DestructableMock>(&dummy, &dummy);
1136 auto obj2 = make_intrusive<DestructableMock>(&resourcesReleased, &wasDestructed);
1137 EXPECT_FALSE(resourcesReleased);
1138 EXPECT_FALSE(wasDestructed);
1140 EXPECT_TRUE(resourcesReleased);
1141 EXPECT_TRUE(wasDestructed);
1147 givenPtr_whenCopyAssignedToBaseClass_thenDestructsOldObject) {
1149 bool resourcesReleased =
false;
1150 bool wasDestructed =
false;
1151 auto obj = make_intrusive<ChildDestructableMock>(&dummy, &dummy);
1153 auto obj2 = make_intrusive<DestructableMock>(&resourcesReleased, &wasDestructed);
1154 EXPECT_FALSE(resourcesReleased);
1155 EXPECT_FALSE(wasDestructed);
1157 EXPECT_TRUE(resourcesReleased);
1158 EXPECT_TRUE(wasDestructed);
1164 givenPtrWithCopy_whenCopyAssigned_thenDestructsOldObjectAfterCopyIsDestructed) {
1166 bool resourcesReleased =
false;
1167 bool wasDestructed =
false;
1168 auto obj = make_intrusive<DestructableMock>(&dummy, &dummy);
1170 auto obj2 = make_intrusive<DestructableMock>(&resourcesReleased, &wasDestructed);
1173 EXPECT_FALSE(resourcesReleased);
1174 EXPECT_FALSE(wasDestructed);
1176 EXPECT_FALSE(resourcesReleased);
1177 EXPECT_FALSE(wasDestructed);
1179 EXPECT_TRUE(resourcesReleased);
1180 EXPECT_TRUE(wasDestructed);
1186 givenPtrWithBaseClassCopy_whenCopyAssigned_thenDestructsOldObjectAfterCopyIsDestructed) {
1188 bool resourcesReleased =
false;
1189 bool wasDestructed =
false;
1190 auto obj = make_intrusive<ChildDestructableMock>(&dummy, &dummy);
1193 make_intrusive<ChildDestructableMock>(&resourcesReleased, &wasDestructed);
1196 EXPECT_FALSE(resourcesReleased);
1197 EXPECT_FALSE(wasDestructed);
1199 EXPECT_FALSE(resourcesReleased);
1200 EXPECT_FALSE(wasDestructed);
1202 EXPECT_TRUE(resourcesReleased);
1203 EXPECT_TRUE(wasDestructed);
1209 givenPtrWithCopy_whenCopyAssignedToBaseClass_thenDestructsOldObjectAfterCopyIsDestructed) {
1211 bool resourcesReleased =
false;
1212 bool wasDestructed =
false;
1213 auto obj = make_intrusive<ChildDestructableMock>(&dummy, &dummy);
1215 auto obj2 = make_intrusive<DestructableMock>(&resourcesReleased, &wasDestructed);
1218 EXPECT_FALSE(resourcesReleased);
1219 EXPECT_FALSE(wasDestructed);
1221 EXPECT_FALSE(resourcesReleased);
1222 EXPECT_FALSE(wasDestructed);
1224 EXPECT_TRUE(resourcesReleased);
1225 EXPECT_TRUE(wasDestructed);
1229 TEST(IntrusivePtrTest, givenPtr_whenCallingReset_thenDestructs) {
1230 bool resourcesReleased =
false;
1231 bool wasDestructed =
false;
1232 auto obj = make_intrusive<DestructableMock>(&resourcesReleased, &wasDestructed);
1233 EXPECT_FALSE(resourcesReleased);
1234 EXPECT_FALSE(wasDestructed);
1236 EXPECT_TRUE(resourcesReleased);
1237 EXPECT_TRUE(wasDestructed);
1242 givenPtrWithCopy_whenCallingReset_thenDestructsAfterCopyDestructed) {
1243 bool resourcesReleased =
false;
1244 bool wasDestructed =
false;
1245 auto obj = make_intrusive<DestructableMock>(&resourcesReleased, &wasDestructed);
1249 EXPECT_FALSE(resourcesReleased);
1250 EXPECT_FALSE(wasDestructed);
1252 EXPECT_TRUE(resourcesReleased);
1253 EXPECT_TRUE(wasDestructed);
1259 givenPtrWithCopy_whenCallingResetOnCopy_thenDestructsAfterOriginalDestructed) {
1260 bool resourcesReleased =
false;
1261 bool wasDestructed =
false;
1262 auto obj = make_intrusive<DestructableMock>(&resourcesReleased, &wasDestructed);
1266 EXPECT_FALSE(resourcesReleased);
1267 EXPECT_FALSE(wasDestructed);
1269 EXPECT_TRUE(resourcesReleased);
1270 EXPECT_TRUE(wasDestructed);
1276 givenPtrWithMoved_whenCallingReset_thenDestructsAfterMovedDestructed) {
1277 bool resourcesReleased =
false;
1278 bool wasDestructed =
false;
1279 auto obj = make_intrusive<DestructableMock>(&resourcesReleased, &wasDestructed);
1281 auto moved = std::move(obj);
1283 EXPECT_FALSE(resourcesReleased);
1284 EXPECT_FALSE(wasDestructed);
1286 EXPECT_TRUE(resourcesReleased);
1287 EXPECT_TRUE(wasDestructed);
1293 givenPtrWithMoved_whenCallingResetOnMoved_thenDestructsImmediately) {
1294 bool resourcesReleased =
false;
1295 bool wasDestructed =
false;
1296 auto obj = make_intrusive<DestructableMock>(&resourcesReleased, &wasDestructed);
1298 auto moved = std::move(obj);
1300 EXPECT_TRUE(resourcesReleased);
1301 EXPECT_TRUE(wasDestructed);
1305 TEST(IntrusivePtrTest, AllowsMoveConstructingToConst) {
1310 TEST(IntrusivePtrTest, AllowsCopyConstructingToConst) {
1315 TEST(IntrusivePtrTest, AllowsMoveAssigningToConst) {
1321 TEST(IntrusivePtrTest, AllowsCopyAssigningToConst) {
1327 TEST(IntrusivePtrTest, givenNewPtr_thenHasUseCount1) {
1329 EXPECT_EQ(1, obj.use_count());
1332 TEST(IntrusivePtrTest, givenNewPtr_thenIsUnique) {
1334 EXPECT_TRUE(obj.unique());
1337 TEST(IntrusivePtrTest, givenEmptyPtr_thenHasUseCount0) {
1339 EXPECT_EQ(0, obj.use_count());
1342 TEST(IntrusivePtrTest, givenEmptyPtr_thenIsNotUnique) {
1344 EXPECT_FALSE(obj.unique());
1347 TEST(IntrusivePtrTest, givenResetPtr_thenHasUseCount0) {
1350 EXPECT_EQ(0, obj.use_count());
1353 TEST(IntrusivePtrTest, givenResetPtr_thenIsNotUnique) {
1356 EXPECT_FALSE(obj.unique());
1359 TEST(IntrusivePtrTest, givenMoveConstructedPtr_thenHasUseCount1) {
1362 EXPECT_EQ(1, obj2.use_count());
1365 TEST(IntrusivePtrTest, givenMoveConstructedPtr_thenIsUnique) {
1368 EXPECT_TRUE(obj2.unique());
1371 TEST(IntrusivePtrTest, givenMoveConstructedPtr_thenOldHasUseCount0) {
1374 EXPECT_EQ(0, obj.use_count());
1377 TEST(IntrusivePtrTest, givenMoveConstructedPtr_thenOldIsNotUnique) {
1380 EXPECT_FALSE(obj.unique());
1383 TEST(IntrusivePtrTest, givenMoveAssignedPtr_thenHasUseCount1) {
1386 obj2 = std::move(obj);
1387 EXPECT_EQ(1, obj2.use_count());
1390 TEST(IntrusivePtrTest, givenMoveAssignedPtr_thenIsUnique) {
1393 obj2 = std::move(obj);
1394 EXPECT_TRUE(obj2.unique());
1397 TEST(IntrusivePtrTest, givenMoveAssignedPtr_thenOldHasUseCount0) {
1400 obj2 = std::move(obj);
1401 EXPECT_EQ(0, obj.use_count());
1404 TEST(IntrusivePtrTest, givenMoveAssignedPtr_thenOldIsNotUnique) {
1407 obj2 = std::move(obj);
1408 EXPECT_FALSE(obj.unique());
1411 TEST(IntrusivePtrTest, givenCopyConstructedPtr_thenHasUseCount2) {
1414 EXPECT_EQ(2, obj2.use_count());
1417 TEST(IntrusivePtrTest, givenCopyConstructedPtr_thenIsNotUnique) {
1420 EXPECT_FALSE(obj2.unique());
1423 TEST(IntrusivePtrTest, givenCopyConstructedPtr_thenOldHasUseCount2) {
1426 EXPECT_EQ(2, obj.use_count());
1429 TEST(IntrusivePtrTest, givenCopyConstructedPtr_thenOldIsNotUnique) {
1432 EXPECT_FALSE(obj.unique());
1437 givenCopyConstructedPtr_whenDestructingCopy_thenHasUseCount1) {
1441 EXPECT_EQ(2, obj.use_count());
1443 EXPECT_EQ(1, obj.use_count());
1448 givenCopyConstructedPtr_whenDestructingCopy_thenIsUnique) {
1452 EXPECT_FALSE(obj.unique());
1454 EXPECT_TRUE(obj.unique());
1459 givenCopyConstructedPtr_whenReassigningCopy_thenHasUseCount1) {
1462 EXPECT_EQ(2, obj.use_count());
1463 obj2 = make_intrusive<SomeClass>();
1464 EXPECT_EQ(1, obj.use_count());
1465 EXPECT_EQ(1, obj2.use_count());
1470 givenCopyConstructedPtr_whenReassigningCopy_thenIsUnique) {
1473 EXPECT_FALSE(obj.unique());
1474 obj2 = make_intrusive<SomeClass>();
1475 EXPECT_TRUE(obj.unique());
1476 EXPECT_TRUE(obj2.unique());
1479 TEST(IntrusivePtrTest, givenCopyAssignedPtr_thenHasUseCount2) {
1483 EXPECT_EQ(2, obj.use_count());
1484 EXPECT_EQ(2, obj2.use_count());
1487 TEST(IntrusivePtrTest, givenCopyAssignedPtr_thenIsNotUnique) {
1491 EXPECT_FALSE(obj.unique());
1492 EXPECT_FALSE(obj2.unique());
1497 givenCopyAssignedPtr_whenDestructingCopy_thenHasUseCount1) {
1502 EXPECT_EQ(2, obj.use_count());
1504 EXPECT_EQ(1, obj.use_count());
1507 TEST(IntrusivePtrTest, givenCopyAssignedPtr_whenDestructingCopy_thenIsUnique) {
1512 EXPECT_FALSE(obj.unique());
1514 EXPECT_TRUE(obj.unique());
1519 givenCopyAssignedPtr_whenReassigningCopy_thenHasUseCount1) {
1523 EXPECT_EQ(2, obj.use_count());
1524 obj2 = make_intrusive<SomeClass>();
1525 EXPECT_EQ(1, obj.use_count());
1526 EXPECT_EQ(1, obj2.use_count());
1529 TEST(IntrusivePtrTest, givenCopyAssignedPtr_whenReassigningCopy_thenIsUnique) {
1533 EXPECT_FALSE(obj.unique());
1534 obj2 = make_intrusive<SomeClass>();
1535 EXPECT_TRUE(obj.unique());
1536 EXPECT_TRUE(obj2.unique());
1539 TEST(IntrusivePtrTest, givenPtr_whenReleasedAndReclaimed_thenDoesntCrash) {
1541 SomeClass* ptr = obj.
release();
1542 EXPECT_FALSE(obj.defined());
1548 givenPtr_whenReleasedAndReclaimed_thenIsDestructedAtEnd) {
1549 bool resourcesReleased =
false;
1550 bool wasDestructed =
false;
1555 make_intrusive<DestructableMock>(&resourcesReleased, &wasDestructed);
1556 DestructableMock* ptr = inner.
release();
1557 EXPECT_FALSE(resourcesReleased);
1558 EXPECT_FALSE(wasDestructed);
1562 EXPECT_FALSE(resourcesReleased);
1563 EXPECT_FALSE(wasDestructed);
1566 EXPECT_TRUE(resourcesReleased);
1567 EXPECT_TRUE(wasDestructed);
1570 TEST(IntrusivePtrTest, givenStackObject_whenReclaimed_thenCrashes) {
1578 TEST(IntrusivePtrTest, givenPtr_whenNonOwningReclaimed_thenDoesntCrash) {
1580 SomeClass* raw_ptr = obj.get();
1581 EXPECT_TRUE(obj.defined());
1584 EXPECT_TRUE(reclaimed.defined());
1585 EXPECT_EQ(reclaimed.get(), obj.get());
1590 givenPtr_whenNonOwningReclaimed_thenIsDestructedAtEnd) {
1591 bool resourcesReleased =
false;
1592 bool wasDestructed =
false;
1597 make_intrusive<DestructableMock>(&resourcesReleased, &wasDestructed);
1598 DestructableMock* raw_ptr = inner.get();
1603 EXPECT_FALSE(resourcesReleased);
1604 EXPECT_FALSE(wasDestructed);
1607 EXPECT_TRUE(resourcesReleased);
1608 EXPECT_TRUE(wasDestructed);
1613 struct IntrusiveAndWeak final {
1619 template <
class T,
class... Args>
1620 IntrusiveAndWeak<T> make_weak_intrusive(Args&&... args) {
1621 return IntrusiveAndWeak<T>(make_intrusive<T>(std::forward<Args>(args)...));
1623 template <
class T,
class... Args>
1625 auto intrusive = make_intrusive<T>(std::forward<Args>(args)...);
1628 template <
class T,
class NullType = c10::detail::
intrusive_target_default_null_type<T>>
1636 "weak_intrusive_ptr<T>::element_type is wrong");
1639 WeakIntrusivePtrTest,
1640 givenPtr_whenCreatingAndDestructing_thenDoesntCrash) {
1641 IntrusiveAndWeak<SomeClass> var = make_weak_intrusive<SomeClass>();
1644 TEST(WeakIntrusivePtrTest, givenPtr_whenLocking_thenReturnsCorrectObject) {
1645 IntrusiveAndWeak<SomeClass> var = make_weak_intrusive<SomeClass>();
1647 EXPECT_EQ(var.ptr.get(), locked.get());
1651 WeakIntrusivePtrTest,
1652 givenValidPtr_whenMoveAssigning_thenPointsToSameObject) {
1653 IntrusiveAndWeak<SomeClass> obj1 = make_weak_intrusive<SomeClass>();
1654 IntrusiveAndWeak<SomeClass> obj2 = make_weak_intrusive<SomeClass>();
1655 SomeClass* obj1ptr = obj1.weak.lock().get();
1656 obj2.weak = std::move(obj1.weak);
1657 EXPECT_EQ(obj1ptr, obj2.weak.lock().get());
1661 WeakIntrusivePtrTest,
1662 givenValidPtr_whenMoveAssigning_thenOldInstanceInvalid) {
1663 IntrusiveAndWeak<SomeClass> obj1 = make_weak_intrusive<SomeClass>();
1664 IntrusiveAndWeak<SomeClass> obj2 = make_weak_intrusive<SomeClass>();
1665 obj2.weak = std::move(obj1.weak);
1666 EXPECT_TRUE(obj1.weak.expired());
1670 WeakIntrusivePtrTest,
1671 givenInvalidPtr_whenMoveAssigning_thenNewInstanceIsValid) {
1672 IntrusiveAndWeak<SomeClass> obj1 = make_weak_intrusive<SomeClass>();
1674 SomeClass* obj1ptr = obj1.weak.lock().get();
1675 obj2 = std::move(obj1.weak);
1676 EXPECT_FALSE(obj2.expired());
1680 WeakIntrusivePtrTest,
1681 givenValidPtr_whenMoveAssigningToSelf_thenPointsToSameObject) {
1682 IntrusiveAndWeak<SomeClass> obj1 = make_weak_intrusive<SomeClass>();
1683 SomeClass* obj1ptr = obj1.weak.lock().get();
1684 obj1.weak = std::move(obj1.weak);
1685 EXPECT_EQ(obj1ptr, obj1.weak.lock().get());
1689 WeakIntrusivePtrTest,
1690 givenValidPtr_whenMoveAssigningToSelf_thenStaysValid) {
1691 IntrusiveAndWeak<SomeClass> obj1 = make_weak_intrusive<SomeClass>();
1692 obj1.weak = std::move(obj1.weak);
1693 EXPECT_FALSE(obj1.weak.expired());
1697 WeakIntrusivePtrTest,
1698 givenInvalidPtr_whenMoveAssigning_thenPointsToSameObject) {
1699 IntrusiveAndWeak<SomeClass> obj1 = make_weak_intrusive<SomeClass>();
1701 SomeClass* obj1ptr = obj1.weak.lock().get();
1702 obj2 = std::move(obj1.weak);
1703 EXPECT_EQ(obj1ptr, obj2.lock().get());
1707 WeakIntrusivePtrTest,
1708 givenInvalidPtr_whenMoveAssigningToSelf_thenStaysInvalid) {
1710 obj1 = std::move(obj1);
1711 EXPECT_TRUE(obj1.expired());
1715 WeakIntrusivePtrTest,
1716 givenWeakOnlyPtr_whenMoveAssigning_thenNewInstanceIsValid) {
1717 IntrusiveAndWeak<SomeClass> obj1 = make_weak_intrusive<SomeClass>();
1719 SomeClass* obj1ptr = obj1.weak.lock().get();
1720 obj2 = std::move(obj1.weak);
1721 EXPECT_FALSE(obj2.expired());
1725 WeakIntrusivePtrTest,
1726 givenWeakOnlyPtr_whenMoveAssigning_thenPointsToSameObject) {
1727 IntrusiveAndWeak<SomeClass> obj1 = make_weak_intrusive<SomeClass>();
1729 SomeClass* obj1ptr = obj1.weak.lock().get();
1730 obj2 = std::move(obj1.weak);
1731 EXPECT_EQ(obj1ptr, obj2.lock().get());
1735 WeakIntrusivePtrTest,
1736 givenWeakOnlyPtr_whenMoveAssigningToSelf_thenStaysInvalid) {
1738 SomeClass* obj1ptr = obj1.lock().get();
1739 obj1 = std::move(obj1);
1740 EXPECT_TRUE(obj1.expired());
1744 WeakIntrusivePtrTest,
1745 givenWeakOnlyPtr_whenMoveAssigningToSelf_thenPointsToSameObject) {
1747 SomeClass* obj1ptr = obj1.lock().get();
1748 obj1 = std::move(obj1);
1749 EXPECT_EQ(obj1ptr, obj1.lock().get());
1753 WeakIntrusivePtrTest,
1754 givenValidPtr_whenMoveAssigningFromInvalidPtr_thenNewInstanceIsInvalid) {
1756 IntrusiveAndWeak<SomeClass> obj2 = make_weak_intrusive<SomeClass>();
1757 EXPECT_FALSE(obj2.weak.expired());
1758 obj2.weak = std::move(obj1);
1759 EXPECT_TRUE(obj2.weak.expired());
1763 WeakIntrusivePtrTest,
1764 givenValidPtr_whenMoveAssigningFromWeakOnlyPtr_thenNewInstanceIsInvalid) {
1766 IntrusiveAndWeak<SomeClass> obj2 = make_weak_intrusive<SomeClass>();
1767 EXPECT_FALSE(obj2.weak.expired());
1768 obj2.weak = std::move(obj1);
1769 EXPECT_TRUE(obj2.weak.expired());
1773 WeakIntrusivePtrTest,
1774 givenValidPtr_whenMoveAssigningToBaseClass_thenPointsToSameObject) {
1775 IntrusiveAndWeak<SomeChildClass> obj1 =
1776 make_weak_intrusive<SomeChildClass>(1);
1777 IntrusiveAndWeak<SomeBaseClass> obj2 = make_weak_intrusive<SomeBaseClass>(2);
1778 SomeBaseClass* obj1ptr = obj1.weak.lock().get();
1779 obj2.weak = std::move(obj1.weak);
1780 EXPECT_EQ(obj1ptr, obj2.weak.lock().get());
1781 EXPECT_EQ(1, obj2.weak.lock()->v);
1785 WeakIntrusivePtrTest,
1786 givenValidPtr_whenMoveAssigningToBaseClass_thenOldInstanceInvalid) {
1787 IntrusiveAndWeak<SomeChildClass> obj1 =
1788 make_weak_intrusive<SomeChildClass>(1);
1789 IntrusiveAndWeak<SomeBaseClass> obj2 = make_weak_intrusive<SomeBaseClass>(2);
1790 obj2.weak = std::move(obj1.weak);
1791 EXPECT_TRUE(obj1.weak.expired());
1795 WeakIntrusivePtrTest,
1796 givenInvalidPtr_whenMoveAssigningToBaseClass_thenNewInstanceIsValid) {
1797 IntrusiveAndWeak<SomeChildClass> obj1 =
1798 make_weak_intrusive<SomeChildClass>(5);
1800 SomeBaseClass* obj1ptr = obj1.weak.lock().get();
1801 obj2 = std::move(obj1.weak);
1802 EXPECT_FALSE(obj2.expired());
1806 WeakIntrusivePtrTest,
1807 givenInvalidPtr_whenMoveAssigningToBaseClass_thenPointsToSameObject) {
1808 IntrusiveAndWeak<SomeChildClass> obj1 =
1809 make_weak_intrusive<SomeChildClass>(5);
1811 SomeBaseClass* obj1ptr = obj1.weak.lock().get();
1812 obj2 = std::move(obj1.weak);
1813 EXPECT_EQ(obj1ptr, obj2.lock().get());
1814 EXPECT_EQ(5, obj2.lock()->v);
1818 WeakIntrusivePtrTest,
1819 givenInvalidPtr_whenMoveAssigningInvalidPtrToBaseClass_thenNewInstanceIsValid) {
1821 IntrusiveAndWeak<SomeBaseClass> obj2 = make_weak_intrusive<SomeBaseClass>(2);
1822 EXPECT_FALSE(obj2.weak.expired());
1823 obj2.weak = std::move(obj1);
1824 EXPECT_TRUE(obj2.weak.expired());
1828 WeakIntrusivePtrTest,
1829 givenWeakOnlyPtr_whenMoveAssigningToBaseClass_thenNewInstanceIsValid) {
1830 IntrusiveAndWeak<SomeChildClass> obj1 =
1831 make_weak_intrusive<SomeChildClass>(5);
1833 SomeBaseClass* obj1ptr = obj1.weak.lock().get();
1834 obj2 = std::move(obj1.weak);
1835 EXPECT_FALSE(obj2.expired());
1839 WeakIntrusivePtrTest,
1840 givenWeakOnlyPtr_whenMoveAssigningToBaseClass_thenPointsToSameObject) {
1841 IntrusiveAndWeak<SomeChildClass> obj1 =
1842 make_weak_intrusive<SomeChildClass>(5);
1844 SomeBaseClass* obj1ptr = obj1.weak.lock().get();
1845 obj2 = std::move(obj1.weak);
1846 EXPECT_EQ(obj1ptr, obj2.lock().get());
1847 EXPECT_EQ(5, obj2.lock()->v);
1851 WeakIntrusivePtrTest,
1852 givenWeakOnlyPtr_whenMoveAssigningInvalidPtrToBaseClass_thenNewInstanceIsValid) {
1854 IntrusiveAndWeak<SomeBaseClass> obj2 = make_weak_intrusive<SomeBaseClass>(2);
1855 EXPECT_FALSE(obj2.weak.expired());
1856 obj2.weak = std::move(obj1);
1857 EXPECT_TRUE(obj2.weak.expired());
1861 WeakIntrusivePtrTest,
1862 givenNullPtr_whenMoveAssigningToDifferentNullptr_thenHasNewNullptr) {
1865 obj2 = std::move(obj1);
1866 EXPECT_NE(NullType1::singleton(), NullType2::singleton());
1867 EXPECT_TRUE(obj1.expired());
1868 EXPECT_TRUE(obj2.expired());
1872 WeakIntrusivePtrTest,
1873 givenValidPtr_whenCopyAssigning_thenPointsToSameObject) {
1874 IntrusiveAndWeak<SomeClass> obj1 = make_weak_intrusive<SomeClass>();
1875 IntrusiveAndWeak<SomeClass> obj2 = make_weak_intrusive<SomeClass>();
1876 SomeClass* obj1ptr = obj1.weak.lock().get();
1877 obj2.weak = obj1.weak;
1878 EXPECT_EQ(obj1ptr, obj2.weak.lock().get());
1882 WeakIntrusivePtrTest,
1883 givenValidPtr_whenCopyAssigning_thenOldInstanceValid) {
1884 IntrusiveAndWeak<SomeClass> obj1 = make_weak_intrusive<SomeClass>();
1885 IntrusiveAndWeak<SomeClass> obj2 = make_weak_intrusive<SomeClass>();
1886 obj2.weak = obj1.weak;
1887 EXPECT_FALSE(obj1.weak.expired());
1891 WeakIntrusivePtrTest,
1892 givenValidPtr_whenCopyAssigningToSelf_thenPointsToSameObject) {
1893 IntrusiveAndWeak<SomeClass> obj1 = make_weak_intrusive<SomeClass>();
1894 SomeClass* obj1ptr = obj1.weak.lock().get();
1895 obj1.weak = obj1.weak;
1896 EXPECT_EQ(obj1ptr, obj1.weak.lock().get());
1900 WeakIntrusivePtrTest,
1901 givenValidPtr_whenCopyAssigningToSelf_thenStaysValid) {
1902 IntrusiveAndWeak<SomeClass> obj1 = make_weak_intrusive<SomeClass>();
1903 obj1.weak = obj1.weak;
1904 EXPECT_FALSE(obj1.weak.expired());
1908 WeakIntrusivePtrTest,
1909 givenInvalidPtr_whenCopyAssigning_thenNewInstanceIsValid) {
1910 IntrusiveAndWeak<SomeClass> obj1 = make_weak_intrusive<SomeClass>();
1912 SomeClass* obj1ptr = obj1.weak.lock().get();
1914 EXPECT_FALSE(obj2.expired());
1918 WeakIntrusivePtrTest,
1919 givenInvalidPtr_whenCopyAssigningToSelf_thenStaysInvalid) {
1922 EXPECT_TRUE(obj1.expired());
1926 WeakIntrusivePtrTest,
1927 givenWeakOnlyPtr_whenCopyAssigning_thenNewInstanceIsValid) {
1928 IntrusiveAndWeak<SomeClass> obj1 = make_weak_intrusive<SomeClass>();
1930 SomeClass* obj1ptr = obj1.weak.lock().get();
1932 EXPECT_FALSE(obj2.expired());
1936 WeakIntrusivePtrTest,
1937 givenWeakOnlyPtr_whenCopyAssigning_thenPointsToSameObject) {
1938 IntrusiveAndWeak<SomeClass> obj1 = make_weak_intrusive<SomeClass>();
1940 SomeClass* obj1ptr = obj1.weak.lock().get();
1942 EXPECT_EQ(obj1ptr, obj2.lock().get());
1946 WeakIntrusivePtrTest,
1947 givenWeakOnlyPtr_whenCopyAssigningToSelf_thenStaysInvalid) {
1949 SomeClass* obj1ptr = obj1.lock().get();
1951 EXPECT_TRUE(obj1.expired());
1955 WeakIntrusivePtrTest,
1956 givenWeakOnlyPtr_whenCopyAssigningToSelf_thenPointsToSameObject) {
1958 SomeClass* obj1ptr = obj1.lock().get();
1960 EXPECT_EQ(obj1ptr, obj1.lock().get());
1964 WeakIntrusivePtrTest,
1965 givenValidPtr_whenCopyAssigningToBaseClass_thenPointsToSameObject) {
1966 IntrusiveAndWeak<SomeChildClass> child =
1967 make_weak_intrusive<SomeChildClass>(3);
1968 IntrusiveAndWeak<SomeBaseClass> base = make_weak_intrusive<SomeBaseClass>(10);
1969 base.weak = child.weak;
1970 EXPECT_EQ(3, base.weak.lock()->v);
1974 WeakIntrusivePtrTest,
1975 givenValidPtr_whenCopyAssigningToBaseClass_thenOldInstanceInvalid) {
1976 IntrusiveAndWeak<SomeChildClass> obj1 =
1977 make_weak_intrusive<SomeChildClass>(3);
1978 IntrusiveAndWeak<SomeBaseClass> obj2 = make_weak_intrusive<SomeBaseClass>(10);
1979 obj2.weak = obj1.weak;
1980 EXPECT_FALSE(obj1.weak.expired());
1984 WeakIntrusivePtrTest,
1985 givenInvalidPtr_whenCopyAssigningToBaseClass_thenNewInstanceIsValid) {
1986 IntrusiveAndWeak<SomeChildClass> obj1 =
1987 make_weak_intrusive<SomeChildClass>(5);
1989 SomeBaseClass* obj1ptr = obj1.weak.lock().get();
1991 EXPECT_FALSE(obj2.expired());
1995 WeakIntrusivePtrTest,
1996 givenInvalidPtr_whenCopyAssigningToBaseClass_thenPointsToSameObject) {
1997 IntrusiveAndWeak<SomeChildClass> obj1 =
1998 make_weak_intrusive<SomeChildClass>(5);
2000 SomeBaseClass* obj1ptr = obj1.weak.lock().get();
2002 EXPECT_EQ(obj1ptr, obj2.lock().get());
2003 EXPECT_EQ(5, obj2.lock()->v);
2007 WeakIntrusivePtrTest,
2008 givenPtr_whenCopyAssigningInvalidPtrToBaseClass_thenNewInstanceIsInvalid) {
2010 IntrusiveAndWeak<SomeBaseClass> obj2 = make_weak_intrusive<SomeBaseClass>(2);
2011 EXPECT_FALSE(obj2.weak.expired());
2013 EXPECT_TRUE(obj2.weak.expired());
2017 WeakIntrusivePtrTest,
2018 givenWeakOnlyPtr_whenCopyAssigningToBaseClass_thenNewInstanceIsValid) {
2019 IntrusiveAndWeak<SomeChildClass> obj1 =
2020 make_weak_intrusive<SomeChildClass>(5);
2022 SomeBaseClass* obj1ptr = obj1.weak.lock().get();
2024 EXPECT_FALSE(obj2.expired());
2028 WeakIntrusivePtrTest,
2029 givenWeakOnlyPtr_whenCopyAssigningToBaseClass_thenPointsToSameObject) {
2030 IntrusiveAndWeak<SomeChildClass> obj1 =
2031 make_weak_intrusive<SomeChildClass>(5);
2033 SomeBaseClass* obj1ptr = obj1.weak.lock().get();
2035 EXPECT_EQ(obj1ptr, obj2.lock().get());
2036 EXPECT_EQ(5, obj2.lock()->v);
2040 WeakIntrusivePtrTest,
2041 givenPtr_whenCopyAssigningWeakOnlyPtrToBaseClass_thenNewInstanceIsValid) {
2043 IntrusiveAndWeak<SomeBaseClass> obj2 = make_weak_intrusive<SomeBaseClass>(2);
2044 EXPECT_FALSE(obj2.weak.expired());
2046 EXPECT_TRUE(obj2.weak.expired());
2050 WeakIntrusivePtrTest,
2051 givenNullPtr_whenCopyAssigningToDifferentNullptr_thenHasNewNullptr) {
2055 EXPECT_NE(NullType1::singleton(), NullType2::singleton());
2056 EXPECT_TRUE(obj1.expired());
2057 EXPECT_TRUE(obj2.expired());
2061 WeakIntrusivePtrTest,
2062 givenPtr_whenMoveConstructing_thenPointsToSameObject) {
2063 IntrusiveAndWeak<SomeClass> obj1 = make_weak_intrusive<SomeClass>();
2064 SomeClass* obj1ptr = obj1.weak.lock().get();
2066 EXPECT_EQ(obj1ptr, obj2.lock().get());
2070 WeakIntrusivePtrTest,
2071 givenPtr_whenMoveConstructing_thenOldInstanceInvalid) {
2072 IntrusiveAndWeak<SomeClass> obj1 = make_weak_intrusive<SomeClass>();
2074 EXPECT_TRUE(obj1.weak.expired());
2077 TEST(WeakIntrusivePtrTest, givenPtr_whenMoveConstructing_thenNewInstanceValid) {
2078 IntrusiveAndWeak<SomeClass> obj1 = make_weak_intrusive<SomeClass>();
2080 EXPECT_FALSE(obj2.expired());
2084 WeakIntrusivePtrTest,
2085 givenPtr_whenMoveConstructingFromInvalidPtr_thenNewInstanceInvalid) {
2088 EXPECT_TRUE(obj2.expired());
2092 WeakIntrusivePtrTest,
2093 givenPtr_whenMoveConstructingFromWeakOnlyPtr_thenNewInstanceInvalid) {
2096 EXPECT_TRUE(obj2.expired());
2100 WeakIntrusivePtrTest,
2101 givenPtr_whenMoveConstructingToBaseClass_thenPointsToSameObject) {
2102 IntrusiveAndWeak<SomeChildClass> child =
2103 make_weak_intrusive<SomeChildClass>(3);
2104 SomeBaseClass* objptr = child.weak.lock().get();
2106 EXPECT_EQ(3, base.lock()->v);
2107 EXPECT_EQ(objptr, base.lock().get());
2111 WeakIntrusivePtrTest,
2112 givenPtr_whenMoveConstructingToBaseClass_thenOldInstanceInvalid) {
2113 IntrusiveAndWeak<SomeChildClass> child =
2114 make_weak_intrusive<SomeChildClass>(3);
2116 EXPECT_TRUE(child.weak.expired());
2120 WeakIntrusivePtrTest,
2121 givenPtr_whenMoveConstructingToBaseClass_thenNewInstanceValid) {
2122 IntrusiveAndWeak<SomeChildClass> obj1 =
2123 make_weak_intrusive<SomeChildClass>(2);
2125 EXPECT_FALSE(obj2.expired());
2129 WeakIntrusivePtrTest,
2130 givenPtr_whenMoveConstructingToBaseClassFromInvalidPtr_thenNewInstanceInvalid) {
2133 EXPECT_TRUE(obj2.expired());
2137 WeakIntrusivePtrTest,
2138 givenPtr_whenMoveConstructingToBaseClassFromWeakOnlyPtr_thenNewInstanceInvalid) {
2141 EXPECT_TRUE(obj2.expired());
2145 WeakIntrusivePtrTest,
2146 givenNullPtr_whenMoveConstructingToDifferentNullptr_thenHasNewNullptr) {
2149 EXPECT_NE(NullType1::singleton(), NullType2::singleton());
2150 EXPECT_TRUE(obj1.expired());
2151 EXPECT_TRUE(obj2.expired());
2155 WeakIntrusivePtrTest,
2156 givenPtr_whenCopyConstructing_thenPointsToSameObject) {
2157 IntrusiveAndWeak<SomeClass> obj1 = make_weak_intrusive<SomeClass>();
2158 SomeClass* obj1ptr = obj1.weak.lock().get();
2160 EXPECT_EQ(obj1ptr, obj2.lock().get());
2161 EXPECT_FALSE(obj1.weak.expired());
2164 TEST(WeakIntrusivePtrTest, givenPtr_whenCopyConstructing_thenOldInstanceValid) {
2165 IntrusiveAndWeak<SomeClass> obj1 = make_weak_intrusive<SomeClass>();
2167 EXPECT_FALSE(obj1.weak.expired());
2170 TEST(WeakIntrusivePtrTest, givenPtr_whenCopyConstructing_thenNewInstanceValid) {
2171 IntrusiveAndWeak<SomeClass> obj1 = make_weak_intrusive<SomeClass>();
2173 EXPECT_FALSE(obj2.expired());
2177 WeakIntrusivePtrTest,
2178 givenPtr_whenCopyConstructingFromInvalidPtr_thenNewInstanceInvalid) {
2181 EXPECT_TRUE(obj2.expired());
2185 WeakIntrusivePtrTest,
2186 givenPtr_whenCopyConstructingFromWeakOnlyPtr_thenNewInstanceInvalid) {
2189 EXPECT_TRUE(obj2.expired());
2193 WeakIntrusivePtrTest,
2194 givenPtr_whenCopyConstructingToBaseClass_thenPointsToSameObject) {
2195 IntrusiveAndWeak<SomeChildClass> child =
2196 make_weak_intrusive<SomeChildClass>(3);
2197 SomeBaseClass* objptr = child.weak.lock().get();
2199 EXPECT_EQ(3, base.lock()->v);
2200 EXPECT_EQ(objptr, base.lock().get());
2204 WeakIntrusivePtrTest,
2205 givenPtr_whenCopyConstructingToBaseClass_thenOldInstanceInvalid) {
2206 IntrusiveAndWeak<SomeChildClass> child =
2207 make_weak_intrusive<SomeChildClass>(3);
2209 EXPECT_FALSE(child.weak.expired());
2213 WeakIntrusivePtrTest,
2214 givenPtr_whenCopyConstructingToBaseClass_thenNewInstanceInvalid) {
2215 IntrusiveAndWeak<SomeChildClass> child =
2216 make_weak_intrusive<SomeChildClass>(3);
2218 EXPECT_FALSE(base.expired());
2222 WeakIntrusivePtrTest,
2223 givenPtr_whenCopyConstructingToBaseClassFromInvalidPtr_thenNewInstanceInvalid) {
2226 EXPECT_TRUE(obj2.expired());
2230 WeakIntrusivePtrTest,
2231 givenPtr_whenCopyConstructingToBaseClassFromWeakOnlyPtr_thenNewInstanceInvalid) {
2234 EXPECT_TRUE(obj2.expired());
2238 WeakIntrusivePtrTest,
2239 givenNullPtr_whenCopyConstructingToDifferentNullptr_thenHasNewNullptr) {
2242 EXPECT_NE(NullType1::singleton(), NullType2::singleton());
2243 EXPECT_TRUE(obj1.expired());
2244 EXPECT_TRUE(obj2.expired());
2247 TEST(WeakIntrusivePtrTest, SwapFunction) {
2248 IntrusiveAndWeak<SomeClass> obj1 = make_weak_intrusive<SomeClass>();
2249 IntrusiveAndWeak<SomeClass> obj2 = make_weak_intrusive<SomeClass>();
2250 SomeClass* obj1ptr = obj1.weak.lock().get();
2251 SomeClass* obj2ptr = obj2.weak.lock().get();
2252 swap(obj1.weak, obj2.weak);
2253 EXPECT_EQ(obj2ptr, obj1.weak.lock().get());
2254 EXPECT_EQ(obj1ptr, obj2.weak.lock().get());
2257 TEST(WeakIntrusivePtrTest, SwapMethod) {
2258 IntrusiveAndWeak<SomeClass> obj1 = make_weak_intrusive<SomeClass>();
2259 IntrusiveAndWeak<SomeClass> obj2 = make_weak_intrusive<SomeClass>();
2260 SomeClass* obj1ptr = obj1.weak.lock().get();
2261 SomeClass* obj2ptr = obj2.weak.lock().get();
2262 obj1.weak.swap(obj2.weak);
2263 EXPECT_EQ(obj2ptr, obj1.weak.lock().get());
2264 EXPECT_EQ(obj1ptr, obj2.weak.lock().get());
2267 TEST(WeakIntrusivePtrTest, SwapFunctionFromInvalid) {
2269 IntrusiveAndWeak<SomeClass> obj2 = make_weak_intrusive<SomeClass>();
2270 SomeClass* obj2ptr = obj2.weak.lock().get();
2271 swap(obj1, obj2.weak);
2272 EXPECT_EQ(obj2ptr, obj1.lock().get());
2273 EXPECT_FALSE(obj1.expired());
2274 EXPECT_TRUE(obj2.weak.expired());
2277 TEST(WeakIntrusivePtrTest, SwapMethodFromInvalid) {
2279 IntrusiveAndWeak<SomeClass> obj2 = make_weak_intrusive<SomeClass>();
2280 SomeClass* obj2ptr = obj2.weak.lock().get();
2281 obj1.swap(obj2.weak);
2282 EXPECT_EQ(obj2ptr, obj1.lock().get());
2283 EXPECT_FALSE(obj1.expired());
2284 EXPECT_TRUE(obj2.weak.expired());
2287 TEST(WeakIntrusivePtrTest, SwapFunctionWithInvalid) {
2288 IntrusiveAndWeak<SomeClass> obj1 = make_weak_intrusive<SomeClass>();
2290 SomeClass* obj1ptr = obj1.weak.lock().get();
2291 swap(obj1.weak, obj2);
2292 EXPECT_TRUE(obj1.weak.expired());
2293 EXPECT_FALSE(obj2.expired());
2294 EXPECT_EQ(obj1ptr, obj2.lock().get());
2297 TEST(WeakIntrusivePtrTest, SwapMethodWithInvalid) {
2298 IntrusiveAndWeak<SomeClass> obj1 = make_weak_intrusive<SomeClass>();
2300 SomeClass* obj1ptr = obj1.weak.lock().get();
2301 obj1.weak.swap(obj2);
2302 EXPECT_TRUE(obj1.weak.expired());
2303 EXPECT_FALSE(obj2.expired());
2304 EXPECT_EQ(obj1ptr, obj2.lock().get());
2307 TEST(WeakIntrusivePtrTest, SwapFunctionInvalidWithInvalid) {
2311 EXPECT_TRUE(obj1.expired());
2312 EXPECT_TRUE(obj2.expired());
2315 TEST(WeakIntrusivePtrTest, SwapMethodInvalidWithInvalid) {
2319 EXPECT_TRUE(obj1.expired());
2320 EXPECT_TRUE(obj2.expired());
2323 TEST(WeakIntrusivePtrTest, SwapFunctionFromWeakOnlyPtr) {
2325 IntrusiveAndWeak<SomeClass> obj2 = make_weak_intrusive<SomeClass>();
2326 SomeClass* obj2ptr = obj2.weak.lock().get();
2327 swap(obj1, obj2.weak);
2328 EXPECT_EQ(obj2ptr, obj1.lock().get());
2329 EXPECT_FALSE(obj1.expired());
2330 EXPECT_TRUE(obj2.weak.expired());
2333 TEST(WeakIntrusivePtrTest, SwapMethodFromWeakOnlyPtr) {
2335 IntrusiveAndWeak<SomeClass> obj2 = make_weak_intrusive<SomeClass>();
2336 SomeClass* obj2ptr = obj2.weak.lock().get();
2337 obj1.swap(obj2.weak);
2338 EXPECT_EQ(obj2ptr, obj1.lock().get());
2339 EXPECT_FALSE(obj1.expired());
2340 EXPECT_TRUE(obj2.weak.expired());
2343 TEST(WeakIntrusivePtrTest, SwapFunctionWithWeakOnlyPtr) {
2344 IntrusiveAndWeak<SomeClass> obj1 = make_weak_intrusive<SomeClass>();
2346 SomeClass* obj1ptr = obj1.weak.lock().get();
2347 swap(obj1.weak, obj2);
2348 EXPECT_TRUE(obj1.weak.expired());
2349 EXPECT_FALSE(obj2.expired());
2350 EXPECT_EQ(obj1ptr, obj2.lock().get());
2353 TEST(WeakIntrusivePtrTest, SwapMethodWithWeakOnlyPtr) {
2354 IntrusiveAndWeak<SomeClass> obj1 = make_weak_intrusive<SomeClass>();
2356 SomeClass* obj1ptr = obj1.weak.lock().get();
2357 obj1.weak.swap(obj2);
2358 EXPECT_TRUE(obj1.weak.expired());
2359 EXPECT_FALSE(obj2.expired());
2360 EXPECT_EQ(obj1ptr, obj2.lock().get());
2363 TEST(WeakIntrusivePtrTest, SwapFunctionWeakOnlyPtrWithWeakOnlyPtr) {
2367 EXPECT_TRUE(obj1.expired());
2368 EXPECT_TRUE(obj2.expired());
2371 TEST(WeakIntrusivePtrTest, SwapMethodWeakOnlyPtrWithWeakOnlyPtr) {
2375 EXPECT_TRUE(obj1.expired());
2376 EXPECT_TRUE(obj2.expired());
2379 TEST(WeakIntrusivePtrTest, CanBePutInContainer) {
2380 std::vector<weak_intrusive_ptr<SomeClass1Parameter>> vec;
2381 IntrusiveAndWeak<SomeClass1Parameter> obj =
2382 make_weak_intrusive<SomeClass1Parameter>(5);
2383 vec.push_back(obj.weak);
2384 EXPECT_EQ(5, vec[0].lock()->param);
2387 TEST(WeakIntrusivePtrTest, CanBePutInSet) {
2388 std::set<weak_intrusive_ptr<SomeClass1Parameter>>
set;
2389 IntrusiveAndWeak<SomeClass1Parameter> obj =
2390 make_weak_intrusive<SomeClass1Parameter>(5);
2391 set.insert(obj.weak);
2392 EXPECT_EQ(5,
set.begin()->lock()->param);
2395 TEST(WeakIntrusivePtrTest, CanBePutInUnorderedSet) {
2396 std::unordered_set<weak_intrusive_ptr<SomeClass1Parameter>>
set;
2397 IntrusiveAndWeak<SomeClass1Parameter> obj =
2398 make_weak_intrusive<SomeClass1Parameter>(5);
2399 set.insert(obj.weak);
2400 EXPECT_EQ(5,
set.begin()->lock()->param);
2403 TEST(WeakIntrusivePtrTest, CanBePutInMap) {
2406 weak_intrusive_ptr<SomeClass1Parameter>>
2408 IntrusiveAndWeak<SomeClass1Parameter> obj1 =
2409 make_weak_intrusive<SomeClass1Parameter>(5);
2410 IntrusiveAndWeak<SomeClass1Parameter> obj2 =
2411 make_weak_intrusive<SomeClass1Parameter>(3);
2412 map.insert(std::make_pair(obj1.weak, obj2.weak));
2413 EXPECT_EQ(5, map.begin()->first.lock()->param);
2414 EXPECT_EQ(3, map.begin()->second.lock()->param);
2417 TEST(WeakIntrusivePtrTest, CanBePutInUnorderedMap) {
2419 weak_intrusive_ptr<SomeClass1Parameter>,
2420 weak_intrusive_ptr<SomeClass1Parameter>>
2422 IntrusiveAndWeak<SomeClass1Parameter> obj1 =
2423 make_weak_intrusive<SomeClass1Parameter>(5);
2424 IntrusiveAndWeak<SomeClass1Parameter> obj2 =
2425 make_weak_intrusive<SomeClass1Parameter>(3);
2426 map.insert(std::make_pair(obj1.weak, obj2.weak));
2427 EXPECT_EQ(5, map.begin()->first.lock()->param);
2428 EXPECT_EQ(3, map.begin()->second.lock()->param);
2431 TEST(WeakIntrusivePtrTest, Equality_AfterCopyConstructor) {
2432 IntrusiveAndWeak<SomeClass> var1 = make_weak_intrusive<SomeClass>();
2434 EXPECT_TRUE(var1.weak == var2);
2435 EXPECT_FALSE(var1.weak != var2);
2438 TEST(WeakIntrusivePtrTest, Equality_AfterCopyAssignment) {
2439 IntrusiveAndWeak<SomeClass> var1 = make_weak_intrusive<SomeClass>();
2440 IntrusiveAndWeak<SomeClass> var2 = make_weak_intrusive<SomeClass>();
2441 var2.weak = var1.weak;
2442 EXPECT_TRUE(var1.weak == var2.weak);
2443 EXPECT_FALSE(var1.weak != var2.weak);
2446 TEST(WeakIntrusivePtrTest, Equality_AfterCopyAssignment_WeakOnly) {
2449 EXPECT_TRUE(var1 == var2);
2450 EXPECT_FALSE(var1 != var2);
2453 TEST(WeakIntrusivePtrTest, Equality_Invalid) {
2456 EXPECT_TRUE(var1 == var2);
2457 EXPECT_FALSE(var1 != var2);
2460 TEST(WeakIntrusivePtrTest, Nonequality) {
2461 IntrusiveAndWeak<SomeClass> var1 = make_intrusive<SomeClass>();
2462 IntrusiveAndWeak<SomeClass> var2 = make_intrusive<SomeClass>();
2463 EXPECT_TRUE(var1.weak != var2.weak);
2464 EXPECT_FALSE(var1.weak == var2.weak);
2467 TEST(WeakIntrusivePtrTest, Nonequality_InvalidLeft) {
2469 IntrusiveAndWeak<SomeClass> var2 = make_intrusive<SomeClass>();
2470 EXPECT_TRUE(var1 != var2.weak);
2471 EXPECT_FALSE(var1 == var2.weak);
2474 TEST(WeakIntrusivePtrTest, Nonequality_InvalidRight) {
2475 IntrusiveAndWeak<SomeClass> var1 = make_intrusive<SomeClass>();
2477 EXPECT_TRUE(var1.weak != var2);
2478 EXPECT_FALSE(var1.weak == var2);
2481 TEST(WeakIntrusivePtrTest, Nonequality_WeakOnly) {
2484 EXPECT_TRUE(var1 != var2);
2485 EXPECT_FALSE(var1 == var2);
2488 TEST(WeakIntrusivePtrTest, HashIsDifferent) {
2489 IntrusiveAndWeak<SomeClass> var1 = make_weak_intrusive<SomeClass>();
2490 IntrusiveAndWeak<SomeClass> var2 = make_weak_intrusive<SomeClass>();
2496 TEST(WeakIntrusivePtrTest, HashIsDifferent_ValidAndInvalid) {
2498 IntrusiveAndWeak<SomeClass> var2 = make_weak_intrusive<SomeClass>();
2504 TEST(WeakIntrusivePtrTest, HashIsDifferent_ValidAndWeakOnly) {
2506 IntrusiveAndWeak<SomeClass> var2 = make_weak_intrusive<SomeClass>();
2512 TEST(WeakIntrusivePtrTest, HashIsDifferent_WeakOnlyAndWeakOnly) {
2520 TEST(WeakIntrusivePtrTest, HashIsSame_AfterCopyConstructor) {
2521 IntrusiveAndWeak<SomeClass> var1 = make_weak_intrusive<SomeClass>();
2528 TEST(WeakIntrusivePtrTest, HashIsSame_AfterCopyConstructor_WeakOnly) {
2536 TEST(WeakIntrusivePtrTest, HashIsSame_AfterCopyAssignment) {
2537 IntrusiveAndWeak<SomeClass> var1 = make_weak_intrusive<SomeClass>();
2538 IntrusiveAndWeak<SomeClass> var2 = make_weak_intrusive<SomeClass>();
2539 var2.weak = var1.weak;
2545 TEST(WeakIntrusivePtrTest, HashIsSame_AfterCopyAssignment_WeakOnly) {
2554 TEST(WeakIntrusivePtrTest, HashIsSame_BothInvalid) {
2562 TEST(WeakIntrusivePtrTest, OneIsLess) {
2563 IntrusiveAndWeak<SomeClass> var1 = make_weak_intrusive<SomeClass>();
2564 IntrusiveAndWeak<SomeClass> var2 = make_weak_intrusive<SomeClass>();
2570 TEST(WeakIntrusivePtrTest, InvalidIsLess1) {
2572 IntrusiveAndWeak<SomeClass> var2 = make_weak_intrusive<SomeClass>();
2576 TEST(WeakIntrusivePtrTest, InvalidIsLess2) {
2577 IntrusiveAndWeak<SomeClass> var1 = make_weak_intrusive<SomeClass>();
2582 TEST(WeakIntrusivePtrTest, InvalidIsNotLessThanInvalid) {
2588 TEST(WeakIntrusivePtrTest, givenPtr_whenCallingResetOnWeakPtr_thenIsInvalid) {
2589 IntrusiveAndWeak<SomeClass> obj = make_weak_intrusive<SomeClass>();
2590 EXPECT_FALSE(obj.weak.expired());
2592 EXPECT_TRUE(obj.weak.expired());
2595 TEST(WeakIntrusivePtrTest, givenPtr_whenCallingResetOnStrongPtr_thenIsInvalid) {
2596 IntrusiveAndWeak<SomeClass> obj = make_weak_intrusive<SomeClass>();
2597 EXPECT_FALSE(obj.weak.expired());
2599 EXPECT_TRUE(obj.weak.expired());
2602 TEST(WeakIntrusivePtrTest, AllowsMoveConstructingToConst) {
2603 IntrusiveAndWeak<SomeClass> a = make_weak_intrusive<SomeClass>();
2607 TEST(WeakIntrusivePtrTest, AllowsCopyConstructingToConst) {
2608 IntrusiveAndWeak<SomeClass> a = make_weak_intrusive<SomeClass>();
2612 TEST(WeakIntrusivePtrTest, AllowsMoveAssigningToConst) {
2613 IntrusiveAndWeak<SomeClass> a = make_weak_intrusive<SomeClass>();
2614 IntrusiveAndWeak<const SomeClass> b = make_weak_intrusive<const SomeClass>();
2615 b.weak = std::move(a.weak);
2618 TEST(WeakIntrusivePtrTest, AllowsCopyAssigningToConst) {
2619 IntrusiveAndWeak<SomeClass> a = make_weak_intrusive<SomeClass>();
2620 IntrusiveAndWeak<const SomeClass> b = make_weak_intrusive<const SomeClass>();
2624 TEST(WeakIntrusivePtrTest, givenNewPtr_thenHasUseCount1) {
2625 IntrusiveAndWeak<SomeClass> obj = make_weak_intrusive<SomeClass>();
2626 EXPECT_EQ(1, obj.weak.use_count());
2629 TEST(WeakIntrusivePtrTest, givenNewPtr_thenIsNotExpired) {
2630 IntrusiveAndWeak<SomeClass> obj = make_weak_intrusive<SomeClass>();
2631 EXPECT_FALSE(obj.weak.expired());
2634 TEST(WeakIntrusivePtrTest, givenInvalidPtr_thenHasUseCount0) {
2636 EXPECT_EQ(0, obj.use_count());
2639 TEST(WeakIntrusivePtrTest, givenInvalidPtr_thenIsExpired) {
2641 EXPECT_TRUE(obj.expired());
2644 TEST(WeakIntrusivePtrTest, givenWeakOnlyPtr_thenHasUseCount0) {
2646 EXPECT_EQ(0, obj.use_count());
2649 TEST(WeakIntrusivePtrTest, givenWeakOnlyPtr_thenIsExpired) {
2651 EXPECT_TRUE(obj.expired());
2654 TEST(WeakIntrusivePtrTest, givenPtr_whenCallingWeakReset_thenHasUseCount0) {
2655 IntrusiveAndWeak<SomeClass> obj = make_weak_intrusive<SomeClass>();
2657 EXPECT_EQ(0, obj.weak.use_count());
2660 TEST(WeakIntrusivePtrTest, givenPtr_whenCallingWeakReset_thenIsExpired) {
2661 IntrusiveAndWeak<SomeClass> obj = make_weak_intrusive<SomeClass>();
2663 EXPECT_TRUE(obj.weak.expired());
2666 TEST(WeakIntrusivePtrTest, givenPtr_whenCallingStrongReset_thenHasUseCount0) {
2667 IntrusiveAndWeak<SomeClass> obj = make_weak_intrusive<SomeClass>();
2669 EXPECT_EQ(0, obj.weak.use_count());
2672 TEST(WeakIntrusivePtrTest, givenPtr_whenCallingStrongReset_thenIsExpired) {
2673 IntrusiveAndWeak<SomeClass> obj = make_weak_intrusive<SomeClass>();
2675 EXPECT_TRUE(obj.weak.expired());
2678 TEST(WeakIntrusivePtrTest, givenMoveConstructedPtr_thenHasUseCount1) {
2679 IntrusiveAndWeak<SomeClass> obj = make_weak_intrusive<SomeClass>();
2681 EXPECT_EQ(1, obj2.use_count());
2684 TEST(WeakIntrusivePtrTest, givenMoveConstructedPtr_thenIsNotExpired) {
2685 IntrusiveAndWeak<SomeClass> obj = make_weak_intrusive<SomeClass>();
2687 EXPECT_FALSE(obj2.expired());
2690 TEST(WeakIntrusivePtrTest, givenMoveConstructedPtr_thenOldHasUseCount0) {
2691 IntrusiveAndWeak<SomeClass> obj = make_weak_intrusive<SomeClass>();
2693 EXPECT_EQ(0, obj.weak.use_count());
2696 TEST(WeakIntrusivePtrTest, givenMoveConstructedPtr_thenOldIsExpired) {
2697 IntrusiveAndWeak<SomeClass> obj = make_weak_intrusive<SomeClass>();
2699 EXPECT_TRUE(obj.weak.expired());
2702 TEST(WeakIntrusivePtrTest, givenMoveAssignedPtr_thenHasUseCount1) {
2703 IntrusiveAndWeak<SomeClass> obj = make_weak_intrusive<SomeClass>();
2704 IntrusiveAndWeak<SomeClass> obj2 = make_weak_intrusive<SomeClass>();
2705 obj2.weak = std::move(obj.weak);
2706 EXPECT_EQ(1, obj2.weak.use_count());
2709 TEST(WeakIntrusivePtrTest, givenMoveAssignedPtr_thenIsNotExpired) {
2710 IntrusiveAndWeak<SomeClass> obj = make_weak_intrusive<SomeClass>();
2711 IntrusiveAndWeak<SomeClass> obj2 = make_weak_intrusive<SomeClass>();
2712 obj2.weak = std::move(obj.weak);
2713 EXPECT_FALSE(obj2.weak.expired());
2716 TEST(WeakIntrusivePtrTest, givenMoveAssignedPtr_thenOldHasUseCount0) {
2717 IntrusiveAndWeak<SomeClass> obj = make_weak_intrusive<SomeClass>();
2718 IntrusiveAndWeak<SomeClass> obj2 = make_weak_intrusive<SomeClass>();
2719 obj2.weak = std::move(obj.weak);
2720 EXPECT_EQ(0, obj.weak.use_count());
2723 TEST(WeakIntrusivePtrTest, givenMoveAssignedPtr_thenOldIsExpired) {
2724 IntrusiveAndWeak<SomeClass> obj = make_weak_intrusive<SomeClass>();
2725 IntrusiveAndWeak<SomeClass> obj2 = make_weak_intrusive<SomeClass>();
2726 obj2.weak = std::move(obj.weak);
2727 EXPECT_TRUE(obj.weak.expired());
2730 TEST(WeakIntrusivePtrTest, givenCopyConstructedPtr_thenHasUseCount1) {
2731 IntrusiveAndWeak<SomeClass> obj = make_weak_intrusive<SomeClass>();
2733 EXPECT_EQ(1, obj2.use_count());
2736 TEST(WeakIntrusivePtrTest, givenCopyConstructedPtr_thenIsNotExpired) {
2737 IntrusiveAndWeak<SomeClass> obj = make_weak_intrusive<SomeClass>();
2739 EXPECT_FALSE(obj2.expired());
2742 TEST(WeakIntrusivePtrTest, givenCopyConstructedPtr_thenOldHasUseCount1) {
2743 IntrusiveAndWeak<SomeClass> obj = make_weak_intrusive<SomeClass>();
2745 EXPECT_EQ(1, obj.weak.use_count());
2748 TEST(WeakIntrusivePtrTest, givenCopyConstructedPtr_thenOldIsNotExpired) {
2749 IntrusiveAndWeak<SomeClass> obj = make_weak_intrusive<SomeClass>();
2751 EXPECT_FALSE(obj.weak.expired());
2755 WeakIntrusivePtrTest,
2756 givenPtr_whenLastStrongPointerResets_thenReleasesResources) {
2757 bool resourcesReleased =
false;
2758 bool wasDestructed =
false;
2759 auto obj = make_weak_intrusive<DestructableMock>(&resourcesReleased, &wasDestructed);
2760 EXPECT_FALSE(resourcesReleased);
2761 EXPECT_FALSE(wasDestructed);
2763 EXPECT_TRUE(resourcesReleased);
2764 EXPECT_FALSE(wasDestructed);
2766 EXPECT_TRUE(resourcesReleased);
2767 EXPECT_TRUE(wasDestructed);
2771 WeakIntrusivePtrTest,
2772 givenPtr_whenDestructedButStillHasStrongPointers_thenDoesntReleaseResources) {
2773 bool resourcesReleased =
false;
2774 bool wasDestructed =
false;
2775 auto obj = make_weak_intrusive<DestructableMock>(&resourcesReleased, &wasDestructed);
2776 EXPECT_FALSE(resourcesReleased);
2777 EXPECT_FALSE(wasDestructed);
2779 EXPECT_FALSE(resourcesReleased);
2780 EXPECT_FALSE(wasDestructed);
2782 EXPECT_TRUE(resourcesReleased);
2783 EXPECT_TRUE(wasDestructed);
2786 TEST(WeakIntrusivePtrTest, givenPtr_whenDestructed_thenDestructsObject) {
2787 bool resourcesReleased =
false;
2788 bool wasDestructed =
false;
2790 auto obj = make_weak_only<DestructableMock>(&resourcesReleased, &wasDestructed);
2791 EXPECT_TRUE(resourcesReleased);
2792 EXPECT_FALSE(wasDestructed);
2794 EXPECT_TRUE(resourcesReleased);
2795 EXPECT_TRUE(wasDestructed);
2799 WeakIntrusivePtrTest,
2800 givenPtr_whenMoveConstructed_thenDestructsObjectAfterSecondDestructed) {
2801 bool resourcesReleased =
false;
2802 bool wasDestructed =
false;
2803 auto obj = make_weak_only<DestructableMock>(&resourcesReleased, &wasDestructed);
2805 auto obj2 = std::move(obj);
2806 EXPECT_TRUE(resourcesReleased);
2807 EXPECT_FALSE(wasDestructed);
2809 EXPECT_TRUE(resourcesReleased);
2810 EXPECT_TRUE(wasDestructed);
2814 WeakIntrusivePtrTest,
2815 givenPtr_whenMoveConstructedToBaseClass_thenDestructsObjectAfterSecondDestructed) {
2816 bool resourcesReleased =
false;
2817 bool wasDestructed =
false;
2818 auto obj = make_weak_only<ChildDestructableMock>(&resourcesReleased, &wasDestructed);
2821 EXPECT_TRUE(resourcesReleased);
2822 EXPECT_FALSE(wasDestructed);
2824 EXPECT_TRUE(resourcesReleased);
2825 EXPECT_TRUE(wasDestructed);
2828 TEST(WeakIntrusivePtrTest, givenPtr_whenMoveAssigned_thenDestructsOldObject) {
2830 bool resourcesReleased =
false;
2831 bool wasDestructed =
false;
2832 auto obj = make_weak_only<DestructableMock>(&dummy, &dummy);
2834 auto obj2 = make_weak_only<DestructableMock>(&resourcesReleased, &wasDestructed);
2835 EXPECT_TRUE(resourcesReleased);
2836 EXPECT_FALSE(wasDestructed);
2837 obj2 = std::move(obj);
2838 EXPECT_TRUE(resourcesReleased);
2839 EXPECT_TRUE(wasDestructed);
2844 WeakIntrusivePtrTest,
2845 givenPtr_whenMoveAssignedToBaseClass_thenDestructsOldObject) {
2847 bool resourcesReleased =
false;
2848 bool wasDestructed =
false;
2849 auto obj = make_weak_only<ChildDestructableMock>(&dummy, &dummy);
2851 auto obj2 = make_weak_only<DestructableMock>(&resourcesReleased, &wasDestructed);
2852 EXPECT_TRUE(resourcesReleased);
2853 EXPECT_FALSE(wasDestructed);
2854 obj2 = std::move(obj);
2855 EXPECT_TRUE(resourcesReleased);
2856 EXPECT_TRUE(wasDestructed);
2861 WeakIntrusivePtrTest,
2862 givenPtrWithCopy_whenMoveAssigned_thenDestructsOldObjectAfterCopyIsDestructed) {
2864 bool resourcesReleased =
false;
2865 bool wasDestructed =
false;
2866 auto obj = make_weak_only<DestructableMock>(&dummy, &dummy);
2868 auto obj2 = make_weak_only<DestructableMock>(&resourcesReleased, &wasDestructed);
2871 EXPECT_TRUE(resourcesReleased);
2872 EXPECT_FALSE(wasDestructed);
2873 obj2 = std::move(obj);
2874 EXPECT_TRUE(resourcesReleased);
2875 EXPECT_FALSE(wasDestructed);
2877 EXPECT_TRUE(resourcesReleased);
2878 EXPECT_TRUE(wasDestructed);
2883 WeakIntrusivePtrTest,
2884 givenPtrWithBaseClassCopy_whenMoveAssigned_thenDestructsOldObjectAfterCopyIsDestructed) {
2886 bool resourcesReleased =
false;
2887 bool wasDestructed =
false;
2888 auto obj = make_weak_only<ChildDestructableMock>(&dummy, &dummy);
2891 make_weak_only<ChildDestructableMock>(&resourcesReleased, &wasDestructed);
2894 EXPECT_TRUE(resourcesReleased);
2895 EXPECT_FALSE(wasDestructed);
2896 obj2 = std::move(obj);
2897 EXPECT_TRUE(resourcesReleased);
2898 EXPECT_FALSE(wasDestructed);
2900 EXPECT_TRUE(resourcesReleased);
2901 EXPECT_TRUE(wasDestructed);
2906 WeakIntrusivePtrTest,
2907 givenPtrWithCopy_whenMoveAssignedToBaseClass_thenDestructsOldObjectAfterCopyIsDestructed) {
2909 bool resourcesReleased =
false;
2910 bool wasDestructed =
false;
2911 auto obj = make_weak_only<ChildDestructableMock>(&dummy, &dummy);
2913 auto obj2 = make_weak_only<DestructableMock>(&resourcesReleased, &wasDestructed);
2916 EXPECT_TRUE(resourcesReleased);
2917 EXPECT_FALSE(wasDestructed);
2918 obj2 = std::move(obj);
2919 EXPECT_TRUE(resourcesReleased);
2920 EXPECT_FALSE(wasDestructed);
2922 EXPECT_TRUE(resourcesReleased);
2923 EXPECT_TRUE(wasDestructed);
2928 WeakIntrusivePtrTest,
2929 givenPtr_whenMoveAssigned_thenDestructsObjectAfterSecondDestructed) {
2931 bool resourcesReleased =
false;
2932 bool wasDestructed =
false;
2933 auto obj = make_weak_only<DestructableMock>(&resourcesReleased, &wasDestructed);
2935 auto obj2 = make_weak_only<DestructableMock>(&dummy, &dummy);
2936 obj2 = std::move(obj);
2937 EXPECT_TRUE(resourcesReleased);
2938 EXPECT_FALSE(wasDestructed);
2940 EXPECT_TRUE(resourcesReleased);
2941 EXPECT_TRUE(wasDestructed);
2945 WeakIntrusivePtrTest,
2946 givenPtr_whenMoveAssignedToBaseClass_thenDestructsObjectAfterSecondDestructed) {
2948 bool resourcesReleased =
false;
2949 bool wasDestructed =
false;
2950 auto obj = make_weak_only<ChildDestructableMock>(&resourcesReleased, &wasDestructed);
2952 auto obj2 = make_weak_only<DestructableMock>(&dummy, &dummy);
2953 obj2 = std::move(obj);
2954 EXPECT_TRUE(resourcesReleased);
2955 EXPECT_FALSE(wasDestructed);
2957 EXPECT_TRUE(resourcesReleased);
2958 EXPECT_TRUE(wasDestructed);
2962 WeakIntrusivePtrTest,
2963 givenPtr_whenCopyConstructedAndDestructed_thenDestructsObjectAfterLastDestruction) {
2964 bool resourcesReleased =
false;
2965 bool wasDestructed =
false;
2967 auto obj = make_weak_only<DestructableMock>(&resourcesReleased, &wasDestructed);
2970 EXPECT_TRUE(resourcesReleased);
2971 EXPECT_FALSE(wasDestructed);
2973 EXPECT_TRUE(resourcesReleased);
2974 EXPECT_FALSE(wasDestructed);
2976 EXPECT_TRUE(resourcesReleased);
2977 EXPECT_TRUE(wasDestructed);
2981 WeakIntrusivePtrTest,
2982 givenPtr_whenCopyConstructedToBaseClassAndDestructed_thenDestructsObjectAfterLastDestruction) {
2983 bool resourcesReleased =
false;
2984 bool wasDestructed =
false;
2986 auto obj = make_weak_only<ChildDestructableMock>(&resourcesReleased, &wasDestructed);
2989 EXPECT_TRUE(resourcesReleased);
2990 EXPECT_FALSE(wasDestructed);
2992 EXPECT_TRUE(resourcesReleased);
2993 EXPECT_FALSE(wasDestructed);
2995 EXPECT_TRUE(resourcesReleased);
2996 EXPECT_TRUE(wasDestructed);
3000 WeakIntrusivePtrTest,
3001 givenPtr_whenCopyConstructedAndOriginalDestructed_thenDestructsObjectAfterLastDestruction) {
3002 bool resourcesReleased =
false;
3003 bool wasDestructed =
false;
3005 auto obj = make_weak_only<DestructableMock>(&resourcesReleased, &wasDestructed);
3008 EXPECT_TRUE(resourcesReleased);
3009 EXPECT_FALSE(wasDestructed);
3011 EXPECT_TRUE(resourcesReleased);
3012 EXPECT_TRUE(wasDestructed);
3016 WeakIntrusivePtrTest,
3017 givenPtr_whenCopyConstructedToBaseClassAndOriginalDestructed_thenDestructsObjectAfterLastDestruction) {
3018 bool resourcesReleased =
false;
3019 bool wasDestructed =
false;
3021 auto obj = make_weak_only<ChildDestructableMock>(&resourcesReleased, &wasDestructed);
3024 EXPECT_TRUE(resourcesReleased);
3025 EXPECT_FALSE(wasDestructed);
3027 EXPECT_TRUE(resourcesReleased);
3028 EXPECT_TRUE(wasDestructed);
3032 WeakIntrusivePtrTest,
3033 givenPtr_whenCopyAssignedAndDestructed_thenDestructsObjectAfterLastDestruction) {
3034 bool resourcesReleased =
false;
3035 bool wasDestructed =
false;
3038 auto obj = make_weak_only<DestructableMock>(&resourcesReleased, &wasDestructed);
3041 make_weak_only<DestructableMock>(&dummy, &dummy);
3043 EXPECT_TRUE(resourcesReleased);
3044 EXPECT_FALSE(wasDestructed);
3046 EXPECT_TRUE(resourcesReleased);
3047 EXPECT_FALSE(wasDestructed);
3049 EXPECT_TRUE(resourcesReleased);
3050 EXPECT_TRUE(wasDestructed);
3054 WeakIntrusivePtrTest,
3055 givenPtr_whenCopyAssignedToBaseClassAndDestructed_thenDestructsObjectAfterLastDestruction) {
3056 bool resourcesReleased =
false;
3057 bool wasDestructed =
false;
3060 auto obj = make_weak_only<ChildDestructableMock>(&resourcesReleased, &wasDestructed);
3063 make_weak_only<DestructableMock>(&dummy, &dummy);
3065 EXPECT_TRUE(resourcesReleased);
3066 EXPECT_FALSE(wasDestructed);
3068 EXPECT_TRUE(resourcesReleased);
3069 EXPECT_FALSE(wasDestructed);
3071 EXPECT_TRUE(resourcesReleased);
3072 EXPECT_TRUE(wasDestructed);
3076 WeakIntrusivePtrTest,
3077 givenPtr_whenCopyAssignedAndOriginalDestructed_thenDestructsObjectAfterLastDestruction) {
3078 bool resourcesReleased =
false;
3079 bool wasDestructed =
false;
3082 auto copy = make_weak_only<DestructableMock>(&dummy, &dummy);
3084 auto obj = make_weak_only<DestructableMock>(&resourcesReleased, &wasDestructed);
3086 EXPECT_TRUE(resourcesReleased);
3087 EXPECT_FALSE(wasDestructed);
3089 EXPECT_TRUE(resourcesReleased);
3090 EXPECT_FALSE(wasDestructed);
3092 EXPECT_TRUE(resourcesReleased);
3093 EXPECT_TRUE(wasDestructed);
3097 WeakIntrusivePtrTest,
3098 givenPtr_whenCopyAssignedToBaseClassAndOriginalDestructed_thenDestructsObjectAfterLastDestruction) {
3099 bool wasDestructed =
false;
3100 bool resourcesReleased =
false;
3103 auto copy = make_weak_only<DestructableMock>(&dummy, &dummy);
3106 make_weak_only<ChildDestructableMock>(&resourcesReleased, &wasDestructed);
3108 EXPECT_TRUE(resourcesReleased);
3109 EXPECT_FALSE(wasDestructed);
3111 EXPECT_TRUE(resourcesReleased);
3112 EXPECT_FALSE(wasDestructed);
3114 EXPECT_TRUE(resourcesReleased);
3115 EXPECT_TRUE(wasDestructed);
3118 TEST(WeakIntrusivePtrTest, givenPtr_whenCopyAssigned_thenDestructsOldObject) {
3120 bool resourcesReleased =
false;
3121 bool wasDestructed =
false;
3122 auto obj = make_weak_only<DestructableMock>(&dummy, &dummy);
3124 auto obj2 = make_weak_only<DestructableMock>(&resourcesReleased, &wasDestructed);
3125 EXPECT_TRUE(resourcesReleased);
3126 EXPECT_FALSE(wasDestructed);
3128 EXPECT_TRUE(resourcesReleased);
3129 EXPECT_TRUE(wasDestructed);
3134 WeakIntrusivePtrTest,
3135 givenPtr_whenCopyAssignedToBaseClass_thenDestructsOldObject) {
3137 bool resourcesReleased =
false;
3138 bool wasDestructed =
false;
3139 auto obj = make_weak_only<ChildDestructableMock>(&dummy, &dummy);
3141 auto obj2 = make_weak_only<DestructableMock>(&resourcesReleased, &wasDestructed);
3142 EXPECT_TRUE(resourcesReleased);
3143 EXPECT_FALSE(wasDestructed);
3145 EXPECT_TRUE(resourcesReleased);
3146 EXPECT_TRUE(wasDestructed);
3151 WeakIntrusivePtrTest,
3152 givenPtrWithCopy_whenCopyAssigned_thenDestructsOldObjectAfterCopyIsDestructed) {
3154 bool resourcesReleased =
false;
3155 bool wasDestructed =
false;
3156 auto obj = make_weak_only<DestructableMock>(&dummy, &dummy);
3158 auto obj2 = make_weak_only<DestructableMock>(&resourcesReleased, &wasDestructed);
3161 EXPECT_TRUE(resourcesReleased);
3162 EXPECT_FALSE(wasDestructed);
3164 EXPECT_TRUE(resourcesReleased);
3165 EXPECT_FALSE(wasDestructed);
3167 EXPECT_TRUE(resourcesReleased);
3168 EXPECT_TRUE(wasDestructed);
3173 WeakIntrusivePtrTest,
3174 givenPtrWithBaseClassCopy_whenCopyAssigned_thenDestructsOldObjectAfterCopyIsDestructed) {
3176 bool resourcesReleased =
false;
3177 bool wasDestructed =
false;
3178 auto obj = make_weak_only<ChildDestructableMock>(&dummy, &dummy);
3181 make_weak_only<ChildDestructableMock>(&resourcesReleased, &wasDestructed);
3184 EXPECT_TRUE(resourcesReleased);
3185 EXPECT_FALSE(wasDestructed);
3187 EXPECT_TRUE(resourcesReleased);
3188 EXPECT_FALSE(wasDestructed);
3190 EXPECT_TRUE(resourcesReleased);
3191 EXPECT_TRUE(wasDestructed);
3196 WeakIntrusivePtrTest,
3197 givenPtrWithCopy_whenCopyAssignedToBaseClass_thenDestructsOldObjectAfterCopyIsDestructed) {
3199 bool resourcesReleased =
false;
3200 bool wasDestructed =
false;
3201 auto obj = make_weak_only<ChildDestructableMock>(&dummy, &dummy);
3203 auto obj2 = make_weak_only<DestructableMock>(&resourcesReleased, &wasDestructed);
3206 EXPECT_TRUE(resourcesReleased);
3207 EXPECT_FALSE(wasDestructed);
3209 EXPECT_TRUE(resourcesReleased);
3210 EXPECT_FALSE(wasDestructed);
3212 EXPECT_TRUE(resourcesReleased);
3213 EXPECT_TRUE(wasDestructed);
3217 TEST(WeakIntrusivePtrTest, givenPtr_whenCallingReset_thenDestructs) {
3218 bool resourcesReleased =
false;
3219 bool wasDestructed =
false;
3220 auto obj = make_weak_only<DestructableMock>(&resourcesReleased, &wasDestructed);
3221 EXPECT_TRUE(resourcesReleased);
3222 EXPECT_FALSE(wasDestructed);
3224 EXPECT_TRUE(resourcesReleased);
3225 EXPECT_TRUE(wasDestructed);
3229 WeakIntrusivePtrTest,
3230 givenPtrWithCopy_whenCallingReset_thenDestructsAfterCopyDestructed) {
3231 bool resourcesReleased =
false;
3232 bool wasDestructed =
false;
3233 auto obj = make_weak_only<DestructableMock>(&resourcesReleased, &wasDestructed);
3237 EXPECT_TRUE(resourcesReleased);
3238 EXPECT_FALSE(wasDestructed);
3240 EXPECT_TRUE(resourcesReleased);
3241 EXPECT_TRUE(wasDestructed);
3246 WeakIntrusivePtrTest,
3247 givenPtrWithCopy_whenCallingResetOnCopy_thenDestructsAfterOriginalDestructed) {
3248 bool resourcesReleased =
false;
3249 bool wasDestructed =
false;
3250 auto obj = make_weak_only<DestructableMock>(&resourcesReleased, &wasDestructed);
3254 EXPECT_TRUE(resourcesReleased);
3255 EXPECT_FALSE(wasDestructed);
3257 EXPECT_TRUE(resourcesReleased);
3258 EXPECT_TRUE(wasDestructed);
3263 WeakIntrusivePtrTest,
3264 givenPtrWithMoved_whenCallingReset_thenDestructsAfterMovedDestructed) {
3265 bool resourcesReleased =
false;
3266 bool wasDestructed =
false;
3267 auto obj = make_weak_only<DestructableMock>(&resourcesReleased, &wasDestructed);
3269 auto moved = std::move(obj);
3271 EXPECT_TRUE(resourcesReleased);
3272 EXPECT_FALSE(wasDestructed);
3274 EXPECT_TRUE(resourcesReleased);
3275 EXPECT_TRUE(wasDestructed);
3280 WeakIntrusivePtrTest,
3281 givenPtrWithMoved_whenCallingResetOnMoved_thenDestructsImmediately) {
3282 bool resourcesReleased =
false;
3283 bool wasDestructed =
false;
3284 auto obj = make_weak_only<DestructableMock>(&resourcesReleased, &wasDestructed);
3286 auto moved = std::move(obj);
3288 EXPECT_TRUE(resourcesReleased);
3289 EXPECT_TRUE(wasDestructed);
3293 TEST(WeakIntrusivePtrTest, givenPtr_whenReleasedAndReclaimed_thenDoesntCrash) {
3294 IntrusiveAndWeak<SomeClass> obj = make_weak_intrusive<SomeClass>();
3295 SomeClass* ptr = obj.weak.release();
3301 WeakIntrusivePtrTest,
3302 givenWeakOnlyPtr_whenReleasedAndReclaimed_thenDoesntCrash) {
3304 SomeClass* ptr = obj.
release();
3310 WeakIntrusivePtrTest,
3311 givenPtr_whenReleasedAndReclaimed_thenIsDestructedAtEnd) {
3312 bool resourcesReleased =
false;
3313 bool wasDestructed =
false;
3316 IntrusiveAndWeak<DestructableMock> outer =
3317 make_weak_intrusive<DestructableMock>(&dummy, &dummy);
3319 IntrusiveAndWeak<DestructableMock> inner =
3320 make_weak_intrusive<DestructableMock>(
3321 &resourcesReleased, &wasDestructed);
3322 EXPECT_FALSE(resourcesReleased);
3323 EXPECT_FALSE(wasDestructed);
3324 DestructableMock* ptr = inner.weak.release();
3325 EXPECT_FALSE(resourcesReleased);
3326 EXPECT_FALSE(wasDestructed);
3327 outer.ptr = inner.ptr;
3331 EXPECT_FALSE(resourcesReleased);
3332 EXPECT_FALSE(wasDestructed);
3334 EXPECT_FALSE(resourcesReleased);
3335 EXPECT_FALSE(wasDestructed);
3338 EXPECT_TRUE(resourcesReleased);
3339 EXPECT_TRUE(wasDestructed);
3343 WeakIntrusivePtrTest,
3344 givenWeakOnlyPtr_whenReleasedAndReclaimed_thenIsDestructedAtEnd) {
3345 bool resourcesReleased =
false;
3346 bool wasDestructed =
false;
3349 make_invalid_weak<DestructableMock>();
3352 make_weak_only<DestructableMock>(&resourcesReleased, &wasDestructed);
3353 EXPECT_TRUE(resourcesReleased);
3354 EXPECT_FALSE(wasDestructed);
3355 DestructableMock* ptr = inner.
release();
3356 EXPECT_TRUE(resourcesReleased);
3357 EXPECT_FALSE(wasDestructed);
3361 EXPECT_TRUE(resourcesReleased);
3362 EXPECT_FALSE(wasDestructed);
3365 EXPECT_TRUE(resourcesReleased);
3366 EXPECT_TRUE(wasDestructed);
3369 TEST(WeakIntrusivePtrTest, givenStackObject_whenReclaimed_thenCrashes) {
intrusive_ptr<T> is an alternative to shared_ptr<T> that has better performance because it does the r...
TTarget * release() noexcept
Returns an owning (!) pointer to the underlying object and makes the intrusive_ptr instance invalid...
TTarget * release() noexcept
Returns an owning (but still only weakly referenced) pointer to the underlying object and makes the w...