Blame nss/external_tests/google_test/gtest/samples/sample10_unittest.cc

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