39 using ::testing::EmptyTestEventListener;
41 using ::testing::Test;
43 using ::testing::TestEventListeners;
44 using ::testing::TestInfo;
45 using ::testing::TestPartResult;
46 using ::testing::UnitTest;
56 void*
operator new(
size_t allocation_size) {
58 return malloc(allocation_size);
61 void operator delete(
void* block,
size_t ) {
66 static int allocated() {
return allocated_; }
69 static int allocated_;
72 int Water::allocated_ = 0;
78 class LeakChecker :
public EmptyTestEventListener {
81 virtual void OnTestStart(
const TestInfo& ) {
82 initially_allocated_ = Water::allocated();
86 virtual void OnTestEnd(
const TestInfo& ) {
87 int difference = Water::allocated() - initially_allocated_;
92 EXPECT_LE(difference, 0) <<
"Leaked " << difference <<
" unit(s) of Water!";
95 int initially_allocated_;
98 TEST(ListenersTest, DoesNotLeak) {
99 Water* water =
new Water;
105 TEST(ListenersTest, LeaksWater) {
106 Water* water =
new Water;
112 int main(
int argc,
char **argv) {
115 bool check_for_leaks =
false;
116 if (argc > 1 && strcmp(argv[1],
"--check_for_leaks") == 0 )
117 check_for_leaks =
true;
119 printf(
"%s\n",
"Run this program with --check_for_leaks to enable "
120 "custom leak checking in the tests.");
124 if (check_for_leaks) {
125 TestEventListeners& listeners = UnitTest::GetInstance()->listeners();
141 listeners.Append(
new LeakChecker);