media/webrtc/trunk/testing/gtest/test/gtest_repeat_test.cc

Wed, 31 Dec 2014 07:53:36 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:53:36 +0100
branch
TOR_BUG_3246
changeset 5
4ab42b5ab56c
permissions
-rw-r--r--

Correct small whitespace inconsistency, lost while renaming variables.

michael@0 1 // Copyright 2008, Google Inc.
michael@0 2 // All rights reserved.
michael@0 3 //
michael@0 4 // Redistribution and use in source and binary forms, with or without
michael@0 5 // modification, are permitted provided that the following conditions are
michael@0 6 // met:
michael@0 7 //
michael@0 8 // * Redistributions of source code must retain the above copyright
michael@0 9 // notice, this list of conditions and the following disclaimer.
michael@0 10 // * Redistributions in binary form must reproduce the above
michael@0 11 // copyright notice, this list of conditions and the following disclaimer
michael@0 12 // in the documentation and/or other materials provided with the
michael@0 13 // distribution.
michael@0 14 // * Neither the name of Google Inc. nor the names of its
michael@0 15 // contributors may be used to endorse or promote products derived from
michael@0 16 // this software without specific prior written permission.
michael@0 17 //
michael@0 18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
michael@0 19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
michael@0 20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
michael@0 21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
michael@0 22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
michael@0 23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
michael@0 24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
michael@0 25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
michael@0 26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
michael@0 27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
michael@0 28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
michael@0 29 //
michael@0 30 // Author: wan@google.com (Zhanyong Wan)
michael@0 31
michael@0 32 // Tests the --gtest_repeat=number flag.
michael@0 33
michael@0 34 #include <stdlib.h>
michael@0 35 #include <iostream>
michael@0 36 #include "gtest/gtest.h"
michael@0 37
michael@0 38 // Indicates that this translation unit is part of Google Test's
michael@0 39 // implementation. It must come before gtest-internal-inl.h is
michael@0 40 // included, or there will be a compiler error. This trick is to
michael@0 41 // prevent a user from accidentally including gtest-internal-inl.h in
michael@0 42 // his code.
michael@0 43 #define GTEST_IMPLEMENTATION_ 1
michael@0 44 #include "src/gtest-internal-inl.h"
michael@0 45 #undef GTEST_IMPLEMENTATION_
michael@0 46
michael@0 47 namespace testing {
michael@0 48
michael@0 49 GTEST_DECLARE_string_(death_test_style);
michael@0 50 GTEST_DECLARE_string_(filter);
michael@0 51 GTEST_DECLARE_int32_(repeat);
michael@0 52
michael@0 53 } // namespace testing
michael@0 54
michael@0 55 using testing::GTEST_FLAG(death_test_style);
michael@0 56 using testing::GTEST_FLAG(filter);
michael@0 57 using testing::GTEST_FLAG(repeat);
michael@0 58
michael@0 59 namespace {
michael@0 60
michael@0 61 // We need this when we are testing Google Test itself and therefore
michael@0 62 // cannot use Google Test assertions.
michael@0 63 #define GTEST_CHECK_INT_EQ_(expected, actual) \
michael@0 64 do {\
michael@0 65 const int expected_val = (expected);\
michael@0 66 const int actual_val = (actual);\
michael@0 67 if (::testing::internal::IsTrue(expected_val != actual_val)) {\
michael@0 68 ::std::cout << "Value of: " #actual "\n"\
michael@0 69 << " Actual: " << actual_val << "\n"\
michael@0 70 << "Expected: " #expected "\n"\
michael@0 71 << "Which is: " << expected_val << "\n";\
michael@0 72 ::testing::internal::posix::Abort();\
michael@0 73 }\
michael@0 74 } while (::testing::internal::AlwaysFalse())
michael@0 75
michael@0 76
michael@0 77 // Used for verifying that global environment set-up and tear-down are
michael@0 78 // inside the gtest_repeat loop.
michael@0 79
michael@0 80 int g_environment_set_up_count = 0;
michael@0 81 int g_environment_tear_down_count = 0;
michael@0 82
michael@0 83 class MyEnvironment : public testing::Environment {
michael@0 84 public:
michael@0 85 MyEnvironment() {}
michael@0 86 virtual void SetUp() { g_environment_set_up_count++; }
michael@0 87 virtual void TearDown() { g_environment_tear_down_count++; }
michael@0 88 };
michael@0 89
michael@0 90 // A test that should fail.
michael@0 91
michael@0 92 int g_should_fail_count = 0;
michael@0 93
michael@0 94 TEST(FooTest, ShouldFail) {
michael@0 95 g_should_fail_count++;
michael@0 96 EXPECT_EQ(0, 1) << "Expected failure.";
michael@0 97 }
michael@0 98
michael@0 99 // A test that should pass.
michael@0 100
michael@0 101 int g_should_pass_count = 0;
michael@0 102
michael@0 103 TEST(FooTest, ShouldPass) {
michael@0 104 g_should_pass_count++;
michael@0 105 }
michael@0 106
michael@0 107 // A test that contains a thread-safe death test and a fast death
michael@0 108 // test. It should pass.
michael@0 109
michael@0 110 int g_death_test_count = 0;
michael@0 111
michael@0 112 TEST(BarDeathTest, ThreadSafeAndFast) {
michael@0 113 g_death_test_count++;
michael@0 114
michael@0 115 GTEST_FLAG(death_test_style) = "threadsafe";
michael@0 116 EXPECT_DEATH_IF_SUPPORTED(::testing::internal::posix::Abort(), "");
michael@0 117
michael@0 118 GTEST_FLAG(death_test_style) = "fast";
michael@0 119 EXPECT_DEATH_IF_SUPPORTED(::testing::internal::posix::Abort(), "");
michael@0 120 }
michael@0 121
michael@0 122 #if GTEST_HAS_PARAM_TEST
michael@0 123 int g_param_test_count = 0;
michael@0 124
michael@0 125 const int kNumberOfParamTests = 10;
michael@0 126
michael@0 127 class MyParamTest : public testing::TestWithParam<int> {};
michael@0 128
michael@0 129 TEST_P(MyParamTest, ShouldPass) {
michael@0 130 // TODO(vladl@google.com): Make parameter value checking robust
michael@0 131 // WRT order of tests.
michael@0 132 GTEST_CHECK_INT_EQ_(g_param_test_count % kNumberOfParamTests, GetParam());
michael@0 133 g_param_test_count++;
michael@0 134 }
michael@0 135 INSTANTIATE_TEST_CASE_P(MyParamSequence,
michael@0 136 MyParamTest,
michael@0 137 testing::Range(0, kNumberOfParamTests));
michael@0 138 #endif // GTEST_HAS_PARAM_TEST
michael@0 139
michael@0 140 // Resets the count for each test.
michael@0 141 void ResetCounts() {
michael@0 142 g_environment_set_up_count = 0;
michael@0 143 g_environment_tear_down_count = 0;
michael@0 144 g_should_fail_count = 0;
michael@0 145 g_should_pass_count = 0;
michael@0 146 g_death_test_count = 0;
michael@0 147 #if GTEST_HAS_PARAM_TEST
michael@0 148 g_param_test_count = 0;
michael@0 149 #endif // GTEST_HAS_PARAM_TEST
michael@0 150 }
michael@0 151
michael@0 152 // Checks that the count for each test is expected.
michael@0 153 void CheckCounts(int expected) {
michael@0 154 GTEST_CHECK_INT_EQ_(expected, g_environment_set_up_count);
michael@0 155 GTEST_CHECK_INT_EQ_(expected, g_environment_tear_down_count);
michael@0 156 GTEST_CHECK_INT_EQ_(expected, g_should_fail_count);
michael@0 157 GTEST_CHECK_INT_EQ_(expected, g_should_pass_count);
michael@0 158 GTEST_CHECK_INT_EQ_(expected, g_death_test_count);
michael@0 159 #if GTEST_HAS_PARAM_TEST
michael@0 160 GTEST_CHECK_INT_EQ_(expected * kNumberOfParamTests, g_param_test_count);
michael@0 161 #endif // GTEST_HAS_PARAM_TEST
michael@0 162 }
michael@0 163
michael@0 164 // Tests the behavior of Google Test when --gtest_repeat is not specified.
michael@0 165 void TestRepeatUnspecified() {
michael@0 166 ResetCounts();
michael@0 167 GTEST_CHECK_INT_EQ_(1, RUN_ALL_TESTS());
michael@0 168 CheckCounts(1);
michael@0 169 }
michael@0 170
michael@0 171 // Tests the behavior of Google Test when --gtest_repeat has the given value.
michael@0 172 void TestRepeat(int repeat) {
michael@0 173 GTEST_FLAG(repeat) = repeat;
michael@0 174
michael@0 175 ResetCounts();
michael@0 176 GTEST_CHECK_INT_EQ_(repeat > 0 ? 1 : 0, RUN_ALL_TESTS());
michael@0 177 CheckCounts(repeat);
michael@0 178 }
michael@0 179
michael@0 180 // Tests using --gtest_repeat when --gtest_filter specifies an empty
michael@0 181 // set of tests.
michael@0 182 void TestRepeatWithEmptyFilter(int repeat) {
michael@0 183 GTEST_FLAG(repeat) = repeat;
michael@0 184 GTEST_FLAG(filter) = "None";
michael@0 185
michael@0 186 ResetCounts();
michael@0 187 GTEST_CHECK_INT_EQ_(0, RUN_ALL_TESTS());
michael@0 188 CheckCounts(0);
michael@0 189 }
michael@0 190
michael@0 191 // Tests using --gtest_repeat when --gtest_filter specifies a set of
michael@0 192 // successful tests.
michael@0 193 void TestRepeatWithFilterForSuccessfulTests(int repeat) {
michael@0 194 GTEST_FLAG(repeat) = repeat;
michael@0 195 GTEST_FLAG(filter) = "*-*ShouldFail";
michael@0 196
michael@0 197 ResetCounts();
michael@0 198 GTEST_CHECK_INT_EQ_(0, RUN_ALL_TESTS());
michael@0 199 GTEST_CHECK_INT_EQ_(repeat, g_environment_set_up_count);
michael@0 200 GTEST_CHECK_INT_EQ_(repeat, g_environment_tear_down_count);
michael@0 201 GTEST_CHECK_INT_EQ_(0, g_should_fail_count);
michael@0 202 GTEST_CHECK_INT_EQ_(repeat, g_should_pass_count);
michael@0 203 GTEST_CHECK_INT_EQ_(repeat, g_death_test_count);
michael@0 204 #if GTEST_HAS_PARAM_TEST
michael@0 205 GTEST_CHECK_INT_EQ_(repeat * kNumberOfParamTests, g_param_test_count);
michael@0 206 #endif // GTEST_HAS_PARAM_TEST
michael@0 207 }
michael@0 208
michael@0 209 // Tests using --gtest_repeat when --gtest_filter specifies a set of
michael@0 210 // failed tests.
michael@0 211 void TestRepeatWithFilterForFailedTests(int repeat) {
michael@0 212 GTEST_FLAG(repeat) = repeat;
michael@0 213 GTEST_FLAG(filter) = "*ShouldFail";
michael@0 214
michael@0 215 ResetCounts();
michael@0 216 GTEST_CHECK_INT_EQ_(1, RUN_ALL_TESTS());
michael@0 217 GTEST_CHECK_INT_EQ_(repeat, g_environment_set_up_count);
michael@0 218 GTEST_CHECK_INT_EQ_(repeat, g_environment_tear_down_count);
michael@0 219 GTEST_CHECK_INT_EQ_(repeat, g_should_fail_count);
michael@0 220 GTEST_CHECK_INT_EQ_(0, g_should_pass_count);
michael@0 221 GTEST_CHECK_INT_EQ_(0, g_death_test_count);
michael@0 222 #if GTEST_HAS_PARAM_TEST
michael@0 223 GTEST_CHECK_INT_EQ_(0, g_param_test_count);
michael@0 224 #endif // GTEST_HAS_PARAM_TEST
michael@0 225 }
michael@0 226
michael@0 227 } // namespace
michael@0 228
michael@0 229 int main(int argc, char **argv) {
michael@0 230 testing::InitGoogleTest(&argc, argv);
michael@0 231 testing::AddGlobalTestEnvironment(new MyEnvironment);
michael@0 232
michael@0 233 TestRepeatUnspecified();
michael@0 234 TestRepeat(0);
michael@0 235 TestRepeat(1);
michael@0 236 TestRepeat(5);
michael@0 237
michael@0 238 TestRepeatWithEmptyFilter(2);
michael@0 239 TestRepeatWithEmptyFilter(3);
michael@0 240
michael@0 241 TestRepeatWithFilterForSuccessfulTests(3);
michael@0 242
michael@0 243 TestRepeatWithFilterForFailedTests(4);
michael@0 244
michael@0 245 // It would be nice to verify that the tests indeed loop forever
michael@0 246 // when GTEST_FLAG(repeat) is negative, but this test will be quite
michael@0 247 // complicated to write. Since this flag is for interactive
michael@0 248 // debugging only and doesn't affect the normal test result, such a
michael@0 249 // test would be an overkill.
michael@0 250
michael@0 251 printf("PASS\n");
michael@0 252 return 0;
michael@0 253 }

mercurial