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
00035 #include "gtest/gtest.h"
00036
00037 #include <stdio.h>
00038 #include <stdlib.h>
00039
00040 #if GTEST_HAS_SEH
00041 # include <windows.h>
00042 #endif
00043
00044 #if GTEST_HAS_EXCEPTIONS
00045 # include <exception>
00046 # include <stdexcept>
00047 #endif
00048
00049 using testing::Test;
00050
00051 #if GTEST_HAS_SEH
00052
00053 class SehExceptionInConstructorTest : public Test {
00054 public:
00055 SehExceptionInConstructorTest() { RaiseException(42, 0, 0, NULL); }
00056 };
00057
00058 TEST_F(SehExceptionInConstructorTest, ThrowsExceptionInConstructor) {}
00059
00060 class SehExceptionInDestructorTest : public Test {
00061 public:
00062 ~SehExceptionInDestructorTest() { RaiseException(42, 0, 0, NULL); }
00063 };
00064
00065 TEST_F(SehExceptionInDestructorTest, ThrowsExceptionInDestructor) {}
00066
00067 class SehExceptionInSetUpTestCaseTest : public Test {
00068 public:
00069 static void SetUpTestCase() { RaiseException(42, 0, 0, NULL); }
00070 };
00071
00072 TEST_F(SehExceptionInSetUpTestCaseTest, ThrowsExceptionInSetUpTestCase) {}
00073
00074 class SehExceptionInTearDownTestCaseTest : public Test {
00075 public:
00076 static void TearDownTestCase() { RaiseException(42, 0, 0, NULL); }
00077 };
00078
00079 TEST_F(SehExceptionInTearDownTestCaseTest, ThrowsExceptionInTearDownTestCase) {}
00080
00081 class SehExceptionInSetUpTest : public Test {
00082 protected:
00083 virtual void SetUp() { RaiseException(42, 0, 0, NULL); }
00084 };
00085
00086 TEST_F(SehExceptionInSetUpTest, ThrowsExceptionInSetUp) {}
00087
00088 class SehExceptionInTearDownTest : public Test {
00089 protected:
00090 virtual void TearDown() { RaiseException(42, 0, 0, NULL); }
00091 };
00092
00093 TEST_F(SehExceptionInTearDownTest, ThrowsExceptionInTearDown) {}
00094
00095 TEST(SehExceptionTest, ThrowsSehException) {
00096 RaiseException(42, 0, 0, NULL);
00097 }
00098
00099 #endif // GTEST_HAS_SEH
00100
00101 #if GTEST_HAS_EXCEPTIONS
00102
00103 class CxxExceptionInConstructorTest : public Test {
00104 public:
00105 CxxExceptionInConstructorTest() {
00106
00107
00108 GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(
00109 throw std::runtime_error("Standard C++ exception"));
00110 }
00111
00112 static void TearDownTestCase() {
00113 printf("%s",
00114 "CxxExceptionInConstructorTest::TearDownTestCase() "
00115 "called as expected.\n");
00116 }
00117
00118 protected:
00119 ~CxxExceptionInConstructorTest() {
00120 ADD_FAILURE() << "CxxExceptionInConstructorTest destructor "
00121 << "called unexpectedly.";
00122 }
00123
00124 virtual void SetUp() {
00125 ADD_FAILURE() << "CxxExceptionInConstructorTest::SetUp() "
00126 << "called unexpectedly.";
00127 }
00128
00129 virtual void TearDown() {
00130 ADD_FAILURE() << "CxxExceptionInConstructorTest::TearDown() "
00131 << "called unexpectedly.";
00132 }
00133 };
00134
00135 TEST_F(CxxExceptionInConstructorTest, ThrowsExceptionInConstructor) {
00136 ADD_FAILURE() << "CxxExceptionInConstructorTest test body "
00137 << "called unexpectedly.";
00138 }
00139
00140
00141 #if !defined(__GXX_EXPERIMENTAL_CXX0X__) && __cplusplus < 201103L
00142 class CxxExceptionInDestructorTest : public Test {
00143 public:
00144 static void TearDownTestCase() {
00145 printf("%s",
00146 "CxxExceptionInDestructorTest::TearDownTestCase() "
00147 "called as expected.\n");
00148 }
00149
00150 protected:
00151 ~CxxExceptionInDestructorTest() {
00152 GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(
00153 throw std::runtime_error("Standard C++ exception"));
00154 }
00155 };
00156
00157 TEST_F(CxxExceptionInDestructorTest, ThrowsExceptionInDestructor) {}
00158 #endif // C++11 mode
00159
00160 class CxxExceptionInSetUpTestCaseTest : public Test {
00161 public:
00162 CxxExceptionInSetUpTestCaseTest() {
00163 printf("%s",
00164 "CxxExceptionInSetUpTestCaseTest constructor "
00165 "called as expected.\n");
00166 }
00167
00168 static void SetUpTestCase() {
00169 throw std::runtime_error("Standard C++ exception");
00170 }
00171
00172 static void TearDownTestCase() {
00173 printf("%s",
00174 "CxxExceptionInSetUpTestCaseTest::TearDownTestCase() "
00175 "called as expected.\n");
00176 }
00177
00178 protected:
00179 ~CxxExceptionInSetUpTestCaseTest() {
00180 printf("%s",
00181 "CxxExceptionInSetUpTestCaseTest destructor "
00182 "called as expected.\n");
00183 }
00184
00185 virtual void SetUp() {
00186 printf("%s",
00187 "CxxExceptionInSetUpTestCaseTest::SetUp() "
00188 "called as expected.\n");
00189 }
00190
00191 virtual void TearDown() {
00192 printf("%s",
00193 "CxxExceptionInSetUpTestCaseTest::TearDown() "
00194 "called as expected.\n");
00195 }
00196 };
00197
00198 TEST_F(CxxExceptionInSetUpTestCaseTest, ThrowsExceptionInSetUpTestCase) {
00199 printf("%s",
00200 "CxxExceptionInSetUpTestCaseTest test body "
00201 "called as expected.\n");
00202 }
00203
00204 class CxxExceptionInTearDownTestCaseTest : public Test {
00205 public:
00206 static void TearDownTestCase() {
00207 throw std::runtime_error("Standard C++ exception");
00208 }
00209 };
00210
00211 TEST_F(CxxExceptionInTearDownTestCaseTest, ThrowsExceptionInTearDownTestCase) {}
00212
00213 class CxxExceptionInSetUpTest : public Test {
00214 public:
00215 static void TearDownTestCase() {
00216 printf("%s",
00217 "CxxExceptionInSetUpTest::TearDownTestCase() "
00218 "called as expected.\n");
00219 }
00220
00221 protected:
00222 ~CxxExceptionInSetUpTest() {
00223 printf("%s",
00224 "CxxExceptionInSetUpTest destructor "
00225 "called as expected.\n");
00226 }
00227
00228 virtual void SetUp() { throw std::runtime_error("Standard C++ exception"); }
00229
00230 virtual void TearDown() {
00231 printf("%s",
00232 "CxxExceptionInSetUpTest::TearDown() "
00233 "called as expected.\n");
00234 }
00235 };
00236
00237 TEST_F(CxxExceptionInSetUpTest, ThrowsExceptionInSetUp) {
00238 ADD_FAILURE() << "CxxExceptionInSetUpTest test body "
00239 << "called unexpectedly.";
00240 }
00241
00242 class CxxExceptionInTearDownTest : public Test {
00243 public:
00244 static void TearDownTestCase() {
00245 printf("%s",
00246 "CxxExceptionInTearDownTest::TearDownTestCase() "
00247 "called as expected.\n");
00248 }
00249
00250 protected:
00251 ~CxxExceptionInTearDownTest() {
00252 printf("%s",
00253 "CxxExceptionInTearDownTest destructor "
00254 "called as expected.\n");
00255 }
00256
00257 virtual void TearDown() {
00258 throw std::runtime_error("Standard C++ exception");
00259 }
00260 };
00261
00262 TEST_F(CxxExceptionInTearDownTest, ThrowsExceptionInTearDown) {}
00263
00264 class CxxExceptionInTestBodyTest : public Test {
00265 public:
00266 static void TearDownTestCase() {
00267 printf("%s",
00268 "CxxExceptionInTestBodyTest::TearDownTestCase() "
00269 "called as expected.\n");
00270 }
00271
00272 protected:
00273 ~CxxExceptionInTestBodyTest() {
00274 printf("%s",
00275 "CxxExceptionInTestBodyTest destructor "
00276 "called as expected.\n");
00277 }
00278
00279 virtual void TearDown() {
00280 printf("%s",
00281 "CxxExceptionInTestBodyTest::TearDown() "
00282 "called as expected.\n");
00283 }
00284 };
00285
00286 TEST_F(CxxExceptionInTestBodyTest, ThrowsStdCxxException) {
00287 throw std::runtime_error("Standard C++ exception");
00288 }
00289
00290 TEST(CxxExceptionTest, ThrowsNonStdCxxException) {
00291 throw "C-string";
00292 }
00293
00294
00295
00296
00297 void TerminateHandler() {
00298 fprintf(stderr, "%s\n", "Unhandled C++ exception terminating the program.");
00299 fflush(NULL);
00300 exit(3);
00301 }
00302
00303 #endif // GTEST_HAS_EXCEPTIONS
00304
00305 int main(int argc, char** argv) {
00306 #if GTEST_HAS_EXCEPTIONS
00307 std::set_terminate(&TerminateHandler);
00308 #endif
00309 testing::InitGoogleTest(&argc, argv);
00310 return RUN_ALL_TESTS();
00311 }