1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/media/webrtc/trunk/testing/gtest/test/gtest_repeat_test.cc Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,253 @@ 1.4 +// Copyright 2008, Google Inc. 1.5 +// All rights reserved. 1.6 +// 1.7 +// Redistribution and use in source and binary forms, with or without 1.8 +// modification, are permitted provided that the following conditions are 1.9 +// met: 1.10 +// 1.11 +// * Redistributions of source code must retain the above copyright 1.12 +// notice, this list of conditions and the following disclaimer. 1.13 +// * Redistributions in binary form must reproduce the above 1.14 +// copyright notice, this list of conditions and the following disclaimer 1.15 +// in the documentation and/or other materials provided with the 1.16 +// distribution. 1.17 +// * Neither the name of Google Inc. nor the names of its 1.18 +// contributors may be used to endorse or promote products derived from 1.19 +// this software without specific prior written permission. 1.20 +// 1.21 +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1.22 +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1.23 +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1.24 +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 1.25 +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 1.26 +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 1.27 +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 1.28 +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 1.29 +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 1.30 +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 1.31 +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.32 +// 1.33 +// Author: wan@google.com (Zhanyong Wan) 1.34 + 1.35 +// Tests the --gtest_repeat=number flag. 1.36 + 1.37 +#include <stdlib.h> 1.38 +#include <iostream> 1.39 +#include "gtest/gtest.h" 1.40 + 1.41 +// Indicates that this translation unit is part of Google Test's 1.42 +// implementation. It must come before gtest-internal-inl.h is 1.43 +// included, or there will be a compiler error. This trick is to 1.44 +// prevent a user from accidentally including gtest-internal-inl.h in 1.45 +// his code. 1.46 +#define GTEST_IMPLEMENTATION_ 1 1.47 +#include "src/gtest-internal-inl.h" 1.48 +#undef GTEST_IMPLEMENTATION_ 1.49 + 1.50 +namespace testing { 1.51 + 1.52 +GTEST_DECLARE_string_(death_test_style); 1.53 +GTEST_DECLARE_string_(filter); 1.54 +GTEST_DECLARE_int32_(repeat); 1.55 + 1.56 +} // namespace testing 1.57 + 1.58 +using testing::GTEST_FLAG(death_test_style); 1.59 +using testing::GTEST_FLAG(filter); 1.60 +using testing::GTEST_FLAG(repeat); 1.61 + 1.62 +namespace { 1.63 + 1.64 +// We need this when we are testing Google Test itself and therefore 1.65 +// cannot use Google Test assertions. 1.66 +#define GTEST_CHECK_INT_EQ_(expected, actual) \ 1.67 + do {\ 1.68 + const int expected_val = (expected);\ 1.69 + const int actual_val = (actual);\ 1.70 + if (::testing::internal::IsTrue(expected_val != actual_val)) {\ 1.71 + ::std::cout << "Value of: " #actual "\n"\ 1.72 + << " Actual: " << actual_val << "\n"\ 1.73 + << "Expected: " #expected "\n"\ 1.74 + << "Which is: " << expected_val << "\n";\ 1.75 + ::testing::internal::posix::Abort();\ 1.76 + }\ 1.77 + } while (::testing::internal::AlwaysFalse()) 1.78 + 1.79 + 1.80 +// Used for verifying that global environment set-up and tear-down are 1.81 +// inside the gtest_repeat loop. 1.82 + 1.83 +int g_environment_set_up_count = 0; 1.84 +int g_environment_tear_down_count = 0; 1.85 + 1.86 +class MyEnvironment : public testing::Environment { 1.87 + public: 1.88 + MyEnvironment() {} 1.89 + virtual void SetUp() { g_environment_set_up_count++; } 1.90 + virtual void TearDown() { g_environment_tear_down_count++; } 1.91 +}; 1.92 + 1.93 +// A test that should fail. 1.94 + 1.95 +int g_should_fail_count = 0; 1.96 + 1.97 +TEST(FooTest, ShouldFail) { 1.98 + g_should_fail_count++; 1.99 + EXPECT_EQ(0, 1) << "Expected failure."; 1.100 +} 1.101 + 1.102 +// A test that should pass. 1.103 + 1.104 +int g_should_pass_count = 0; 1.105 + 1.106 +TEST(FooTest, ShouldPass) { 1.107 + g_should_pass_count++; 1.108 +} 1.109 + 1.110 +// A test that contains a thread-safe death test and a fast death 1.111 +// test. It should pass. 1.112 + 1.113 +int g_death_test_count = 0; 1.114 + 1.115 +TEST(BarDeathTest, ThreadSafeAndFast) { 1.116 + g_death_test_count++; 1.117 + 1.118 + GTEST_FLAG(death_test_style) = "threadsafe"; 1.119 + EXPECT_DEATH_IF_SUPPORTED(::testing::internal::posix::Abort(), ""); 1.120 + 1.121 + GTEST_FLAG(death_test_style) = "fast"; 1.122 + EXPECT_DEATH_IF_SUPPORTED(::testing::internal::posix::Abort(), ""); 1.123 +} 1.124 + 1.125 +#if GTEST_HAS_PARAM_TEST 1.126 +int g_param_test_count = 0; 1.127 + 1.128 +const int kNumberOfParamTests = 10; 1.129 + 1.130 +class MyParamTest : public testing::TestWithParam<int> {}; 1.131 + 1.132 +TEST_P(MyParamTest, ShouldPass) { 1.133 + // TODO(vladl@google.com): Make parameter value checking robust 1.134 + // WRT order of tests. 1.135 + GTEST_CHECK_INT_EQ_(g_param_test_count % kNumberOfParamTests, GetParam()); 1.136 + g_param_test_count++; 1.137 +} 1.138 +INSTANTIATE_TEST_CASE_P(MyParamSequence, 1.139 + MyParamTest, 1.140 + testing::Range(0, kNumberOfParamTests)); 1.141 +#endif // GTEST_HAS_PARAM_TEST 1.142 + 1.143 +// Resets the count for each test. 1.144 +void ResetCounts() { 1.145 + g_environment_set_up_count = 0; 1.146 + g_environment_tear_down_count = 0; 1.147 + g_should_fail_count = 0; 1.148 + g_should_pass_count = 0; 1.149 + g_death_test_count = 0; 1.150 +#if GTEST_HAS_PARAM_TEST 1.151 + g_param_test_count = 0; 1.152 +#endif // GTEST_HAS_PARAM_TEST 1.153 +} 1.154 + 1.155 +// Checks that the count for each test is expected. 1.156 +void CheckCounts(int expected) { 1.157 + GTEST_CHECK_INT_EQ_(expected, g_environment_set_up_count); 1.158 + GTEST_CHECK_INT_EQ_(expected, g_environment_tear_down_count); 1.159 + GTEST_CHECK_INT_EQ_(expected, g_should_fail_count); 1.160 + GTEST_CHECK_INT_EQ_(expected, g_should_pass_count); 1.161 + GTEST_CHECK_INT_EQ_(expected, g_death_test_count); 1.162 +#if GTEST_HAS_PARAM_TEST 1.163 + GTEST_CHECK_INT_EQ_(expected * kNumberOfParamTests, g_param_test_count); 1.164 +#endif // GTEST_HAS_PARAM_TEST 1.165 +} 1.166 + 1.167 +// Tests the behavior of Google Test when --gtest_repeat is not specified. 1.168 +void TestRepeatUnspecified() { 1.169 + ResetCounts(); 1.170 + GTEST_CHECK_INT_EQ_(1, RUN_ALL_TESTS()); 1.171 + CheckCounts(1); 1.172 +} 1.173 + 1.174 +// Tests the behavior of Google Test when --gtest_repeat has the given value. 1.175 +void TestRepeat(int repeat) { 1.176 + GTEST_FLAG(repeat) = repeat; 1.177 + 1.178 + ResetCounts(); 1.179 + GTEST_CHECK_INT_EQ_(repeat > 0 ? 1 : 0, RUN_ALL_TESTS()); 1.180 + CheckCounts(repeat); 1.181 +} 1.182 + 1.183 +// Tests using --gtest_repeat when --gtest_filter specifies an empty 1.184 +// set of tests. 1.185 +void TestRepeatWithEmptyFilter(int repeat) { 1.186 + GTEST_FLAG(repeat) = repeat; 1.187 + GTEST_FLAG(filter) = "None"; 1.188 + 1.189 + ResetCounts(); 1.190 + GTEST_CHECK_INT_EQ_(0, RUN_ALL_TESTS()); 1.191 + CheckCounts(0); 1.192 +} 1.193 + 1.194 +// Tests using --gtest_repeat when --gtest_filter specifies a set of 1.195 +// successful tests. 1.196 +void TestRepeatWithFilterForSuccessfulTests(int repeat) { 1.197 + GTEST_FLAG(repeat) = repeat; 1.198 + GTEST_FLAG(filter) = "*-*ShouldFail"; 1.199 + 1.200 + ResetCounts(); 1.201 + GTEST_CHECK_INT_EQ_(0, RUN_ALL_TESTS()); 1.202 + GTEST_CHECK_INT_EQ_(repeat, g_environment_set_up_count); 1.203 + GTEST_CHECK_INT_EQ_(repeat, g_environment_tear_down_count); 1.204 + GTEST_CHECK_INT_EQ_(0, g_should_fail_count); 1.205 + GTEST_CHECK_INT_EQ_(repeat, g_should_pass_count); 1.206 + GTEST_CHECK_INT_EQ_(repeat, g_death_test_count); 1.207 +#if GTEST_HAS_PARAM_TEST 1.208 + GTEST_CHECK_INT_EQ_(repeat * kNumberOfParamTests, g_param_test_count); 1.209 +#endif // GTEST_HAS_PARAM_TEST 1.210 +} 1.211 + 1.212 +// Tests using --gtest_repeat when --gtest_filter specifies a set of 1.213 +// failed tests. 1.214 +void TestRepeatWithFilterForFailedTests(int repeat) { 1.215 + GTEST_FLAG(repeat) = repeat; 1.216 + GTEST_FLAG(filter) = "*ShouldFail"; 1.217 + 1.218 + ResetCounts(); 1.219 + GTEST_CHECK_INT_EQ_(1, RUN_ALL_TESTS()); 1.220 + GTEST_CHECK_INT_EQ_(repeat, g_environment_set_up_count); 1.221 + GTEST_CHECK_INT_EQ_(repeat, g_environment_tear_down_count); 1.222 + GTEST_CHECK_INT_EQ_(repeat, g_should_fail_count); 1.223 + GTEST_CHECK_INT_EQ_(0, g_should_pass_count); 1.224 + GTEST_CHECK_INT_EQ_(0, g_death_test_count); 1.225 +#if GTEST_HAS_PARAM_TEST 1.226 + GTEST_CHECK_INT_EQ_(0, g_param_test_count); 1.227 +#endif // GTEST_HAS_PARAM_TEST 1.228 +} 1.229 + 1.230 +} // namespace 1.231 + 1.232 +int main(int argc, char **argv) { 1.233 + testing::InitGoogleTest(&argc, argv); 1.234 + testing::AddGlobalTestEnvironment(new MyEnvironment); 1.235 + 1.236 + TestRepeatUnspecified(); 1.237 + TestRepeat(0); 1.238 + TestRepeat(1); 1.239 + TestRepeat(5); 1.240 + 1.241 + TestRepeatWithEmptyFilter(2); 1.242 + TestRepeatWithEmptyFilter(3); 1.243 + 1.244 + TestRepeatWithFilterForSuccessfulTests(3); 1.245 + 1.246 + TestRepeatWithFilterForFailedTests(4); 1.247 + 1.248 + // It would be nice to verify that the tests indeed loop forever 1.249 + // when GTEST_FLAG(repeat) is negative, but this test will be quite 1.250 + // complicated to write. Since this flag is for interactive 1.251 + // debugging only and doesn't affect the normal test result, such a 1.252 + // test would be an overkill. 1.253 + 1.254 + printf("PASS\n"); 1.255 + return 0; 1.256 +}