blob: 0c8c4ca16dd736709b1ffe3ab7326b853a4378f1 [file] [log] [blame]
Manuel Klimek41aa1082012-01-31 19:58:34 +00001//===- 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
13namespace llvm {
14
15struct VirtualRefCounted : public RefCountedBaseVPTR {
16 virtual void f() {}
17};
18
19// Run this test with valgrind to detect memory leaks.
20TEST(IntrusiveRefCntPtr, RefCountedBaseVPTRCopyDoesNotLeak) {
21 VirtualRefCounted *V1 = new VirtualRefCounted;
22 IntrusiveRefCntPtr<VirtualRefCounted> R1 = V1;
23 VirtualRefCounted *V2 = new VirtualRefCounted(*V1);
24 IntrusiveRefCntPtr<VirtualRefCounted> R2 = V2;
25}
26
27struct SimpleRefCounted : public RefCountedBase<SimpleRefCounted> {};
28
29// Run this test with valgrind to detect memory leaks.
30TEST(IntrusiveRefCntPtr, RefCountedBaseCopyDoesNotLeak) {
31 SimpleRefCounted *S1 = new SimpleRefCounted;
32 IntrusiveRefCntPtr<SimpleRefCounted> R1 = S1;
33 SimpleRefCounted *S2 = new SimpleRefCounted(*S1);
34 IntrusiveRefCntPtr<SimpleRefCounted> R2 = S2;
35}
36
37struct InterceptRefCounted : public RefCountedBase<InterceptRefCounted> {
38 InterceptRefCounted(bool *Released, bool *Retained)
39 : Released(Released), Retained(Retained) {}
40 bool * const Released;
41 bool * const Retained;
42};
43template <> struct IntrusiveRefCntPtrInfo<InterceptRefCounted> {
44 static void retain(InterceptRefCounted *I) {
45 *I->Retained = true;
46 I->Retain();
47 }
48 static void release(InterceptRefCounted *I) {
49 *I->Released = true;
50 I->Release();
51 }
52};
53TEST(IntrusiveRefCntPtr, UsesTraitsToRetainAndRelease) {
54 bool Released = false;
55 bool Retained = false;
56 {
57 InterceptRefCounted *I = new InterceptRefCounted(&Released, &Retained);
58 IntrusiveRefCntPtr<InterceptRefCounted> R = I;
59 }
60 EXPECT_TRUE(Released);
61 EXPECT_TRUE(Retained);
62}
63
64} // end namespace llvm