test_tools.ipp

来自「Boost provides free peer-reviewed portab」· IPP 代码 · 共 631 行 · 第 1/2 页

IPP
631
字号
        char const* left_begin_descr    = va_arg( args, char const* );        char const* left_end_descr      = va_arg( args, char const* );        char const* right_begin_descr   = va_arg( args, char const* );        char const* right_end_descr     = va_arg( args, char const* );        unit_test_log << unit_test::log::begin( file_name, line_num )                       << ll << prefix                       << "{ " << left_begin_descr  << ", " << left_end_descr  << " } == { "                               << right_begin_descr << ", " << right_end_descr << " }"                      << suffix;        va_end( args );                if( !pr.has_empty_message() )            unit_test_log << ". " << pr.message();        unit_test_log << unit_test::log::end();        break;    }    case CHECK_BITWISE_EQUAL: {        va_list args;        va_start( args, num_of_args );        char const* left_descr    = va_arg( args, char const* );        char const* right_descr   = va_arg( args, char const* );        unit_test_log << unit_test::log::begin( file_name, line_num )                      << ll << prefix << left_descr  << " =.= " << right_descr << suffix;        va_end( args );                if( !pr.has_empty_message() )            unit_test_log << ". " << pr.message();        unit_test_log << unit_test::log::end();        break;    }    }    switch( tl ) {    case PASS:        framework::assertion_result( true );        return true;    case WARN:        return false;    case CHECK:        framework::assertion_result( false );        return false;            case REQUIRE:        framework::assertion_result( false );        framework::test_unit_aborted( framework::current_test_case() );        throw execution_aborted();    }    return true;}//____________________________________________________________________________//predicate_resultequal_impl( char const* left, char const* right ){    return (left && right) ? std::strcmp( left, right ) == 0 : (left == right);}//____________________________________________________________________________//#if !defined( BOOST_NO_CWCHAR )predicate_resultequal_impl( wchar_t const* left, wchar_t const* right ){    return (left && right) ? std::wcscmp( left, right ) == 0 : (left == right);}#endif // !defined( BOOST_NO_CWCHAR )//____________________________________________________________________________//boolis_defined_impl( const_string symbol_name, const_string symbol_value ){    symbol_value.trim_left( 2 );    return symbol_name != symbol_value;}//____________________________________________________________________________//} // namespace tt_detail// ************************************************************************** //// **************               output_test_stream             ************** //// ************************************************************************** //struct output_test_stream::Impl{    std::fstream    m_pattern;    bool            m_match_or_save;    bool            m_text_or_binary;    std::string     m_synced_string;    char            get_char()    {        char res;        do {            m_pattern.get( res );        } while( m_text_or_binary && res == '\r' && !m_pattern.fail() && !m_pattern.eof() );        return res;    }    void            check_and_fill( predicate_result& res )    {        if( !res.p_predicate_value )            res.message() << "Output content: \"" << m_synced_string << '\"';    }};//____________________________________________________________________________//output_test_stream::output_test_stream( const_string pattern_file_name, bool match_or_save, bool text_or_binary ): m_pimpl( new Impl ){    if( !pattern_file_name.is_empty() ) {        std::ios::openmode m = match_or_save ? std::ios::in : std::ios::out;        if( !text_or_binary )            m |= std::ios::binary;        m_pimpl->m_pattern.open( pattern_file_name.begin(), m );        BOOST_WARN_MESSAGE( m_pimpl->m_pattern.is_open(),                             "Couldn't open pattern file " << pattern_file_name                                << " for " << (match_or_save ? "reading" : "writing") );    }    m_pimpl->m_match_or_save    = match_or_save;    m_pimpl->m_text_or_binary   = text_or_binary;}//____________________________________________________________________________//output_test_stream::~output_test_stream(){    delete m_pimpl;}//____________________________________________________________________________//predicate_resultoutput_test_stream::is_empty( bool flush_stream ){    sync();    result_type res( m_pimpl->m_synced_string.empty() );    m_pimpl->check_and_fill( res );    if( flush_stream )        flush();    return res;}//____________________________________________________________________________//predicate_resultoutput_test_stream::check_length( std::size_t length_, bool flush_stream ){    sync();    result_type res( m_pimpl->m_synced_string.length() == length_ );    m_pimpl->check_and_fill( res );    if( flush_stream )        flush();    return res;}//____________________________________________________________________________//predicate_resultoutput_test_stream::is_equal( const_string arg, bool flush_stream ){    sync();    result_type res( const_string( m_pimpl->m_synced_string ) == arg );    m_pimpl->check_and_fill( res );    if( flush_stream )        flush();    return res;}//____________________________________________________________________________//predicate_resultoutput_test_stream::match_pattern( bool flush_stream ){    sync();    result_type result( true );    if( !m_pimpl->m_pattern.is_open() ) {        result = false;        result.message() << "Pattern file could not be opened!";    }    else {        if( m_pimpl->m_match_or_save ) {            for ( std::string::size_type i = 0; i < m_pimpl->m_synced_string.length(); ++i ) {                char c = m_pimpl->get_char();                result = !m_pimpl->m_pattern.fail() &&                         !m_pimpl->m_pattern.eof()  &&                         (m_pimpl->m_synced_string[i] == c);                if( !result ) {                    std::string::size_type suffix_size  = (std::min)( m_pimpl->m_synced_string.length() - i,                                                                    static_cast<std::string::size_type>(5) );                    // try to log area around the mismatch                    result.message() << "Mismatch at position " << i << '\n'                        << "..." << m_pimpl->m_synced_string.substr( i, suffix_size ) << "..." << '\n'                        << "..." << c;                    std::string::size_type counter = suffix_size;                    while( --counter ) {                        char c = m_pimpl->get_char();                        if( m_pimpl->m_pattern.fail() || m_pimpl->m_pattern.eof() )                            break;                        result.message() << c;                    }                    result.message() << "...";                    // skip rest of the bytes. May help for further matching                    m_pimpl->m_pattern.ignore(                         static_cast<std::streamsize>( m_pimpl->m_synced_string.length() - i - suffix_size) );                    break;                }            }        }        else {            m_pimpl->m_pattern.write( m_pimpl->m_synced_string.c_str(),                                      static_cast<std::streamsize>( m_pimpl->m_synced_string.length() ) );            m_pimpl->m_pattern.flush();        }    }    if( flush_stream )        flush();    return result;}//____________________________________________________________________________//voidoutput_test_stream::flush(){    m_pimpl->m_synced_string.erase();#ifndef BOOST_NO_STRINGSTREAM    str( std::string() );#else    seekp( 0, std::ios::beg );#endif}//____________________________________________________________________________//std::size_toutput_test_stream::length(){    sync();    return m_pimpl->m_synced_string.length();}//____________________________________________________________________________//voidoutput_test_stream::sync(){#ifdef BOOST_NO_STRINGSTREAM    m_pimpl->m_synced_string.assign( str(), pcount() );    freeze( false );#else    m_pimpl->m_synced_string = str();#endif}//____________________________________________________________________________//} // namespace test_tools} // namespace boost//____________________________________________________________________________//#include <boost/test/detail/enable_warnings.hpp>#endif // BOOST_TEST_TEST_TOOLS_IPP_012205GER

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?