You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

141 lines
4.9 KiB

  1. // Copyright 2009 Google Inc. All Rights Reserved.
  2. //
  3. // Redistribution and use in source and binary forms, with or without
  4. // modification, are permitted provided that the following conditions are
  5. // met:
  6. //
  7. // * Redistributions of source code must retain the above copyright
  8. // notice, this list of conditions and the following disclaimer.
  9. // * Redistributions in binary form must reproduce the above
  10. // copyright notice, this list of conditions and the following disclaimer
  11. // in the documentation and/or other materials provided with the
  12. // distribution.
  13. // * Neither the name of Google Inc. nor the names of its
  14. // contributors may be used to endorse or promote products derived from
  15. // this software without specific prior written permission.
  16. //
  17. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  20. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  21. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  22. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  23. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  24. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  25. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  27. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. //
  29. // Author: vladl@google.com (Vlad Losev)
  30. // This sample shows how to use Google Test listener API to implement
  31. // a primitive leak checker.
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include "gtest/gtest.h"
  35. using ::testing::EmptyTestEventListener;
  36. using ::testing::InitGoogleTest;
  37. using ::testing::Test;
  38. using ::testing::TestEventListeners;
  39. using ::testing::TestInfo;
  40. using ::testing::TestPartResult;
  41. using ::testing::UnitTest;
  42. namespace {
  43. // We will track memory used by this class.
  44. class Water {
  45. public:
  46. // Normal Water declarations go here.
  47. // operator new and operator delete help us control water allocation.
  48. void* operator new(size_t allocation_size) {
  49. allocated_++;
  50. return malloc(allocation_size);
  51. }
  52. void operator delete(void* block, size_t /* allocation_size */) {
  53. allocated_--;
  54. free(block);
  55. }
  56. static int allocated() { return allocated_; }
  57. private:
  58. static int allocated_;
  59. };
  60. int Water::allocated_ = 0;
  61. // This event listener monitors how many Water objects are created and
  62. // destroyed by each test, and reports a failure if a test leaks some Water
  63. // objects. It does this by comparing the number of live Water objects at
  64. // the beginning of a test and at the end of a test.
  65. class LeakChecker : public EmptyTestEventListener {
  66. private:
  67. // Called before a test starts.
  68. virtual void OnTestStart(const TestInfo& /* test_info */) {
  69. initially_allocated_ = Water::allocated();
  70. }
  71. // Called after a test ends.
  72. virtual void OnTestEnd(const TestInfo& /* test_info */) {
  73. int difference = Water::allocated() - initially_allocated_;
  74. // You can generate a failure in any event handler except
  75. // OnTestPartResult. Just use an appropriate Google Test assertion to do
  76. // it.
  77. EXPECT_LE(difference, 0) << "Leaked " << difference << " unit(s) of Water!";
  78. }
  79. int initially_allocated_;
  80. };
  81. TEST(ListenersTest, DoesNotLeak) {
  82. Water* water = new Water;
  83. delete water;
  84. }
  85. // This should fail when the --check_for_leaks command line flag is
  86. // specified.
  87. TEST(ListenersTest, LeaksWater) {
  88. Water* water = new Water;
  89. EXPECT_TRUE(water != NULL);
  90. }
  91. } // namespace
  92. int main(int argc, char **argv) {
  93. InitGoogleTest(&argc, argv);
  94. bool check_for_leaks = false;
  95. if (argc > 1 && strcmp(argv[1], "--check_for_leaks") == 0 )
  96. check_for_leaks = true;
  97. else
  98. printf("%s\n", "Run this program with --check_for_leaks to enable "
  99. "custom leak checking in the tests.");
  100. // If we are given the --check_for_leaks command line flag, installs the
  101. // leak checker.
  102. if (check_for_leaks) {
  103. TestEventListeners& listeners = UnitTest::GetInstance()->listeners();
  104. // Adds the leak checker to the end of the test event listener list,
  105. // after the default text output printer and the default XML report
  106. // generator.
  107. //
  108. // The order is important - it ensures that failures generated in the
  109. // leak checker's OnTestEnd() method are processed by the text and XML
  110. // printers *before* their OnTestEnd() methods are called, such that
  111. // they are attributed to the right test. Remember that a listener
  112. // receives an OnXyzStart event *after* listeners preceding it in the
  113. // list received that event, and receives an OnXyzEnd event *before*
  114. // listeners preceding it.
  115. //
  116. // We don't need to worry about deleting the new listener later, as
  117. // Google Test will do it.
  118. listeners.Append(new LeakChecker);
  119. }
  120. return RUN_ALL_TESTS();
  121. }