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