36 using ::testing::EmptyTestEventListener;
38 using ::testing::Test;
39 using ::testing::TestEventListeners;
40 using ::testing::TestInfo;
41 using ::testing::UnitTest;
50 void*
operator new(
size_t allocation_size) {
52 return malloc(allocation_size);
55 void operator delete(
void* block,
size_t ) {
60 static int allocated() {
return allocated_; }
63 static int allocated_;
66 int Water::allocated_ = 0;
72 class LeakChecker :
public EmptyTestEventListener {
75 void OnTestStart(
const TestInfo& )
override {
76 initially_allocated_ = Water::allocated();
80 void OnTestEnd(
const TestInfo& )
override {
81 int difference = Water::allocated() - initially_allocated_;
86 EXPECT_LE(difference, 0) <<
"Leaked " << difference <<
" unit(s) of Water!";
89 int initially_allocated_;
92 TEST(ListenersTest, DoesNotLeak) {
93 Water* water =
new Water;
99 TEST(ListenersTest, LeaksWater) {
100 Water* water =
new Water;
105 int main(
int argc,
char** argv) {
108 bool check_for_leaks =
false;
109 if (argc > 1 && strcmp(argv[1],
"--check_for_leaks") == 0)
110 check_for_leaks =
true;
113 "Run this program with --check_for_leaks to enable "
114 "custom leak checking in the tests.");
118 if (check_for_leaks) {
119 TestEventListeners& listeners = UnitTest::GetInstance()->listeners();
135 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)