1// Copyright (c) 2011 The Chromium Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5#include "base/debug/leak_tracker.h" 6#include "base/memory/scoped_ptr.h" 7#include "testing/gtest/include/gtest/gtest.h" 8 9namespace base { 10namespace debug { 11 12namespace { 13 14class ClassA { 15 private: 16 LeakTracker<ClassA> leak_tracker_; 17}; 18 19class ClassB { 20 private: 21 LeakTracker<ClassB> leak_tracker_; 22}; 23 24#ifndef ENABLE_LEAK_TRACKER 25 26// If leak tracking is disabled, we should do nothing. 27TEST(LeakTrackerTest, NotEnabled) { 28 EXPECT_EQ(-1, LeakTracker<ClassA>::NumLiveInstances()); 29 EXPECT_EQ(-1, LeakTracker<ClassB>::NumLiveInstances()); 30 31 // Use scoped_ptr so compiler doesn't complain about unused variables. 32 scoped_ptr<ClassA> a1(new ClassA); 33 scoped_ptr<ClassB> b1(new ClassB); 34 scoped_ptr<ClassB> b2(new ClassB); 35 36 EXPECT_EQ(-1, LeakTracker<ClassA>::NumLiveInstances()); 37 EXPECT_EQ(-1, LeakTracker<ClassB>::NumLiveInstances()); 38} 39 40#else 41 42TEST(LeakTrackerTest, Basic) { 43 { 44 ClassA a1; 45 46 EXPECT_EQ(1, LeakTracker<ClassA>::NumLiveInstances()); 47 EXPECT_EQ(0, LeakTracker<ClassB>::NumLiveInstances()); 48 49 ClassB b1; 50 ClassB b2; 51 52 EXPECT_EQ(1, LeakTracker<ClassA>::NumLiveInstances()); 53 EXPECT_EQ(2, LeakTracker<ClassB>::NumLiveInstances()); 54 55 scoped_ptr<ClassA> a2(new ClassA); 56 57 EXPECT_EQ(2, LeakTracker<ClassA>::NumLiveInstances()); 58 EXPECT_EQ(2, LeakTracker<ClassB>::NumLiveInstances()); 59 60 a2.reset(); 61 62 EXPECT_EQ(1, LeakTracker<ClassA>::NumLiveInstances()); 63 EXPECT_EQ(2, LeakTracker<ClassB>::NumLiveInstances()); 64 } 65 66 EXPECT_EQ(0, LeakTracker<ClassA>::NumLiveInstances()); 67 EXPECT_EQ(0, LeakTracker<ClassB>::NumLiveInstances()); 68} 69 70// Try some orderings of create/remove to hit different cases in the linked-list 71// assembly. 72TEST(LeakTrackerTest, LinkedList) { 73 EXPECT_EQ(0, LeakTracker<ClassB>::NumLiveInstances()); 74 75 scoped_ptr<ClassA> a1(new ClassA); 76 scoped_ptr<ClassA> a2(new ClassA); 77 scoped_ptr<ClassA> a3(new ClassA); 78 scoped_ptr<ClassA> a4(new ClassA); 79 80 EXPECT_EQ(4, LeakTracker<ClassA>::NumLiveInstances()); 81 82 // Remove the head of the list (a1). 83 a1.reset(); 84 EXPECT_EQ(3, LeakTracker<ClassA>::NumLiveInstances()); 85 86 // Remove the tail of the list (a4). 87 a4.reset(); 88 EXPECT_EQ(2, LeakTracker<ClassA>::NumLiveInstances()); 89 90 // Append to the new tail of the list (a3). 91 scoped_ptr<ClassA> a5(new ClassA); 92 EXPECT_EQ(3, LeakTracker<ClassA>::NumLiveInstances()); 93 94 a2.reset(); 95 a3.reset(); 96 97 EXPECT_EQ(1, LeakTracker<ClassA>::NumLiveInstances()); 98 99 a5.reset(); 100 EXPECT_EQ(0, LeakTracker<ClassA>::NumLiveInstances()); 101} 102 103TEST(LeakTrackerTest, NoOpCheckForLeaks) { 104 // There are no live instances of ClassA, so this should do nothing. 105 LeakTracker<ClassA>::CheckForLeaks(); 106} 107 108#endif // ENABLE_LEAK_TRACKER 109 110} // namespace 111 112} // namespace debug 113} // namespace base 114