gtest_repeat_test.cc 7.01 KB
Newer Older
shiqian's avatar
shiqian committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// Copyright 2008, Google Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
//     * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Gennadiy Civil's avatar
 
Gennadiy Civil committed
29

shiqian's avatar
shiqian committed
30
31
32
33
34

// Tests the --gtest_repeat=number flag.

#include <stdlib.h>
#include <iostream>
35
#include "gtest/gtest.h"
shiqian's avatar
shiqian committed
36
37
38
39
#include "src/gtest-internal-inl.h"

namespace {

40
41
// We need this when we are testing Google Test itself and therefore
// cannot use Google Test assertions.
shiqian's avatar
shiqian committed
42
43
44
45
#define GTEST_CHECK_INT_EQ_(expected, actual) \
  do {\
    const int expected_val = (expected);\
    const int actual_val = (actual);\
46
    if (::testing::internal::IsTrue(expected_val != actual_val)) {\
shiqian's avatar
shiqian committed
47
48
49
50
      ::std::cout << "Value of: " #actual "\n"\
                  << "  Actual: " << actual_val << "\n"\
                  << "Expected: " #expected "\n"\
                  << "Which is: " << expected_val << "\n";\
51
      ::testing::internal::posix::Abort();\
shiqian's avatar
shiqian committed
52
    }\
53
  } while (::testing::internal::AlwaysFalse())
shiqian's avatar
shiqian committed
54
55
56


// Used for verifying that global environment set-up and tear-down are
57
// inside the --gtest_repeat loop.
shiqian's avatar
shiqian committed
58
59
60
61
62
63
64

int g_environment_set_up_count = 0;
int g_environment_tear_down_count = 0;

class MyEnvironment : public testing::Environment {
 public:
  MyEnvironment() {}
Abseil Team's avatar
Abseil Team committed
65
66
  void SetUp() override { g_environment_set_up_count++; }
  void TearDown() override { g_environment_tear_down_count++; }
shiqian's avatar
shiqian committed
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
};

// A test that should fail.

int g_should_fail_count = 0;

TEST(FooTest, ShouldFail) {
  g_should_fail_count++;
  EXPECT_EQ(0, 1) << "Expected failure.";
}

// A test that should pass.

int g_should_pass_count = 0;

TEST(FooTest, ShouldPass) {
  g_should_pass_count++;
}

// A test that contains a thread-safe death test and a fast death
// test.  It should pass.

int g_death_test_count = 0;

TEST(BarDeathTest, ThreadSafeAndFast) {
  g_death_test_count++;

Abseil Team's avatar
Abseil Team committed
94
  GTEST_FLAG_SET(death_test_style, "threadsafe");
95
  EXPECT_DEATH_IF_SUPPORTED(::testing::internal::posix::Abort(), "");
shiqian's avatar
shiqian committed
96

Abseil Team's avatar
Abseil Team committed
97
  GTEST_FLAG_SET(death_test_style, "fast");
98
  EXPECT_DEATH_IF_SUPPORTED(::testing::internal::posix::Abort(), "");
shiqian's avatar
shiqian committed
99
100
}

101
102
103
104
105
106
107
108
109
110
int g_param_test_count = 0;

const int kNumberOfParamTests = 10;

class MyParamTest : public testing::TestWithParam<int> {};

TEST_P(MyParamTest, ShouldPass) {
  GTEST_CHECK_INT_EQ_(g_param_test_count % kNumberOfParamTests, GetParam());
  g_param_test_count++;
}
misterg's avatar
misterg committed
111
112
113
INSTANTIATE_TEST_SUITE_P(MyParamSequence,
                         MyParamTest,
                         testing::Range(0, kNumberOfParamTests));
114

shiqian's avatar
shiqian committed
115
116
117
118
119
120
121
// Resets the count for each test.
void ResetCounts() {
  g_environment_set_up_count = 0;
  g_environment_tear_down_count = 0;
  g_should_fail_count = 0;
  g_should_pass_count = 0;
  g_death_test_count = 0;
122
  g_param_test_count = 0;
shiqian's avatar
shiqian committed
123
124
125
126
127
128
129
130
131
}

// Checks that the count for each test is expected.
void CheckCounts(int expected) {
  GTEST_CHECK_INT_EQ_(expected, g_environment_set_up_count);
  GTEST_CHECK_INT_EQ_(expected, g_environment_tear_down_count);
  GTEST_CHECK_INT_EQ_(expected, g_should_fail_count);
  GTEST_CHECK_INT_EQ_(expected, g_should_pass_count);
  GTEST_CHECK_INT_EQ_(expected, g_death_test_count);
132
  GTEST_CHECK_INT_EQ_(expected * kNumberOfParamTests, g_param_test_count);
shiqian's avatar
shiqian committed
133
134
135
136
137
138
139
140
141
142
143
}

// Tests the behavior of Google Test when --gtest_repeat is not specified.
void TestRepeatUnspecified() {
  ResetCounts();
  GTEST_CHECK_INT_EQ_(1, RUN_ALL_TESTS());
  CheckCounts(1);
}

// Tests the behavior of Google Test when --gtest_repeat has the given value.
void TestRepeat(int repeat) {
Abseil Team's avatar
Abseil Team committed
144
  GTEST_FLAG_SET(repeat, repeat);
shiqian's avatar
shiqian committed
145
146
147
148
149
150
151
152
153

  ResetCounts();
  GTEST_CHECK_INT_EQ_(repeat > 0 ? 1 : 0, RUN_ALL_TESTS());
  CheckCounts(repeat);
}

// Tests using --gtest_repeat when --gtest_filter specifies an empty
// set of tests.
void TestRepeatWithEmptyFilter(int repeat) {
Abseil Team's avatar
Abseil Team committed
154
155
  GTEST_FLAG_SET(repeat, repeat);
  GTEST_FLAG_SET(filter, "None");
shiqian's avatar
shiqian committed
156
157
158
159
160
161
162
163
164

  ResetCounts();
  GTEST_CHECK_INT_EQ_(0, RUN_ALL_TESTS());
  CheckCounts(0);
}

// Tests using --gtest_repeat when --gtest_filter specifies a set of
// successful tests.
void TestRepeatWithFilterForSuccessfulTests(int repeat) {
Abseil Team's avatar
Abseil Team committed
165
166
  GTEST_FLAG_SET(repeat, repeat);
  GTEST_FLAG_SET(filter, "*-*ShouldFail");
shiqian's avatar
shiqian committed
167
168
169
170
171
172
173
174

  ResetCounts();
  GTEST_CHECK_INT_EQ_(0, RUN_ALL_TESTS());
  GTEST_CHECK_INT_EQ_(repeat, g_environment_set_up_count);
  GTEST_CHECK_INT_EQ_(repeat, g_environment_tear_down_count);
  GTEST_CHECK_INT_EQ_(0, g_should_fail_count);
  GTEST_CHECK_INT_EQ_(repeat, g_should_pass_count);
  GTEST_CHECK_INT_EQ_(repeat, g_death_test_count);
175
  GTEST_CHECK_INT_EQ_(repeat * kNumberOfParamTests, g_param_test_count);
shiqian's avatar
shiqian committed
176
177
178
179
180
}

// Tests using --gtest_repeat when --gtest_filter specifies a set of
// failed tests.
void TestRepeatWithFilterForFailedTests(int repeat) {
Abseil Team's avatar
Abseil Team committed
181
182
  GTEST_FLAG_SET(repeat, repeat);
  GTEST_FLAG_SET(filter, "*ShouldFail");
shiqian's avatar
shiqian committed
183
184
185
186
187
188
189
190

  ResetCounts();
  GTEST_CHECK_INT_EQ_(1, RUN_ALL_TESTS());
  GTEST_CHECK_INT_EQ_(repeat, g_environment_set_up_count);
  GTEST_CHECK_INT_EQ_(repeat, g_environment_tear_down_count);
  GTEST_CHECK_INT_EQ_(repeat, g_should_fail_count);
  GTEST_CHECK_INT_EQ_(0, g_should_pass_count);
  GTEST_CHECK_INT_EQ_(0, g_death_test_count);
191
  GTEST_CHECK_INT_EQ_(0, g_param_test_count);
shiqian's avatar
shiqian committed
192
193
194
195
196
197
}

}  // namespace

int main(int argc, char **argv) {
  testing::InitGoogleTest(&argc, argv);
198

shiqian's avatar
shiqian committed
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
  testing::AddGlobalTestEnvironment(new MyEnvironment);

  TestRepeatUnspecified();
  TestRepeat(0);
  TestRepeat(1);
  TestRepeat(5);

  TestRepeatWithEmptyFilter(2);
  TestRepeatWithEmptyFilter(3);

  TestRepeatWithFilterForSuccessfulTests(3);

  TestRepeatWithFilterForFailedTests(4);

  // It would be nice to verify that the tests indeed loop forever
  // when GTEST_FLAG(repeat) is negative, but this test will be quite
  // complicated to write.  Since this flag is for interactive
  // debugging only and doesn't affect the normal test result, such a
  // test would be an overkill.

  printf("PASS\n");
  return 0;
}