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