tesseract v5.3.3.20231005
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
31// Tests the --gtest_repeat=number flag.
32
33#include <stdlib.h>
34#include <iostream>
35#include "gtest/gtest.h"
37
38namespace testing {
39
40GTEST_DECLARE_string_(death_test_style);
43
44} // namespace testing
45
46using testing::GTEST_FLAG(death_test_style);
47using testing::GTEST_FLAG(filter);
48using testing::GTEST_FLAG(repeat);
49
50namespace {
51
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) \
55 do {\
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();\
64 }\
65 } while (::testing::internal::AlwaysFalse())
66
67
68// Used for verifying that global environment set-up and tear-down are
69// inside the --gtest_repeat loop.
70
71int g_environment_set_up_count = 0;
72int g_environment_tear_down_count = 0;
73
74class MyEnvironment : public testing::Environment {
75 public:
76 MyEnvironment() {}
77 void SetUp() override { g_environment_set_up_count++; }
78 void TearDown() override { g_environment_tear_down_count++; }
79};
80
81// A test that should fail.
82
83int g_should_fail_count = 0;
84
85TEST(FooTest, ShouldFail) {
86 g_should_fail_count++;
87 EXPECT_EQ(0, 1) << "Expected failure.";
88}
89
90// A test that should pass.
91
92int g_should_pass_count = 0;
93
94TEST(FooTest, ShouldPass) {
95 g_should_pass_count++;
96}
97
98// A test that contains a thread-safe death test and a fast death
99// test. It should pass.
100
101int g_death_test_count = 0;
102
103TEST(BarDeathTest, ThreadSafeAndFast) {
104 g_death_test_count++;
105
106 GTEST_FLAG(death_test_style) = "threadsafe";
108
109 GTEST_FLAG(death_test_style) = "fast";
111}
112
113int g_param_test_count = 0;
114
115const int kNumberOfParamTests = 10;
116
117class MyParamTest : public testing::TestWithParam<int> {};
118
119TEST_P(MyParamTest, ShouldPass) {
120 GTEST_CHECK_INT_EQ_(g_param_test_count % kNumberOfParamTests, GetParam());
121 g_param_test_count++;
122}
123INSTANTIATE_TEST_SUITE_P(MyParamSequence,
124 MyParamTest,
125 testing::Range(0, kNumberOfParamTests));
126
127// Resets the count for each test.
128void ResetCounts() {
129 g_environment_set_up_count = 0;
130 g_environment_tear_down_count = 0;
131 g_should_fail_count = 0;
132 g_should_pass_count = 0;
133 g_death_test_count = 0;
134 g_param_test_count = 0;
135}
136
137// Checks that the count for each test is expected.
138void CheckCounts(int expected) {
139 GTEST_CHECK_INT_EQ_(expected, g_environment_set_up_count);
140 GTEST_CHECK_INT_EQ_(expected, g_environment_tear_down_count);
141 GTEST_CHECK_INT_EQ_(expected, g_should_fail_count);
142 GTEST_CHECK_INT_EQ_(expected, g_should_pass_count);
143 GTEST_CHECK_INT_EQ_(expected, g_death_test_count);
144 GTEST_CHECK_INT_EQ_(expected * kNumberOfParamTests, g_param_test_count);
145}
146
147// Tests the behavior of Google Test when --gtest_repeat is not specified.
148void TestRepeatUnspecified() {
149 ResetCounts();
151 CheckCounts(1);
152}
153
154// Tests the behavior of Google Test when --gtest_repeat has the given value.
155void TestRepeat(int repeat) {
156 GTEST_FLAG(repeat) = repeat;
157
158 ResetCounts();
159 GTEST_CHECK_INT_EQ_(repeat > 0 ? 1 : 0, RUN_ALL_TESTS());
160 CheckCounts(repeat);
161}
162
163// Tests using --gtest_repeat when --gtest_filter specifies an empty
164// set of tests.
165void TestRepeatWithEmptyFilter(int repeat) {
166 GTEST_FLAG(repeat) = repeat;
167 GTEST_FLAG(filter) = "None";
168
169 ResetCounts();
171 CheckCounts(0);
172}
173
174// Tests using --gtest_repeat when --gtest_filter specifies a set of
175// successful tests.
176void TestRepeatWithFilterForSuccessfulTests(int repeat) {
177 GTEST_FLAG(repeat) = repeat;
178 GTEST_FLAG(filter) = "*-*ShouldFail";
179
180 ResetCounts();
182 GTEST_CHECK_INT_EQ_(repeat, g_environment_set_up_count);
183 GTEST_CHECK_INT_EQ_(repeat, g_environment_tear_down_count);
184 GTEST_CHECK_INT_EQ_(0, g_should_fail_count);
185 GTEST_CHECK_INT_EQ_(repeat, g_should_pass_count);
186 GTEST_CHECK_INT_EQ_(repeat, g_death_test_count);
187 GTEST_CHECK_INT_EQ_(repeat * kNumberOfParamTests, g_param_test_count);
188}
189
190// Tests using --gtest_repeat when --gtest_filter specifies a set of
191// failed tests.
192void TestRepeatWithFilterForFailedTests(int repeat) {
193 GTEST_FLAG(repeat) = repeat;
194 GTEST_FLAG(filter) = "*ShouldFail";
195
196 ResetCounts();
198 GTEST_CHECK_INT_EQ_(repeat, g_environment_set_up_count);
199 GTEST_CHECK_INT_EQ_(repeat, g_environment_tear_down_count);
200 GTEST_CHECK_INT_EQ_(repeat, g_should_fail_count);
201 GTEST_CHECK_INT_EQ_(0, g_should_pass_count);
202 GTEST_CHECK_INT_EQ_(0, g_death_test_count);
203 GTEST_CHECK_INT_EQ_(0, g_param_test_count);
204}
205
206} // namespace
207
208int main(int argc, char **argv) {
209 testing::InitGoogleTest(&argc, argv);
210
211 testing::AddGlobalTestEnvironment(new MyEnvironment);
212
213 TestRepeatUnspecified();
214 TestRepeat(0);
215 TestRepeat(1);
216 TestRepeat(5);
217
218 TestRepeatWithEmptyFilter(2);
219 TestRepeatWithEmptyFilter(3);
220
221 TestRepeatWithFilterForSuccessfulTests(3);
222
223 TestRepeatWithFilterForFailedTests(4);
224
225 // It would be nice to verify that the tests indeed loop forever
226 // when GTEST_FLAG(repeat) is negative, but this test will be quite
227 // complicated to write. Since this flag is for interactive
228 // debugging only and doesn't affect the normal test result, such a
229 // test would be an overkill.
230
231 printf("PASS\n");
232 return 0;
233}
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:2043
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
Definition: gtest.h:2489
#define TEST(test_suite_name, test_name)
Definition: gtest.h:2353
#define GTEST_FLAG(name)
Definition: gtest-port.h:2205
#define EXPECT_DEATH_IF_SUPPORTED(statement, regex)
#define TEST_P(test_suite_name, test_name)
#define INSTANTIATE_TEST_SUITE_P(prefix, test_suite_name,...)
int main(int argc, char **argv)
#define GTEST_CHECK_INT_EQ_(expected, actual)
Environment * AddGlobalTestEnvironment(Environment *env)
Definition: gtest.h:1493
GTEST_DECLARE_int32_(repeat)
GTEST_DECLARE_string_(death_test_style)
internal::ParamGenerator< T > Range(T start, T end, IncrementT step)
GTEST_API_ void InitGoogleTest(int *argc, char **argv)
Definition: gtest.cc:6660
virtual void TearDown()
Definition: gtest.h:1071
virtual void SetUp()
Definition: gtest.h:1068