| 1 | //  (C) Copyright Gennadiy Rozental 2005. | 
|---|
| 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 |  | 
|---|
| 6 | //  See http://www.boost.org/libs/test for the library home page. | 
|---|
| 7 | // | 
|---|
| 8 | //  File        : $RCSfile: exception_safety.hpp,v $ | 
|---|
| 9 | // | 
|---|
| 10 | //  Version     : $Revision: 1.4 $ | 
|---|
| 11 | // | 
|---|
| 12 | //  Description : Facilities to perform exception safety tests | 
|---|
| 13 | // *************************************************************************** | 
|---|
| 14 |  | 
|---|
| 15 | #ifndef BOOST_TEST_EXCEPTION_SAFETY_HPP_111705GER | 
|---|
| 16 | #define BOOST_TEST_EXCEPTION_SAFETY_HPP_111705GER | 
|---|
| 17 |  | 
|---|
| 18 | // Boost.Test | 
|---|
| 19 | #include <boost/test/detail/config.hpp> | 
|---|
| 20 |  | 
|---|
| 21 | #include <boost/test/utils/callback.hpp> | 
|---|
| 22 |  | 
|---|
| 23 | // STL | 
|---|
| 24 | #include <memory> | 
|---|
| 25 |  | 
|---|
| 26 | #include <boost/test/detail/suppress_warnings.hpp> | 
|---|
| 27 |  | 
|---|
| 28 | //____________________________________________________________________________// | 
|---|
| 29 |  | 
|---|
| 30 | // ************************************************************************** // | 
|---|
| 31 | // **************          BOOST_TEST_EXCEPTION_SAFETY         ************** // | 
|---|
| 32 | // ************************************************************************** // | 
|---|
| 33 |  | 
|---|
| 34 | #define BOOST_TEST_EXCEPTION_SAFETY( test_name )                        \ | 
|---|
| 35 | struct test_name : public BOOST_AUTO_TEST_CASE_FIXTURE                  \ | 
|---|
| 36 | { void test_method(); };                                                \ | 
|---|
| 37 |                                                                         \ | 
|---|
| 38 | static void BOOST_AUTO_TC_INVOKER( test_name )()                        \ | 
|---|
| 39 | {                                                                       \ | 
|---|
| 40 |     test_name t;                                                        \ | 
|---|
| 41 |     ::boost::itest::exception_safety(                                   \ | 
|---|
| 42 |         boost::bind( &test_name::test_method, t ),                      \ | 
|---|
| 43 |         BOOST_STRINGIZE(test_name) );                                   \ | 
|---|
| 44 | }                                                                       \ | 
|---|
| 45 |                                                                         \ | 
|---|
| 46 | struct BOOST_AUTO_TC_UNIQUE_ID( test_name ) {};                         \ | 
|---|
| 47 |                                                                         \ | 
|---|
| 48 | BOOST_AUTO_TC_REGISTRAR( test_name )(                                   \ | 
|---|
| 49 |     boost::unit_test::make_test_case(                                   \ | 
|---|
| 50 |         &BOOST_AUTO_TC_INVOKER( test_name ), #test_name ),              \ | 
|---|
| 51 |     boost::unit_test::ut_detail::auto_tc_exp_fail<                      \ | 
|---|
| 52 |         BOOST_AUTO_TC_UNIQUE_ID( test_name )>::value );                 \ | 
|---|
| 53 |                                                                         \ | 
|---|
| 54 | void test_name::test_method()                                           \ | 
|---|
| 55 | /**/ | 
|---|
| 56 |  | 
|---|
| 57 | namespace boost { | 
|---|
| 58 |  | 
|---|
| 59 | namespace itest { | 
|---|
| 60 |  | 
|---|
| 61 | // ************************************************************************** // | 
|---|
| 62 | // **************             exception safety test            ************** // | 
|---|
| 63 | // ************************************************************************** // | 
|---|
| 64 |  | 
|---|
| 65 | void    BOOST_TEST_DECL exception_safety( unit_test::callback0<> const& F,  | 
|---|
| 66 |                                           unit_test::const_string test_name = "" ); | 
|---|
| 67 |  | 
|---|
| 68 | } // namespace itest | 
|---|
| 69 |  | 
|---|
| 70 | } // namespace boost | 
|---|
| 71 |  | 
|---|
| 72 | // ************************************************************************** // | 
|---|
| 73 | // **************     global operator new/delete overloads     ************** // | 
|---|
| 74 | // ************************************************************************** // | 
|---|
| 75 |  | 
|---|
| 76 | #ifndef BOOST_ITEST_NO_NEW_OVERLOADS | 
|---|
| 77 |  | 
|---|
| 78 | #include <boost/test/interaction_based.hpp> | 
|---|
| 79 |  | 
|---|
| 80 | # ifdef BOOST_NO_STDC_NAMESPACE | 
|---|
| 81 | namespace std { using ::isprint; using ::malloc; using ::free; } | 
|---|
| 82 | # endif | 
|---|
| 83 |  | 
|---|
| 84 | inline void* | 
|---|
| 85 | operator new( std::size_t s ) throw(std::bad_alloc) | 
|---|
| 86 | { | 
|---|
| 87 |     void* res = std::malloc(s ? s : 1); | 
|---|
| 88 |  | 
|---|
| 89 |     if( res ) | 
|---|
| 90 |         ::boost::itest::manager::instance().allocated( 0, 0, res, s ); | 
|---|
| 91 |     else | 
|---|
| 92 |         throw std::bad_alloc(); | 
|---|
| 93 |  | 
|---|
| 94 |     return res; | 
|---|
| 95 | } | 
|---|
| 96 |  | 
|---|
| 97 | //____________________________________________________________________________// | 
|---|
| 98 |  | 
|---|
| 99 | inline void* | 
|---|
| 100 | operator new( std::size_t s, std::nothrow_t const& ) throw() | 
|---|
| 101 | { | 
|---|
| 102 |     void* res = std::malloc(s ? s : 1); | 
|---|
| 103 |  | 
|---|
| 104 |     if( res ) | 
|---|
| 105 |         ::boost::itest::manager::instance().allocated( 0, 0, res, s ); | 
|---|
| 106 |  | 
|---|
| 107 |     return res; | 
|---|
| 108 | } | 
|---|
| 109 |  | 
|---|
| 110 | //____________________________________________________________________________// | 
|---|
| 111 |  | 
|---|
| 112 | inline void* | 
|---|
| 113 | operator new[]( std::size_t s ) throw(std::bad_alloc) | 
|---|
| 114 | { | 
|---|
| 115 |     void* res = std::malloc(s ? s : 1); | 
|---|
| 116 |  | 
|---|
| 117 |     if( res ) | 
|---|
| 118 |         ::boost::itest::manager::instance().allocated( 0, 0, res, s ); | 
|---|
| 119 |     else | 
|---|
| 120 |         throw std::bad_alloc(); | 
|---|
| 121 |  | 
|---|
| 122 |     return res; | 
|---|
| 123 | } | 
|---|
| 124 |  | 
|---|
| 125 | //____________________________________________________________________________// | 
|---|
| 126 |  | 
|---|
| 127 | inline void* | 
|---|
| 128 | operator new[]( std::size_t s, std::nothrow_t const& ) throw() | 
|---|
| 129 | { | 
|---|
| 130 |     void* res = std::malloc(s ? s : 1); | 
|---|
| 131 |  | 
|---|
| 132 |     if( res ) | 
|---|
| 133 |         ::boost::itest::manager::instance().allocated( 0, 0, res, s ); | 
|---|
| 134 |  | 
|---|
| 135 |     return res; | 
|---|
| 136 | } | 
|---|
| 137 |  | 
|---|
| 138 | //____________________________________________________________________________// | 
|---|
| 139 |  | 
|---|
| 140 | inline void | 
|---|
| 141 | operator delete( void* p ) throw() | 
|---|
| 142 | { | 
|---|
| 143 |     ::boost::itest::manager::instance().freed( p ); | 
|---|
| 144 |  | 
|---|
| 145 |     std::free( p ); | 
|---|
| 146 | } | 
|---|
| 147 |  | 
|---|
| 148 | //____________________________________________________________________________// | 
|---|
| 149 |  | 
|---|
| 150 | inline void | 
|---|
| 151 | operator delete( void* p, std::nothrow_t const& ) throw() | 
|---|
| 152 | { | 
|---|
| 153 |     ::boost::itest::manager::instance().freed( p ); | 
|---|
| 154 |  | 
|---|
| 155 |     std::free( p ); | 
|---|
| 156 | } | 
|---|
| 157 |  | 
|---|
| 158 | //____________________________________________________________________________// | 
|---|
| 159 |  | 
|---|
| 160 | inline void | 
|---|
| 161 | operator delete[]( void* p ) throw() | 
|---|
| 162 | { | 
|---|
| 163 |     ::boost::itest::manager::instance().freed( p ); | 
|---|
| 164 |  | 
|---|
| 165 |     std::free( p ); | 
|---|
| 166 | } | 
|---|
| 167 |  | 
|---|
| 168 | //____________________________________________________________________________// | 
|---|
| 169 |  | 
|---|
| 170 | inline void | 
|---|
| 171 | operator delete[]( void* p, std::nothrow_t const& ) throw() | 
|---|
| 172 | { | 
|---|
| 173 |     ::boost::itest::manager::instance().freed( p ); | 
|---|
| 174 |  | 
|---|
| 175 |     std::free( p ); | 
|---|
| 176 | } | 
|---|
| 177 |  | 
|---|
| 178 | //____________________________________________________________________________// | 
|---|
| 179 |  | 
|---|
| 180 | #endif // BOOST_ITEST_NO_NEW_OVERLOADS | 
|---|
| 181 |  | 
|---|
| 182 | //____________________________________________________________________________// | 
|---|
| 183 |  | 
|---|
| 184 | #include <boost/test/detail/enable_warnings.hpp> | 
|---|
| 185 |  | 
|---|
| 186 | // *************************************************************************** | 
|---|
| 187 | //  Revision History : | 
|---|
| 188 | //   | 
|---|
| 189 | //  $Log: exception_safety.hpp,v $ | 
|---|
| 190 | //  Revision 1.4  2006/01/28 08:52:35  rogeeff | 
|---|
| 191 | //  operator new overloads made inline to: | 
|---|
| 192 | //  1. prevent issues with export them from DLL | 
|---|
| 193 | //  2. release link issue fixed | 
|---|
| 194 | // | 
|---|
| 195 | //  Revision 1.3  2006/01/15 11:14:38  rogeeff | 
|---|
| 196 | //  simpl_mock -> mock_object<>::prototype() | 
|---|
| 197 | //  operator new need to be rethinked | 
|---|
| 198 | // | 
|---|
| 199 | //  Revision 1.2  2005/12/20 23:50:13  rogeeff | 
|---|
| 200 | //  unit_test.hpp removed | 
|---|
| 201 | // | 
|---|
| 202 | //  Revision 1.1  2005/12/14 05:03:46  rogeeff | 
|---|
| 203 | //  exception safety automatic testing facilties | 
|---|
| 204 | // | 
|---|
| 205 | // *************************************************************************** | 
|---|
| 206 |  | 
|---|
| 207 | #endif // BOOST_TEST_EXCEPTION_SAFETY_HPP_111705GER | 
|---|