sample10_unittest.cc 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. // This sample shows how to use Google Test listener API to implement
  29. // a primitive leak checker.
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include "gtest/gtest.h"
  33. using ::testing::EmptyTestEventListener;
  34. using ::testing::InitGoogleTest;
  35. using ::testing::Test;
  36. using ::testing::TestEventListeners;
  37. using ::testing::TestInfo;
  38. using ::testing::TestPartResult;
  39. using ::testing::UnitTest;
  40. namespace {
  41. // We will track memory used by this class.
  42. class Water {
  43. public:
  44. // Normal Water declarations go here.
  45. // operator new and operator delete help us control water allocation.
  46. void* operator new(size_t allocation_size) {
  47. allocated_++;
  48. return malloc(allocation_size);
  49. }
  50. void operator delete(void* block, size_t /* allocation_size */) {
  51. allocated_--;
  52. free(block);
  53. }
  54. static int allocated() { return allocated_; }
  55. private:
  56. static int allocated_;
  57. };
  58. int Water::allocated_ = 0;
  59. // This event listener monitors how many Water objects are created and
  60. // destroyed by each test, and reports a failure if a test leaks some Water
  61. // objects. It does this by comparing the number of live Water objects at
  62. // the beginning of a test and at the end of a test.
  63. class LeakChecker : public EmptyTestEventListener {
  64. private:
  65. // Called before a test starts.
  66. void OnTestStart(const TestInfo& /* test_info */) override {
  67. initially_allocated_ = Water::allocated();
  68. }
  69. // Called after a test ends.
  70. void OnTestEnd(const TestInfo& /* test_info */) override {
  71. int difference = Water::allocated() - initially_allocated_;
  72. // You can generate a failure in any event handler except
  73. // OnTestPartResult. Just use an appropriate Google Test assertion to do
  74. // it.
  75. EXPECT_LE(difference, 0) << "Leaked " << difference << " unit(s) of Water!";
  76. }
  77. int initially_allocated_;
  78. };
  79. TEST(ListenersTest, DoesNotLeak) {
  80. Water* water = new Water;
  81. delete water;
  82. }
  83. // This should fail when the --check_for_leaks command line flag is
  84. // specified.
  85. TEST(ListenersTest, LeaksWater) {
  86. Water* water = new Water;
  87. EXPECT_TRUE(water != nullptr);
  88. }
  89. } // namespace
  90. int main(int argc, char **argv) {
  91. InitGoogleTest(&argc, argv);
  92. bool check_for_leaks = false;
  93. if (argc > 1 && strcmp(argv[1], "--check_for_leaks") == 0 )
  94. check_for_leaks = true;
  95. else
  96. printf("%s\n", "Run this program with --check_for_leaks to enable "
  97. "custom leak checking in the tests.");
  98. // If we are given the --check_for_leaks command line flag, installs the
  99. // leak checker.
  100. if (check_for_leaks) {
  101. TestEventListeners& listeners = UnitTest::GetInstance()->listeners();
  102. // Adds the leak checker to the end of the test event listener list,
  103. // after the default text output printer and the default XML report
  104. // generator.
  105. //
  106. // The order is important - it ensures that failures generated in the
  107. // leak checker's OnTestEnd() method are processed by the text and XML
  108. // printers *before* their OnTestEnd() methods are called, such that
  109. // they are attributed to the right test. Remember that a listener
  110. // receives an OnXyzStart event *after* listeners preceding it in the
  111. // list received that event, and receives an OnXyzEnd event *before*
  112. // listeners preceding it.
  113. //
  114. // We don't need to worry about deleting the new listener later, as
  115. // Google Test will do it.
  116. listeners.Append(new LeakChecker);
  117. }
  118. return RUN_ALL_TESTS();
  119. }