Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 #include <stdlib.h>
00035 #include <iostream>
00036 #include "gtest/gtest.h"
00037
00038
00039
00040
00041
00042
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 }
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
00062
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
00078
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
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
00100
00101 int g_should_pass_count = 0;
00102
00103 TEST(FooTest, ShouldPass) {
00104 g_should_pass_count++;
00105 }
00106
00107
00108
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
00131
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
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
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
00165 void TestRepeatUnspecified() {
00166 ResetCounts();
00167 GTEST_CHECK_INT_EQ_(1, RUN_ALL_TESTS());
00168 CheckCounts(1);
00169 }
00170
00171
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
00181
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
00192
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
00210
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 }
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
00246
00247
00248
00249
00250
00251 printf("PASS\n");
00252 return 0;
00253 }