Manuel Klimek | 41aa108 | 2012-01-31 19:58:34 +0000 | [diff] [blame] | 1 | //===- unittest/ADT/IntrusiveRefCntPtrTest.cpp ----------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "llvm/ADT/IntrusiveRefCntPtr.h" |
| 11 | #include "gtest/gtest.h" |
| 12 | |
Juergen Ributzka | ba0f991 | 2013-11-19 03:08:35 +0000 | [diff] [blame] | 13 | namespace llvm { |
Juergen Ributzka | 3543625 | 2013-11-19 00:57:56 +0000 | [diff] [blame] | 14 | |
Justin Lebar | 517714c | 2016-12-29 19:59:26 +0000 | [diff] [blame] | 15 | namespace { |
| 16 | struct SimpleRefCounted : public RefCountedBase<SimpleRefCounted> { |
| 17 | SimpleRefCounted() { ++NumInstances; } |
Justin Lebar | d5845b6 | 2017-01-04 22:49:55 +0000 | [diff] [blame] | 18 | SimpleRefCounted(const SimpleRefCounted &) : RefCountedBase() { |
| 19 | ++NumInstances; |
| 20 | } |
Justin Lebar | 517714c | 2016-12-29 19:59:26 +0000 | [diff] [blame] | 21 | ~SimpleRefCounted() { --NumInstances; } |
Manuel Klimek | 41aa108 | 2012-01-31 19:58:34 +0000 | [diff] [blame] | 22 | |
Justin Lebar | 517714c | 2016-12-29 19:59:26 +0000 | [diff] [blame] | 23 | static int NumInstances; |
| 24 | }; |
| 25 | int SimpleRefCounted::NumInstances = 0; |
| 26 | } // anonymous namespace |
Manuel Klimek | 41aa108 | 2012-01-31 19:58:34 +0000 | [diff] [blame] | 27 | |
Manuel Klimek | 41aa108 | 2012-01-31 19:58:34 +0000 | [diff] [blame] | 28 | TEST(IntrusiveRefCntPtr, RefCountedBaseCopyDoesNotLeak) { |
Justin Lebar | 517714c | 2016-12-29 19:59:26 +0000 | [diff] [blame] | 29 | EXPECT_EQ(0, SimpleRefCounted::NumInstances); |
| 30 | { |
| 31 | SimpleRefCounted *S1 = new SimpleRefCounted; |
| 32 | IntrusiveRefCntPtr<SimpleRefCounted> R1 = S1; |
| 33 | SimpleRefCounted *S2 = new SimpleRefCounted(*S1); |
| 34 | IntrusiveRefCntPtr<SimpleRefCounted> R2 = S2; |
| 35 | EXPECT_EQ(2, SimpleRefCounted::NumInstances); |
| 36 | } |
| 37 | EXPECT_EQ(0, SimpleRefCounted::NumInstances); |
Manuel Klimek | 41aa108 | 2012-01-31 19:58:34 +0000 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | struct InterceptRefCounted : public RefCountedBase<InterceptRefCounted> { |
| 41 | InterceptRefCounted(bool *Released, bool *Retained) |
| 42 | : Released(Released), Retained(Retained) {} |
| 43 | bool * const Released; |
| 44 | bool * const Retained; |
| 45 | }; |
| 46 | template <> struct IntrusiveRefCntPtrInfo<InterceptRefCounted> { |
| 47 | static void retain(InterceptRefCounted *I) { |
| 48 | *I->Retained = true; |
| 49 | I->Retain(); |
| 50 | } |
| 51 | static void release(InterceptRefCounted *I) { |
| 52 | *I->Released = true; |
| 53 | I->Release(); |
| 54 | } |
| 55 | }; |
| 56 | TEST(IntrusiveRefCntPtr, UsesTraitsToRetainAndRelease) { |
| 57 | bool Released = false; |
| 58 | bool Retained = false; |
| 59 | { |
| 60 | InterceptRefCounted *I = new InterceptRefCounted(&Released, &Retained); |
| 61 | IntrusiveRefCntPtr<InterceptRefCounted> R = I; |
| 62 | } |
| 63 | EXPECT_TRUE(Released); |
| 64 | EXPECT_TRUE(Retained); |
| 65 | } |
| 66 | |
| 67 | } // end namespace llvm |