1 // Copyright 2008, Google Inc.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
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
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.
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.
30 // Author: wan@google.com (Zhanyong Wan)
32 // Tests the --gtest_repeat=number flag.
36 #include "gtest/gtest.h"
38 // Indicates that this translation unit is part of Google Test's
39 // implementation. It must come before gtest-internal-inl.h is
40 // included, or there will be a compiler error. This trick is to
41 // prevent a user from accidentally including gtest-internal-inl.h in
43 #define GTEST_IMPLEMENTATION_ 1
44 #include "src/gtest-internal-inl.h"
45 #undef GTEST_IMPLEMENTATION_
49 GTEST_DECLARE_string_(death_test_style
);
50 GTEST_DECLARE_string_(filter
);
51 GTEST_DECLARE_int32_(repeat
);
53 } // namespace testing
55 using testing::GTEST_FLAG(death_test_style
);
56 using testing::GTEST_FLAG(filter
);
57 using testing::GTEST_FLAG(repeat
);
61 // We need this when we are testing Google Test itself and therefore
62 // cannot use Google Test assertions.
63 #define GTEST_CHECK_INT_EQ_(expected, actual) \
65 const int expected_val = (expected);\
66 const int actual_val = (actual);\
67 if (::testing::internal::IsTrue(expected_val != actual_val)) {\
68 ::std::cout << "Value of: " #actual "\n"\
69 << " Actual: " << actual_val << "\n"\
70 << "Expected: " #expected "\n"\
71 << "Which is: " << expected_val << "\n";\
72 ::testing::internal::posix::Abort();\
74 } while (::testing::internal::AlwaysFalse())
77 // Used for verifying that global environment set-up and tear-down are
78 // inside the gtest_repeat loop.
80 int g_environment_set_up_count
= 0;
81 int g_environment_tear_down_count
= 0;
83 class MyEnvironment
: public testing::Environment
{
86 virtual void SetUp() { g_environment_set_up_count
++; }
87 virtual void TearDown() { g_environment_tear_down_count
++; }
90 // A test that should fail.
92 int g_should_fail_count
= 0;
94 TEST(FooTest
, ShouldFail
) {
95 g_should_fail_count
++;
96 EXPECT_EQ(0, 1) << "Expected failure.";
99 // A test that should pass.
101 int g_should_pass_count
= 0;
103 TEST(FooTest
, ShouldPass
) {
104 g_should_pass_count
++;
107 // A test that contains a thread-safe death test and a fast death
108 // test. It should pass.
110 int g_death_test_count
= 0;
112 TEST(BarDeathTest
, ThreadSafeAndFast
) {
113 g_death_test_count
++;
115 GTEST_FLAG(death_test_style
) = "threadsafe";
116 EXPECT_DEATH_IF_SUPPORTED(::testing::internal::posix::Abort(), "");
118 GTEST_FLAG(death_test_style
) = "fast";
119 EXPECT_DEATH_IF_SUPPORTED(::testing::internal::posix::Abort(), "");
122 #if GTEST_HAS_PARAM_TEST
123 int g_param_test_count
= 0;
125 const int kNumberOfParamTests
= 10;
127 class MyParamTest
: public testing::TestWithParam
<int> {};
129 TEST_P(MyParamTest
, ShouldPass
) {
130 // TODO(vladl@google.com): Make parameter value checking robust
131 // WRT order of tests.
132 GTEST_CHECK_INT_EQ_(g_param_test_count
% kNumberOfParamTests
, GetParam());
133 g_param_test_count
++;
135 INSTANTIATE_TEST_CASE_P(MyParamSequence
,
137 testing::Range(0, kNumberOfParamTests
));
138 #endif // GTEST_HAS_PARAM_TEST
140 // Resets the count for each test.
142 g_environment_set_up_count
= 0;
143 g_environment_tear_down_count
= 0;
144 g_should_fail_count
= 0;
145 g_should_pass_count
= 0;
146 g_death_test_count
= 0;
147 #if GTEST_HAS_PARAM_TEST
148 g_param_test_count
= 0;
149 #endif // GTEST_HAS_PARAM_TEST
152 // Checks that the count for each test is expected.
153 void CheckCounts(int expected
) {
154 GTEST_CHECK_INT_EQ_(expected
, g_environment_set_up_count
);
155 GTEST_CHECK_INT_EQ_(expected
, g_environment_tear_down_count
);
156 GTEST_CHECK_INT_EQ_(expected
, g_should_fail_count
);
157 GTEST_CHECK_INT_EQ_(expected
, g_should_pass_count
);
158 GTEST_CHECK_INT_EQ_(expected
, g_death_test_count
);
159 #if GTEST_HAS_PARAM_TEST
160 GTEST_CHECK_INT_EQ_(expected
* kNumberOfParamTests
, g_param_test_count
);
161 #endif // GTEST_HAS_PARAM_TEST
164 // Tests the behavior of Google Test when --gtest_repeat is not specified.
165 void TestRepeatUnspecified() {
167 GTEST_CHECK_INT_EQ_(1, RUN_ALL_TESTS());
171 // Tests the behavior of Google Test when --gtest_repeat has the given value.
172 void TestRepeat(int repeat
) {
173 GTEST_FLAG(repeat
) = repeat
;
176 GTEST_CHECK_INT_EQ_(repeat
> 0 ? 1 : 0, RUN_ALL_TESTS());
180 // Tests using --gtest_repeat when --gtest_filter specifies an empty
182 void TestRepeatWithEmptyFilter(int repeat
) {
183 GTEST_FLAG(repeat
) = repeat
;
184 GTEST_FLAG(filter
) = "None";
187 GTEST_CHECK_INT_EQ_(0, RUN_ALL_TESTS());
191 // Tests using --gtest_repeat when --gtest_filter specifies a set of
193 void TestRepeatWithFilterForSuccessfulTests(int repeat
) {
194 GTEST_FLAG(repeat
) = repeat
;
195 GTEST_FLAG(filter
) = "*-*ShouldFail";
198 GTEST_CHECK_INT_EQ_(0, RUN_ALL_TESTS());
199 GTEST_CHECK_INT_EQ_(repeat
, g_environment_set_up_count
);
200 GTEST_CHECK_INT_EQ_(repeat
, g_environment_tear_down_count
);
201 GTEST_CHECK_INT_EQ_(0, g_should_fail_count
);
202 GTEST_CHECK_INT_EQ_(repeat
, g_should_pass_count
);
203 GTEST_CHECK_INT_EQ_(repeat
, g_death_test_count
);
204 #if GTEST_HAS_PARAM_TEST
205 GTEST_CHECK_INT_EQ_(repeat
* kNumberOfParamTests
, g_param_test_count
);
206 #endif // GTEST_HAS_PARAM_TEST
209 // Tests using --gtest_repeat when --gtest_filter specifies a set of
211 void TestRepeatWithFilterForFailedTests(int repeat
) {
212 GTEST_FLAG(repeat
) = repeat
;
213 GTEST_FLAG(filter
) = "*ShouldFail";
216 GTEST_CHECK_INT_EQ_(1, RUN_ALL_TESTS());
217 GTEST_CHECK_INT_EQ_(repeat
, g_environment_set_up_count
);
218 GTEST_CHECK_INT_EQ_(repeat
, g_environment_tear_down_count
);
219 GTEST_CHECK_INT_EQ_(repeat
, g_should_fail_count
);
220 GTEST_CHECK_INT_EQ_(0, g_should_pass_count
);
221 GTEST_CHECK_INT_EQ_(0, g_death_test_count
);
222 #if GTEST_HAS_PARAM_TEST
223 GTEST_CHECK_INT_EQ_(0, g_param_test_count
);
224 #endif // GTEST_HAS_PARAM_TEST
229 int main(int argc
, char **argv
) {
230 testing::InitGoogleTest(&argc
, argv
);
231 testing::AddGlobalTestEnvironment(new MyEnvironment
);
233 TestRepeatUnspecified();
238 TestRepeatWithEmptyFilter(2);
239 TestRepeatWithEmptyFilter(3);
241 TestRepeatWithFilterForSuccessfulTests(3);
243 TestRepeatWithFilterForFailedTests(4);
245 // It would be nice to verify that the tests indeed loop forever
246 // when GTEST_FLAG(repeat) is negative, but this test will be quite
247 // complicated to write. Since this flag is for interactive
248 // debugging only and doesn't affect the normal test result, such a
249 // test would be an overkill.