• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            不倦的候鳥成長(zhǎng)日記

            ——候鳥,候補(bǔ)的菜鳥也
            隨筆 - 6, 文章 - 0, 評(píng)論 - 5, 引用 - 0
            數(shù)據(jù)加載中……

            [轉(zhuǎn)]Boost Test Library

            boost 的 Test

            test 庫中有如下的組件:
            Execution Monitor  一個(gè)基本用于 program 和 test program 的異常與錯(cuò)誤檢測(cè)與報(bào)告機(jī)制,Execution Monitor 調(diào)用用戶提供的函數(shù)并報(bào)告所有捕獲的運(yùn)行時(shí)的異常,它只被其他 Boost Test Library components 內(nèi)部調(diào)用,當(dāng)然也可以用于一些 production environment 控制那些會(huì)導(dǎo)致程序崩潰的函數(shù)的調(diào)用;

            Program Execution Monitor  一個(gè)簡(jiǎn)單的 helper facility 用于監(jiān)控一個(gè)程序的運(yùn)行,Program Execution Monitor 提供了 main() 函數(shù)和 Execution Monitor 監(jiān)控程序的執(zhí)行,可以用以 production environment 產(chǎn)生一致錯(cuò)誤報(bào)告,控制在 test environment 環(huán)境中運(yùn)行的程序,直接使用 Test Execution Monitor;

            Test Tools  一個(gè)用以進(jìn)行 testing 的一個(gè) toolbox,Test Tools 被用來測(cè)試在 Test Execution Monitor 或 Unit Test Framework 控制下運(yùn)行的程序;

            Test Execution Monitor  讓一個(gè)測(cè)試程序在 monitored environment 環(huán)境中運(yùn)行,Test Execution Monitor 提供了 main() 來控制被測(cè)試程序的運(yùn)行并可以讓 Test Tools 實(shí)現(xiàn)測(cè)試的邏輯,它被用在 test environment,如果要控制 production code 的運(yùn)行使用 Program Execution Monitor;

            Unit Test Framework  用以簡(jiǎn)化編寫和組織 test cases 的 framework,支持簡(jiǎn)單函數(shù)或者是成員函數(shù)編寫的 test cases 并將他們組織成一個(gè) test suites 的 tree,該 framework 使用 Test Tools 來實(shí)現(xiàn) test cases 并提供了一種機(jī)制用以管理 log report level 和 result report level;

            minimal testing facility  提供 Boost Test 最初版本的提供的功能的簡(jiǎn)單 facility,提供了和 Test Execution Monitor 一樣的機(jī)制,但額外定義了一些簡(jiǎn)單提供 Test Tools 類似功能的 test tools,它不需要和任何的外部組件 link,適合簡(jiǎn)單和快速測(cè)試的需要,使用于 test environment。
            (http://yaekees.spaces.live.com/blog/cns!1955EE8C6707277A!146.entry)


            boost Test 的 Execution Monitor

            使用 Execution Monitor 的三部曲:
            1. #include <boost/test/execution_monitor.hpp>
            2. Make an instance of boost::execution_monitor
            3. Optionally register custom exception translators for exception classes you want special processing
             
            調(diào)用 execution_monitor::execute( function_to_monitor, catch_system_exception, timeout ) 運(yùn)行 monitored function。如果調(diào)用成功則返回一個(gè) integer value,如果有如下的事情發(fā)生
            1. Uncaught C++ exception
            2. hardware or software signal, trap, or other exception
            3. Timeout reached
            4. debug assert event occurred (under Microsoft Visual C++ or compatible compiler)
            the method execution_monitor::execute( ... ) throws the boost::execution_exception
            如果希望程序 error message 被轉(zhuǎn)化為  execution_exception 的 error message,則扔出如下的三類異常:C string,std:string,any exception class in std::exception hierarchy。
             
            終止 monitored function 而不讓 Execution Monitor 報(bào)告 any error 的最佳方法是拋出 boost::execution_aborted。如果不喜歡 "unknown exception caught" message 而更愿意使用自定義的 exception,可以向 execution monitor 為 any exception types 注冊(cè) translator 函數(shù),如
            ex_mon.register_exception_translator<my_exception1>( &translate_my_exception1 );
            my_exception1 是異常類型,translate_my_exception1 是異常處理函數(shù)。
            class execution_monitor {
            public:
                virtual     ~execution_monitor();
               
                template<typename Exception, typename ExceptionTranslator>
                void        register_exception_translator( ExceptionTranslator const& tr, boost::type<Exception>* = 0 );
             
                int         execute( unit_test::callback0<int> const& F, bool catch_system_errors = true, int timeout = 0 );
            }; // exception monitor
             
            Execution Monitor 用 boost::execution_exception 報(bào)告捕獲的問題,其最大的特點(diǎn)是不分配任何 memory 因此在內(nèi)存稀缺環(huán)境中使用。
            class execution_exception {
            public:
                execution_exception( error_code ec, const_string what_msg );
                enum error_code {
                    cpp_exception_error,    // see note (1) below
                    user_error,             // user reported nonfatal error
                    system_error,           // see note (2) below
                    timeout_error,          // only detectable on certain platforms
                    user_fatal_error,       // user reported fatal error
                    system_fatal_error      // see note (2) below
                };
                error_code   code() const;  // use this method to get an error code for the exception
                const_string what() const;  // use this method to get an error message for the exception
            };
            Note 1 :uncaught C++ exceptions 被當(dāng)做 error,如果應(yīng)用程序捕獲到 C++ exception,則該 exception 不會(huì)到 boost::execution_monitor;
            Note 2 :這些 error 包括 UNIX signals 和 Windows structured exceptions,這些經(jīng)常由 hardware traps 觸發(fā)。

            execution_monitor 可以動(dòng)態(tài)連接,庫中沒有提供 main 函數(shù)。由于只有一個(gè) libs/test/execution_monitor.cpp 文件,因此可以直接 copy 該文件。
            (http://yaekees.spaces.live.com/blog/cns!1955EE8C6707277A!148.entry)


            boost Test 的 Program Execution Monitor

            C++ program 可以通過 return value 和 throwing an exception 報(bào)告 user-detected errors,而如 dereferencing an invalid pointer 的 System-detected errors 則以其他方式報(bào)告。
             
            Boost Test Library 的 Program Execution Monitor 減輕了用戶處理復(fù)雜的 error detection 和 reporting,并提供了 main() 函數(shù)在 monitored environment 來調(diào)用用戶提供的 cpp_main() 函數(shù),main() 函數(shù)以一致的方式檢測(cè)和報(bào)告多種 errors 的發(fā)生,將其轉(zhuǎn)化為一致的 return code 返回給 host enviroment。
            BOOST_TEST_CATCH_SYSTEM_ERRORS 設(shè)置允許 Execution Monitor 捕獲 system errors,默認(rèn)值為 "yes"。
            BOOST_PRG_MON_CONFIRM 設(shè)置是否允許用戶交互確認(rèn)程序正確運(yùn)行,默認(rèn)值為"yes"。
             
            Program Execution Monitor 使用 Execution Monitor 監(jiān)視用戶提供的 cpp_main() 函數(shù)的運(yùn)行。盡管 Program Execution Monitor 在 libs/test/src/cpp_main.cpp 提供了 main() 函數(shù),但是為了 link 正確,用戶必須提供一個(gè)與 main() 函數(shù)一樣接口的 cpp_main() 函數(shù)。如 cpp_main() 拋出異常或返回非 0 值,Program Execution Monitor 就認(rèn)為程序發(fā)生錯(cuò)誤。
            Program Execution Monitor 在 cout 和 cerr 流上分別提供詳細(xì)和簡(jiǎn)要的錯(cuò)誤報(bào)告。Program Execution Monitor 提供的 main() 函數(shù)返回如下的值
            1. boost::exit_success - no errors
            2. boost::exit_failure - non-zero and non-boost::exit_success return code from cpp_main().
            3. boost::exit_exception_failure - cpp_main() throw an exception.
            (http://yaekees.spaces.live.com/blog/cns!1955EE8C6707277A!149.entry)


            boost Test 的 Test Tools

            Test Tools 為了讓用戶使用方便,提供了一系列的宏,這些宏分三級(jí),其效果是不同的:
            WARN 不增加引用計(jì)數(shù),繼續(xù)執(zhí)行程序
            CHECK 增加應(yīng)用計(jì)數(shù),繼續(xù)執(zhí)行程序
            REQUIRE 增加應(yīng)用計(jì)數(shù),中斷程序的運(yùn)行
             
            使用 CHECK level tools 實(shí)現(xiàn) assertions,使用 WARN level tools 檢驗(yàn)不太重要但是正確的方面,如:性能、可移植性、有用性,如 assertions 失敗就不應(yīng)該讓程序繼續(xù)運(yùn)行則使用 REQUIRE level tools。
             
            Test Tools 提供了兩個(gè) .hpp 文件 boost/test/test_tools.hpp 和 boost/test/floating_point_comparison.hpp,一個(gè) .cpp 文件 libs/test/test_tools.cpp
            BOOST_WARN( P )
            BOOST_CHECK( P )
            BOOST_REQUIRE( P )
            BOOST_WARN_MESSAGE( P, M )
            BOOST_CHECK_MESSAGE( P, M )
            BOOST_REQUIRE_MESSAGE( P, M )
            BOOST_ERROR( M )BOOST_FAIL( M )
            BOOST_MESSAGE( M )
            BOOST_CHECKPOINT( M )
            BOOST_WARN_THROW( S, E )
            BOOST_CHECK_THROW( S, E )
            BOOST_REQUIRE_THROW( S, E )
            BOOST_WARN_EXCEPTION( S, E, P )
            BOOST_CHECK_EXCEPTION( S, E, P )
            BOOST_REQUIRE_EXCEPTION( S, E, P )
            BOOST_IGNORE_CHECK( e )
            BOOST_WARN_NO_THROW( S )
            BOOST_CHECK_NO_THROW( S )
            BOOST_REQUIRE_NO_THROW( S )
            BOOST_WARN_CLOSE( L, R, T )
            BOOST_CHECK_CLOSE( L, R, T )
            BOOST_REQUIRE_CLOSE( L, R, T )
            BOOST_WARN_SMALL( FPV, T )
            BOOST_CHECK_SMALL( FPV, T )
            BOOST_REQUIRE_SMALL( FPV, T )
            BOOST_WARN_PREDICATE( P, ARGS )
            BOOST_CHECK_PREDICATE( P, ARGS )
            BOOST_REQUIRE_PREDICATE( P, ARGS )
            BOOST_WARN_EQUAL_COLLECTIONS( L_begin, L_end, R_begin, R_end )
            BOOST_CHECK_EQUAL_COLLECTIONS( L_begin, L_end, R_begin, R_end )
            BOOST_REQUIRE_EQUAL_COLLECTIONS( L_begin, L_end, R_begin, R_end )
            BOOST_WARN_BITWISE_EQUAL( L, R )
            BOOST_CHECK_BITWISE_EQUAL( L, R )
            BOOST_REQUIRE_BITWISE_EQUAL( L, R )
            BOOST_IS_DEFINED( symb )
            BOOST_BITWISE_EQUAL( L, R )
            (http://yaekees.spaces.live.com/blog/cns!1955EE8C6707277A!145.entry)


            boost Test 的 Test Execution Monitor

            Test Execution Monitor 結(jié)合了 Test Tools 和 Execution Monitor 的特點(diǎn)簡(jiǎn)化了煩瑣測(cè)試工作,它提供了 main() 函數(shù)調(diào)用用戶提供的 test_main() 函數(shù),用戶可以使用 Test Tools 來進(jìn)行復(fù)雜的驗(yàn)證工作。
             
            Test Execution Monitor 被設(shè)計(jì)用來進(jìn)行測(cè)試簡(jiǎn)單的程序或者從已存在的 production code 中 dig a problem。Program Execution Monitor 更適合監(jiān)控 production (non-test) programs(因?yàn)樗挥绊懗绦虻男阅埽?Unit Test Framework 更適合 complex test programs,因?yàn)?Unit Test Framework 可以
            1. 可以將 test 分割到多個(gè) test cases,它會(huì)為每一個(gè) test case 分別產(chǎn)生 pass/fail 的統(tǒng)計(jì)信息;
            2. 假如某一個(gè) test case 失敗了不會(huì)影響其他的 test;
            3. 可以通過指定 name 來運(yùn)行特定的 test case
            4. 分離的 test cases 運(yùn)行更清楚發(fā)現(xiàn)特定測(cè)試模塊的目的
            5. 可以設(shè)置更多的選項(xiàng)
            (http://yaekees.spaces.live.com/blog/cns!1955ee8c6707277a!150.entry)


            boost Test 的 Unit Test Framework

            regression testing 只關(guān)注程序運(yùn)行的時(shí)候是否有錯(cuò)誤發(fā)生,而 unit test 則需要盡可能詳細(xì)的輸出錯(cuò)誤的信息。Unit Test Framework 用 test tools 簡(jiǎn)化了 test cases 的編寫并將其組織為有層次的 test suites。它提供了 main() 函數(shù)初始化 framework,通過命令行參數(shù)或者是環(huán)境變量設(shè)置參數(shù),并通過 init_unit_test_suite(argc, argv),然后運(yùn)行用戶的 test suite。Framework 跟蹤所有的 passed/failed 的 Test Tools assertions,可以通過 test cases 的數(shù)目(part 和 total)得到測(cè)試進(jìn)度,并且可以多種形式提供結(jié)果。Unit Test Framework 可被用來進(jìn)行簡(jiǎn)單測(cè)試和復(fù)雜重要測(cè)試,它不適合用在 production code,同時(shí)它以運(yùn)行時(shí)的效率為代價(jià)加速編譯。
             
            該函數(shù)負(fù)責(zé)創(chuàng)建和初始化頂層的 test_suite 實(shí)例,如 test_suite 創(chuàng)建失敗該函數(shù)返回 NULL 指針,測(cè)試終止并返回一個(gè) boost::exit_test_failure。Framework 在測(cè)試運(yùn)行時(shí)傳遞特定的命令行參數(shù),同時(shí)排斥其他 framework 指定的參數(shù),同時(shí) framework 負(fù)責(zé) test_suite 的生命周期,在 test 終止時(shí)會(huì)被銷毀。
             
            假如 test cases 會(huì)丟出自定義的異常,可以像 Execution Monitor 那樣注冊(cè)特定的 translator,注冊(cè)函數(shù)的原型定義如下
            template<typename Exception, typename ExceptionTranslator>
            void boost::unit_test::register_exception_translator( ExceptionTranslator const& tr, boost::type<Exception>* d = 0 )
             
            一旦測(cè)試結(jié)束,framework 會(huì)報(bào)告結(jié)果并返回 return code。下面是集成在 unit test framework 內(nèi)部的的返回值
            boost::exit_success  returned if no errors occurred during test or success result code was explicitly requested with the no result code framework parameter
            boost::exit_test_failure  returned if nonfatal errors detected and no uncaught exceptions thrown or the framework fails to initialize the test suite
            boost::exit_exception_failure  returned if fatal errors detected or uncaught exceptions thrown
             
            在 VC7.1+stlport 4.62 上 unit_test_example3.cpp 沒有通過(對(duì)多種繼承沒有通過)
             
            簡(jiǎn)單的使用方法:
            1. 首先定義 #define BOOST_AUTO_TEST_MAIN
            2. 包含 #include <boost/test/auto_unit_test.hpp>
            3. 創(chuàng)建一個(gè) test_suite
            BOOST_AUTO_TEST_CASE( test )
            {
                BOOST_CHECK( true );
            }
            然后 link libboost_test_exec_monitor-vc71-mt-sp-1_33.lib 就可以了。
             
            另一類使用方法,首先包含如下
            #include <boost/test/unit_test.hpp>
            #include <boost/test/unit_test_monitor.hpp>
            using namespace boost::unit_test;
            然后聲明 test_suite* init_unit_test_suite(int /*argc*/, char* /*argv*/[]) {}
            一個(gè)典型用法是
            test_suite* init_unit_test_suite( int /*argc*/, char* /*argv*/[] ) {
                test_suite* test = BOOST_TEST_SUITE("custom_exception_test");
                unit_test_monitor.register_exception_translator<my_exception1>( &my_exception1_translator );
                unit_test_monitor.register_exception_translator<my_exception2>( &my_exception2_translator );
                test->add( BOOST_TEST_CASE( &throw_my_exception1 ) );
                test->add( BOOST_TEST_CASE( &throw_my_exception2 ) );
                return test;
            }
            (http://yaekees.spaces.live.com/blog/cns!1955EE8C6707277A!151.entry)


            boost 的 minimal testing facility

            只適合使用在 test environment 中,不需要 link 任何外部的組件。用戶只需要提供了如下的函數(shù)
            int test_main( int argc, char* argv[] ) 就可以了。minimal testing facility 提供了 BOOST_CHECK(predicate),BOOST_REQUIRE(predicate),BOOST_ERROR(message), BOOST_FAIL(message)。除了這四個(gè) MACRO 以外可以通過拋出異常和 return 返回值報(bào)告錯(cuò)誤。下面是 boost 提供的一個(gè)示例。
            #include <boost/test/minimal.hpp>
            int add( int i, int j ) { return i+j; }
            int test_main( int, char *[] )             // note the name!
            {
                // six ways to detect and report the same error:
                BOOST_CHECK( add( 2,2 ) == 4 );        // #1 continues on error
                BOOST_REQUIRE( add( 2,2 ) == 4 );      // #2 throws on error
                if( add( 2,2 ) != 4 )
                  BOOST_ERROR( "Ouch..." );            // #3 continues on error
                if( add( 2,2 ) != 4 )
                  BOOST_FAIL( "Ouch..." );             // #4 throws on error
                if( add( 2,2 ) != 4 ) throw "Oops..."; // #5 throws on error
                return add( 2, 2 ) == 4 ? 0 : 1;       // #6 returns error code
            }
            BOOST_CHECK 如果該表達(dá)式失敗了,會(huì)出現(xiàn)源代碼文件名、代碼行號(hào)、并且會(huì)增加 error count,一旦程序終止 error count 會(huì)顯示在 std::cout;
            BOOST_REQUIRE 類似于 BOOST_CHECK,但是會(huì)拋出被  Minimal testing facility 捕獲的異常。
            (http://yaekees.spaces.live.com/blog/cns!1955EE8C6707277A!147.entry)

            posted on 2008-03-29 15:39 不倦 閱讀(3305) 評(píng)論(1)  編輯 收藏 引用 所屬分類: C++單元測(cè)試框架

            評(píng)論

            # re: [轉(zhuǎn)]Boost Test Library  回復(fù)  更多評(píng)論   

            不錯(cuò)不錯(cuò) 很受教啊
            美中不足的是具體的例子舉的太少了
            2008-06-12 11:07 | jazz
            亚洲国产精品久久久久| 美女写真久久影院| 久久久久人妻一区精品色 | 欧美精品国产综合久久| 777午夜精品久久av蜜臀 | 久久精品国产一区二区| 久久免费99精品国产自在现线| 国产A级毛片久久久精品毛片| 国产午夜久久影院| 久久亚洲AV成人无码| 久久99热精品| 久久久久av无码免费网| 国产精品99久久久久久宅男 | 久久久久亚洲av成人无码电影| 久久笫一福利免费导航| 热re99久久精品国产99热| 午夜精品久久久久久久久| 国产免费久久精品99久久| 国内精品久久久久影院一蜜桃 | 国产精品国色综合久久| 久久人与动人物a级毛片| 国内精品久久久久久久久| 国内精品久久久久| 人妻无码中文久久久久专区| 色8激情欧美成人久久综合电| 久久精品无码一区二区三区| 日日噜噜夜夜狠狠久久丁香五月| 久久久久久久久久免免费精品| 久久99精品久久久久久hb无码 | 久久婷婷国产剧情内射白浆| 久久久久97国产精华液好用吗| 26uuu久久五月天| 久久国产乱子伦精品免费强| 久久天天躁狠狠躁夜夜网站| 天天爽天天狠久久久综合麻豆| 色天使久久综合网天天| 一级女性全黄久久生活片免费 | 色综合合久久天天给综看| 久久精品国产黑森林| 国产亚洲美女精品久久久| 品成人欧美大片久久国产欧美...|