1 //===- unittest/ADT/IntrusiveRefCntPtrTest.cpp ----------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/ADT/IntrusiveRefCntPtr.h"
11 #include "gtest/gtest.h"
15 struct VirtualRefCounted : public RefCountedBaseVPTR {
19 // Run this test with valgrind to detect memory leaks.
20 TEST(IntrusiveRefCntPtr, RefCountedBaseVPTRCopyDoesNotLeak) {
21 VirtualRefCounted *V1 = new VirtualRefCounted;
22 IntrusiveRefCntPtr<VirtualRefCounted> R1 = V1;
23 VirtualRefCounted *V2 = new VirtualRefCounted(*V1);
24 IntrusiveRefCntPtr<VirtualRefCounted> R2 = V2;
27 struct SimpleRefCounted : public RefCountedBase<SimpleRefCounted> {};
29 // Run this test with valgrind to detect memory leaks.
30 TEST(IntrusiveRefCntPtr, RefCountedBaseCopyDoesNotLeak) {
31 SimpleRefCounted *S1 = new SimpleRefCounted;
32 IntrusiveRefCntPtr<SimpleRefCounted> R1 = S1;
33 SimpleRefCounted *S2 = new SimpleRefCounted(*S1);
34 IntrusiveRefCntPtr<SimpleRefCounted> R2 = S2;
37 struct InterceptRefCounted : public RefCountedBase<InterceptRefCounted> {
38 InterceptRefCounted(bool *Released, bool *Retained)
39 : Released(Released), Retained(Retained) {}
40 bool * const Released;
41 bool * const Retained;
43 template <> struct IntrusiveRefCntPtrInfo<InterceptRefCounted> {
44 static void retain(InterceptRefCounted *I) {
48 static void release(InterceptRefCounted *I) {
53 TEST(IntrusiveRefCntPtr, UsesTraitsToRetainAndRelease) {
54 bool Released = false;
55 bool Retained = false;
57 InterceptRefCounted *I = new InterceptRefCounted(&Released, &Retained);
58 IntrusiveRefCntPtr<InterceptRefCounted> R = I;
60 EXPECT_TRUE(Released);
61 EXPECT_TRUE(Retained);
64 } // end namespace llvm