leak_tracker_unittest.cc 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. #include "butil/debug/leak_tracker.h"
  5. #include "butil/memory/scoped_ptr.h"
  6. #include <gtest/gtest.h>
  7. namespace butil {
  8. namespace debug {
  9. namespace {
  10. class ClassA {
  11. private:
  12. LeakTracker<ClassA> leak_tracker_;
  13. };
  14. class ClassB {
  15. private:
  16. LeakTracker<ClassB> leak_tracker_;
  17. };
  18. #ifndef ENABLE_LEAK_TRACKER
  19. // If leak tracking is disabled, we should do nothing.
  20. TEST(LeakTrackerTest, NotEnabled) {
  21. EXPECT_EQ(-1, LeakTracker<ClassA>::NumLiveInstances());
  22. EXPECT_EQ(-1, LeakTracker<ClassB>::NumLiveInstances());
  23. // Use scoped_ptr so compiler doesn't complain about unused variables.
  24. scoped_ptr<ClassA> a1(new ClassA);
  25. scoped_ptr<ClassB> b1(new ClassB);
  26. scoped_ptr<ClassB> b2(new ClassB);
  27. EXPECT_EQ(-1, LeakTracker<ClassA>::NumLiveInstances());
  28. EXPECT_EQ(-1, LeakTracker<ClassB>::NumLiveInstances());
  29. }
  30. #else
  31. TEST(LeakTrackerTest, Basic) {
  32. {
  33. ClassA a1;
  34. EXPECT_EQ(1, LeakTracker<ClassA>::NumLiveInstances());
  35. EXPECT_EQ(0, LeakTracker<ClassB>::NumLiveInstances());
  36. ClassB b1;
  37. ClassB b2;
  38. EXPECT_EQ(1, LeakTracker<ClassA>::NumLiveInstances());
  39. EXPECT_EQ(2, LeakTracker<ClassB>::NumLiveInstances());
  40. scoped_ptr<ClassA> a2(new ClassA);
  41. EXPECT_EQ(2, LeakTracker<ClassA>::NumLiveInstances());
  42. EXPECT_EQ(2, LeakTracker<ClassB>::NumLiveInstances());
  43. a2.reset();
  44. EXPECT_EQ(1, LeakTracker<ClassA>::NumLiveInstances());
  45. EXPECT_EQ(2, LeakTracker<ClassB>::NumLiveInstances());
  46. }
  47. EXPECT_EQ(0, LeakTracker<ClassA>::NumLiveInstances());
  48. EXPECT_EQ(0, LeakTracker<ClassB>::NumLiveInstances());
  49. }
  50. // Try some orderings of create/remove to hit different cases in the linked-list
  51. // assembly.
  52. TEST(LeakTrackerTest, LinkedList) {
  53. EXPECT_EQ(0, LeakTracker<ClassB>::NumLiveInstances());
  54. scoped_ptr<ClassA> a1(new ClassA);
  55. scoped_ptr<ClassA> a2(new ClassA);
  56. scoped_ptr<ClassA> a3(new ClassA);
  57. scoped_ptr<ClassA> a4(new ClassA);
  58. EXPECT_EQ(4, LeakTracker<ClassA>::NumLiveInstances());
  59. // Remove the head of the list (a1).
  60. a1.reset();
  61. EXPECT_EQ(3, LeakTracker<ClassA>::NumLiveInstances());
  62. // Remove the tail of the list (a4).
  63. a4.reset();
  64. EXPECT_EQ(2, LeakTracker<ClassA>::NumLiveInstances());
  65. // Append to the new tail of the list (a3).
  66. scoped_ptr<ClassA> a5(new ClassA);
  67. EXPECT_EQ(3, LeakTracker<ClassA>::NumLiveInstances());
  68. a2.reset();
  69. a3.reset();
  70. EXPECT_EQ(1, LeakTracker<ClassA>::NumLiveInstances());
  71. a5.reset();
  72. EXPECT_EQ(0, LeakTracker<ClassA>::NumLiveInstances());
  73. }
  74. TEST(LeakTrackerTest, NoOpCheckForLeaks) {
  75. // There are no live instances of ClassA, so this should do nothing.
  76. LeakTracker<ClassA>::CheckForLeaks();
  77. }
  78. #endif // ENABLE_LEAK_TRACKER
  79. } // namespace
  80. } // namespace debug
  81. } // namespace butil