DIV CSS 佈局教程網

您现在的位置: DIV+CSS佈局教程網 >> 網頁腳本 >  >> JavaScript入門知識 >> AJAX入門 >> AJAX詳解
Boost 檢查(測試)函式庫示例(RobinKin 整理)
編輯:AJAX詳解     

Boost 的 test_tools 提供了一組便利的巨集,用來在程式中進行條件檢查與錯誤回報。以下範例示範了六種偵測並回報同一個錯誤的寫法:

//file name: test.cpp   RobinKin 整理
#include <boost/test/test_tools.hpp>

int add( int i, int j ) { return i+j; }

int test_main( int, char* [] )  // 注意函式名稱!
{
    // 用六種方式偵測並回報同一個錯誤:
    BOOST_CHECK( add(2,2) == 4 );          // #1 出錯時繼續執行

    BOOST_REQUIRE( add(2,2) == 4 );        // #2 出錯時丟出例外

    if ( add(2,2) != 4 )
        BOOST_ERROR( "Ouch...");           // #3 出錯時繼續執行

    if ( add(2,2) != 4 )
        BOOST_FAIL( "Ouch..." );           // #4 出錯時丟出例外

    if ( add(2,2) != 4 )
        throw "Oops...";                 // #5 出錯時丟出例外

    return add(2,2) == 4 ? 0 : 1;           // #6 回傳錯誤碼
}

// EOF
//編譯方法
g++ test -lboost_test_exec_monitor-gcc test.cpp

從範例不難看出,BOOST_CHECK 與 BOOST_ERROR 屬於「出錯後繼續執行」這一族,而 BOOST_REQUIRE 與 BOOST_FAIL 則是「出錯即丟出例外」;最後一種則是以回傳值的方式來表達測試結果。開發者可依據測試情境,彈性挑選最適合的檢查巨集。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved