1 //===- unittest/ADT/IntrusiveRefCntPtrTest.cpp ----------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "llvm/ADT/IntrusiveRefCntPtr.h"
10 #include "gtest/gtest.h"
15 struct SimpleRefCounted
: public RefCountedBase
<SimpleRefCounted
> {
16 SimpleRefCounted() { ++NumInstances
; }
17 SimpleRefCounted(const SimpleRefCounted
&) : RefCountedBase() {
20 ~SimpleRefCounted() { --NumInstances
; }
22 static int NumInstances
;
24 int SimpleRefCounted::NumInstances
= 0;
25 } // anonymous namespace
27 TEST(IntrusiveRefCntPtr
, RefCountedBaseCopyDoesNotLeak
) {
28 EXPECT_EQ(0, SimpleRefCounted::NumInstances
);
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
);
36 EXPECT_EQ(0, SimpleRefCounted::NumInstances
);
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
;
45 template <> struct IntrusiveRefCntPtrInfo
<InterceptRefCounted
> {
46 static void retain(InterceptRefCounted
*I
) {
50 static void release(InterceptRefCounted
*I
) {
55 TEST(IntrusiveRefCntPtr
, UsesTraitsToRetainAndRelease
) {
56 bool Released
= false;
57 bool Retained
= false;
59 InterceptRefCounted
*I
= new InterceptRefCounted(&Released
, &Retained
);
60 IntrusiveRefCntPtr
<InterceptRefCounted
> R
= I
;
62 EXPECT_TRUE(Released
);
63 EXPECT_TRUE(Retained
);
66 } // end namespace llvm