blob: 0217b179abdf1713ae27ab67a62a72d9ab7a0f90 [file] [log] [blame]
ericroman@google.com7e41f132009-08-12 06:38:54 +09001// 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
3// found in the LICENSE file.
4
5#include "base/leak_tracker.h"
6#include "base/scoped_ptr.h"
7#include "testing/gtest/include/gtest/gtest.h"
8
9namespace {
10
11class ClassA {
12 private:
13 base::LeakTracker<ClassA> leak_tracker_;
14};
15
16class ClassB {
17 private:
18 base::LeakTracker<ClassB> leak_tracker_;
19};
20
eroman@chromium.orgb0a25412009-09-17 06:14:08 +090021#ifndef ENABLE_LEAK_TRACKER
ericroman@google.com7e41f132009-08-12 06:38:54 +090022
eroman@chromium.orgb0a25412009-09-17 06:14:08 +090023// If leak tracking is disabled, we should do nothing.
24TEST(LeakTrackerTest, NotEnabled) {
ericroman@google.com7e41f132009-08-12 06:38:54 +090025 EXPECT_EQ(-1, base::LeakTracker<ClassA>::NumLiveInstances());
26 EXPECT_EQ(-1, base::LeakTracker<ClassB>::NumLiveInstances());
27
28 // Use scoped_ptr so compiler doesn't complain about unused variables.
29 scoped_ptr<ClassA> a1(new ClassA);
30 scoped_ptr<ClassB> b1(new ClassB);
31 scoped_ptr<ClassB> b2(new ClassB);
32
33 EXPECT_EQ(-1, base::LeakTracker<ClassA>::NumLiveInstances());
34 EXPECT_EQ(-1, base::LeakTracker<ClassB>::NumLiveInstances());
35}
36
37#else
38
eroman@chromium.orgb0a25412009-09-17 06:14:08 +090039TEST(LeakTrackerTest, Basic) {
ericroman@google.com7e41f132009-08-12 06:38:54 +090040 {
41 ClassA a1;
42
43 EXPECT_EQ(1, base::LeakTracker<ClassA>::NumLiveInstances());
44 EXPECT_EQ(0, base::LeakTracker<ClassB>::NumLiveInstances());
45
46 ClassB b1;
47 ClassB b2;
48
49 EXPECT_EQ(1, base::LeakTracker<ClassA>::NumLiveInstances());
50 EXPECT_EQ(2, base::LeakTracker<ClassB>::NumLiveInstances());
51
52 scoped_ptr<ClassA> a2(new ClassA);
53
54 EXPECT_EQ(2, base::LeakTracker<ClassA>::NumLiveInstances());
55 EXPECT_EQ(2, base::LeakTracker<ClassB>::NumLiveInstances());
56
57 a2.reset();
58
59 EXPECT_EQ(1, base::LeakTracker<ClassA>::NumLiveInstances());
60 EXPECT_EQ(2, base::LeakTracker<ClassB>::NumLiveInstances());
61 }
62
63 EXPECT_EQ(0, base::LeakTracker<ClassA>::NumLiveInstances());
64 EXPECT_EQ(0, base::LeakTracker<ClassB>::NumLiveInstances());
65}
66
67// Try some orderings of create/remove to hit different cases in the linked-list
68// assembly.
eroman@chromium.orgb0a25412009-09-17 06:14:08 +090069TEST(LeakTrackerTest, LinkedList) {
ericroman@google.com7e41f132009-08-12 06:38:54 +090070 EXPECT_EQ(0, base::LeakTracker<ClassB>::NumLiveInstances());
71
72 scoped_ptr<ClassA> a1(new ClassA);
73 scoped_ptr<ClassA> a2(new ClassA);
74 scoped_ptr<ClassA> a3(new ClassA);
75 scoped_ptr<ClassA> a4(new ClassA);
76
77 EXPECT_EQ(4, base::LeakTracker<ClassA>::NumLiveInstances());
78
79 // Remove the head of the list (a1).
80 a1.reset();
81 EXPECT_EQ(3, base::LeakTracker<ClassA>::NumLiveInstances());
82
83 // Remove the tail of the list (a4).
84 a4.reset();
85 EXPECT_EQ(2, base::LeakTracker<ClassA>::NumLiveInstances());
86
87 // Append to the new tail of the list (a3).
88 scoped_ptr<ClassA> a5(new ClassA);
89 EXPECT_EQ(3, base::LeakTracker<ClassA>::NumLiveInstances());
90
91 a2.reset();
92 a3.reset();
93
94 EXPECT_EQ(1, base::LeakTracker<ClassA>::NumLiveInstances());
95
96 a5.reset();
97 EXPECT_EQ(0, base::LeakTracker<ClassA>::NumLiveInstances());
98}
99
eroman@chromium.orgb0a25412009-09-17 06:14:08 +0900100TEST(LeakTrackerTest, NoOpCheckForLeaks) {
ericroman@google.com7e41f132009-08-12 06:38:54 +0900101 // There are no live instances of ClassA, so this should do nothing.
102 base::LeakTracker<ClassA>::CheckForLeaks();
103}
104
eroman@chromium.orgb0a25412009-09-17 06:14:08 +0900105#endif // ENABLE_LEAK_TRACKER
ericroman@google.com7e41f132009-08-12 06:38:54 +0900106
107} // namespace