Debian Codegraph @

test/core/gprpp/ref_counted_ptr_test.cc

raw
1 /*
2 *
3 * Copyright 2017 gRPC authors.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18
19 #include "src/core/lib/gprpp/ref_counted_ptr.h"
20
21 #include "gtest/gtest.h"
22
23 #include <grpc/support/log.h>
24
25 #include "src/core/lib/gprpp/dual_ref_counted.h"
26 #include "src/core/lib/gprpp/ref_counted.h"
27 #include "test/core/util/test_config.h"
28
29 namespace grpc_core {
30 namespace testing {
31 namespace {
32
33 //
34 // RefCountedPtr<> tests
35 //
36
37 class Foo : public RefCounted<Foo> {
38 public:
39 Foo() : value_(0) {}
40
41 explicit Foo(int value) : value_(value) {}
42
43 int value() const { return value_; }
44
45 private:
46 int value_;
47 };
48
49 TEST(RefCountedPtr, DefaultConstructor) { RefCountedPtr<Foo> foo; }
50
51 TEST(RefCountedPtr, ExplicitConstructorEmpty) {
52 RefCountedPtr<Foo> foo(nullptr);
53 }
54
55 TEST(RefCountedPtr, ExplicitConstructor) { RefCountedPtr<Foo> foo(new Foo()); }
56
57 TEST(RefCountedPtr, MoveConstructor) {
58 RefCountedPtr<Foo> foo(new Foo());
59 RefCountedPtr<Foo> foo2(std::move(foo));
60 // NOLINTNEXTLINE(bugprone-use-after-move)
61 EXPECT_EQ(nullptr, foo.get());
62 EXPECT_NE(nullptr, foo2.get());
63 }
64
65 TEST(RefCountedPtr, MoveAssignment) {
66 RefCountedPtr<Foo> foo(new Foo());
67 RefCountedPtr<Foo> foo2 = std::move(foo);
68 // NOLINTNEXTLINE(bugprone-use-after-move)
69 EXPECT_EQ(nullptr, foo.get());
70 EXPECT_NE(nullptr, foo2.get());
71 }
72
73 TEST(RefCountedPtr, CopyConstructor) {
74 RefCountedPtr<Foo> foo(new Foo());
75 RefCountedPtr<Foo> foo2(foo);
76 EXPECT_NE(nullptr, foo.get());
77 EXPECT_EQ(foo.get(), foo2.get());
78 }
79
80 TEST(RefCountedPtr, CopyAssignment) {
81 RefCountedPtr<Foo> foo(new Foo());
82 RefCountedPtr<Foo> foo2 = foo;
83 EXPECT_NE(nullptr, foo.get());
84 EXPECT_EQ(foo.get(), foo2.get());
85 }
86
87 TEST(RefCountedPtr, CopyAssignmentWhenEmpty) {
88 RefCountedPtr<Foo> foo;
89 RefCountedPtr<Foo> foo2;
90 foo2 = foo;
91 EXPECT_EQ(nullptr, foo.get());
92 EXPECT_EQ(nullptr, foo2.get());
93 }
94
95 TEST(RefCountedPtr, CopyAssignmentToSelf) {
96 RefCountedPtr<Foo> foo(new Foo());
97 foo = *&foo; // The "*&" avoids warnings from LLVM -Wself-assign.
98 }
99
100 TEST(RefCountedPtr, EnclosedScope) {
101 RefCountedPtr<Foo> foo(new Foo());
102 {
103 RefCountedPtr<Foo> foo2(std::move(foo));
104 // NOLINTNEXTLINE(bugprone-use-after-move)
105 EXPECT_EQ(nullptr, foo.get());
106 EXPECT_NE(nullptr, foo2.get());
107 }
108 EXPECT_EQ(nullptr, foo.get());
109 }
110
111 TEST(RefCountedPtr, ResetFromNullToNonNull) {
112 RefCountedPtr<Foo> foo;
113 EXPECT_EQ(nullptr, foo.get());
114 foo.reset(new Foo());
115 EXPECT_NE(nullptr, foo.get());
116 }
117
118 TEST(RefCountedPtr, ResetFromNonNullToNonNull) {
119 RefCountedPtr<Foo> foo(new Foo());
120 EXPECT_NE(nullptr, foo.get());
121 Foo* original = foo.get();
122 foo.reset(new Foo());
123 EXPECT_NE(nullptr, foo.get());
124 EXPECT_NE(original, foo.get());
125 }
126
127 TEST(RefCountedPtr, ResetFromNonNullToNull) {
128 RefCountedPtr<Foo> foo(new Foo());
129 EXPECT_NE(nullptr, foo.get());
130 foo.reset();
131 EXPECT_EQ(nullptr, foo.get());
132 }
133
134 TEST(RefCountedPtr, ResetFromNullToNull) {
135 RefCountedPtr<Foo> foo;
136 EXPECT_EQ(nullptr, foo.get());
137 foo.reset();
138 EXPECT_EQ(nullptr, foo.get());
139 }
140
141 TEST(RefCountedPtr, DerefernceOperators) {
142 RefCountedPtr<Foo> foo(new Foo());
143 foo->value();
144 Foo& foo_ref = *foo;
145 foo_ref.value();
146 }
147
148 TEST(RefCountedPtr, EqualityOperators) {
149 RefCountedPtr<Foo> foo(new Foo());
150 RefCountedPtr<Foo> bar = foo;
151 RefCountedPtr<Foo> empty;
152 // Test equality between RefCountedPtrs.
153 EXPECT_EQ(foo, bar);
154 EXPECT_NE(foo, empty);
155 // Test equality with bare pointers.
156 EXPECT_EQ(foo, foo.get());
157 EXPECT_EQ(empty, nullptr);
158 EXPECT_NE(foo, nullptr);
159 }
160
161 TEST(RefCountedPtr, Swap) {
162 Foo* foo = new Foo();
163 Foo* bar = new Foo();
164 RefCountedPtr<Foo> ptr1(foo);
165 RefCountedPtr<Foo> ptr2(bar);
166 ptr1.swap(ptr2);
167 EXPECT_EQ(foo, ptr2.get());
168 EXPECT_EQ(bar, ptr1.get());
169 RefCountedPtr<Foo> ptr3;
170 ptr3.swap(ptr2);
171 EXPECT_EQ(nullptr, ptr2.get());
172 EXPECT_EQ(foo, ptr3.get());
173 }
174
175 TEST(MakeRefCounted, NoArgs) {
176 RefCountedPtr<Foo> foo = MakeRefCounted<Foo>();
177 EXPECT_EQ(0, foo->value());
178 }
179
180 TEST(MakeRefCounted, Args) {
181 RefCountedPtr<Foo> foo = MakeRefCounted<Foo>(3);
182 EXPECT_EQ(3, foo->value());
183 }
184
185 class FooWithTracing : public RefCounted<FooWithTracing> {
186 public:
187 FooWithTracing() : RefCounted("FooWithTracing") {}
188 };
189
190 TEST(RefCountedPtr, RefCountedWithTracing) {
191 RefCountedPtr<FooWithTracing> foo(new FooWithTracing());
192 RefCountedPtr<FooWithTracing> foo2 = foo->Ref(DEBUG_LOCATION, "foo");
193 foo2.release();
194 foo->Unref(DEBUG_LOCATION, "foo");
195 }
196
197 class BaseClass : public RefCounted<BaseClass> {
198 public:
199 BaseClass() {}
200 };
201
202 class Subclass : public BaseClass {
203 public:
204 Subclass() {}
205 };
206
207 TEST(RefCountedPtr, ConstructFromSubclass) {
208 RefCountedPtr<BaseClass> p(new Subclass());
209 }
210
211 TEST(RefCountedPtr, CopyAssignFromSubclass) {
212 RefCountedPtr<BaseClass> b;
213 EXPECT_EQ(nullptr, b.get());
214 RefCountedPtr<Subclass> s = MakeRefCounted<Subclass>();
215 b = s;
216 EXPECT_NE(nullptr, b.get());
217 }
218
219 TEST(RefCountedPtr, MoveAssignFromSubclass) {
220 RefCountedPtr<BaseClass> b;
221 EXPECT_EQ(nullptr, b.get());
222 RefCountedPtr<Subclass> s = MakeRefCounted<Subclass>();
223 b = std::move(s);
224 EXPECT_NE(nullptr, b.get());
225 }
226
227 TEST(RefCountedPtr, ResetFromSubclass) {
228 RefCountedPtr<BaseClass> b;
229 EXPECT_EQ(nullptr, b.get());
230 b.reset(new Subclass());
231 EXPECT_NE(nullptr, b.get());
232 }
233
234 TEST(RefCountedPtr, EqualityWithSubclass) {
235 Subclass* s = new Subclass();
236 RefCountedPtr<BaseClass> b(s);
237 EXPECT_EQ(b, s);
238 }
239
240 void FunctionTakingBaseClass(RefCountedPtr<BaseClass> p) {
241 p.reset(); // To appease clang-tidy.
242 }
243
244 TEST(RefCountedPtr, CanPassSubclassToFunctionExpectingBaseClass) {
245 RefCountedPtr<Subclass> p = MakeRefCounted<Subclass>();
246 FunctionTakingBaseClass(p);
247 }
248
249 void FunctionTakingSubclass(RefCountedPtr<Subclass> p) {
250 p.reset(); // To appease clang-tidy.
251 }
252
253 TEST(RefCountedPtr, CanPassSubclassToFunctionExpectingSubclass) {
254 RefCountedPtr<Subclass> p = MakeRefCounted<Subclass>();
255 FunctionTakingSubclass(p);
256 }
257
258 //
259 // WeakRefCountedPtr<> tests
260 //
261
262 class Bar : public DualRefCounted<Bar> {
263 public:
264 Bar() : value_(0) {}
265
266 explicit Bar(int value) : value_(value) {}
267
268 ~Bar() override { GPR_ASSERT(shutting_down_); }
269
270 void Orphan() override { shutting_down_ = true; }
271
272 int value() const { return value_; }
273
274 private:
275 int value_;
276 bool shutting_down_ = false;
277 };
278
279 TEST(WeakRefCountedPtr, DefaultConstructor) { WeakRefCountedPtr<Bar> bar; }
280
281 TEST(WeakRefCountedPtr, ExplicitConstructorEmpty) {
282 WeakRefCountedPtr<Bar> bar(nullptr);
283 }
284
285 TEST(WeakRefCountedPtr, ExplicitConstructor) {
286 RefCountedPtr<Bar> bar_strong(new Bar());
287 bar_strong->WeakRef().release();
288 WeakRefCountedPtr<Bar> bar(bar_strong.get());
289 }
290
291 TEST(WeakRefCountedPtr, MoveConstructor) {
292 RefCountedPtr<Bar> bar_strong(new Bar());
293 WeakRefCountedPtr<Bar> bar = bar_strong->WeakRef();
294 WeakRefCountedPtr<Bar> bar2(std::move(bar));
295 EXPECT_EQ(nullptr, bar.get()); // NOLINT
296 EXPECT_NE(nullptr, bar2.get());
297 }
298
299 TEST(WeakRefCountedPtr, MoveAssignment) {
300 RefCountedPtr<Bar> bar_strong(new Bar());
301 WeakRefCountedPtr<Bar> bar = bar_strong->WeakRef();
302 WeakRefCountedPtr<Bar> bar2 = std::move(bar);
303 EXPECT_EQ(nullptr, bar.get()); // NOLINT
304 EXPECT_NE(nullptr, bar2.get());
305 }
306
307 TEST(WeakRefCountedPtr, CopyConstructor) {
308 RefCountedPtr<Bar> bar_strong(new Bar());
309 WeakRefCountedPtr<Bar> bar = bar_strong->WeakRef();
310 WeakRefCountedPtr<Bar> bar2(bar);
311 EXPECT_NE(nullptr, bar.get());
312 EXPECT_EQ(bar.get(), bar2.get());
313 }
314
315 TEST(WeakRefCountedPtr, CopyAssignment) {
316 RefCountedPtr<Bar> bar_strong(new Bar());
317 WeakRefCountedPtr<Bar> bar = bar_strong->WeakRef();
318 WeakRefCountedPtr<Bar> bar2 = bar;
319 EXPECT_NE(nullptr, bar.get());
320 EXPECT_EQ(bar.get(), bar2.get());
321 }
322
323 TEST(WeakRefCountedPtr, CopyAssignmentWhenEmpty) {
324 WeakRefCountedPtr<Bar> bar;
325 WeakRefCountedPtr<Bar> bar2;
326 bar2 = bar;
327 EXPECT_EQ(nullptr, bar.get());
328 EXPECT_EQ(nullptr, bar2.get());
329 }
330
331 TEST(WeakRefCountedPtr, CopyAssignmentToSelf) {
332 RefCountedPtr<Bar> bar_strong(new Bar());
333 WeakRefCountedPtr<Bar> bar = bar_strong->WeakRef();
334 bar = *&bar; // The "*&" avoids warnings from LLVM -Wself-assign.
335 }
336
337 TEST(WeakRefCountedPtr, EnclosedScope) {
338 RefCountedPtr<Bar> bar_strong(new Bar());
339 WeakRefCountedPtr<Bar> bar = bar_strong->WeakRef();
340 {
341 WeakRefCountedPtr<Bar> bar2(std::move(bar));
342 // NOLINTNEXTLINE(bugprone-use-after-move)
343 EXPECT_EQ(nullptr, bar.get());
344 EXPECT_NE(nullptr, bar2.get());
345 }
346 EXPECT_EQ(nullptr, bar.get());
347 }
348
349 TEST(WeakRefCountedPtr, ResetFromNullToNonNull) {
350 RefCountedPtr<Bar> bar_strong(new Bar());
351 WeakRefCountedPtr<Bar> bar;
352 EXPECT_EQ(nullptr, bar.get());
353 bar_strong->WeakRef().release();
354 bar.reset(bar_strong.get());
355 EXPECT_NE(nullptr, bar.get());
356 }
357
358 TEST(WeakRefCountedPtr, ResetFromNonNullToNonNull) {
359 RefCountedPtr<Bar> bar_strong(new Bar());
360 RefCountedPtr<Bar> bar2_strong(new Bar());
361 WeakRefCountedPtr<Bar> bar = bar_strong->WeakRef();
362 EXPECT_NE(nullptr, bar.get());
363 bar2_strong->WeakRef().release();
364 bar.reset(bar2_strong.get());
365 EXPECT_NE(nullptr, bar.get());
366 EXPECT_NE(bar_strong.get(), bar.get());
367 }
368
369 TEST(WeakRefCountedPtr, ResetFromNonNullToNull) {
370 RefCountedPtr<Bar> bar_strong(new Bar());
371 WeakRefCountedPtr<Bar> bar = bar_strong->WeakRef();
372 EXPECT_NE(nullptr, bar.get());
373 bar.reset();
374 EXPECT_EQ(nullptr, bar.get());
375 }
376
377 TEST(WeakRefCountedPtr, ResetFromNullToNull) {
378 WeakRefCountedPtr<Bar> bar;
379 EXPECT_EQ(nullptr, bar.get());
380 bar.reset();
381 EXPECT_EQ(nullptr, bar.get());
382 }
383
384 TEST(WeakRefCountedPtr, DerefernceOperators) {
385 RefCountedPtr<Bar> bar_strong(new Bar());
386 WeakRefCountedPtr<Bar> bar = bar_strong->WeakRef();
387 bar->value();
388 Bar& bar_ref = *bar;
389 bar_ref.value();
390 }
391
392 TEST(WeakRefCountedPtr, EqualityOperators) {
393 RefCountedPtr<Bar> bar_strong(new Bar());
394 WeakRefCountedPtr<Bar> bar = bar_strong->WeakRef();
395 WeakRefCountedPtr<Bar> bar2 = bar;
396 WeakRefCountedPtr<Bar> empty;
397 // Test equality between RefCountedPtrs.
398 EXPECT_EQ(bar, bar2);
399 EXPECT_NE(bar, empty);
400 // Test equality with bare pointers.
401 EXPECT_EQ(bar, bar.get());
402 EXPECT_EQ(empty, nullptr);
403 EXPECT_NE(bar, nullptr);
404 }
405
406 TEST(WeakRefCountedPtr, Swap) {
407 RefCountedPtr<Bar> bar_strong(new Bar());
408 RefCountedPtr<Bar> bar2_strong(new Bar());
409 WeakRefCountedPtr<Bar> bar = bar_strong->WeakRef();
410 WeakRefCountedPtr<Bar> bar2 = bar2_strong->WeakRef();
411 bar.swap(bar2);
412 EXPECT_EQ(bar_strong.get(), bar2.get());
413 EXPECT_EQ(bar2_strong.get(), bar.get());
414 WeakRefCountedPtr<Bar> bar3;
415 bar3.swap(bar2);
416 EXPECT_EQ(nullptr, bar2.get());
417 EXPECT_EQ(bar_strong.get(), bar3.get());
418 }
419
420 class BarWithTracing : public DualRefCounted<BarWithTracing> {
421 public:
422 BarWithTracing() : DualRefCounted("BarWithTracing") {}
423
424 ~BarWithTracing() override { GPR_ASSERT(shutting_down_); }
425
426 void Orphan() override { shutting_down_ = true; }
427
428 private:
429 bool shutting_down_ = false;
430 };
431
432 TEST(WeakRefCountedPtr, RefCountedWithTracing) {
433 RefCountedPtr<BarWithTracing> bar_strong(new BarWithTracing());
434 WeakRefCountedPtr<BarWithTracing> bar = bar_strong->WeakRef();
435 WeakRefCountedPtr<BarWithTracing> bar2 = bar->WeakRef(DEBUG_LOCATION, "bar");
436 bar2.release();
437 bar->WeakUnref(DEBUG_LOCATION, "bar");
438 }
439
440 class WeakBaseClass : public DualRefCounted<WeakBaseClass> {
441 public:
442 WeakBaseClass() {}
443
444 ~WeakBaseClass() override { GPR_ASSERT(shutting_down_); }
445
446 void Orphan() override { shutting_down_ = true; }
447
448 private:
449 bool shutting_down_ = false;
450 };
451
452 class WeakSubclass : public WeakBaseClass {
453 public:
454 WeakSubclass() {}
455 };
456
457 TEST(WeakRefCountedPtr, ConstructFromWeakSubclass) {
458 RefCountedPtr<WeakSubclass> strong(new WeakSubclass());
459 WeakRefCountedPtr<WeakBaseClass> p(strong->WeakRef().release());
460 }
461
462 TEST(WeakRefCountedPtr, CopyAssignFromWeakSubclass) {
463 RefCountedPtr<WeakSubclass> strong(new WeakSubclass());
464 WeakRefCountedPtr<WeakBaseClass> b;
465 EXPECT_EQ(nullptr, b.get());
466 WeakRefCountedPtr<WeakSubclass> s = strong->WeakRef();
467 b = s;
468 EXPECT_NE(nullptr, b.get());
469 }
470
471 TEST(WeakRefCountedPtr, MoveAssignFromWeakSubclass) {
472 RefCountedPtr<WeakSubclass> strong(new WeakSubclass());
473 WeakRefCountedPtr<WeakBaseClass> b;
474 EXPECT_EQ(nullptr, b.get());
475 WeakRefCountedPtr<WeakSubclass> s = strong->WeakRef();
476 b = std::move(s);
477 EXPECT_NE(nullptr, b.get());
478 }
479
480 TEST(WeakRefCountedPtr, ResetFromWeakSubclass) {
481 RefCountedPtr<WeakSubclass> strong(new WeakSubclass());
482 WeakRefCountedPtr<WeakBaseClass> b;
483 EXPECT_EQ(nullptr, b.get());
484 b.reset(strong->WeakRef().release());
485 EXPECT_NE(nullptr, b.get());
486 }
487
488 TEST(WeakRefCountedPtr, EqualityWithWeakSubclass) {
489 RefCountedPtr<WeakSubclass> strong(new WeakSubclass());
490 WeakRefCountedPtr<WeakBaseClass> b = strong->WeakRef();
491 EXPECT_EQ(b, strong.get());
492 }
493
494 void FunctionTakingWeakBaseClass(WeakRefCountedPtr<WeakBaseClass> p) {
495 p.reset(); // To appease clang-tidy.
496 }
497
498 TEST(WeakRefCountedPtr, CanPassWeakSubclassToFunctionExpectingWeakBaseClass) {
499 RefCountedPtr<WeakSubclass> strong(new WeakSubclass());
500 WeakRefCountedPtr<WeakSubclass> p = strong->WeakRef();
501 FunctionTakingWeakBaseClass(p);
502 }
503
504 void FunctionTakingWeakSubclass(WeakRefCountedPtr<WeakSubclass> p) {
505 p.reset(); // To appease clang-tidy.
506 }
507
508 TEST(WeakRefCountedPtr, CanPassWeakSubclassToFunctionExpectingWeakSubclass) {
509 RefCountedPtr<WeakSubclass> strong(new WeakSubclass());
510 WeakRefCountedPtr<WeakSubclass> p = strong->WeakRef();
511 FunctionTakingWeakSubclass(p);
512 }
513
514 } // namespace
515 } // namespace testing
516 } // namespace grpc_core
517
518 int main(int argc, char** argv) {
519 grpc::testing::TestEnvironment env(&argc, argv);
520 ::testing::InitGoogleTest(&argc, argv);
521 return RUN_ALL_TESTS();
522 }