[yaml2obj/obj2yaml] - Add support for .stack_sizes sections.
[llvm-complete.git] / unittests / ADT / IntrusiveRefCntPtrTest.cpp
blobc248b04ed5a2c505e5e94689eea1754877c3fc85
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 struct SimpleRefCounted : public RefCountedBase<SimpleRefCounted> {
16 SimpleRefCounted() { ++NumInstances; }
17 SimpleRefCounted(const SimpleRefCounted &) : RefCountedBase() {
18 ++NumInstances;
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) {
47 *I->Retained = true;
48 I->Retain();
50 static void release(InterceptRefCounted *I) {
51 *I->Released = true;
52 I->Release();
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