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 // Provide out-of-line definition to prevent weak vtable.
20 void VirtualRefCounted::f() {}
22 // Run this test with valgrind to detect memory leaks.
23 TEST(IntrusiveRefCntPtr, RefCountedBaseVPTRCopyDoesNotLeak) {
24 VirtualRefCounted *V1 = new VirtualRefCounted;
25 IntrusiveRefCntPtr<VirtualRefCounted> R1 = V1;
26 VirtualRefCounted *V2 = new VirtualRefCounted(*V1);
27 IntrusiveRefCntPtr<VirtualRefCounted> R2 = V2;
30 struct SimpleRefCounted : public RefCountedBase<SimpleRefCounted> {};
32 // Run this test with valgrind to detect memory leaks.
33 TEST(IntrusiveRefCntPtr, RefCountedBaseCopyDoesNotLeak) {
34 SimpleRefCounted *S1 = new SimpleRefCounted;
35 IntrusiveRefCntPtr<SimpleRefCounted> R1 = S1;
36 SimpleRefCounted *S2 = new SimpleRefCounted(*S1);
37 IntrusiveRefCntPtr<SimpleRefCounted> R2 = S2;
40 struct InterceptRefCounted : public RefCountedBase<InterceptRefCounted> {
41 InterceptRefCounted(bool *Released, bool *Retained)
42 : Released(Released), Retained(Retained) {}
43 bool * const Released;
44 bool * const Retained;
46 template <> struct IntrusiveRefCntPtrInfo<InterceptRefCounted> {
47 static void retain(InterceptRefCounted *I) {
51 static void release(InterceptRefCounted *I) {
56 TEST(IntrusiveRefCntPtr, UsesTraitsToRetainAndRelease) {
57 bool Released = false;
58 bool Retained = false;
60 InterceptRefCounted *I = new InterceptRefCounted(&Released, &Retained);
61 IntrusiveRefCntPtr<InterceptRefCounted> R = I;
63 EXPECT_TRUE(Released);
64 EXPECT_TRUE(Retained);
67 } // end namespace llvm