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