31 MaybeOwned<PseudoDeletable> ptr1;
32 EXPECT_FALSE(ptr1.IsOwned());
34 EXPECT_FALSE(ptr1.Get());
36 MaybeOwned<PseudoDeletable> ptr2;
37 EXPECT_TRUE(ptr1 == ptr2);
38 EXPECT_FALSE(ptr1 != ptr2);
43 PseudoDeletable thing1(100, &delete_count);
45 MaybeOwned<PseudoDeletable> ptr(&thing1);
46 EXPECT_FALSE(ptr.IsOwned());
47 EXPECT_EQ(ptr.Get(), &thing1);
48 EXPECT_EQ(100, ptr->GetID());
49 EXPECT_TRUE(ptr == &thing1);
50 EXPECT_FALSE(ptr != &thing1);
52 MaybeOwned<PseudoDeletable> empty;
53 EXPECT_FALSE(ptr == empty);
54 EXPECT_TRUE(ptr != empty);
56 EXPECT_EQ(0, delete_count);
59 PseudoDeletable thing2(200, &delete_count);
61 MaybeOwned<PseudoDeletable> ptr(&thing1);
63 EXPECT_FALSE(ptr.IsOwned());
64 EXPECT_EQ(ptr.Get(), &thing2);
65 EXPECT_EQ(200, ptr->GetID());
67 EXPECT_EQ(0, delete_count);
70 int owned_delete_count = 0;
72 MaybeOwned<PseudoDeletable> ptr(&thing1);
73 EXPECT_EQ(100, ptr->GetID());
74 ptr =
std::make_unique<PseudoDeletable>(300, &owned_delete_count);
75 EXPECT_TRUE(ptr.IsOwned());
76 EXPECT_EQ(300, ptr->GetID());
78 EXPECT_EQ(0, delete_count);
79 EXPECT_EQ(1, owned_delete_count);
82TEST(MaybeOwned, UnownedPtr) {
84 PseudoDeletable thing1(100, &delete_count);
85 PseudoDeletable thing2(200, &delete_count);
89 MaybeOwned<PseudoDeletable> ptr1(unowned1);
90 MaybeOwned<PseudoDeletable> ptr2(unowned2);
94 EXPECT_EQ(0, delete_count);
100 MaybeOwned<PseudoDeletable> ptr(
101 std::make_unique<PseudoDeletable>(100, &delete_count));
102 EXPECT_TRUE(ptr.IsOwned());
103 EXPECT_EQ(100, ptr->GetID());
105 MaybeOwned<PseudoDeletable> empty;
106 EXPECT_FALSE(ptr == empty);
107 EXPECT_TRUE(ptr != empty);
109 EXPECT_EQ(1, delete_count);
113 MaybeOwned<PseudoDeletable> ptr(
114 std::make_unique<PseudoDeletable>(200, &delete_count));
115 ptr =
std::make_unique<PseudoDeletable>(300, &delete_count);
116 EXPECT_TRUE(ptr.IsOwned());
117 EXPECT_EQ(300, ptr->GetID());
118 EXPECT_EQ(1, delete_count);
120 EXPECT_EQ(2, delete_count);
123 int unowned_delete_count = 0;
124 PseudoDeletable thing2(400, &unowned_delete_count);
126 MaybeOwned<PseudoDeletable> ptr(
127 std::make_unique<PseudoDeletable>(500, &delete_count));
129 EXPECT_FALSE(ptr.IsOwned());
130 EXPECT_EQ(400, ptr->GetID());
131 EXPECT_EQ(1, delete_count);
132 EXPECT_EQ(0, unowned_delete_count);
134 EXPECT_EQ(1, delete_count);
135 EXPECT_EQ(0, unowned_delete_count);
139 int delete_count = 0;
141 std::unique_ptr<PseudoDeletable> stolen;
143 MaybeOwned<PseudoDeletable> ptr(
144 std::make_unique<PseudoDeletable>(100, &delete_count));
145 EXPECT_TRUE(ptr.IsOwned());
146 stolen = ptr.Release();
147 EXPECT_FALSE(ptr.IsOwned());
148 EXPECT_EQ(ptr, stolen);
149 EXPECT_EQ(0, delete_count);
151 EXPECT_EQ(0, delete_count);
153 EXPECT_EQ(1, delete_count);
157 int delete_count = 0;
158 PseudoDeletable thing1(100, &delete_count);
160 MaybeOwned<PseudoDeletable> ptr1(&thing1);
161 MaybeOwned<PseudoDeletable> ptr2(
162 std::make_unique<PseudoDeletable>(200, &delete_count));
163 EXPECT_FALSE(ptr1.IsOwned());
164 EXPECT_TRUE(ptr2.IsOwned());
166 MaybeOwned<PseudoDeletable> ptr3(std::move(ptr1));
167 MaybeOwned<PseudoDeletable> ptr4(std::move(ptr2));
168 EXPECT_FALSE(ptr1.IsOwned());
169 EXPECT_FALSE(ptr1.Get());
170 EXPECT_TRUE(ptr2.IsOwned());
171 EXPECT_FALSE(ptr2.Get());
172 EXPECT_FALSE(ptr3.IsOwned());
173 EXPECT_TRUE(ptr4.IsOwned());
174 EXPECT_EQ(0, delete_count);
175 EXPECT_EQ(100, ptr3->GetID());
176 EXPECT_EQ(200, ptr4->GetID());
178 MaybeOwned<PseudoDeletable> ptr5;
179 MaybeOwned<PseudoDeletable> ptr6;
180 ptr5 =
std::move(ptr3);
181 ptr6 =
std::move(ptr4);
182 EXPECT_FALSE(ptr3.IsOwned());
183 EXPECT_FALSE(ptr3.Get());
184 EXPECT_TRUE(ptr4.IsOwned());
185 EXPECT_FALSE(ptr4.Get());
186 EXPECT_FALSE(ptr5.IsOwned());
187 EXPECT_TRUE(ptr6.IsOwned());
188 EXPECT_EQ(0, delete_count);
189 EXPECT_EQ(100, ptr5->GetID());
190 EXPECT_EQ(200, ptr6->GetID());
192 EXPECT_EQ(1, delete_count);