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