37 using ::testing::EmptyTestEventListener;
39 using ::testing::Test;
40 using ::testing::TestEventListeners;
41 using ::testing::TestInfo;
42 using ::testing::TestPartResult;
43 using ::testing::UnitTest;
52 void*
operator new(
size_t allocation_size) {
54 return malloc(allocation_size);
57 void operator delete(
void* block,
size_t ) {
62 static int allocated() {
return allocated_; }
65 static int allocated_;
68 int Water::allocated_ = 0;
74 class LeakChecker :
public EmptyTestEventListener {
77 void OnTestStart(
const TestInfo& )
override {
78 initially_allocated_ = Water::allocated();
82 void OnTestEnd(
const TestInfo& )
override {
83 int difference = Water::allocated() - initially_allocated_;
88 EXPECT_LE(difference, 0) <<
"Leaked " << difference <<
" unit(s) of Water!";
91 int initially_allocated_;
94 TEST(ListenersTest, DoesNotLeak) {
95 Water* water =
new Water;
101 TEST(ListenersTest, LeaksWater) {
102 Water* water =
new Water;
107 int main(
int argc,
char **argv) {
110 bool check_for_leaks =
false;
111 if (argc > 1 && strcmp(argv[1],
"--check_for_leaks") == 0 )
112 check_for_leaks =
true;
114 printf(
"%s\n",
"Run this program with --check_for_leaks to enable "
115 "custom leak checking in the tests.");
119 if (check_for_leaks) {
120 TestEventListeners& listeners = UnitTest::GetInstance()->listeners();
136 listeners.Append(
new LeakChecker);
#define EXPECT_LE(val1, val2)
#define TEST(test_suite_name, test_name)
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
GTEST_API_ void InitGoogleTest(int *argc, char **argv)
#define EXPECT_TRUE(condition)