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.
31 // Tests the --gtest_repeat=number flag.
35 #include "gtest/gtest.h"
36 #include "src/gtest-internal-inl.h"
40 // We need this when we are testing Google Test itself and therefore
41 // cannot use Google Test assertions.
42 #define GTEST_CHECK_INT_EQ_(expected, actual) \
44 const int expected_val = (expected);\
45 const int actual_val = (actual);\
46 if (::testing::internal::IsTrue(expected_val != actual_val)) {\
47 ::std::cout << "Value of: " #actual "\n"\
48 << " Actual: " << actual_val << "\n"\
49 << "Expected: " #expected "\n"\
50 << "Which is: " << expected_val << "\n";\
51 ::testing::internal::posix::Abort();\
53 } while (::testing::internal::AlwaysFalse())
56 // Used for verifying that global environment set-up and tear-down are
57 // inside the --gtest_repeat loop.
59 int g_environment_set_up_count = 0;
60 int g_environment_tear_down_count = 0;
62 class MyEnvironment : public testing::Environment {
65 void SetUp() override { g_environment_set_up_count++; }
66 void TearDown() override { g_environment_tear_down_count++; }
69 // A test that should fail.
71 int g_should_fail_count = 0;
73 TEST(FooTest, ShouldFail) {
74 g_should_fail_count++;
75 EXPECT_EQ(0, 1) << "Expected failure.";
78 // A test that should pass.
80 int g_should_pass_count = 0;
82 TEST(FooTest, ShouldPass) {
83 g_should_pass_count++;
86 // A test that contains a thread-safe death test and a fast death
87 // test. It should pass.
89 int g_death_test_count = 0;
91 TEST(BarDeathTest, ThreadSafeAndFast) {
94 GTEST_FLAG_SET(death_test_style, "threadsafe");
95 EXPECT_DEATH_IF_SUPPORTED(::testing::internal::posix::Abort(), "");
97 GTEST_FLAG_SET(death_test_style, "fast");
98 EXPECT_DEATH_IF_SUPPORTED(::testing::internal::posix::Abort(), "");
101 int g_param_test_count = 0;
103 const int kNumberOfParamTests = 10;
105 class MyParamTest : public testing::TestWithParam<int> {};
107 TEST_P(MyParamTest, ShouldPass) {
108 GTEST_CHECK_INT_EQ_(g_param_test_count % kNumberOfParamTests, GetParam());
109 g_param_test_count++;
111 INSTANTIATE_TEST_SUITE_P(MyParamSequence,
113 testing::Range(0, kNumberOfParamTests));
115 // Resets the count for each test.
117 g_environment_set_up_count = 0;
118 g_environment_tear_down_count = 0;
119 g_should_fail_count = 0;
120 g_should_pass_count = 0;
121 g_death_test_count = 0;
122 g_param_test_count = 0;
125 // Checks that the count for each test is expected.
126 void CheckCounts(int expected) {
127 GTEST_CHECK_INT_EQ_(expected, g_environment_set_up_count);
128 GTEST_CHECK_INT_EQ_(expected, g_environment_tear_down_count);
129 GTEST_CHECK_INT_EQ_(expected, g_should_fail_count);
130 GTEST_CHECK_INT_EQ_(expected, g_should_pass_count);
131 GTEST_CHECK_INT_EQ_(expected, g_death_test_count);
132 GTEST_CHECK_INT_EQ_(expected * kNumberOfParamTests, g_param_test_count);
135 // Tests the behavior of Google Test when --gtest_repeat is not specified.
136 void TestRepeatUnspecified() {
138 GTEST_CHECK_INT_EQ_(1, RUN_ALL_TESTS());
142 // Tests the behavior of Google Test when --gtest_repeat has the given value.
143 void TestRepeat(int repeat) {
144 GTEST_FLAG_SET(repeat, repeat);
147 GTEST_CHECK_INT_EQ_(repeat > 0 ? 1 : 0, RUN_ALL_TESTS());
151 // Tests using --gtest_repeat when --gtest_filter specifies an empty
153 void TestRepeatWithEmptyFilter(int repeat) {
154 GTEST_FLAG_SET(repeat, repeat);
155 GTEST_FLAG_SET(filter, "None");
158 GTEST_CHECK_INT_EQ_(0, RUN_ALL_TESTS());
162 // Tests using --gtest_repeat when --gtest_filter specifies a set of
164 void TestRepeatWithFilterForSuccessfulTests(int repeat) {
165 GTEST_FLAG_SET(repeat, repeat);
166 GTEST_FLAG_SET(filter, "*-*ShouldFail");
169 GTEST_CHECK_INT_EQ_(0, RUN_ALL_TESTS());
170 GTEST_CHECK_INT_EQ_(repeat, g_environment_set_up_count);
171 GTEST_CHECK_INT_EQ_(repeat, g_environment_tear_down_count);
172 GTEST_CHECK_INT_EQ_(0, g_should_fail_count);
173 GTEST_CHECK_INT_EQ_(repeat, g_should_pass_count);
174 GTEST_CHECK_INT_EQ_(repeat, g_death_test_count);
175 GTEST_CHECK_INT_EQ_(repeat * kNumberOfParamTests, g_param_test_count);
178 // Tests using --gtest_repeat when --gtest_filter specifies a set of
180 void TestRepeatWithFilterForFailedTests(int repeat) {
181 GTEST_FLAG_SET(repeat, repeat);
182 GTEST_FLAG_SET(filter, "*ShouldFail");
185 GTEST_CHECK_INT_EQ_(1, RUN_ALL_TESTS());
186 GTEST_CHECK_INT_EQ_(repeat, g_environment_set_up_count);
187 GTEST_CHECK_INT_EQ_(repeat, g_environment_tear_down_count);
188 GTEST_CHECK_INT_EQ_(repeat, g_should_fail_count);
189 GTEST_CHECK_INT_EQ_(0, g_should_pass_count);
190 GTEST_CHECK_INT_EQ_(0, g_death_test_count);
191 GTEST_CHECK_INT_EQ_(0, g_param_test_count);
196 int main(int argc, char **argv) {
197 testing::InitGoogleTest(&argc, argv);
199 testing::AddGlobalTestEnvironment(new MyEnvironment);
201 TestRepeatUnspecified();
206 TestRepeatWithEmptyFilter(2);
207 TestRepeatWithEmptyFilter(3);
209 TestRepeatWithFilterForSuccessfulTests(3);
211 TestRepeatWithFilterForFailedTests(4);
213 // It would be nice to verify that the tests indeed loop forever
214 // when GTEST_FLAG(repeat) is negative, but this test will be quite
215 // complicated to write. Since this flag is for interactive
216 // debugging only and doesn't affect the normal test result, such a
217 // test would be an overkill.