Fix test failures introduced by PR #113697 (#116941)
[llvm-project.git] / llvm / unittests / ADT / IntrusiveRefCntPtrTest.cpp
blobf4f208348280488acb45b5ef7c0d10150e7e3088
1 //===- unittest/ADT/IntrusiveRefCntPtrTest.cpp ----------------------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
9 #include "llvm/ADT/IntrusiveRefCntPtr.h"
10 #include "gtest/gtest.h"
12 namespace llvm {
14 namespace {
15 int NumInstances = 0;
16 template <template <typename> class Base>
17 struct SimpleRefCounted : Base<SimpleRefCounted<Base>> {
18 SimpleRefCounted() { ++NumInstances; }
19 SimpleRefCounted(const SimpleRefCounted &RHS) : Base<SimpleRefCounted>(RHS) {
20 ++NumInstances;
22 ~SimpleRefCounted() { --NumInstances; }
24 } // anonymous namespace
26 template <typename T> struct IntrusiveRefCntPtrTest : testing::Test {};
28 typedef ::testing::Types<SimpleRefCounted<RefCountedBase>,
29 SimpleRefCounted<ThreadSafeRefCountedBase>>
30 IntrusiveRefCntTypes;
31 TYPED_TEST_SUITE(IntrusiveRefCntPtrTest, IntrusiveRefCntTypes, );
33 TYPED_TEST(IntrusiveRefCntPtrTest, RefCountedBaseCopyDoesNotLeak) {
34 EXPECT_EQ(0, NumInstances);
36 TypeParam *S1 = new TypeParam;
37 IntrusiveRefCntPtr<TypeParam> R1 = S1;
38 TypeParam *S2 = new TypeParam(*S1);
39 IntrusiveRefCntPtr<TypeParam> R2 = S2;
40 EXPECT_EQ(2, NumInstances);
42 EXPECT_EQ(0, NumInstances);
45 TYPED_TEST(IntrusiveRefCntPtrTest, InteropsWithUniquePtr) {
46 EXPECT_EQ(0, NumInstances);
48 auto S1 = std::make_unique<TypeParam>();
49 IntrusiveRefCntPtr<TypeParam> R1 = std::move(S1);
50 EXPECT_EQ(1, NumInstances);
51 EXPECT_EQ(S1, nullptr);
53 EXPECT_EQ(0, NumInstances);
56 TYPED_TEST(IntrusiveRefCntPtrTest, MakeIntrusiveRefCnt) {
57 EXPECT_EQ(0, NumInstances);
59 auto S1 = makeIntrusiveRefCnt<TypeParam>();
60 auto S2 = makeIntrusiveRefCnt<const TypeParam>();
61 EXPECT_EQ(2, NumInstances);
62 static_assert(
63 std::is_same<decltype(S1), IntrusiveRefCntPtr<TypeParam>>::value,
64 "Non-const type mismatch");
65 static_assert(
66 std::is_same<decltype(S2), IntrusiveRefCntPtr<const TypeParam>>::value,
67 "Const type mismatch");
69 EXPECT_EQ(0, NumInstances);
72 struct InterceptRefCounted : public RefCountedBase<InterceptRefCounted> {
73 InterceptRefCounted(bool *Released, bool *Retained)
74 : Released(Released), Retained(Retained) {}
75 bool * const Released;
76 bool * const Retained;
78 template <> struct IntrusiveRefCntPtrInfo<InterceptRefCounted> {
79 static void retain(InterceptRefCounted *I) {
80 *I->Retained = true;
81 I->Retain();
83 static void release(InterceptRefCounted *I) {
84 *I->Released = true;
85 I->Release();
88 TEST(IntrusiveRefCntPtr, UsesTraitsToRetainAndRelease) {
89 bool Released = false;
90 bool Retained = false;
92 InterceptRefCounted *I = new InterceptRefCounted(&Released, &Retained);
93 IntrusiveRefCntPtr<InterceptRefCounted> R = I;
95 EXPECT_TRUE(Released);
96 EXPECT_TRUE(Retained);
99 // Test that the generic constructors use SFINAE to disable invalid
100 // conversions.
101 struct X : RefCountedBase<X> {};
102 struct Y : X {};
103 struct Z : RefCountedBase<Z> {};
104 static_assert(
105 !std::is_convertible_v<IntrusiveRefCntPtr<X> &&, IntrusiveRefCntPtr<Y>>,
106 "X&& -> Y should be rejected with SFINAE");
107 static_assert(!std::is_convertible_v<const IntrusiveRefCntPtr<X> &,
108 IntrusiveRefCntPtr<Y>>,
109 "const X& -> Y should be rejected with SFINAE");
110 static_assert(!std::is_convertible_v<std::unique_ptr<X>, IntrusiveRefCntPtr<Y>>,
111 "X -> Y should be rejected with SFINAE");
112 static_assert(
113 !std::is_convertible_v<IntrusiveRefCntPtr<X> &&, IntrusiveRefCntPtr<Z>>,
114 "X&& -> Z should be rejected with SFINAE");
115 static_assert(!std::is_convertible_v<const IntrusiveRefCntPtr<X> &,
116 IntrusiveRefCntPtr<Z>>,
117 "const X& -> Z should be rejected with SFINAE");
118 static_assert(!std::is_convertible_v<std::unique_ptr<X>, IntrusiveRefCntPtr<Z>>,
119 "X -> Z should be rejected with SFINAE");
121 TEST(IntrusiveRefCntPtr, InteropsWithConvertible) {
122 // Check converting constructors and operator=.
123 auto Y1 = makeIntrusiveRefCnt<Y>();
124 auto Y2 = makeIntrusiveRefCnt<Y>();
125 auto Y3 = makeIntrusiveRefCnt<Y>();
126 auto Y4 = makeIntrusiveRefCnt<Y>();
127 const void *P1 = Y1.get();
128 const void *P2 = Y2.get();
129 const void *P3 = Y3.get();
130 const void *P4 = Y4.get();
131 IntrusiveRefCntPtr<X> X1 = std::move(Y1);
132 IntrusiveRefCntPtr<X> X2 = Y2;
133 IntrusiveRefCntPtr<X> X3;
134 IntrusiveRefCntPtr<X> X4;
135 X3 = std::move(Y3);
136 X4 = Y4;
137 EXPECT_EQ(P1, X1.get());
138 EXPECT_EQ(P2, X2.get());
139 EXPECT_EQ(P3, X3.get());
140 EXPECT_EQ(P4, X4.get());
143 TEST(IntrusiveRefCntPtrTest, Unique) {
144 IntrusiveRefCntPtr<X> X1;
145 EXPECT_EQ(X1.useCount(), 0u);
146 X1 = new X();
147 EXPECT_EQ(X1.useCount(), 1u);
149 IntrusiveRefCntPtr<X> X2 = X1;
150 EXPECT_EQ(X1.useCount(), 2u);
151 EXPECT_EQ(X2.useCount(), 2u);
153 EXPECT_EQ(X1.useCount(), 1u);
156 } // end namespace llvm