gtest_repeat_test.cc revision fbaaef999ba563838ebd00874ed8a1c01fbf286d
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>
37
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
42// his code.
43#define GTEST_IMPLEMENTATION_ 1
44#include "src/gtest-internal-inl.h"
45#undef GTEST_IMPLEMENTATION_
46
47namespace testing {
48
49GTEST_DECLARE_string_(death_test_style);
50GTEST_DECLARE_string_(filter);
51GTEST_DECLARE_int32_(repeat);
52
53}  // namespace testing
54
55using testing::GTEST_FLAG(death_test_style);
56using testing::GTEST_FLAG(filter);
57using testing::GTEST_FLAG(repeat);
58
59namespace {
60
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) \
64  do {\
65    const int expected_val = (expected);\
66    const int actual_val = (actual);\
67    if (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      abort();\
73    }\
74  } while(false)
75
76
77// Used for verifying that global environment set-up and tear-down are
78// inside the gtest_repeat loop.
79
80int g_environment_set_up_count = 0;
81int g_environment_tear_down_count = 0;
82
83class MyEnvironment : public testing::Environment {
84 public:
85  MyEnvironment() {}
86  virtual void SetUp() { g_environment_set_up_count++; }
87  virtual void TearDown() { g_environment_tear_down_count++; }
88};
89
90// A test that should fail.
91
92int g_should_fail_count = 0;
93
94TEST(FooTest, ShouldFail) {
95  g_should_fail_count++;
96  EXPECT_EQ(0, 1) << "Expected failure.";
97}
98
99// A test that should pass.
100
101int g_should_pass_count = 0;
102
103TEST(FooTest, ShouldPass) {
104  g_should_pass_count++;
105}
106
107// A test that contains a thread-safe death test and a fast death
108// test.  It should pass.
109
110int g_death_test_count = 0;
111
112TEST(BarDeathTest, ThreadSafeAndFast) {
113  g_death_test_count++;
114
115#if GTEST_HAS_DEATH_TEST
116  GTEST_FLAG(death_test_style) = "threadsafe";
117  EXPECT_DEATH(abort(), "");
118
119  GTEST_FLAG(death_test_style) = "fast";
120  EXPECT_DEATH(abort(), "");
121#endif  // GTEST_HAS_DEATH_TEST
122}
123
124#if GTEST_HAS_PARAM_TEST
125int g_param_test_count = 0;
126
127const int kNumberOfParamTests = 10;
128
129class MyParamTest : public testing::TestWithParam<int> {};
130
131TEST_P(MyParamTest, ShouldPass) {
132  // TODO(vladl@google.com): Make parameter value checking robust
133  //                         WRT order of tests.
134  GTEST_CHECK_INT_EQ_(g_param_test_count % kNumberOfParamTests, GetParam());
135  g_param_test_count++;
136}
137INSTANTIATE_TEST_CASE_P(MyParamSequence,
138                        MyParamTest,
139                        testing::Range(0, kNumberOfParamTests));
140#endif  // GTEST_HAS_PARAM_TEST
141
142// Resets the count for each test.
143void ResetCounts() {
144  g_environment_set_up_count = 0;
145  g_environment_tear_down_count = 0;
146  g_should_fail_count = 0;
147  g_should_pass_count = 0;
148  g_death_test_count = 0;
149#if GTEST_HAS_PARAM_TEST
150  g_param_test_count = 0;
151#endif  // GTEST_HAS_PARAM_TEST
152}
153
154// Checks that the count for each test is expected.
155void CheckCounts(int expected) {
156  GTEST_CHECK_INT_EQ_(expected, g_environment_set_up_count);
157  GTEST_CHECK_INT_EQ_(expected, g_environment_tear_down_count);
158  GTEST_CHECK_INT_EQ_(expected, g_should_fail_count);
159  GTEST_CHECK_INT_EQ_(expected, g_should_pass_count);
160  GTEST_CHECK_INT_EQ_(expected, g_death_test_count);
161#if GTEST_HAS_PARAM_TEST
162  GTEST_CHECK_INT_EQ_(expected * kNumberOfParamTests, g_param_test_count);
163#endif  // GTEST_HAS_PARAM_TEST
164}
165
166// Tests the behavior of Google Test when --gtest_repeat is not specified.
167void TestRepeatUnspecified() {
168  ResetCounts();
169  GTEST_CHECK_INT_EQ_(1, RUN_ALL_TESTS());
170  CheckCounts(1);
171}
172
173// Tests the behavior of Google Test when --gtest_repeat has the given value.
174void TestRepeat(int repeat) {
175  GTEST_FLAG(repeat) = repeat;
176
177  ResetCounts();
178  GTEST_CHECK_INT_EQ_(repeat > 0 ? 1 : 0, RUN_ALL_TESTS());
179  CheckCounts(repeat);
180}
181
182// Tests using --gtest_repeat when --gtest_filter specifies an empty
183// set of tests.
184void TestRepeatWithEmptyFilter(int repeat) {
185  GTEST_FLAG(repeat) = repeat;
186  GTEST_FLAG(filter) = "None";
187
188  ResetCounts();
189  GTEST_CHECK_INT_EQ_(0, RUN_ALL_TESTS());
190  CheckCounts(0);
191}
192
193// Tests using --gtest_repeat when --gtest_filter specifies a set of
194// successful tests.
195void TestRepeatWithFilterForSuccessfulTests(int repeat) {
196  GTEST_FLAG(repeat) = repeat;
197  GTEST_FLAG(filter) = "*-*ShouldFail";
198
199  ResetCounts();
200  GTEST_CHECK_INT_EQ_(0, RUN_ALL_TESTS());
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_(0, g_should_fail_count);
204  GTEST_CHECK_INT_EQ_(repeat, g_should_pass_count);
205  GTEST_CHECK_INT_EQ_(repeat, g_death_test_count);
206#if GTEST_HAS_PARAM_TEST
207  GTEST_CHECK_INT_EQ_(repeat * kNumberOfParamTests, g_param_test_count);
208#endif  // GTEST_HAS_PARAM_TEST
209}
210
211// Tests using --gtest_repeat when --gtest_filter specifies a set of
212// failed tests.
213void TestRepeatWithFilterForFailedTests(int repeat) {
214  GTEST_FLAG(repeat) = repeat;
215  GTEST_FLAG(filter) = "*ShouldFail";
216
217  ResetCounts();
218  GTEST_CHECK_INT_EQ_(1, RUN_ALL_TESTS());
219  GTEST_CHECK_INT_EQ_(repeat, g_environment_set_up_count);
220  GTEST_CHECK_INT_EQ_(repeat, g_environment_tear_down_count);
221  GTEST_CHECK_INT_EQ_(repeat, g_should_fail_count);
222  GTEST_CHECK_INT_EQ_(0, g_should_pass_count);
223  GTEST_CHECK_INT_EQ_(0, g_death_test_count);
224#if GTEST_HAS_PARAM_TEST
225  GTEST_CHECK_INT_EQ_(0, g_param_test_count);
226#endif  // GTEST_HAS_PARAM_TEST
227}
228
229}  // namespace
230
231int main(int argc, char **argv) {
232  testing::InitGoogleTest(&argc, argv);
233  testing::AddGlobalTestEnvironment(new MyEnvironment);
234
235  TestRepeatUnspecified();
236  TestRepeat(0);
237  TestRepeat(1);
238  TestRepeat(5);
239
240  TestRepeatWithEmptyFilter(2);
241  TestRepeatWithEmptyFilter(3);
242
243  TestRepeatWithFilterForSuccessfulTests(3);
244
245  TestRepeatWithFilterForFailedTests(4);
246
247  // It would be nice to verify that the tests indeed loop forever
248  // when GTEST_FLAG(repeat) is negative, but this test will be quite
249  // complicated to write.  Since this flag is for interactive
250  // debugging only and doesn't affect the normal test result, such a
251  // test would be an overkill.
252
253  printf("PASS\n");
254  return 0;
255}
256