⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 test_tools_test.cpp

📁 Boost provides free peer-reviewed portable C++ source libraries. We emphasize libraries that work
💻 CPP
📖 第 1 页 / 共 2 页
字号:
    BOOST_CHECK_NO_THROW( i++ );    BOOST_CHECK_NO_THROW( throw my_exception() ); // unreachable code warning is expected}//____________________________________________________________________________//struct B {    B( int i ) : m_i( i ) {}    int m_i;};bool          operator==( B const& b1, B const& b2 ) { return b1.m_i == b2.m_i; }std::ostream& operator<<( std::ostream& str, B const& b ) { return str << "B(" << b.m_i << ")"; }//____________________________________________________________________________//struct C {    C( int i, int id ) : m_i( i ), m_id( id ) {}    int m_i;    int m_id;};boost::test_tools::predicate_resultoperator==( C const& c1, C const& c2 ){    boost::test_tools::predicate_result res( c1.m_i == c2.m_i && c1.m_id == c2.m_id );    if( !res ) {        if( c1.m_i != c2.m_i )            res.message() << "Index mismatch";        else            res.message() << "Id mismatch";    }    return res;}std::ostream& operator<<( std::ostream& str, C const& c ) { return str << "C(" << c.m_i << ',' << c.m_id << ")"; }//____________________________________________________________________________//TEST_CASE( test_BOOST_CHECK_EQUAL ){    int i=1;    int j=2;    BOOST_CHECK_EQUAL( i, j );    BOOST_CHECK_EQUAL( ++i, j );    BOOST_CHECK_EQUAL( i++, j );    char const* str1 = "test1";    char const* str2 = "test12";    BOOST_CHECK_EQUAL( str1, str2 );    unit_test_log.set_threshold_level( log_successful_tests );    BOOST_CHECK_EQUAL( i+1, j );    char const* str3 = "1test1";    BOOST_CHECK_EQUAL( str1, str3+1 );    unit_test_log.set_threshold_level( log_all_errors );    str1 = NULL;    str2 = NULL;    BOOST_CHECK_EQUAL( str1, str2 );    str1 = "test";    str2 = NULL;    CHECK_CRITICAL_TOOL_USAGE( BOOST_REQUIRE_EQUAL( str1, str2 ) );    B b1(1);    B b2(2);    unit_test_log.set_threshold_level( log_warnings );    BOOST_WARN_EQUAL( b1, b2 );    unit_test_log.set_threshold_level( log_all_errors );    C c1( 0, 100 );    C c2( 0, 101 );    C c3( 1, 102 );    BOOST_CHECK_EQUAL( c1, c3 );    BOOST_CHECK_EQUAL( c1, c2 );    char ch1 = -2;    char ch2 = -3;    BOOST_CHECK_EQUAL(ch1, ch2);}//____________________________________________________________________________//TEST_CASE( test_BOOST_CHECK_LOGICAL_EXPR ){    int i=1;    int j=2;    BOOST_CHECK_NE( i, j );    BOOST_CHECK_NE( ++i, j );    BOOST_CHECK_LT( i, j );    BOOST_CHECK_GT( i, j );    BOOST_CHECK_LE( i, j );    BOOST_CHECK_GE( i, j );    ++i;    BOOST_CHECK_LE( i, j );    BOOST_CHECK_GE( j, i );    char const* str1 = "test1";    char const* str2 = "test1";    BOOST_CHECK_NE( str1, str2 );}//____________________________________________________________________________//bool is_even( int i )        { return i%2 == 0;  }int  foo( int arg, int mod ) { return arg % mod; }bool moo( int arg1, int arg2, int mod ) { return ((arg1+arg2) % mod) == 0; }BOOST_TEST_DONT_PRINT_LOG_VALUE( std::list<int> )boost::test_tools::predicate_resultcompare_lists( std::list<int> const& l1, std::list<int> const& l2 ){    if( l1.size() != l2.size() ) {        boost::test_tools::predicate_result res( false );        res.message() << "Different sizes [" << l1.size() << "!=" << l2.size() << "]";        return res;    }    return true;}TEST_CASE( test_BOOST_CHECK_PREDICATE ){    BOOST_CHECK_PREDICATE( is_even, (14) );    int i = 17;    BOOST_CHECK_PREDICATE( is_even, (i) );    using std::not_equal_to;    BOOST_CHECK_PREDICATE( not_equal_to<int>(), (i)(17) );    int j=15;    BOOST_CHECK_PREDICATE( boost::bind( is_even, boost::bind( &foo, _1, _2 ) ), (i)(j) );    unit_test_log.set_threshold_level( log_warnings );    BOOST_WARN_PREDICATE( moo, (12)(i)(j) );    unit_test_log.set_threshold_level( log_all_errors );    std::list<int> l1, l2, l3;    l1.push_back( 1 );    l3.push_back( 1 );    l1.push_back( 2 );    l3.push_back( 3 );    BOOST_CHECK_PREDICATE( compare_lists, (l1)(l2) );}//____________________________________________________________________________//TEST_CASE( test_BOOST_REQUIRE_PREDICATE ){    int arg1 = 1;    int arg2 = 2;    using std::less_equal;    CHECK_CRITICAL_TOOL_USAGE( BOOST_REQUIRE_PREDICATE( less_equal<int>(), (arg1)(arg2) ) );    CHECK_CRITICAL_TOOL_USAGE( BOOST_REQUIRE_PREDICATE( less_equal<int>(), (arg2)(arg1) ) );}//____________________________________________________________________________//TEST_CASE( test_BOOST_CHECK_EQUAL_COLLECTIONS ){    unit_test_log.set_threshold_level( log_all_errors );    int pattern [] = { 1, 2, 3, 4, 5, 6, 7 };    std::list<int> testlist;    testlist.push_back( 1 );    testlist.push_back( 2 );    testlist.push_back( 4 ); // 3    testlist.push_back( 4 );    testlist.push_back( 5 );    testlist.push_back( 7 ); // 6    testlist.push_back( 7 );    BOOST_CHECK_EQUAL_COLLECTIONS( testlist.begin(), testlist.end(), pattern, pattern+7 );    BOOST_CHECK_EQUAL_COLLECTIONS( testlist.begin(), testlist.end(), pattern, pattern+2 );}//____________________________________________________________________________//TEST_CASE( test_BOOST_CHECK_BITWISE_EQUAL ){    BOOST_CHECK_BITWISE_EQUAL( 0x16, 0x16 );    BOOST_CHECK_BITWISE_EQUAL( (char)0x06, (char)0x16 );    unit_test_log.set_threshold_level( log_warnings );    BOOST_WARN_BITWISE_EQUAL( (char)0x26, (char)0x04 );    unit_test_log.set_threshold_level( log_all_errors );    CHECK_CRITICAL_TOOL_USAGE( BOOST_REQUIRE_BITWISE_EQUAL( (char)0x26, (int)0x26 ) );}//____________________________________________________________________________//struct A {    friend std::ostream& operator<<( std::ostream& str, A const& a ) { str << "struct A"; return str;}};TEST_CASE( test_BOOST_TEST_MESSAGE ){    unit_test_log.set_threshold_level( log_messages );    BOOST_TEST_MESSAGE( "still testing" );    BOOST_TEST_MESSAGE( "1+1=" << 2 );    int i = 2;    BOOST_TEST_MESSAGE( i << "+" << i << "=" << (i+i) );    A a = A();    BOOST_TEST_MESSAGE( a );#if BOOST_TEST_USE_STD_LOCALE    BOOST_TEST_MESSAGE( std::hex << std::showbase << 20 );#else    BOOST_TEST_MESSAGE( "0x14" );#endif    BOOST_TEST_MESSAGE( std::setw( 4 ) << 20 );}//____________________________________________________________________________//TEST_CASE( test_BOOST_TEST_CHECKPOINT ){    BOOST_TEST_CHECKPOINT( "Going to do a silly things" );    throw "some error";}//____________________________________________________________________________//bool foo() { throw 1; return true; }TEST_CASE( test_BOOST_TEST_PASSPOINT ){    BOOST_CHECK( foo() );}//____________________________________________________________________________//TEST_CASE( test_BOOST_IS_DEFINED ){#define SYMBOL1#define SYMBOL2 std::cout#define ONE_ARG( arg ) arg#define TWO_ARG( arg1, arg2 ) BOOST_JOIN( arg1, arg2 )    BOOST_CHECK( BOOST_IS_DEFINED(SYMBOL1) );    BOOST_CHECK( BOOST_IS_DEFINED(SYMBOL2) );    BOOST_CHECK( !BOOST_IS_DEFINED( SYMBOL3 ) );    BOOST_CHECK( BOOST_IS_DEFINED( ONE_ARG(arg1) ) );    BOOST_CHECK( !BOOST_IS_DEFINED( ONE_ARG1(arg1) ) );    BOOST_CHECK( BOOST_IS_DEFINED( TWO_ARG(arg1,arg2) ) );    BOOST_CHECK( !BOOST_IS_DEFINED( TWO_ARG1(arg1,arg2) ) );}//____________________________________________________________________________//// !! CHECK_SMALL// EOF

⌨️ 快捷键说明

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