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 GTEST_DECLARE_string_(death_test_style);
41 GTEST_DECLARE_string_(filter);
42 GTEST_DECLARE_int32_(repeat);
44 } // namespace testing
46 using testing::GTEST_FLAG(death_test_style);
47 using testing::GTEST_FLAG(filter);
48 using testing::GTEST_FLAG(repeat);
52 // We need this when we are testing Google Test itself and therefore
53 // cannot use Google Test assertions.
54 #define GTEST_CHECK_INT_EQ_(expected, actual) \
56 const int expected_val = (expected);\
57 const int actual_val = (actual);\
58 if (::testing::internal::IsTrue(expected_val != actual_val)) {\
59 ::std::cout << "Value of: " #actual "\n"\
60 << " Actual: " << actual_val << "\n"\
61 << "Expected: " #expected "\n"\
62 << "Which is: " << expected_val << "\n";\
63 ::testing::internal::posix::Abort();\
65 } while (::testing::internal::AlwaysFalse())
68 // Used for verifying that global environment set-up and tear-down are
69 // inside the --gtest_repeat loop.
71 int g_environment_set_up_count = 0;
72 int g_environment_tear_down_count = 0;
74 class MyEnvironment : public testing::Environment {
77 virtual void SetUp() { g_environment_set_up_count++; }
78 virtual void TearDown() { g_environment_tear_down_count++; }
81 // A test that should fail.
83 int g_should_fail_count = 0;
85 TEST(FooTest, ShouldFail) {
86 g_should_fail_count++;
87 EXPECT_EQ(0, 1) << "Expected failure.";
90 // A test that should pass.
92 int g_should_pass_count = 0;
94 TEST(FooTest, ShouldPass) {
95 g_should_pass_count++;
98 // A test that contains a thread-safe death test and a fast death
99 // test. It should pass.
101 int g_death_test_count = 0;
103 TEST(BarDeathTest, ThreadSafeAndFast) {
104 g_death_test_count++;
106 GTEST_FLAG(death_test_style) = "threadsafe";
107 EXPECT_DEATH_IF_SUPPORTED(::testing::internal::posix::Abort(), "");
109 GTEST_FLAG(death_test_style) = "fast";
110 EXPECT_DEATH_IF_SUPPORTED(::testing::internal::posix::Abort(), "");
113 int g_param_test_count = 0;
115 const int kNumberOfParamTests = 10;
117 class MyParamTest : public testing::TestWithParam<int> {};
119 TEST_P(MyParamTest, ShouldPass) {
120 // FIXME: Make parameter value checking robust WRT order of tests.
121 GTEST_CHECK_INT_EQ_(g_param_test_count % kNumberOfParamTests, GetParam());
122 g_param_test_count++;
124 INSTANTIATE_TEST_CASE_P(MyParamSequence,
126 testing::Range(0, kNumberOfParamTests));
128 // Resets the count for each test.
130 g_environment_set_up_count = 0;
131 g_environment_tear_down_count = 0;
132 g_should_fail_count = 0;
133 g_should_pass_count = 0;
134 g_death_test_count = 0;
135 g_param_test_count = 0;
138 // Checks that the count for each test is expected.
139 void CheckCounts(int expected) {
140 GTEST_CHECK_INT_EQ_(expected, g_environment_set_up_count);
141 GTEST_CHECK_INT_EQ_(expected, g_environment_tear_down_count);
142 GTEST_CHECK_INT_EQ_(expected, g_should_fail_count);
143 GTEST_CHECK_INT_EQ_(expected, g_should_pass_count);
144 GTEST_CHECK_INT_EQ_(expected, g_death_test_count);
145 GTEST_CHECK_INT_EQ_(expected * kNumberOfParamTests, g_param_test_count);
148 // Tests the behavior of Google Test when --gtest_repeat is not specified.
149 void TestRepeatUnspecified() {
151 GTEST_CHECK_INT_EQ_(1, RUN_ALL_TESTS());
155 // Tests the behavior of Google Test when --gtest_repeat has the given value.
156 void TestRepeat(int repeat) {
157 GTEST_FLAG(repeat) = repeat;
160 GTEST_CHECK_INT_EQ_(repeat > 0 ? 1 : 0, RUN_ALL_TESTS());
164 // Tests using --gtest_repeat when --gtest_filter specifies an empty
166 void TestRepeatWithEmptyFilter(int repeat) {
167 GTEST_FLAG(repeat) = repeat;
168 GTEST_FLAG(filter) = "None";
171 GTEST_CHECK_INT_EQ_(0, RUN_ALL_TESTS());
175 // Tests using --gtest_repeat when --gtest_filter specifies a set of
177 void TestRepeatWithFilterForSuccessfulTests(int repeat) {
178 GTEST_FLAG(repeat) = repeat;
179 GTEST_FLAG(filter) = "*-*ShouldFail";
182 GTEST_CHECK_INT_EQ_(0, RUN_ALL_TESTS());
183 GTEST_CHECK_INT_EQ_(repeat, g_environment_set_up_count);
184 GTEST_CHECK_INT_EQ_(repeat, g_environment_tear_down_count);
185 GTEST_CHECK_INT_EQ_(0, g_should_fail_count);
186 GTEST_CHECK_INT_EQ_(repeat, g_should_pass_count);
187 GTEST_CHECK_INT_EQ_(repeat, g_death_test_count);
188 GTEST_CHECK_INT_EQ_(repeat * kNumberOfParamTests, g_param_test_count);
191 // Tests using --gtest_repeat when --gtest_filter specifies a set of
193 void TestRepeatWithFilterForFailedTests(int repeat) {
194 GTEST_FLAG(repeat) = repeat;
195 GTEST_FLAG(filter) = "*ShouldFail";
198 GTEST_CHECK_INT_EQ_(1, 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_(repeat, g_should_fail_count);
202 GTEST_CHECK_INT_EQ_(0, g_should_pass_count);
203 GTEST_CHECK_INT_EQ_(0, g_death_test_count);
204 GTEST_CHECK_INT_EQ_(0, g_param_test_count);
209 int main(int argc, char **argv) {
210 testing::InitGoogleTest(&argc, argv);
212 testing::AddGlobalTestEnvironment(new MyEnvironment);
214 TestRepeatUnspecified();
219 TestRepeatWithEmptyFilter(2);
220 TestRepeatWithEmptyFilter(3);
222 TestRepeatWithFilterForSuccessfulTests(3);
224 TestRepeatWithFilterForFailedTests(4);
226 // It would be nice to verify that the tests indeed loop forever
227 // when GTEST_FLAG(repeat) is negative, but this test will be quite
228 // complicated to write. Since this flag is for interactive
229 // debugging only and doesn't affect the normal test result, such a
230 // test would be an overkill.