|
Packit |
bd1cd8 |
// Copyright 2009 Google Inc. All Rights Reserved.
|
|
Packit |
bd1cd8 |
//
|
|
Packit |
bd1cd8 |
// Redistribution and use in source and binary forms, with or without
|
|
Packit |
bd1cd8 |
// modification, are permitted provided that the following conditions are
|
|
Packit |
bd1cd8 |
// met:
|
|
Packit |
bd1cd8 |
//
|
|
Packit |
bd1cd8 |
// * Redistributions of source code must retain the above copyright
|
|
Packit |
bd1cd8 |
// notice, this list of conditions and the following disclaimer.
|
|
Packit |
bd1cd8 |
// * Redistributions in binary form must reproduce the above
|
|
Packit |
bd1cd8 |
// copyright notice, this list of conditions and the following disclaimer
|
|
Packit |
bd1cd8 |
// in the documentation and/or other materials provided with the
|
|
Packit |
bd1cd8 |
// distribution.
|
|
Packit |
bd1cd8 |
// * Neither the name of Google Inc. nor the names of its
|
|
Packit |
bd1cd8 |
// contributors may be used to endorse or promote products derived from
|
|
Packit |
bd1cd8 |
// this software without specific prior written permission.
|
|
Packit |
bd1cd8 |
//
|
|
Packit |
bd1cd8 |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
Packit |
bd1cd8 |
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
Packit |
bd1cd8 |
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
Packit |
bd1cd8 |
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
Packit |
bd1cd8 |
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
Packit |
bd1cd8 |
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
Packit |
bd1cd8 |
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
Packit |
bd1cd8 |
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
Packit |
bd1cd8 |
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
Packit |
bd1cd8 |
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
Packit |
bd1cd8 |
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
Packit |
bd1cd8 |
//
|
|
Packit |
bd1cd8 |
// Author: vladl@google.com (Vlad Losev)
|
|
Packit |
bd1cd8 |
|
|
Packit |
bd1cd8 |
// This sample shows how to use Google Test listener API to implement
|
|
Packit |
bd1cd8 |
// a primitive leak checker.
|
|
Packit |
bd1cd8 |
|
|
Packit |
bd1cd8 |
#include <stdio.h>
|
|
Packit |
bd1cd8 |
#include <stdlib.h>
|
|
Packit |
bd1cd8 |
|
|
Packit |
bd1cd8 |
#include "gtest/gtest.h"
|
|
Packit |
bd1cd8 |
|
|
Packit |
bd1cd8 |
using ::testing::EmptyTestEventListener;
|
|
Packit |
bd1cd8 |
using ::testing::InitGoogleTest;
|
|
Packit |
bd1cd8 |
using ::testing::Test;
|
|
Packit |
bd1cd8 |
using ::testing::TestCase;
|
|
Packit |
bd1cd8 |
using ::testing::TestEventListeners;
|
|
Packit |
bd1cd8 |
using ::testing::TestInfo;
|
|
Packit |
bd1cd8 |
using ::testing::TestPartResult;
|
|
Packit |
bd1cd8 |
using ::testing::UnitTest;
|
|
Packit |
bd1cd8 |
|
|
Packit |
bd1cd8 |
namespace {
|
|
Packit |
bd1cd8 |
|
|
Packit |
bd1cd8 |
// We will track memory used by this class.
|
|
Packit |
bd1cd8 |
class Water {
|
|
Packit |
bd1cd8 |
public:
|
|
Packit |
bd1cd8 |
// Normal Water declarations go here.
|
|
Packit |
bd1cd8 |
|
|
Packit |
bd1cd8 |
// operator new and operator delete help us control water allocation.
|
|
Packit |
bd1cd8 |
void* operator new(size_t allocation_size) {
|
|
Packit |
bd1cd8 |
allocated_++;
|
|
Packit |
bd1cd8 |
return malloc(allocation_size);
|
|
Packit |
bd1cd8 |
}
|
|
Packit |
bd1cd8 |
|
|
Packit |
bd1cd8 |
void operator delete(void* block, size_t /* allocation_size */) {
|
|
Packit |
bd1cd8 |
allocated_--;
|
|
Packit |
bd1cd8 |
free(block);
|
|
Packit |
bd1cd8 |
}
|
|
Packit |
bd1cd8 |
|
|
Packit |
bd1cd8 |
static int allocated() { return allocated_; }
|
|
Packit |
bd1cd8 |
|
|
Packit |
bd1cd8 |
private:
|
|
Packit |
bd1cd8 |
static int allocated_;
|
|
Packit |
bd1cd8 |
};
|
|
Packit |
bd1cd8 |
|
|
Packit |
bd1cd8 |
int Water::allocated_ = 0;
|
|
Packit |
bd1cd8 |
|
|
Packit |
bd1cd8 |
// This event listener monitors how many Water objects are created and
|
|
Packit |
bd1cd8 |
// destroyed by each test, and reports a failure if a test leaks some Water
|
|
Packit |
bd1cd8 |
// objects. It does this by comparing the number of live Water objects at
|
|
Packit |
bd1cd8 |
// the beginning of a test and at the end of a test.
|
|
Packit |
bd1cd8 |
class LeakChecker : public EmptyTestEventListener {
|
|
Packit |
bd1cd8 |
private:
|
|
Packit |
bd1cd8 |
// Called before a test starts.
|
|
Packit |
bd1cd8 |
virtual void OnTestStart(const TestInfo& /* test_info */) {
|
|
Packit |
bd1cd8 |
initially_allocated_ = Water::allocated();
|
|
Packit |
bd1cd8 |
}
|
|
Packit |
bd1cd8 |
|
|
Packit |
bd1cd8 |
// Called after a test ends.
|
|
Packit |
bd1cd8 |
virtual void OnTestEnd(const TestInfo& /* test_info */) {
|
|
Packit |
bd1cd8 |
int difference = Water::allocated() - initially_allocated_;
|
|
Packit |
bd1cd8 |
|
|
Packit |
bd1cd8 |
// You can generate a failure in any event handler except
|
|
Packit |
bd1cd8 |
// OnTestPartResult. Just use an appropriate Google Test assertion to do
|
|
Packit |
bd1cd8 |
// it.
|
|
Packit |
bd1cd8 |
EXPECT_LE(difference, 0) << "Leaked " << difference << " unit(s) of Water!";
|
|
Packit |
bd1cd8 |
}
|
|
Packit |
bd1cd8 |
|
|
Packit |
bd1cd8 |
int initially_allocated_;
|
|
Packit |
bd1cd8 |
};
|
|
Packit |
bd1cd8 |
|
|
Packit |
bd1cd8 |
TEST(ListenersTest, DoesNotLeak) {
|
|
Packit |
bd1cd8 |
Water* water = new Water;
|
|
Packit |
bd1cd8 |
delete water;
|
|
Packit |
bd1cd8 |
}
|
|
Packit |
bd1cd8 |
|
|
Packit |
bd1cd8 |
// This should fail when the --check_for_leaks command line flag is
|
|
Packit |
bd1cd8 |
// specified.
|
|
Packit |
bd1cd8 |
TEST(ListenersTest, LeaksWater) {
|
|
Packit |
bd1cd8 |
Water* water = new Water;
|
|
Packit |
bd1cd8 |
EXPECT_TRUE(water != NULL);
|
|
Packit |
bd1cd8 |
}
|
|
Packit |
bd1cd8 |
|
|
Packit |
bd1cd8 |
} // namespace
|
|
Packit |
bd1cd8 |
|
|
Packit |
bd1cd8 |
int main(int argc, char **argv) {
|
|
Packit |
bd1cd8 |
InitGoogleTest(&argc, argv);
|
|
Packit |
bd1cd8 |
|
|
Packit |
bd1cd8 |
bool check_for_leaks = false;
|
|
Packit |
bd1cd8 |
if (argc > 1 && strcmp(argv[1], "--check_for_leaks") == 0 )
|
|
Packit |
bd1cd8 |
check_for_leaks = true;
|
|
Packit |
bd1cd8 |
else
|
|
Packit |
bd1cd8 |
printf("%s\n", "Run this program with --check_for_leaks to enable "
|
|
Packit |
bd1cd8 |
"custom leak checking in the tests.");
|
|
Packit |
bd1cd8 |
|
|
Packit |
bd1cd8 |
// If we are given the --check_for_leaks command line flag, installs the
|
|
Packit |
bd1cd8 |
// leak checker.
|
|
Packit |
bd1cd8 |
if (check_for_leaks) {
|
|
Packit |
bd1cd8 |
TestEventListeners& listeners = UnitTest::GetInstance()->listeners();
|
|
Packit |
bd1cd8 |
|
|
Packit |
bd1cd8 |
// Adds the leak checker to the end of the test event listener list,
|
|
Packit |
bd1cd8 |
// after the default text output printer and the default XML report
|
|
Packit |
bd1cd8 |
// generator.
|
|
Packit |
bd1cd8 |
//
|
|
Packit |
bd1cd8 |
// The order is important - it ensures that failures generated in the
|
|
Packit |
bd1cd8 |
// leak checker's OnTestEnd() method are processed by the text and XML
|
|
Packit |
bd1cd8 |
// printers *before* their OnTestEnd() methods are called, such that
|
|
Packit |
bd1cd8 |
// they are attributed to the right test. Remember that a listener
|
|
Packit |
bd1cd8 |
// receives an OnXyzStart event *after* listeners preceding it in the
|
|
Packit |
bd1cd8 |
// list received that event, and receives an OnXyzEnd event *before*
|
|
Packit |
bd1cd8 |
// listeners preceding it.
|
|
Packit |
bd1cd8 |
//
|
|
Packit |
bd1cd8 |
// We don't need to worry about deleting the new listener later, as
|
|
Packit |
bd1cd8 |
// Google Test will do it.
|
|
Packit |
bd1cd8 |
listeners.Append(new LeakChecker);
|
|
Packit |
bd1cd8 |
}
|
|
Packit |
bd1cd8 |
return RUN_ALL_TESTS();
|
|
Packit |
bd1cd8 |
}
|