sample10_unittest.cc
Go to the documentation of this file.
00001 // Copyright 2009 Google Inc. All Rights Reserved.
00002 //
00003 // Redistribution and use in source and binary forms, with or without
00004 // modification, are permitted provided that the following conditions are
00005 // met:
00006 //
00007 //     * Redistributions of source code must retain the above copyright
00008 // notice, this list of conditions and the following disclaimer.
00009 //     * Redistributions in binary form must reproduce the above
00010 // copyright notice, this list of conditions and the following disclaimer
00011 // in the documentation and/or other materials provided with the
00012 // distribution.
00013 //     * Neither the name of Google Inc. nor the names of its
00014 // contributors may be used to endorse or promote products derived from
00015 // this software without specific prior written permission.
00016 //
00017 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00018 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00019 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00020 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00021 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00022 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00023 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00024 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00025 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00026 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00027 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00028 //
00029 // Author: vladl@google.com (Vlad Losev)
00030 
00031 // This sample shows how to use Google Test listener API to implement
00032 // a primitive leak checker.
00033 
00034 #include <stdio.h>
00035 #include <stdlib.h>
00036 
00037 #include "gtest/gtest.h"
00038 
00039 using ::testing::EmptyTestEventListener;
00040 using ::testing::InitGoogleTest;
00041 using ::testing::Test;
00042 using ::testing::TestCase;
00043 using ::testing::TestEventListeners;
00044 using ::testing::TestInfo;
00045 using ::testing::TestPartResult;
00046 using ::testing::UnitTest;
00047 
00048 namespace {
00049 
00050 // We will track memory used by this class.
00051 class Water {
00052  public:
00053   // Normal Water declarations go here.
00054 
00055   // operator new and operator delete help us control water allocation.
00056   void* operator new(size_t allocation_size) {
00057     allocated_++;
00058     return malloc(allocation_size);
00059   }
00060 
00061   void operator delete(void* block, size_t /* allocation_size */) {
00062     allocated_--;
00063     free(block);
00064   }
00065 
00066   static int allocated() { return allocated_; }
00067 
00068  private:
00069   static int allocated_;
00070 };
00071 
00072 int Water::allocated_ = 0;
00073 
00074 // This event listener monitors how many Water objects are created and
00075 // destroyed by each test, and reports a failure if a test leaks some Water
00076 // objects. It does this by comparing the number of live Water objects at
00077 // the beginning of a test and at the end of a test.
00078 class LeakChecker : public EmptyTestEventListener {
00079  private:
00080   // Called before a test starts.
00081   virtual void OnTestStart(const TestInfo& /* test_info */) {
00082     initially_allocated_ = Water::allocated();
00083   }
00084 
00085   // Called after a test ends.
00086   virtual void OnTestEnd(const TestInfo& /* test_info */) {
00087     int difference = Water::allocated() - initially_allocated_;
00088 
00089     // You can generate a failure in any event handler except
00090     // OnTestPartResult. Just use an appropriate Google Test assertion to do
00091     // it.
00092     EXPECT_LE(difference, 0) << "Leaked " << difference << " unit(s) of Water!";
00093   }
00094 
00095   int initially_allocated_;
00096 };
00097 
00098 TEST(ListenersTest, DoesNotLeak) {
00099   Water* water = new Water;
00100   delete water;
00101 }
00102 
00103 // This should fail when the --check_for_leaks command line flag is
00104 // specified.
00105 TEST(ListenersTest, LeaksWater) {
00106   Water* water = new Water;
00107   EXPECT_TRUE(water != NULL);
00108 }
00109 
00110 }  // namespace
00111 
00112 int main(int argc, char **argv) {
00113   InitGoogleTest(&argc, argv);
00114 
00115   bool check_for_leaks = false;
00116   if (argc > 1 && strcmp(argv[1], "--check_for_leaks") == 0 )
00117     check_for_leaks = true;
00118   else
00119     printf("%s\n", "Run this program with --check_for_leaks to enable "
00120            "custom leak checking in the tests.");
00121 
00122   // If we are given the --check_for_leaks command line flag, installs the
00123   // leak checker.
00124   if (check_for_leaks) {
00125     TestEventListeners& listeners = UnitTest::GetInstance()->listeners();
00126 
00127     // Adds the leak checker to the end of the test event listener list,
00128     // after the default text output printer and the default XML report
00129     // generator.
00130     //
00131     // The order is important - it ensures that failures generated in the
00132     // leak checker's OnTestEnd() method are processed by the text and XML
00133     // printers *before* their OnTestEnd() methods are called, such that
00134     // they are attributed to the right test. Remember that a listener
00135     // receives an OnXyzStart event *after* listeners preceding it in the
00136     // list received that event, and receives an OnXyzEnd event *before*
00137     // listeners preceding it.
00138     //
00139     // We don't need to worry about deleting the new listener later, as
00140     // Google Test will do it.
00141     listeners.Append(new LeakChecker);
00142   }
00143   return RUN_ALL_TESTS();
00144 }


rc_visard_driver
Author(s): Heiko Hirschmueller , Christian Emmerich , Felix Ruess
autogenerated on Thu Jun 6 2019 20:43:06