• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  struct my_exception1 {};
24  struct my_exception2 {};
25  
26  class exception_watcher2
27  {
28      unsigned int& m_count;
29  
30  public:
exception_watcher2(unsigned int & count)31      explicit exception_watcher2(unsigned int& count) : m_count(count) {}
~exception_watcher2()32      ~exception_watcher2() { m_count = boost::core::uncaught_exceptions(); }
33  };
34  
35  class exception_watcher1
36  {
37      unsigned int& m_count1;
38      unsigned int& m_count2;
39  
40  public:
exception_watcher1(unsigned int & count1,unsigned int & count2)41      exception_watcher1(unsigned int& count1, unsigned int& count2) : m_count1(count1), m_count2(count2) {}
~exception_watcher1()42      ~exception_watcher1()
43      {
44          m_count1 = boost::core::uncaught_exceptions();
45          try
46          {
47              exception_watcher2 watcher2(m_count2);
48              throw my_exception2();
49          }
50          catch (...)
51          {
52          }
53      }
54  };
55  
56  // Tests for uncaught_exceptions when used in nested destructors while an exception propagates
test_in_nested_destructors()57  void test_in_nested_destructors()
58  {
59      const unsigned int root_count = boost::core::uncaught_exceptions();
60  
61      unsigned int level1_count = root_count, level2_count = root_count;
62      try
63      {
64          exception_watcher1 watcher1(level1_count, level2_count);
65          throw my_exception1();
66      }
67      catch (...)
68      {
69      }
70  
71      BOOST_TEST_NE(root_count, level1_count);
72      BOOST_TEST_NE(root_count, level2_count);
73      BOOST_TEST_NE(level1_count, level2_count);
74  }
75  
main()76  int main()
77  {
78      test_in_nested_destructors();
79  
80      return boost::report_errors();
81  }
82  
83  #else
84  
main()85  int main()
86  {
87      return 0;
88  }
89  
90  #endif
91