exception_safety.hpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. // (C) Copyright Gennadiy Rozental 2005-2008.
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. // See http://www.boost.org/libs/test for the library home page.
  6. //
  7. // File : $RCSfile$
  8. //
  9. // Version : $Revision: 49312 $
  10. //
  11. // Description : Facilities to perform exception safety tests
  12. // ***************************************************************************
  13. #ifndef BOOST_TEST_EXCEPTION_SAFETY_HPP_111705GER
  14. #define BOOST_TEST_EXCEPTION_SAFETY_HPP_111705GER
  15. // Boost.Test
  16. #include <boost/test/detail/config.hpp>
  17. #include <boost/test/utils/callback.hpp>
  18. #include <boost/test/utils/basic_cstring/basic_cstring.hpp>
  19. // STL
  20. #include <memory>
  21. #include <boost/test/detail/suppress_warnings.hpp>
  22. //____________________________________________________________________________//
  23. // ************************************************************************** //
  24. // ************** BOOST_TEST_EXCEPTION_SAFETY ************** //
  25. // ************************************************************************** //
  26. #define BOOST_TEST_EXCEPTION_SAFETY( test_name ) \
  27. struct test_name : public BOOST_AUTO_TEST_CASE_FIXTURE \
  28. { void test_method(); }; \
  29. \
  30. static void BOOST_AUTO_TC_INVOKER( test_name )() \
  31. { \
  32. test_name t; \
  33. ::boost::itest::exception_safety( \
  34. boost::bind( &test_name::test_method, t ), \
  35. BOOST_STRINGIZE(test_name) ); \
  36. } \
  37. \
  38. struct BOOST_AUTO_TC_UNIQUE_ID( test_name ) {}; \
  39. \
  40. BOOST_AUTO_TU_REGISTRAR( test_name )( \
  41. boost::unit_test::make_test_case( \
  42. &BOOST_AUTO_TC_INVOKER( test_name ), #test_name ), \
  43. boost::unit_test::ut_detail::auto_tc_exp_fail< \
  44. BOOST_AUTO_TC_UNIQUE_ID( test_name )>::instance()->value() ); \
  45. \
  46. void test_name::test_method() \
  47. /**/
  48. namespace boost {
  49. namespace itest {
  50. // ************************************************************************** //
  51. // ************** exception safety test ************** //
  52. // ************************************************************************** //
  53. void BOOST_TEST_DECL exception_safety( unit_test::callback0<> const& F,
  54. unit_test::const_string test_name = "" );
  55. } // namespace itest
  56. } // namespace boost
  57. // ************************************************************************** //
  58. // ************** global operator new/delete overloads ************** //
  59. // ************************************************************************** //
  60. #ifndef BOOST_ITEST_NO_NEW_OVERLOADS
  61. #include <boost/test/interaction_based.hpp>
  62. # ifdef BOOST_NO_STDC_NAMESPACE
  63. namespace std { using ::isprint; using ::malloc; using ::free; }
  64. # endif
  65. inline void*
  66. operator new( std::size_t s ) throw(std::bad_alloc)
  67. {
  68. void* res = std::malloc(s ? s : 1);
  69. if( res )
  70. ::boost::itest::manager::instance().allocated( 0, 0, res, s );
  71. else
  72. throw std::bad_alloc();
  73. return res;
  74. }
  75. //____________________________________________________________________________//
  76. inline void*
  77. operator new( std::size_t s, std::nothrow_t const& ) throw()
  78. {
  79. void* res = std::malloc(s ? s : 1);
  80. if( res )
  81. ::boost::itest::manager::instance().allocated( 0, 0, res, s );
  82. return res;
  83. }
  84. //____________________________________________________________________________//
  85. inline void*
  86. operator new[]( std::size_t s ) throw(std::bad_alloc)
  87. {
  88. void* res = std::malloc(s ? s : 1);
  89. if( res )
  90. ::boost::itest::manager::instance().allocated( 0, 0, res, s );
  91. else
  92. throw std::bad_alloc();
  93. return res;
  94. }
  95. //____________________________________________________________________________//
  96. inline void*
  97. operator new[]( std::size_t s, std::nothrow_t const& ) throw()
  98. {
  99. void* res = std::malloc(s ? s : 1);
  100. if( res )
  101. ::boost::itest::manager::instance().allocated( 0, 0, res, s );
  102. return res;
  103. }
  104. //____________________________________________________________________________//
  105. inline void
  106. operator delete( void* p ) throw()
  107. {
  108. ::boost::itest::manager::instance().freed( p );
  109. std::free( p );
  110. }
  111. //____________________________________________________________________________//
  112. inline void
  113. operator delete( void* p, std::nothrow_t const& ) throw()
  114. {
  115. ::boost::itest::manager::instance().freed( p );
  116. std::free( p );
  117. }
  118. //____________________________________________________________________________//
  119. inline void
  120. operator delete[]( void* p ) throw()
  121. {
  122. ::boost::itest::manager::instance().freed( p );
  123. std::free( p );
  124. }
  125. //____________________________________________________________________________//
  126. inline void
  127. operator delete[]( void* p, std::nothrow_t const& ) throw()
  128. {
  129. ::boost::itest::manager::instance().freed( p );
  130. std::free( p );
  131. }
  132. //____________________________________________________________________________//
  133. #endif // BOOST_ITEST_NO_NEW_OVERLOADS
  134. //____________________________________________________________________________//
  135. #include <boost/test/detail/enable_warnings.hpp>
  136. #endif // BOOST_TEST_EXCEPTION_SAFETY_HPP_111705GER