Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
gtest_repeat_test.cc
Go to the documentation of this file.
1// Copyright 2008, Google Inc.
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are
6// met:
7//
8// * Redistributions of source code must retain the above copyright
9// notice, this list of conditions and the following disclaimer.
10// * Redistributions in binary form must reproduce the above
11// copyright notice, this list of conditions and the following disclaimer
12// in the documentation and/or other materials provided with the
13// distribution.
14// * Neither the name of Google Inc. nor the names of its
15// contributors may be used to endorse or promote products derived from
16// this software without specific prior written permission.
17//
18// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29//
30// Author: wan@google.com (Zhanyong Wan)
31
32// Tests the --gtest_repeat=number flag.
33
34#include <stdlib.h>
35#include <iostream>
36#include "gtest/gtest.h"
38
39namespace testing {
40
41GTEST_DECLARE_string_(death_test_style);
44
45} // namespace testing
46
47using testing::GTEST_FLAG(death_test_style);
48using testing::GTEST_FLAG(filter);
49using testing::GTEST_FLAG(repeat);
50
51namespace {
52
53// We need this when we are testing Google Test itself and therefore
54// cannot use Google Test assertions.
55#define GTEST_CHECK_INT_EQ_(expected, actual) \
56 do {\
57 const int expected_val = (expected);\
58 const int actual_val = (actual);\
59 if (::testing::internal::IsTrue(expected_val != actual_val)) {\
60 ::std::cout << "Value of: " #actual "\n"\
61 << " Actual: " << actual_val << "\n"\
62 << "Expected: " #expected "\n"\
63 << "Which is: " << expected_val << "\n";\
64 ::testing::internal::posix::Abort();\
65 }\
66 } while (::testing::internal::AlwaysFalse())
67
68
69// Used for verifying that global environment set-up and tear-down are
70// inside the --gtest_repeat loop.
71
72int g_environment_set_up_count = 0;
73int g_environment_tear_down_count = 0;
74
75class MyEnvironment : public testing::Environment {
76 public:
77 MyEnvironment() {}
78 virtual void SetUp() { g_environment_set_up_count++; }
79 virtual void TearDown() { g_environment_tear_down_count++; }
80};
81
82// A test that should fail.
83
84int g_should_fail_count = 0;
85
86TEST(FooTest, ShouldFail) {
87 g_should_fail_count++;
88 EXPECT_EQ(0, 1) << "Expected failure.";
89}
90
91// A test that should pass.
92
93int g_should_pass_count = 0;
94
95TEST(FooTest, ShouldPass) {
96 g_should_pass_count++;
97}
98
99// A test that contains a thread-safe death test and a fast death
100// test. It should pass.
101
102int g_death_test_count = 0;
103
104TEST(BarDeathTest, ThreadSafeAndFast) {
105 g_death_test_count++;
106
107 GTEST_FLAG(death_test_style) = "threadsafe";
109
110 GTEST_FLAG(death_test_style) = "fast";
112}
113
114int g_param_test_count = 0;
115
116const int kNumberOfParamTests = 10;
117
118class MyParamTest : public testing::TestWithParam<int> {};
119
120TEST_P(MyParamTest, ShouldPass) {
121 // TODO(vladl@google.com): Make parameter value checking robust
122 // WRT order of tests.
123 GTEST_CHECK_INT_EQ_(g_param_test_count % kNumberOfParamTests, GetParam());
124 g_param_test_count++;
125}
126INSTANTIATE_TEST_CASE_P(MyParamSequence,
127 MyParamTest,
128 testing::Range(0, kNumberOfParamTests));
129
130// Resets the count for each test.
131void ResetCounts() {
132 g_environment_set_up_count = 0;
133 g_environment_tear_down_count = 0;
134 g_should_fail_count = 0;
135 g_should_pass_count = 0;
136 g_death_test_count = 0;
137 g_param_test_count = 0;
138}
139
140// Checks that the count for each test is expected.
141void CheckCounts(int expected) {
142 GTEST_CHECK_INT_EQ_(expected, g_environment_set_up_count);
143 GTEST_CHECK_INT_EQ_(expected, g_environment_tear_down_count);
144 GTEST_CHECK_INT_EQ_(expected, g_should_fail_count);
145 GTEST_CHECK_INT_EQ_(expected, g_should_pass_count);
146 GTEST_CHECK_INT_EQ_(expected, g_death_test_count);
147 GTEST_CHECK_INT_EQ_(expected * kNumberOfParamTests, g_param_test_count);
148}
149
150// Tests the behavior of Google Test when --gtest_repeat is not specified.
151void TestRepeatUnspecified() {
152 ResetCounts();
154 CheckCounts(1);
155}
156
157// Tests the behavior of Google Test when --gtest_repeat has the given value.
158void TestRepeat(int repeat) {
159 GTEST_FLAG(repeat) = repeat;
160
161 ResetCounts();
162 GTEST_CHECK_INT_EQ_(repeat > 0 ? 1 : 0, RUN_ALL_TESTS());
163 CheckCounts(repeat);
164}
165
166// Tests using --gtest_repeat when --gtest_filter specifies an empty
167// set of tests.
168void TestRepeatWithEmptyFilter(int repeat) {
169 GTEST_FLAG(repeat) = repeat;
170 GTEST_FLAG(filter) = "None";
171
172 ResetCounts();
174 CheckCounts(0);
175}
176
177// Tests using --gtest_repeat when --gtest_filter specifies a set of
178// successful tests.
179void TestRepeatWithFilterForSuccessfulTests(int repeat) {
180 GTEST_FLAG(repeat) = repeat;
181 GTEST_FLAG(filter) = "*-*ShouldFail";
182
183 ResetCounts();
185 GTEST_CHECK_INT_EQ_(repeat, g_environment_set_up_count);
186 GTEST_CHECK_INT_EQ_(repeat, g_environment_tear_down_count);
187 GTEST_CHECK_INT_EQ_(0, g_should_fail_count);
188 GTEST_CHECK_INT_EQ_(repeat, g_should_pass_count);
189 GTEST_CHECK_INT_EQ_(repeat, g_death_test_count);
190 GTEST_CHECK_INT_EQ_(repeat * kNumberOfParamTests, g_param_test_count);
191}
192
193// Tests using --gtest_repeat when --gtest_filter specifies a set of
194// failed tests.
195void TestRepeatWithFilterForFailedTests(int repeat) {
196 GTEST_FLAG(repeat) = repeat;
197 GTEST_FLAG(filter) = "*ShouldFail";
198
199 ResetCounts();
201 GTEST_CHECK_INT_EQ_(repeat, g_environment_set_up_count);
202 GTEST_CHECK_INT_EQ_(repeat, g_environment_tear_down_count);
203 GTEST_CHECK_INT_EQ_(repeat, g_should_fail_count);
204 GTEST_CHECK_INT_EQ_(0, g_should_pass_count);
205 GTEST_CHECK_INT_EQ_(0, g_death_test_count);
206 GTEST_CHECK_INT_EQ_(0, g_param_test_count);
207}
208
209} // namespace
210
211int main(int argc, char **argv) {
213
214 testing::AddGlobalTestEnvironment(new MyEnvironment);
215
216 TestRepeatUnspecified();
217 TestRepeat(0);
218 TestRepeat(1);
219 TestRepeat(5);
220
221 TestRepeatWithEmptyFilter(2);
222 TestRepeatWithEmptyFilter(3);
223
224 TestRepeatWithFilterForSuccessfulTests(3);
225
226 TestRepeatWithFilterForFailedTests(4);
227
228 // It would be nice to verify that the tests indeed loop forever
229 // when GTEST_FLAG(repeat) is negative, but this test will be quite
230 // complicated to write. Since this flag is for interactive
231 // debugging only and doesn't affect the normal test result, such a
232 // test would be an overkill.
233
234 printf("PASS\n");
235 return 0;
236}
#define EXPECT_DEATH_IF_SUPPORTED(statement, regex)
#define INSTANTIATE_TEST_CASE_P(prefix, test_case_name, generator,...)
#define TEST_P(test_case_name, test_name)
#define GTEST_FLAG(name)
#define GTEST_DECLARE_string_(name)
#define GTEST_DECLARE_int32_(name)
#define EXPECT_EQ(val1, val2)
Definition gtest.h:1954
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
Definition gtest.h:2328
#define TEST(test_case_name, test_name)
Definition gtest.h:2275
#define GTEST_CHECK_INT_EQ_(expected, actual)
char ** argv
Environment * AddGlobalTestEnvironment(Environment *env)
Definition gtest.h:1391
internal::ParamGenerator< T > Range(T start, T end, IncrementT step)
GTEST_API_ void InitGoogleTest(int *argc, char **argv)
Definition gtest.cc:5787