| 1 | // Copyright 2022 Peter Dimov |
| 2 | // Distributed under the Boost Software License, Version 1.0. |
| 3 | // https://www.boost.org/LICENSE_1_0.txt |
| 4 | |
| 5 | #include <boost/config.hpp> |
| 6 | |
| 7 | #if defined(BOOST_NO_EXCEPTIONS) |
| 8 | |
| 9 | #include <boost/config/pragma_message.hpp> |
| 10 | BOOST_PRAGMA_MESSAGE( "Skipping test because BOOST_NO_EXCEPTIONS is defined" ) |
| 11 | |
| 12 | int main() {} |
| 13 | |
| 14 | #elif defined(BOOST_NO_CXX11_HDR_EXCEPTION) |
| 15 | |
| 16 | #include <boost/config/pragma_message.hpp> |
| 17 | BOOST_PRAGMA_MESSAGE( "Skipping test because BOOST_NO_CXX11_HDR_EXCEPTION is defined" ) |
| 18 | |
| 19 | int main() {} |
| 20 | |
| 21 | #else |
| 22 | |
| 23 | #include <boost/exception_ptr.hpp> |
| 24 | #include <boost/exception/exception.hpp> |
| 25 | #include <boost/core/lightweight_test.hpp> |
| 26 | #include <exception> |
| 27 | #include <new> |
| 28 | #include <stdexcept> |
| 29 | |
| 30 | class my_exception |
| 31 | { |
| 32 | }; |
| 33 | |
| 34 | class my_exception2: public std::exception |
| 35 | { |
| 36 | }; |
| 37 | |
| 38 | class my_exception3: public std::bad_alloc |
| 39 | { |
| 40 | }; |
| 41 | |
| 42 | class my_exception4: public std::exception, public boost::exception |
| 43 | { |
| 44 | }; |
| 45 | |
| 46 | class my_exception5: public std::logic_error, public virtual boost::exception |
| 47 | { |
| 48 | public: |
| 49 | |
| 50 | my_exception5(): std::logic_error( "" ) {} |
| 51 | }; |
| 52 | |
| 53 | int main() |
| 54 | { |
| 55 | try |
| 56 | { |
| 57 | throw my_exception(); |
| 58 | } |
| 59 | catch( ... ) |
| 60 | { |
| 61 | boost::exception_ptr p = boost::current_exception(); |
| 62 | BOOST_TEST_THROWS( boost::rethrow_exception( p ), my_exception ); |
| 63 | } |
| 64 | |
| 65 | try |
| 66 | { |
| 67 | throw my_exception2(); |
| 68 | } |
| 69 | catch( ... ) |
| 70 | { |
| 71 | boost::exception_ptr p = boost::current_exception(); |
| 72 | BOOST_TEST_THROWS( boost::rethrow_exception( p ), my_exception2 ); |
| 73 | BOOST_TEST_THROWS( boost::rethrow_exception( p ), std::exception ); |
| 74 | } |
| 75 | |
| 76 | try |
| 77 | { |
| 78 | throw my_exception3(); |
| 79 | } |
| 80 | catch( ... ) |
| 81 | { |
| 82 | boost::exception_ptr p = boost::current_exception(); |
| 83 | BOOST_TEST_THROWS( boost::rethrow_exception( p ), my_exception3 ); |
| 84 | BOOST_TEST_THROWS( boost::rethrow_exception( p ), std::bad_alloc ); |
| 85 | } |
| 86 | |
| 87 | try |
| 88 | { |
| 89 | throw my_exception4(); |
| 90 | } |
| 91 | catch( ... ) |
| 92 | { |
| 93 | boost::exception_ptr p = boost::current_exception(); |
| 94 | BOOST_TEST_THROWS( boost::rethrow_exception( p ), my_exception4 ); |
| 95 | BOOST_TEST_THROWS( boost::rethrow_exception( p ), std::exception ); |
| 96 | BOOST_TEST_THROWS( boost::rethrow_exception( p ), boost::exception ); |
| 97 | } |
| 98 | |
| 99 | try |
| 100 | { |
| 101 | throw my_exception5(); |
| 102 | } |
| 103 | catch( ... ) |
| 104 | { |
| 105 | boost::exception_ptr p = boost::current_exception(); |
| 106 | BOOST_TEST_THROWS( boost::rethrow_exception( p ), my_exception5 ); |
| 107 | BOOST_TEST_THROWS( boost::rethrow_exception( p ), std::logic_error ); |
| 108 | BOOST_TEST_THROWS( boost::rethrow_exception( p ), boost::exception ); |
| 109 | } |
| 110 | |
| 111 | return boost::report_errors(); |
| 112 | } |
| 113 | |
| 114 | #endif |
| 115 | |