media/webrtc/trunk/testing/gtest/samples/sample10_unittest.cc

Wed, 31 Dec 2014 07:53:36 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:53:36 +0100
branch
TOR_BUG_3246
changeset 5
4ab42b5ab56c
permissions
-rw-r--r--

Correct small whitespace inconsistency, lost while renaming variables.

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

mercurial