Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
sample10_unittest.cc
Go to the documentation of this file.
1// Copyright 2009 Google Inc. All Rights Reserved.
2//
3// Redistribution and use in source and binary forms, with or without
4// modification, are permitted provided that the following conditions are
5// met:
6//
7// * Redistributions of source code must retain the above copyright
8// notice, this list of conditions and the following disclaimer.
9// * Redistributions in binary form must reproduce the above
10// copyright notice, this list of conditions and the following disclaimer
11// in the documentation and/or other materials provided with the
12// distribution.
13// * Neither the name of Google Inc. nor the names of its
14// contributors may be used to endorse or promote products derived from
15// this software without specific prior written permission.
16//
17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28//
29// Author: vladl@google.com (Vlad Losev)
30
31// This sample shows how to use Google Test listener API to implement
32// a primitive leak checker.
33
34#include <stdio.h>
35#include <stdlib.h>
36
37#include "gtest/gtest.h"
38using ::testing::EmptyTestEventListener;
39using ::testing::InitGoogleTest;
40using ::testing::Test;
41using ::testing::TestEventListeners;
42using ::testing::TestInfo;
43using ::testing::TestPartResult;
44using ::testing::UnitTest;
45
46namespace {
47// We will track memory used by this class.
48class Water {
49 public:
50 // Normal Water declarations go here.
51
52 // operator new and operator delete help us control water allocation.
53 void* operator new(size_t allocation_size) {
54 allocated_++;
55 return malloc(allocation_size);
56 }
57
58 void operator delete(void* block, size_t /* allocation_size */) {
59 allocated_--;
60 free(block);
61 }
62
63 static int allocated() { return allocated_; }
64
65 private:
66 static int allocated_;
67};
68
69int Water::allocated_ = 0;
70
71// This event listener monitors how many Water objects are created and
72// destroyed by each test, and reports a failure if a test leaks some Water
73// objects. It does this by comparing the number of live Water objects at
74// the beginning of a test and at the end of a test.
75class LeakChecker : public EmptyTestEventListener {
76 private:
77 // Called before a test starts.
78 virtual void OnTestStart(const TestInfo& /* test_info */) {
79 initially_allocated_ = Water::allocated();
80 }
81
82 // Called after a test ends.
83 virtual void OnTestEnd(const TestInfo& /* test_info */) {
84 int difference = Water::allocated() - initially_allocated_;
85
86 // You can generate a failure in any event handler except
87 // OnTestPartResult. Just use an appropriate Google Test assertion to do
88 // it.
89 EXPECT_LE(difference, 0) << "Leaked " << difference << " unit(s) of Water!";
90 }
91
92 int initially_allocated_;
93};
94
95TEST(ListenersTest, DoesNotLeak) {
96 Water* water = new Water;
97 delete water;
98}
99
100// This should fail when the --check_for_leaks command line flag is
101// specified.
102TEST(ListenersTest, LeaksWater) {
103 Water* water = new Water;
104 EXPECT_TRUE(water != NULL);
105}
106} // namespace
107
108int main(int argc, char **argv) {
109 InitGoogleTest(&argc, argv);
110
111 bool check_for_leaks = false;
112 if (argc > 1 && strcmp(argv[1], "--check_for_leaks") == 0 )
113 check_for_leaks = true;
114 else
115 printf("%s\n", "Run this program with --check_for_leaks to enable "
116 "custom leak checking in the tests.");
117
118 // If we are given the --check_for_leaks command line flag, installs the
119 // leak checker.
120 if (check_for_leaks) {
121 TestEventListeners& listeners = UnitTest::GetInstance()->listeners();
122
123 // Adds the leak checker to the end of the test event listener list,
124 // after the default text output printer and the default XML report
125 // generator.
126 //
127 // The order is important - it ensures that failures generated in the
128 // leak checker's OnTestEnd() method are processed by the text and XML
129 // printers *before* their OnTestEnd() methods are called, such that
130 // they are attributed to the right test. Remember that a listener
131 // receives an OnXyzStart event *after* listeners preceding it in the
132 // list received that event, and receives an OnXyzEnd event *before*
133 // listeners preceding it.
134 //
135 // We don't need to worry about deleting the new listener later, as
136 // Google Test will do it.
137 listeners.Append(new LeakChecker);
138 }
139 return RUN_ALL_TESTS();
140}
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
Definition gtest.h:2328
#define EXPECT_TRUE(condition)
Definition gtest.h:1895
#define TEST(test_case_name, test_name)
Definition gtest.h:2275
#define EXPECT_LE(val1, val2)
Definition gtest.h:1960
char ** argv