21 #include <gtest/gtest.h>
38 class Foo :
public RefCounted<Foo> {
50 TEST(RefCountedPtr, DefaultConstructor) { RefCountedPtr<Foo>
foo; }
52 TEST(RefCountedPtr, ExplicitConstructorEmpty) {
53 RefCountedPtr<Foo>
foo(
nullptr);
56 TEST(RefCountedPtr, ExplicitConstructor) { RefCountedPtr<Foo>
foo(
new Foo()); }
58 TEST(RefCountedPtr, MoveConstructor) {
59 RefCountedPtr<Foo>
foo(
new Foo());
66 TEST(RefCountedPtr, MoveAssignment) {
67 RefCountedPtr<Foo>
foo(
new Foo());
74 TEST(RefCountedPtr, CopyConstructor) {
75 RefCountedPtr<Foo>
foo(
new Foo());
76 RefCountedPtr<Foo> foo2(
foo);
81 TEST(RefCountedPtr, CopyAssignment) {
82 RefCountedPtr<Foo>
foo(
new Foo());
83 RefCountedPtr<Foo> foo2 =
foo;
88 TEST(RefCountedPtr, CopyAssignmentWhenEmpty) {
89 RefCountedPtr<Foo>
foo;
90 RefCountedPtr<Foo> foo2;
96 TEST(RefCountedPtr, CopyAssignmentToSelf) {
97 RefCountedPtr<Foo>
foo(
new Foo());
101 TEST(RefCountedPtr, EnclosedScope) {
102 RefCountedPtr<Foo>
foo(
new Foo());
112 TEST(RefCountedPtr, ResetFromNullToNonNull) {
113 RefCountedPtr<Foo>
foo;
119 TEST(RefCountedPtr, ResetFromNonNullToNonNull) {
120 RefCountedPtr<Foo>
foo(
new Foo());
122 Foo* original =
foo.get();
128 TEST(RefCountedPtr, ResetFromNonNullToNull) {
129 RefCountedPtr<Foo>
foo(
new Foo());
135 TEST(RefCountedPtr, ResetFromNullToNull) {
136 RefCountedPtr<Foo>
foo;
142 TEST(RefCountedPtr, DerefernceOperators) {
143 RefCountedPtr<Foo>
foo(
new Foo());
149 TEST(RefCountedPtr, EqualityOperators) {
150 RefCountedPtr<Foo>
foo(
new Foo());
151 RefCountedPtr<Foo>
bar =
foo;
152 RefCountedPtr<Foo>
empty;
165 RefCountedPtr<Foo> ptr1(
foo);
166 RefCountedPtr<Foo> ptr2(
bar);
170 RefCountedPtr<Foo> ptr3;
177 RefCountedPtr<Foo>
foo = MakeRefCounted<Foo>();
182 RefCountedPtr<Foo>
foo = MakeRefCounted<Foo>(3);
186 class FooWithTracing :
public RefCounted<FooWithTracing> {
188 FooWithTracing() : RefCounted(
"FooWithTracing") {}
191 TEST(RefCountedPtr, RefCountedWithTracing) {
192 RefCountedPtr<FooWithTracing>
foo(
new FooWithTracing());
198 class BaseClass :
public RefCounted<BaseClass> {
203 class Subclass :
public BaseClass {
208 TEST(RefCountedPtr, ConstructFromSubclass) {
209 RefCountedPtr<BaseClass>
p(
new Subclass());
212 TEST(RefCountedPtr, CopyAssignFromSubclass) {
213 RefCountedPtr<BaseClass>
b;
215 RefCountedPtr<Subclass>
s = MakeRefCounted<Subclass>();
220 TEST(RefCountedPtr, MoveAssignFromSubclass) {
221 RefCountedPtr<BaseClass>
b;
223 RefCountedPtr<Subclass>
s = MakeRefCounted<Subclass>();
228 TEST(RefCountedPtr, ResetFromSubclass) {
229 RefCountedPtr<BaseClass>
b;
231 b.reset(
new Subclass());
235 TEST(RefCountedPtr, EqualityWithSubclass) {
236 Subclass*
s =
new Subclass();
237 RefCountedPtr<BaseClass>
b(s);
241 void FunctionTakingBaseClass(RefCountedPtr<BaseClass> p) {
245 TEST(RefCountedPtr, CanPassSubclassToFunctionExpectingBaseClass) {
246 RefCountedPtr<Subclass>
p = MakeRefCounted<Subclass>();
247 FunctionTakingBaseClass(p);
250 void FunctionTakingSubclass(RefCountedPtr<Subclass> p) {
254 TEST(RefCountedPtr, CanPassSubclassToFunctionExpectingSubclass) {
255 RefCountedPtr<Subclass>
p = MakeRefCounted<Subclass>();
256 FunctionTakingSubclass(p);
263 class Bar :
public DualRefCounted<Bar> {
280 TEST(WeakRefCountedPtr, DefaultConstructor) { WeakRefCountedPtr<Bar>
bar; }
282 TEST(WeakRefCountedPtr, ExplicitConstructorEmpty) {
283 WeakRefCountedPtr<Bar>
bar(
nullptr);
286 TEST(WeakRefCountedPtr, ExplicitConstructor) {
287 RefCountedPtr<Bar> bar_strong(
new Bar());
288 bar_strong->WeakRef().release();
289 WeakRefCountedPtr<Bar>
bar(bar_strong.get());
292 TEST(WeakRefCountedPtr, MoveConstructor) {
293 RefCountedPtr<Bar> bar_strong(
new Bar());
294 WeakRefCountedPtr<Bar>
bar = bar_strong->WeakRef();
300 TEST(WeakRefCountedPtr, MoveAssignment) {
301 RefCountedPtr<Bar> bar_strong(
new Bar());
302 WeakRefCountedPtr<Bar>
bar = bar_strong->WeakRef();
308 TEST(WeakRefCountedPtr, CopyConstructor) {
309 RefCountedPtr<Bar> bar_strong(
new Bar());
310 WeakRefCountedPtr<Bar>
bar = bar_strong->WeakRef();
311 WeakRefCountedPtr<Bar> bar2(
bar);
316 TEST(WeakRefCountedPtr, CopyAssignment) {
317 RefCountedPtr<Bar> bar_strong(
new Bar());
318 WeakRefCountedPtr<Bar>
bar = bar_strong->WeakRef();
319 WeakRefCountedPtr<Bar> bar2 =
bar;
324 TEST(WeakRefCountedPtr, CopyAssignmentWhenEmpty) {
325 WeakRefCountedPtr<Bar>
bar;
326 WeakRefCountedPtr<Bar> bar2;
332 TEST(WeakRefCountedPtr, CopyAssignmentToSelf) {
333 RefCountedPtr<Bar> bar_strong(
new Bar());
334 WeakRefCountedPtr<Bar>
bar = bar_strong->WeakRef();
338 TEST(WeakRefCountedPtr, EnclosedScope) {
339 RefCountedPtr<Bar> bar_strong(
new Bar());
340 WeakRefCountedPtr<Bar>
bar = bar_strong->WeakRef();
350 TEST(WeakRefCountedPtr, ResetFromNullToNonNull) {
351 RefCountedPtr<Bar> bar_strong(
new Bar());
352 WeakRefCountedPtr<Bar>
bar;
354 bar_strong->WeakRef().release();
355 bar.reset(bar_strong.get());
359 TEST(WeakRefCountedPtr, ResetFromNonNullToNonNull) {
360 RefCountedPtr<Bar> bar_strong(
new Bar());
361 RefCountedPtr<Bar> bar2_strong(
new Bar());
362 WeakRefCountedPtr<Bar>
bar = bar_strong->WeakRef();
364 bar2_strong->WeakRef().release();
365 bar.reset(bar2_strong.get());
370 TEST(WeakRefCountedPtr, ResetFromNonNullToNull) {
371 RefCountedPtr<Bar> bar_strong(
new Bar());
372 WeakRefCountedPtr<Bar>
bar = bar_strong->WeakRef();
378 TEST(WeakRefCountedPtr, ResetFromNullToNull) {
379 WeakRefCountedPtr<Bar>
bar;
385 TEST(WeakRefCountedPtr, DerefernceOperators) {
386 RefCountedPtr<Bar> bar_strong(
new Bar());
387 WeakRefCountedPtr<Bar>
bar = bar_strong->WeakRef();
393 TEST(WeakRefCountedPtr, EqualityOperators) {
394 RefCountedPtr<Bar> bar_strong(
new Bar());
395 WeakRefCountedPtr<Bar>
bar = bar_strong->WeakRef();
396 WeakRefCountedPtr<Bar> bar2 =
bar;
397 WeakRefCountedPtr<Bar>
empty;
408 RefCountedPtr<Bar> bar_strong(
new Bar());
409 RefCountedPtr<Bar> bar2_strong(
new Bar());
410 WeakRefCountedPtr<Bar>
bar = bar_strong->WeakRef();
411 WeakRefCountedPtr<Bar> bar2 = bar2_strong->WeakRef();
415 WeakRefCountedPtr<Bar> bar3;
421 class BarWithTracing :
public DualRefCounted<BarWithTracing> {
423 BarWithTracing() : DualRefCounted(
"BarWithTracing") {}
433 TEST(WeakRefCountedPtr, RefCountedWithTracing) {
434 RefCountedPtr<BarWithTracing> bar_strong(
new BarWithTracing());
435 WeakRefCountedPtr<BarWithTracing>
bar = bar_strong->WeakRef();
441 class WeakBaseClass :
public DualRefCounted<WeakBaseClass> {
453 class WeakSubclass :
public WeakBaseClass {
458 TEST(WeakRefCountedPtr, ConstructFromWeakSubclass) {
459 RefCountedPtr<WeakSubclass> strong(
new WeakSubclass());
460 WeakRefCountedPtr<WeakBaseClass>
p(strong->WeakRef().release());
463 TEST(WeakRefCountedPtr, CopyAssignFromWeakSubclass) {
464 RefCountedPtr<WeakSubclass> strong(
new WeakSubclass());
465 WeakRefCountedPtr<WeakBaseClass>
b;
467 WeakRefCountedPtr<WeakSubclass>
s = strong->WeakRef();
472 TEST(WeakRefCountedPtr, MoveAssignFromWeakSubclass) {
473 RefCountedPtr<WeakSubclass> strong(
new WeakSubclass());
474 WeakRefCountedPtr<WeakBaseClass>
b;
476 WeakRefCountedPtr<WeakSubclass>
s = strong->WeakRef();
481 TEST(WeakRefCountedPtr, ResetFromWeakSubclass) {
482 RefCountedPtr<WeakSubclass> strong(
new WeakSubclass());
483 WeakRefCountedPtr<WeakBaseClass>
b;
485 b.reset(strong->WeakRef().release());
489 TEST(WeakRefCountedPtr, EqualityWithWeakSubclass) {
490 RefCountedPtr<WeakSubclass> strong(
new WeakSubclass());
491 WeakRefCountedPtr<WeakBaseClass>
b = strong->WeakRef();
495 void FunctionTakingWeakBaseClass(WeakRefCountedPtr<WeakBaseClass> p) {
499 TEST(WeakRefCountedPtr, CanPassWeakSubclassToFunctionExpectingWeakBaseClass) {
500 RefCountedPtr<WeakSubclass> strong(
new WeakSubclass());
501 WeakRefCountedPtr<WeakSubclass>
p = strong->WeakRef();
502 FunctionTakingWeakBaseClass(p);
505 void FunctionTakingWeakSubclass(WeakRefCountedPtr<WeakSubclass> p) {
509 TEST(WeakRefCountedPtr, CanPassWeakSubclassToFunctionExpectingWeakSubclass) {
510 RefCountedPtr<WeakSubclass> strong(
new WeakSubclass());
511 WeakRefCountedPtr<WeakSubclass>
p = strong->WeakRef();
512 FunctionTakingWeakSubclass(p);
519 int main(
int argc,
char** argv) {