exception_safety.ipp
来自「Boost provides free peer-reviewed portab」· IPP 代码 · 共 538 行 · 第 1/2 页
IPP
538 行
}//____________________________________________________________________________//boolexception_safety_tester::decision_point( const_string file, std::size_t line_num ){ activity_guard ag( m_internal_activity ); if( m_exec_path_point < m_execution_path.size() ) { BOOST_REQUIRE_MESSAGE( m_execution_path[m_exec_path_point].m_type == EPP_DECISION && m_execution_path[m_exec_path_point].m_file_name == file && m_execution_path[m_exec_path_point].m_line_num == line_num, "Function under test exibit non-deterministic behavior" ); } else { m_execution_path.push_back( execution_path_point( EPP_DECISION, file, line_num ) ); m_execution_path.back().m_decision.value = true; m_execution_path.back().m_decision.forced_exception_point = m_forced_exception_point; } return m_execution_path[m_exec_path_point++].m_decision.value;}//____________________________________________________________________________//unsignedexception_safety_tester::enter_scope( const_string file, std::size_t line_num, const_string scope_name ){ activity_guard ag( m_internal_activity ); if( m_exec_path_point < m_execution_path.size() ) { BOOST_REQUIRE_MESSAGE( m_execution_path[m_exec_path_point].m_type == EPP_SCOPE && m_execution_path[m_exec_path_point].m_file_name == file && m_execution_path[m_exec_path_point].m_line_num == line_num, "Function under test exibit non-deterministic behavior" ); } else { m_execution_path.push_back( execution_path_point( EPP_SCOPE, file, line_num ) ); } m_execution_path[m_exec_path_point].m_scope.size = 0; m_execution_path[m_exec_path_point].m_scope.name = scope_name.begin(); return m_exec_path_point++;}//____________________________________________________________________________//voidexception_safety_tester::leave_scope( unsigned enter_scope_point ){ activity_guard ag( m_internal_activity ); BOOST_REQUIRE_MESSAGE( m_execution_path[enter_scope_point].m_type == EPP_SCOPE, "Function under test exibit non-deterministic behavior" ); m_execution_path[enter_scope_point].m_scope.size = m_exec_path_point - enter_scope_point;}//____________________________________________________________________________//voidexception_safety_tester::allocated( const_string file, std::size_t line_num, void* p, std::size_t s ){ if( m_internal_activity ) return; activity_guard ag( m_internal_activity ); if( m_exec_path_point < m_execution_path.size() ) BOOST_REQUIRE_MESSAGE( m_execution_path[m_exec_path_point].m_type == EPP_ALLOC, "Function under test exibit non-deterministic behavior" ); else m_execution_path.push_back( execution_path_point( EPP_ALLOC, file, line_num ) ); m_execution_path[m_exec_path_point].m_alloc.ptr = p; m_execution_path[m_exec_path_point].m_alloc.size = s; m_memory_in_use.insert( std::make_pair( p, m_exec_path_point++ ) );}//____________________________________________________________________________//voidexception_safety_tester::freed( void* p ){ if( m_internal_activity ) return; activity_guard ag( m_internal_activity ); registry::iterator it = m_memory_in_use.find( p ); if( it != m_memory_in_use.end() ) { m_execution_path[it->second].m_alloc.ptr = 0; m_memory_in_use.erase( it ); }}//____________________________________________________________________________//voidexception_safety_tester::assertion_result( bool passed ){ if( !m_internal_activity && !passed ) { m_invairant_failed = true; failure_point(); }}//____________________________________________________________________________//voidexception_safety_tester::failure_point(){ if( m_exec_path_counter == m_break_exec_path ) debug::debugger_break(); throw unique_exception();}//____________________________________________________________________________// namespace {inline voidformat_location( wrap_stringstream& formatter, execution_path_point const& /*p*/, unsigned indent ){ if( indent ) formatter << std::left << std::setw( indent ) << "";// !! ?? optional if( p.m_file_name )// formatter << p.m_file_name << '(' << p.m_line_num << "): ";}//____________________________________________________________________________//template<typename ExecPathIt>inline voidformat_execution_path( wrap_stringstream& formatter, ExecPathIt it, ExecPathIt end, unsigned indent = 0 ){ while( it != end ) { switch( it->m_type ) { case EPP_SCOPE: format_location( formatter, *it, indent ); formatter << "> \"" << it->m_scope.name << "\"\n"; format_execution_path( formatter, it+1, it + it->m_scope.size, indent + 2 ); format_location( formatter, *it, indent ); formatter << "< \"" << it->m_scope.name << "\"\n"; it += it->m_scope.size; break; case EPP_DECISION: format_location( formatter, *it, indent ); formatter << "Decision made as " << std::boolalpha << it->m_decision.value << '\n'; ++it; break; case EPP_EXCEPT: format_location( formatter, *it, indent ); formatter << "Forced failure"; if( it->m_except.description ) formatter << ": " << it->m_except.description; formatter << "\n"; ++it; break; case EPP_ALLOC: if( it->m_alloc.ptr ) { format_location( formatter, *it, indent ); formatter << "Allocated memory block 0x" << std::uppercase << it->m_alloc.ptr << ", " << it->m_alloc.size << " bytes long: <"; unsigned i; for( i = 0; i < std::min<std::size_t>( it->m_alloc.size, 8 ); i++ ) { unsigned char c = ((unsigned char*)it->m_alloc.ptr)[i]; if( (std::isprint)( c ) ) formatter << c; else formatter << '.'; } formatter << "> "; for( i = 0; i < std::min<std::size_t>( it->m_alloc.size, 8 ); i++ ) { unsigned c = ((unsigned char*)it->m_alloc.ptr)[i]; formatter << std::hex << std::uppercase << c << ' '; } formatter << "\n"; } ++it; break; } }}//____________________________________________________________________________//} // local namespacevoidexception_safety_tester::report_error(){ activity_guard ag( m_internal_activity ); unit_test_log << unit_test::log::begin( m_execution_path.back().m_file_name, m_execution_path.back().m_line_num ) << log_all_errors; wrap_stringstream formatter; if( m_invairant_failed ) formatter << "Failed invariant"; if( m_memory_in_use.size() != 0 ) { if( m_invairant_failed ) formatter << " and "; formatter << (unsigned int)m_memory_in_use.size() << " memory leak"; if( m_memory_in_use.size() > 1 ) formatter << 's'; } formatter << " detected in the execution path " << m_exec_path_counter << ":\n"; format_execution_path( formatter, m_execution_path.begin(), m_execution_path.end() ); unit_test_log << const_string( formatter.str() ) << unit_test::log::end();}//____________________________________________________________________________//// ************************************************************************** //// ************** exception safety test ************** //// ************************************************************************** //void BOOST_TEST_DECLexception_safety( callback0<> const& F, const_string test_name ){ exception_safety_tester est( test_name ); do { try { F(); } catch( exception_safety_tester::unique_exception const& ) {} } while( est.next_execution_path() );}//____________________________________________________________________________//} // namespace itest} // namespace boost//____________________________________________________________________________//#include <boost/test/detail/enable_warnings.hpp>#endif // non-ancient compiler#endif // BOOST_TEST_EXECUTION_SAFETY_IPP_112005GER
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?