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