| 1 | /* |
|---|---|
| 2 | * Copyright Andrey Semashev 2018. |
| 3 | * Distributed under the Boost Software License, Version 1.0. |
| 4 | * (See accompanying file LICENSE_1_0.txt or copy at |
| 5 | * https://www.boost.org/LICENSE_1_0.txt) |
| 6 | */ |
| 7 | /*! |
| 8 | * \file uncaught_exceptions_np.cpp |
| 9 | * \author Andrey Semashev |
| 10 | * \date 2018-11-10 |
| 11 | * |
| 12 | * \brief This file contains tests for the uncaught_exceptions function. |
| 13 | * |
| 14 | * This file contains checks that are compiler specific and not quite portable or require C++17. |
| 15 | */ |
| 16 | |
| 17 | #include <boost/core/uncaught_exceptions.hpp> |
| 18 | |
| 19 | #if !defined(BOOST_CORE_UNCAUGHT_EXCEPTIONS_EMULATED) |
| 20 | |
| 21 | #include <boost/core/lightweight_test.hpp> |
| 22 | |
| 23 | #if defined(_MSC_VER) |
| 24 | # pragma warning(disable: 4512) // assignment operator could not be generated |
| 25 | #endif |
| 26 | |
| 27 | struct my_exception1 {}; |
| 28 | struct my_exception2 {}; |
| 29 | |
| 30 | class exception_watcher2 |
| 31 | { |
| 32 | unsigned int& m_count; |
| 33 | |
| 34 | public: |
| 35 | explicit exception_watcher2(unsigned int& count) : m_count(count) {} |
| 36 | ~exception_watcher2() { m_count = boost::core::uncaught_exceptions(); } |
| 37 | }; |
| 38 | |
| 39 | class exception_watcher1 |
| 40 | { |
| 41 | unsigned int& m_count1; |
| 42 | unsigned int& m_count2; |
| 43 | |
| 44 | public: |
| 45 | exception_watcher1(unsigned int& count1, unsigned int& count2) : m_count1(count1), m_count2(count2) {} |
| 46 | ~exception_watcher1() |
| 47 | { |
| 48 | m_count1 = boost::core::uncaught_exceptions(); |
| 49 | try |
| 50 | { |
| 51 | exception_watcher2 watcher2(m_count2); |
| 52 | throw my_exception2(); |
| 53 | } |
| 54 | catch (...) |
| 55 | { |
| 56 | } |
| 57 | } |
| 58 | }; |
| 59 | |
| 60 | // Tests for uncaught_exceptions when used in nested destructors while an exception propagates |
| 61 | void test_in_nested_destructors() |
| 62 | { |
| 63 | const unsigned int root_count = boost::core::uncaught_exceptions(); |
| 64 | |
| 65 | unsigned int level1_count = root_count, level2_count = root_count; |
| 66 | try |
| 67 | { |
| 68 | exception_watcher1 watcher1(level1_count, level2_count); |
| 69 | throw my_exception1(); |
| 70 | } |
| 71 | catch (...) |
| 72 | { |
| 73 | } |
| 74 | |
| 75 | BOOST_TEST_NE(root_count, level1_count); |
| 76 | BOOST_TEST_NE(root_count, level2_count); |
| 77 | BOOST_TEST_NE(level1_count, level2_count); |
| 78 | } |
| 79 | |
| 80 | int main() |
| 81 | { |
| 82 | test_in_nested_destructors(); |
| 83 | |
| 84 | return boost::report_errors(); |
| 85 | } |
| 86 | |
| 87 | #else |
| 88 | |
| 89 | int main() |
| 90 | { |
| 91 | return 0; |
| 92 | } |
| 93 | |
| 94 | #endif |
| 95 |
