cc: Remove unused disable_hi_res_timer_tasks_on_battery flag.
[chromium-blink-merge.git] / base / debug / leak_tracker_unittest.cc
blob99df4c17e1d9c7174728cddc74ce9ec4888dfc42
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"
9 namespace base {
10 namespace debug {
12 namespace {
14 class ClassA {
15 private:
16 LeakTracker<ClassA> leak_tracker_;
19 class ClassB {
20 private:
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());
40 #else
42 TEST(LeakTrackerTest, Basic) {
44 ClassA a1;
46 EXPECT_EQ(1, LeakTracker<ClassA>::NumLiveInstances());
47 EXPECT_EQ(0, LeakTracker<ClassB>::NumLiveInstances());
49 ClassB b1;
50 ClassB b2;
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());
60 a2.reset();
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
71 // assembly.
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).
83 a1.reset();
84 EXPECT_EQ(3, LeakTracker<ClassA>::NumLiveInstances());
86 // Remove the tail of the list (a4).
87 a4.reset();
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());
94 a2.reset();
95 a3.reset();
97 EXPECT_EQ(1, LeakTracker<ClassA>::NumLiveInstances());
99 a5.reset();
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
110 } // namespace
112 } // namespace debug
113 } // namespace base