[weak vtables] Place class definitions into anonymous namespaces to prevent weak...
[oota-llvm.git] / unittests / ADT / IntrusiveRefCntPtrTest.cpp
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
13 namespace {
14 struct VirtualRefCounted : public llvm::RefCountedBaseVPTR {
15   virtual void f() {}
16 };
17 }
18
19 namespace llvm {
20
21 // Run this test with valgrind to detect memory leaks.
22 TEST(IntrusiveRefCntPtr, RefCountedBaseVPTRCopyDoesNotLeak) {
23   VirtualRefCounted *V1 = new VirtualRefCounted;
24   IntrusiveRefCntPtr<VirtualRefCounted> R1 = V1;
25   VirtualRefCounted *V2 = new VirtualRefCounted(*V1);
26   IntrusiveRefCntPtr<VirtualRefCounted> R2 = V2;
27 }
28
29 struct SimpleRefCounted : public RefCountedBase<SimpleRefCounted> {};
30
31 // Run this test with valgrind to detect memory leaks.
32 TEST(IntrusiveRefCntPtr, RefCountedBaseCopyDoesNotLeak) {
33   SimpleRefCounted *S1 = new SimpleRefCounted;
34   IntrusiveRefCntPtr<SimpleRefCounted> R1 = S1;
35   SimpleRefCounted *S2 = new SimpleRefCounted(*S1);
36   IntrusiveRefCntPtr<SimpleRefCounted> R2 = S2;
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