Manuel Klimek | 31becd7 | 2012-01-31 19:58:34 +0000 | [diff] [blame] | 1 | //===- unittest/ADT/IntrusiveRefCntPtrTest.cpp ----------------------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Manuel Klimek | 31becd7 | 2012-01-31 19:58:34 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "llvm/ADT/IntrusiveRefCntPtr.h" |
| 10 | #include "gtest/gtest.h" |
| 11 | |
Juergen Ributzka | 05c5a93 | 2013-11-19 03:08:35 +0000 | [diff] [blame] | 12 | namespace llvm { |
Juergen Ributzka | d12ccbd | 2013-11-19 00:57:56 +0000 | [diff] [blame] | 13 | |
Justin Lebar | 175ab74 | 2016-12-29 19:59:26 +0000 | [diff] [blame] | 14 | namespace { |
| 15 | struct SimpleRefCounted : public RefCountedBase<SimpleRefCounted> { |
| 16 | SimpleRefCounted() { ++NumInstances; } |
Justin Lebar | 5718444 | 2017-01-04 22:49:55 +0000 | [diff] [blame] | 17 | SimpleRefCounted(const SimpleRefCounted &) : RefCountedBase() { |
| 18 | ++NumInstances; |
| 19 | } |
Justin Lebar | 175ab74 | 2016-12-29 19:59:26 +0000 | [diff] [blame] | 20 | ~SimpleRefCounted() { --NumInstances; } |
Manuel Klimek | 31becd7 | 2012-01-31 19:58:34 +0000 | [diff] [blame] | 21 | |
Justin Lebar | 175ab74 | 2016-12-29 19:59:26 +0000 | [diff] [blame] | 22 | static int NumInstances; |
| 23 | }; |
| 24 | int SimpleRefCounted::NumInstances = 0; |
| 25 | } // anonymous namespace |
Manuel Klimek | 31becd7 | 2012-01-31 19:58:34 +0000 | [diff] [blame] | 26 | |
Manuel Klimek | 31becd7 | 2012-01-31 19:58:34 +0000 | [diff] [blame] | 27 | TEST(IntrusiveRefCntPtr, RefCountedBaseCopyDoesNotLeak) { |
Justin Lebar | 175ab74 | 2016-12-29 19:59:26 +0000 | [diff] [blame] | 28 | EXPECT_EQ(0, SimpleRefCounted::NumInstances); |
| 29 | { |
| 30 | SimpleRefCounted *S1 = new SimpleRefCounted; |
| 31 | IntrusiveRefCntPtr<SimpleRefCounted> R1 = S1; |
| 32 | SimpleRefCounted *S2 = new SimpleRefCounted(*S1); |
| 33 | IntrusiveRefCntPtr<SimpleRefCounted> R2 = S2; |
| 34 | EXPECT_EQ(2, SimpleRefCounted::NumInstances); |
| 35 | } |
| 36 | EXPECT_EQ(0, SimpleRefCounted::NumInstances); |
Manuel Klimek | 31becd7 | 2012-01-31 19:58:34 +0000 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | struct InterceptRefCounted : public RefCountedBase<InterceptRefCounted> { |
| 40 | InterceptRefCounted(bool *Released, bool *Retained) |
| 41 | : Released(Released), Retained(Retained) {} |
| 42 | bool * const Released; |
| 43 | bool * const Retained; |
| 44 | }; |
| 45 | template <> struct IntrusiveRefCntPtrInfo<InterceptRefCounted> { |
| 46 | static void retain(InterceptRefCounted *I) { |
| 47 | *I->Retained = true; |
| 48 | I->Retain(); |
| 49 | } |
| 50 | static void release(InterceptRefCounted *I) { |
| 51 | *I->Released = true; |
| 52 | I->Release(); |
| 53 | } |
| 54 | }; |
| 55 | TEST(IntrusiveRefCntPtr, UsesTraitsToRetainAndRelease) { |
| 56 | bool Released = false; |
| 57 | bool Retained = false; |
| 58 | { |
| 59 | InterceptRefCounted *I = new InterceptRefCounted(&Released, &Retained); |
| 60 | IntrusiveRefCntPtr<InterceptRefCounted> R = I; |
| 61 | } |
| 62 | EXPECT_TRUE(Released); |
| 63 | EXPECT_TRUE(Retained); |
| 64 | } |
| 65 | |
| 66 | } // end namespace llvm |