38using ::testing::EmptyTestEventListener;
39using ::testing::InitGoogleTest;
41using ::testing::TestEventListeners;
42using ::testing::TestInfo;
43using ::testing::TestPartResult;
44using ::testing::UnitTest;
53 void*
operator new(
size_t allocation_size) {
55 return malloc(allocation_size);
58 void operator delete(
void* block,
size_t ) {
63 static int allocated() {
return allocated_; }
66 static int allocated_;
69int Water::allocated_ = 0;
75class LeakChecker :
public EmptyTestEventListener {
78 virtual void OnTestStart(
const TestInfo& ) {
79 initially_allocated_ = Water::allocated();
83 virtual void OnTestEnd(
const TestInfo& ) {
84 int difference = Water::allocated() - initially_allocated_;
89 EXPECT_LE(difference, 0) <<
"Leaked " << difference <<
" unit(s) of Water!";
92 int initially_allocated_;
95TEST(ListenersTest, DoesNotLeak) {
96 Water* water =
new Water;
102TEST(ListenersTest, LeaksWater) {
103 Water* water =
new Water;
109 InitGoogleTest(&argc,
argv);
111 bool check_for_leaks =
false;
112 if (argc > 1 && strcmp(
argv[1],
"--check_for_leaks") == 0 )
113 check_for_leaks =
true;
115 printf(
"%s\n",
"Run this program with --check_for_leaks to enable "
116 "custom leak checking in the tests.");
120 if (check_for_leaks) {
121 TestEventListeners& listeners = UnitTest::GetInstance()->listeners();
137 listeners.Append(
new LeakChecker);
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
#define EXPECT_TRUE(condition)
#define TEST(test_case_name, test_name)
#define EXPECT_LE(val1, val2)