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.
5 #include "base/debug/leak_tracker.h"
6 #include "base/memory/scoped_ptr.h"
7 #include "testing/gtest/include/gtest/gtest.h"
16 LeakTracker
<ClassA
> leak_tracker_
;
21 LeakTracker
<ClassB
> leak_tracker_
;
24 #ifndef ENABLE_LEAK_TRACKER
26 // If leak tracking is disabled, we should do nothing.
27 TEST(LeakTrackerTest
, NotEnabled
) {
28 EXPECT_EQ(-1, LeakTracker
<ClassA
>::NumLiveInstances());
29 EXPECT_EQ(-1, LeakTracker
<ClassB
>::NumLiveInstances());
31 // Use scoped_ptr so compiler doesn't complain about unused variables.
32 scoped_ptr
<ClassA
> a1(new ClassA
);
33 scoped_ptr
<ClassB
> b1(new ClassB
);
34 scoped_ptr
<ClassB
> b2(new ClassB
);
36 EXPECT_EQ(-1, LeakTracker
<ClassA
>::NumLiveInstances());
37 EXPECT_EQ(-1, LeakTracker
<ClassB
>::NumLiveInstances());
42 TEST(LeakTrackerTest
, Basic
) {
46 EXPECT_EQ(1, LeakTracker
<ClassA
>::NumLiveInstances());
47 EXPECT_EQ(0, LeakTracker
<ClassB
>::NumLiveInstances());
52 EXPECT_EQ(1, LeakTracker
<ClassA
>::NumLiveInstances());
53 EXPECT_EQ(2, LeakTracker
<ClassB
>::NumLiveInstances());
55 scoped_ptr
<ClassA
> a2(new ClassA
);
57 EXPECT_EQ(2, LeakTracker
<ClassA
>::NumLiveInstances());
58 EXPECT_EQ(2, LeakTracker
<ClassB
>::NumLiveInstances());
62 EXPECT_EQ(1, LeakTracker
<ClassA
>::NumLiveInstances());
63 EXPECT_EQ(2, LeakTracker
<ClassB
>::NumLiveInstances());
66 EXPECT_EQ(0, LeakTracker
<ClassA
>::NumLiveInstances());
67 EXPECT_EQ(0, LeakTracker
<ClassB
>::NumLiveInstances());
70 // Try some orderings of create/remove to hit different cases in the linked-list
72 TEST(LeakTrackerTest
, LinkedList
) {
73 EXPECT_EQ(0, LeakTracker
<ClassB
>::NumLiveInstances());
75 scoped_ptr
<ClassA
> a1(new ClassA
);
76 scoped_ptr
<ClassA
> a2(new ClassA
);
77 scoped_ptr
<ClassA
> a3(new ClassA
);
78 scoped_ptr
<ClassA
> a4(new ClassA
);
80 EXPECT_EQ(4, LeakTracker
<ClassA
>::NumLiveInstances());
82 // Remove the head of the list (a1).
84 EXPECT_EQ(3, LeakTracker
<ClassA
>::NumLiveInstances());
86 // Remove the tail of the list (a4).
88 EXPECT_EQ(2, LeakTracker
<ClassA
>::NumLiveInstances());
90 // Append to the new tail of the list (a3).
91 scoped_ptr
<ClassA
> a5(new ClassA
);
92 EXPECT_EQ(3, LeakTracker
<ClassA
>::NumLiveInstances());
97 EXPECT_EQ(1, LeakTracker
<ClassA
>::NumLiveInstances());
100 EXPECT_EQ(0, LeakTracker
<ClassA
>::NumLiveInstances());
103 TEST(LeakTrackerTest
, NoOpCheckForLeaks
) {
104 // There are no live instances of ClassA, so this should do nothing.
105 LeakTracker
<ClassA
>::CheckForLeaks();
108 #endif // ENABLE_LEAK_TRACKER