37using ::testing::EmptyTestEventListener;
40using ::testing::TestEventListeners;
41using ::testing::TestInfo;
42using ::testing::TestPartResult;
43using ::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_;
68int Water::allocated_ = 0;
74class 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_;
94TEST(ListenersTest, DoesNotLeak) {
95 Water* water =
new Water;
101TEST(ListenersTest, LeaksWater) {
102 Water* water =
new Water;
107int 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);
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
#define TEST(test_suite_name, test_name)
#define EXPECT_TRUE(condition)
#define EXPECT_LE(val1, val2)
int main(int argc, char **argv)
GTEST_API_ void InitGoogleTest(int *argc, char **argv)