gtest_repeat_test.cc
Go to the documentation of this file.
00001 // Copyright 2008, Google Inc.
00002 // All rights reserved.
00003 //
00004 // Redistribution and use in source and binary forms, with or without
00005 // modification, are permitted provided that the following conditions are
00006 // met:
00007 //
00008 //     * Redistributions of source code must retain the above copyright
00009 // notice, this list of conditions and the following disclaimer.
00010 //     * Redistributions in binary form must reproduce the above
00011 // copyright notice, this list of conditions and the following disclaimer
00012 // in the documentation and/or other materials provided with the
00013 // distribution.
00014 //     * Neither the name of Google Inc. nor the names of its
00015 // contributors may be used to endorse or promote products derived from
00016 // this software without specific prior written permission.
00017 //
00018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00019 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00020 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00021 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00022 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00023 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00024 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00025 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00026 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00027 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00028 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029 //
00030 // Author: wan@google.com (Zhanyong Wan)
00031 
00032 // Tests the --gtest_repeat=number flag.
00033 
00034 #include <stdlib.h>
00035 #include <iostream>
00036 #include "gtest/gtest.h"
00037 
00038 // Indicates that this translation unit is part of Google Test's
00039 // implementation.  It must come before gtest-internal-inl.h is
00040 // included, or there will be a compiler error.  This trick is to
00041 // prevent a user from accidentally including gtest-internal-inl.h in
00042 // his code.
00043 #define GTEST_IMPLEMENTATION_ 1
00044 #include "src/gtest-internal-inl.h"
00045 #undef GTEST_IMPLEMENTATION_
00046 
00047 namespace testing {
00048 
00049 GTEST_DECLARE_string_(death_test_style);
00050 GTEST_DECLARE_string_(filter);
00051 GTEST_DECLARE_int32_(repeat);
00052 
00053 }  // namespace testing
00054 
00055 using testing::GTEST_FLAG(death_test_style);
00056 using testing::GTEST_FLAG(filter);
00057 using testing::GTEST_FLAG(repeat);
00058 
00059 namespace {
00060 
00061 // We need this when we are testing Google Test itself and therefore
00062 // cannot use Google Test assertions.
00063 #define GTEST_CHECK_INT_EQ_(expected, actual) \
00064   do {\
00065     const int expected_val = (expected);\
00066     const int actual_val = (actual);\
00067     if (::testing::internal::IsTrue(expected_val != actual_val)) {\
00068       ::std::cout << "Value of: " #actual "\n"\
00069                   << "  Actual: " << actual_val << "\n"\
00070                   << "Expected: " #expected "\n"\
00071                   << "Which is: " << expected_val << "\n";\
00072       ::testing::internal::posix::Abort();\
00073     }\
00074   } while (::testing::internal::AlwaysFalse())
00075 
00076 
00077 // Used for verifying that global environment set-up and tear-down are
00078 // inside the gtest_repeat loop.
00079 
00080 int g_environment_set_up_count = 0;
00081 int g_environment_tear_down_count = 0;
00082 
00083 class MyEnvironment : public testing::Environment {
00084  public:
00085   MyEnvironment() {}
00086   virtual void SetUp() { g_environment_set_up_count++; }
00087   virtual void TearDown() { g_environment_tear_down_count++; }
00088 };
00089 
00090 // A test that should fail.
00091 
00092 int g_should_fail_count = 0;
00093 
00094 TEST(FooTest, ShouldFail) {
00095   g_should_fail_count++;
00096   EXPECT_EQ(0, 1) << "Expected failure.";
00097 }
00098 
00099 // A test that should pass.
00100 
00101 int g_should_pass_count = 0;
00102 
00103 TEST(FooTest, ShouldPass) {
00104   g_should_pass_count++;
00105 }
00106 
00107 // A test that contains a thread-safe death test and a fast death
00108 // test.  It should pass.
00109 
00110 int g_death_test_count = 0;
00111 
00112 TEST(BarDeathTest, ThreadSafeAndFast) {
00113   g_death_test_count++;
00114 
00115   GTEST_FLAG(death_test_style) = "threadsafe";
00116   EXPECT_DEATH_IF_SUPPORTED(::testing::internal::posix::Abort(), "");
00117 
00118   GTEST_FLAG(death_test_style) = "fast";
00119   EXPECT_DEATH_IF_SUPPORTED(::testing::internal::posix::Abort(), "");
00120 }
00121 
00122 #if GTEST_HAS_PARAM_TEST
00123 int g_param_test_count = 0;
00124 
00125 const int kNumberOfParamTests = 10;
00126 
00127 class MyParamTest : public testing::TestWithParam<int> {};
00128 
00129 TEST_P(MyParamTest, ShouldPass) {
00130   // TODO(vladl@google.com): Make parameter value checking robust
00131   //                         WRT order of tests.
00132   GTEST_CHECK_INT_EQ_(g_param_test_count % kNumberOfParamTests, GetParam());
00133   g_param_test_count++;
00134 }
00135 INSTANTIATE_TEST_CASE_P(MyParamSequence,
00136                         MyParamTest,
00137                         testing::Range(0, kNumberOfParamTests));
00138 #endif  // GTEST_HAS_PARAM_TEST
00139 
00140 // Resets the count for each test.
00141 void ResetCounts() {
00142   g_environment_set_up_count = 0;
00143   g_environment_tear_down_count = 0;
00144   g_should_fail_count = 0;
00145   g_should_pass_count = 0;
00146   g_death_test_count = 0;
00147 #if GTEST_HAS_PARAM_TEST
00148   g_param_test_count = 0;
00149 #endif  // GTEST_HAS_PARAM_TEST
00150 }
00151 
00152 // Checks that the count for each test is expected.
00153 void CheckCounts(int expected) {
00154   GTEST_CHECK_INT_EQ_(expected, g_environment_set_up_count);
00155   GTEST_CHECK_INT_EQ_(expected, g_environment_tear_down_count);
00156   GTEST_CHECK_INT_EQ_(expected, g_should_fail_count);
00157   GTEST_CHECK_INT_EQ_(expected, g_should_pass_count);
00158   GTEST_CHECK_INT_EQ_(expected, g_death_test_count);
00159 #if GTEST_HAS_PARAM_TEST
00160   GTEST_CHECK_INT_EQ_(expected * kNumberOfParamTests, g_param_test_count);
00161 #endif  // GTEST_HAS_PARAM_TEST
00162 }
00163 
00164 // Tests the behavior of Google Test when --gtest_repeat is not specified.
00165 void TestRepeatUnspecified() {
00166   ResetCounts();
00167   GTEST_CHECK_INT_EQ_(1, RUN_ALL_TESTS());
00168   CheckCounts(1);
00169 }
00170 
00171 // Tests the behavior of Google Test when --gtest_repeat has the given value.
00172 void TestRepeat(int repeat) {
00173   GTEST_FLAG(repeat) = repeat;
00174 
00175   ResetCounts();
00176   GTEST_CHECK_INT_EQ_(repeat > 0 ? 1 : 0, RUN_ALL_TESTS());
00177   CheckCounts(repeat);
00178 }
00179 
00180 // Tests using --gtest_repeat when --gtest_filter specifies an empty
00181 // set of tests.
00182 void TestRepeatWithEmptyFilter(int repeat) {
00183   GTEST_FLAG(repeat) = repeat;
00184   GTEST_FLAG(filter) = "None";
00185 
00186   ResetCounts();
00187   GTEST_CHECK_INT_EQ_(0, RUN_ALL_TESTS());
00188   CheckCounts(0);
00189 }
00190 
00191 // Tests using --gtest_repeat when --gtest_filter specifies a set of
00192 // successful tests.
00193 void TestRepeatWithFilterForSuccessfulTests(int repeat) {
00194   GTEST_FLAG(repeat) = repeat;
00195   GTEST_FLAG(filter) = "*-*ShouldFail";
00196 
00197   ResetCounts();
00198   GTEST_CHECK_INT_EQ_(0, RUN_ALL_TESTS());
00199   GTEST_CHECK_INT_EQ_(repeat, g_environment_set_up_count);
00200   GTEST_CHECK_INT_EQ_(repeat, g_environment_tear_down_count);
00201   GTEST_CHECK_INT_EQ_(0, g_should_fail_count);
00202   GTEST_CHECK_INT_EQ_(repeat, g_should_pass_count);
00203   GTEST_CHECK_INT_EQ_(repeat, g_death_test_count);
00204 #if GTEST_HAS_PARAM_TEST
00205   GTEST_CHECK_INT_EQ_(repeat * kNumberOfParamTests, g_param_test_count);
00206 #endif  // GTEST_HAS_PARAM_TEST
00207 }
00208 
00209 // Tests using --gtest_repeat when --gtest_filter specifies a set of
00210 // failed tests.
00211 void TestRepeatWithFilterForFailedTests(int repeat) {
00212   GTEST_FLAG(repeat) = repeat;
00213   GTEST_FLAG(filter) = "*ShouldFail";
00214 
00215   ResetCounts();
00216   GTEST_CHECK_INT_EQ_(1, RUN_ALL_TESTS());
00217   GTEST_CHECK_INT_EQ_(repeat, g_environment_set_up_count);
00218   GTEST_CHECK_INT_EQ_(repeat, g_environment_tear_down_count);
00219   GTEST_CHECK_INT_EQ_(repeat, g_should_fail_count);
00220   GTEST_CHECK_INT_EQ_(0, g_should_pass_count);
00221   GTEST_CHECK_INT_EQ_(0, g_death_test_count);
00222 #if GTEST_HAS_PARAM_TEST
00223   GTEST_CHECK_INT_EQ_(0, g_param_test_count);
00224 #endif  // GTEST_HAS_PARAM_TEST
00225 }
00226 
00227 }  // namespace
00228 
00229 int main(int argc, char **argv) {
00230   testing::InitGoogleTest(&argc, argv);
00231   testing::AddGlobalTestEnvironment(new MyEnvironment);
00232 
00233   TestRepeatUnspecified();
00234   TestRepeat(0);
00235   TestRepeat(1);
00236   TestRepeat(5);
00237 
00238   TestRepeatWithEmptyFilter(2);
00239   TestRepeatWithEmptyFilter(3);
00240 
00241   TestRepeatWithFilterForSuccessfulTests(3);
00242 
00243   TestRepeatWithFilterForFailedTests(4);
00244 
00245   // It would be nice to verify that the tests indeed loop forever
00246   // when GTEST_FLAG(repeat) is negative, but this test will be quite
00247   // complicated to write.  Since this flag is for interactive
00248   // debugging only and doesn't affect the normal test result, such a
00249   // test would be an overkill.
00250 
00251   printf("PASS\n");
00252   return 0;
00253 }


ros_opcua_impl_freeopcua
Author(s): Denis Štogl
autogenerated on Sat Jun 8 2019 18:24:55