Sacado Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 // Tests the --gtest_repeat=number flag.
31 
32 #include <stdlib.h>
33 
34 #include <iostream>
35 
36 #include "gtest/gtest.h"
37 #include "src/gtest-internal-inl.h"
38 
39 namespace {
40 
41 // We need this when we are testing Google Test itself and therefore
42 // cannot use Google Test assertions.
43 #define GTEST_CHECK_INT_EQ_(expected, actual) \
44  do { \
45  const int expected_val = (expected); \
46  const int actual_val = (actual); \
47  if (::testing::internal::IsTrue(expected_val != actual_val)) { \
48  ::std::cout << "Value of: " #actual "\n" \
49  << " Actual: " << actual_val << "\n" \
50  << "Expected: " #expected "\n" \
51  << "Which is: " << expected_val << "\n"; \
52  ::testing::internal::posix::Abort(); \
53  } \
54  } while (::testing::internal::AlwaysFalse())
55 
56 // Used for verifying that global environment set-up and tear-down are
57 // inside the --gtest_repeat loop.
58 
59 int g_environment_set_up_count = 0;
60 int g_environment_tear_down_count = 0;
61 
62 class MyEnvironment : public testing::Environment {
63  public:
64  void SetUp() override { g_environment_set_up_count++; }
65  void TearDown() override { g_environment_tear_down_count++; }
66 };
67 
68 // A test that should fail.
69 
70 int g_should_fail_count = 0;
71 
72 TEST(FooTest, ShouldFail) {
73  g_should_fail_count++;
74  EXPECT_EQ(0, 1) << "Expected failure.";
75 }
76 
77 // A test that should pass.
78 
79 int g_should_pass_count = 0;
80 
81 TEST(FooTest, ShouldPass) { g_should_pass_count++; }
82 
83 // A test that contains a thread-safe death test and a fast death
84 // test. It should pass.
85 
86 int g_death_test_count = 0;
87 
88 TEST(BarDeathTest, ThreadSafeAndFast) {
89  g_death_test_count++;
90 
91  GTEST_FLAG_SET(death_test_style, "threadsafe");
93 
94  GTEST_FLAG_SET(death_test_style, "fast");
96 }
97 
98 int g_param_test_count = 0;
99 
100 const int kNumberOfParamTests = 10;
101 
102 class MyParamTest : public testing::TestWithParam<int> {};
103 
104 TEST_P(MyParamTest, ShouldPass) {
105  GTEST_CHECK_INT_EQ_(g_param_test_count % kNumberOfParamTests, GetParam());
106  g_param_test_count++;
107 }
108 INSTANTIATE_TEST_SUITE_P(MyParamSequence, MyParamTest,
109  testing::Range(0, kNumberOfParamTests));
110 
111 // Resets the count for each test.
112 void ResetCounts() {
113  g_environment_set_up_count = 0;
114  g_environment_tear_down_count = 0;
115  g_should_fail_count = 0;
116  g_should_pass_count = 0;
117  g_death_test_count = 0;
118  g_param_test_count = 0;
119  testing::AddGlobalTestEnvironment(new MyEnvironment);
120 }
121 
122 // Checks that the count for each test is expected.
123 void CheckCounts(int expected) {
124  GTEST_CHECK_INT_EQ_(expected, g_environment_set_up_count);
125  GTEST_CHECK_INT_EQ_(expected, g_environment_tear_down_count);
126  GTEST_CHECK_INT_EQ_(expected, g_should_fail_count);
127  GTEST_CHECK_INT_EQ_(expected, g_should_pass_count);
128  GTEST_CHECK_INT_EQ_(expected, g_death_test_count);
129  GTEST_CHECK_INT_EQ_(expected * kNumberOfParamTests, g_param_test_count);
130 }
131 
132 // Tests the behavior of Google Test when --gtest_repeat is not specified.
133 void TestRepeatUnspecified() {
134  ResetCounts();
136  CheckCounts(1);
137 }
138 
139 // Tests the behavior of Google Test when --gtest_repeat has the given value.
140 void TestRepeat(int repeat) {
141  GTEST_FLAG_SET(repeat, repeat);
142  GTEST_FLAG_SET(recreate_environments_when_repeating, true);
143 
144  ResetCounts();
145  GTEST_CHECK_INT_EQ_(repeat > 0 ? 1 : 0, RUN_ALL_TESTS());
146  CheckCounts(repeat);
147 }
148 
149 // Tests using --gtest_repeat when --gtest_filter specifies an empty
150 // set of tests.
151 void TestRepeatWithEmptyFilter(int repeat) {
152  GTEST_FLAG_SET(repeat, repeat);
153  GTEST_FLAG_SET(recreate_environments_when_repeating, true);
154  GTEST_FLAG_SET(filter, "None");
155 
156  ResetCounts();
158  CheckCounts(0);
159 }
160 
161 // Tests using --gtest_repeat when --gtest_filter specifies a set of
162 // successful tests.
163 void TestRepeatWithFilterForSuccessfulTests(int repeat) {
164  GTEST_FLAG_SET(repeat, repeat);
165  GTEST_FLAG_SET(recreate_environments_when_repeating, true);
166  GTEST_FLAG_SET(filter, "*-*ShouldFail");
167 
168  ResetCounts();
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);
176 }
177 
178 // Tests using --gtest_repeat when --gtest_filter specifies a set of
179 // failed tests.
180 void TestRepeatWithFilterForFailedTests(int repeat) {
181  GTEST_FLAG_SET(repeat, repeat);
182  GTEST_FLAG_SET(recreate_environments_when_repeating, true);
183  GTEST_FLAG_SET(filter, "*ShouldFail");
184 
185  ResetCounts();
187  GTEST_CHECK_INT_EQ_(repeat, g_environment_set_up_count);
188  GTEST_CHECK_INT_EQ_(repeat, g_environment_tear_down_count);
189  GTEST_CHECK_INT_EQ_(repeat, g_should_fail_count);
190  GTEST_CHECK_INT_EQ_(0, g_should_pass_count);
191  GTEST_CHECK_INT_EQ_(0, g_death_test_count);
192  GTEST_CHECK_INT_EQ_(0, g_param_test_count);
193 }
194 
195 } // namespace
196 
197 int main(int argc, char **argv) {
198  testing::InitGoogleTest(&argc, argv);
199 
200  TestRepeatUnspecified();
201  TestRepeat(0);
202  TestRepeat(1);
203  TestRepeat(5);
204 
205  TestRepeatWithEmptyFilter(2);
206  TestRepeatWithEmptyFilter(3);
207 
208  TestRepeatWithFilterForSuccessfulTests(3);
209 
210  TestRepeatWithFilterForFailedTests(4);
211 
212  // It would be nice to verify that the tests indeed loop forever
213  // when GTEST_FLAG(repeat) is negative, but this test will be quite
214  // complicated to write. Since this flag is for interactive
215  // debugging only and doesn't affect the normal test result, such a
216  // test would be an overkill.
217 
218  printf("PASS\n");
219  return 0;
220 }
#define EXPECT_DEATH_IF_SUPPORTED(statement, regex)
Environment * AddGlobalTestEnvironment(Environment *env)
Definition: gtest.h:1345
virtual void SetUp()
Definition: gtest.h:899
#define GTEST_CHECK_INT_EQ_(expected, actual)
#define TEST(test_suite_name, test_name)
Definition: gtest.h:2192
internal::ParamGenerator< T > Range(T start, T end, IncrementT step)
int main()
Definition: ad_example.cpp:171
#define GTEST_FLAG_SET(name, value)
Definition: gtest-port.h:2343
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1884
#define TEST_P(test_suite_name, test_name)
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
Definition: gtest.h:2334
GTEST_API_ void InitGoogleTest(int *argc, char **argv)
Definition: gtest.cc:6885
#define INSTANTIATE_TEST_SUITE_P(prefix, test_suite_name,...)
virtual void TearDown()
Definition: gtest.h:902
static ExpectedAnswer expected[4]