throw_exception_test.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. //Copyright (c) 2006-2009 Emil Dotchevski and Reverge Studios, Inc.
  2. //Distributed under the Boost Software License, Version 1.0. (See accompanying
  3. //file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  4. #include "helper2.hpp"
  5. #include <boost/exception/get_error_info.hpp>
  6. #include <boost/exception/info.hpp>
  7. #include <boost/exception_ptr.hpp>
  8. #include <boost/detail/lightweight_test.hpp>
  9. typedef boost::error_info<struct tag_test_int,int> test_data;
  10. struct
  11. exception1:
  12. std::exception
  13. {
  14. };
  15. struct
  16. exception2:
  17. std::exception,
  18. boost::exception
  19. {
  20. };
  21. void
  22. boost_throw_exception_test()
  23. {
  24. try
  25. {
  26. BOOST_THROW_EXCEPTION(exception1());
  27. BOOST_ERROR("BOOST_THROW_EXCEPTION failed to throw.");
  28. }
  29. catch(
  30. boost::exception & x )
  31. {
  32. char const * const * function=boost::get_error_info<boost::throw_function>(x);
  33. char const * const * file=boost::get_error_info<boost::throw_file>(x);
  34. int const * line=boost::get_error_info<boost::throw_line>(x);
  35. BOOST_TEST( file && *file );
  36. BOOST_TEST( function && *function );
  37. BOOST_TEST( line && *line==32 );
  38. }
  39. catch(
  40. ... )
  41. {
  42. BOOST_TEST(false);
  43. }
  44. try
  45. {
  46. BOOST_THROW_EXCEPTION(exception2() << test_data(42));
  47. BOOST_ERROR("BOOST_THROW_EXCEPTION failed to throw.");
  48. }
  49. catch(
  50. boost::exception & x )
  51. {
  52. char const * const * function=boost::get_error_info<boost::throw_function>(x);
  53. char const * const * file=boost::get_error_info<boost::throw_file>(x);
  54. int const * line=boost::get_error_info<boost::throw_line>(x);
  55. int const * data=boost::get_error_info<test_data>(x);
  56. BOOST_TEST( file && *file );
  57. BOOST_TEST( function && *function );
  58. BOOST_TEST( line && *line==52 );
  59. BOOST_TEST( data && *data==42 );
  60. }
  61. catch(
  62. ... )
  63. {
  64. BOOST_TEST(false);
  65. }
  66. }
  67. void
  68. throw_fwd( void (*thrower)(int) )
  69. {
  70. try
  71. {
  72. thrower(42);
  73. }
  74. catch(
  75. boost::exception & x )
  76. {
  77. x << test_data(42);
  78. throw;
  79. }
  80. }
  81. template <class T>
  82. void
  83. tester()
  84. {
  85. try
  86. {
  87. throw_fwd( &boost::exception_test::throw_test_exception<T> );
  88. BOOST_ASSERT(false);
  89. }
  90. catch(
  91. ... )
  92. {
  93. boost::exception_ptr p = boost::current_exception();
  94. try
  95. {
  96. rethrow_exception(p);
  97. BOOST_TEST(false);
  98. }
  99. catch(
  100. T & y )
  101. {
  102. #ifdef BOOST_NO_RTTI
  103. try
  104. {
  105. throw;
  106. }
  107. catch(
  108. boost::exception & y )
  109. {
  110. #endif
  111. BOOST_TEST(boost::get_error_info<test_data>(y));
  112. if( int const * d=boost::get_error_info<test_data>(y) )
  113. BOOST_TEST(*d==42);
  114. #ifdef BOOST_NO_RTTI
  115. }
  116. catch(
  117. ... )
  118. {
  119. BOOST_TEST(false);
  120. }
  121. #endif
  122. BOOST_TEST(y.x_==42);
  123. }
  124. catch(
  125. ... )
  126. {
  127. BOOST_TEST(false);
  128. }
  129. }
  130. }
  131. int
  132. main()
  133. {
  134. boost_throw_exception_test();
  135. tester<boost::exception_test::derives_boost_exception>();
  136. tester<boost::exception_test::derives_boost_exception_virtually>();
  137. tester<boost::exception_test::derives_std_exception>();
  138. return boost::report_errors();
  139. }