1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/media/webrtc/trunk/testing/gtest/samples/sample10_unittest.cc Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,144 @@ 1.4 +// Copyright 2009 Google Inc. All Rights Reserved. 1.5 +// 1.6 +// Redistribution and use in source and binary forms, with or without 1.7 +// modification, are permitted provided that the following conditions are 1.8 +// met: 1.9 +// 1.10 +// * Redistributions of source code must retain the above copyright 1.11 +// notice, this list of conditions and the following disclaimer. 1.12 +// * Redistributions in binary form must reproduce the above 1.13 +// copyright notice, this list of conditions and the following disclaimer 1.14 +// in the documentation and/or other materials provided with the 1.15 +// distribution. 1.16 +// * Neither the name of Google Inc. nor the names of its 1.17 +// contributors may be used to endorse or promote products derived from 1.18 +// this software without specific prior written permission. 1.19 +// 1.20 +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1.21 +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1.22 +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1.23 +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 1.24 +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 1.25 +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 1.26 +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 1.27 +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 1.28 +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 1.29 +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 1.30 +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.31 +// 1.32 +// Author: vladl@google.com (Vlad Losev) 1.33 + 1.34 +// This sample shows how to use Google Test listener API to implement 1.35 +// a primitive leak checker. 1.36 + 1.37 +#include <stdio.h> 1.38 +#include <stdlib.h> 1.39 + 1.40 +#include "gtest/gtest.h" 1.41 + 1.42 +using ::testing::EmptyTestEventListener; 1.43 +using ::testing::InitGoogleTest; 1.44 +using ::testing::Test; 1.45 +using ::testing::TestCase; 1.46 +using ::testing::TestEventListeners; 1.47 +using ::testing::TestInfo; 1.48 +using ::testing::TestPartResult; 1.49 +using ::testing::UnitTest; 1.50 + 1.51 +namespace { 1.52 + 1.53 +// We will track memory used by this class. 1.54 +class Water { 1.55 + public: 1.56 + // Normal Water declarations go here. 1.57 + 1.58 + // operator new and operator delete help us control water allocation. 1.59 + void* operator new(size_t allocation_size) { 1.60 + allocated_++; 1.61 + return malloc(allocation_size); 1.62 + } 1.63 + 1.64 + void operator delete(void* block, size_t /* allocation_size */) { 1.65 + allocated_--; 1.66 + free(block); 1.67 + } 1.68 + 1.69 + static int allocated() { return allocated_; } 1.70 + 1.71 + private: 1.72 + static int allocated_; 1.73 +}; 1.74 + 1.75 +int Water::allocated_ = 0; 1.76 + 1.77 +// This event listener monitors how many Water objects are created and 1.78 +// destroyed by each test, and reports a failure if a test leaks some Water 1.79 +// objects. It does this by comparing the number of live Water objects at 1.80 +// the beginning of a test and at the end of a test. 1.81 +class LeakChecker : public EmptyTestEventListener { 1.82 + private: 1.83 + // Called before a test starts. 1.84 + virtual void OnTestStart(const TestInfo& /* test_info */) { 1.85 + initially_allocated_ = Water::allocated(); 1.86 + } 1.87 + 1.88 + // Called after a test ends. 1.89 + virtual void OnTestEnd(const TestInfo& /* test_info */) { 1.90 + int difference = Water::allocated() - initially_allocated_; 1.91 + 1.92 + // You can generate a failure in any event handler except 1.93 + // OnTestPartResult. Just use an appropriate Google Test assertion to do 1.94 + // it. 1.95 + EXPECT_LE(difference, 0) << "Leaked " << difference << " unit(s) of Water!"; 1.96 + } 1.97 + 1.98 + int initially_allocated_; 1.99 +}; 1.100 + 1.101 +TEST(ListenersTest, DoesNotLeak) { 1.102 + Water* water = new Water; 1.103 + delete water; 1.104 +} 1.105 + 1.106 +// This should fail when the --check_for_leaks command line flag is 1.107 +// specified. 1.108 +TEST(ListenersTest, LeaksWater) { 1.109 + Water* water = new Water; 1.110 + EXPECT_TRUE(water != NULL); 1.111 +} 1.112 + 1.113 +} // namespace 1.114 + 1.115 +int main(int argc, char **argv) { 1.116 + InitGoogleTest(&argc, argv); 1.117 + 1.118 + bool check_for_leaks = false; 1.119 + if (argc > 1 && strcmp(argv[1], "--check_for_leaks") == 0 ) 1.120 + check_for_leaks = true; 1.121 + else 1.122 + printf("%s\n", "Run this program with --check_for_leaks to enable " 1.123 + "custom leak checking in the tests."); 1.124 + 1.125 + // If we are given the --check_for_leaks command line flag, installs the 1.126 + // leak checker. 1.127 + if (check_for_leaks) { 1.128 + TestEventListeners& listeners = UnitTest::GetInstance()->listeners(); 1.129 + 1.130 + // Adds the leak checker to the end of the test event listener list, 1.131 + // after the default text output printer and the default XML report 1.132 + // generator. 1.133 + // 1.134 + // The order is important - it ensures that failures generated in the 1.135 + // leak checker's OnTestEnd() method are processed by the text and XML 1.136 + // printers *before* their OnTestEnd() methods are called, such that 1.137 + // they are attributed to the right test. Remember that a listener 1.138 + // receives an OnXyzStart event *after* listeners preceding it in the 1.139 + // list received that event, and receives an OnXyzEnd event *before* 1.140 + // listeners preceding it. 1.141 + // 1.142 + // We don't need to worry about deleting the new listener later, as 1.143 + // Google Test will do it. 1.144 + listeners.Append(new LeakChecker); 1.145 + } 1.146 + return RUN_ALL_TESTS(); 1.147 +}