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