throw_from_library_test.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // Copyright 2018 Peter Dimov
  2. //
  3. // Distributed under the Boost Software License, Version 1.0.
  4. //
  5. // See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt
  7. #include "lib1_throw.hpp"
  8. #include "lib2_throw.hpp"
  9. #include "lib3_throw.hpp"
  10. #include <boost/exception/exception.hpp>
  11. #include <boost/exception_ptr.hpp>
  12. #include <boost/exception/get_error_info.hpp>
  13. #include <boost/core/lightweight_test.hpp>
  14. void test_catch_by_type()
  15. {
  16. BOOST_TEST_THROWS( lib1::f(), lib1::exception );
  17. BOOST_TEST_THROWS( lib2::f(), lib2::exception );
  18. BOOST_TEST_THROWS( lib3::f(), lib3::exception );
  19. }
  20. void test_catch_by_exception()
  21. {
  22. BOOST_TEST_THROWS( lib2::f(), boost::exception );
  23. BOOST_TEST_THROWS( lib3::f(), boost::exception );
  24. }
  25. void test_exception_ptr()
  26. {
  27. try
  28. {
  29. lib2::f();
  30. }
  31. catch( ... )
  32. {
  33. boost::exception_ptr p = boost::current_exception();
  34. BOOST_TEST_THROWS( boost::rethrow_exception( p ), lib2::exception );
  35. BOOST_TEST_THROWS( boost::rethrow_exception( p ), boost::exception );
  36. }
  37. try
  38. {
  39. lib3::f();
  40. }
  41. catch( ... )
  42. {
  43. boost::exception_ptr p = boost::current_exception();
  44. BOOST_TEST_THROWS( boost::rethrow_exception( p ), lib3::exception );
  45. BOOST_TEST_THROWS( boost::rethrow_exception( p ), boost::exception );
  46. }
  47. }
  48. void test_throw_line()
  49. {
  50. try
  51. {
  52. lib3::f();
  53. }
  54. catch( boost::exception const & x )
  55. {
  56. int const * line = boost::get_error_info<boost::throw_line>( x );
  57. BOOST_TEST( line != 0 );
  58. BOOST_TEST_EQ( *line, 13 );
  59. }
  60. }
  61. int main()
  62. {
  63. test_catch_by_type();
  64. test_catch_by_exception();
  65. test_exception_ptr();
  66. test_throw_line();
  67. return boost::report_errors();
  68. }