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

📄 execute_test.cpp

📁 Boost provides free peer-reviewed portable C++ source libraries. We emphasize libraries that work
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/* * Distributed under the Boost Software License, Version 1.0.(See accompanying  * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.) *  * See http://www.boost.org/libs/iostreams for documentation. * * Tests the function templates boost::iostreams::detail::execute_all and * boost::iostreams::detail::execute_foreach * * File:        libs/iostreams/test/execute_test.cpp * Date:        Thu Dec 06 13:21:54 MST 2007 * Copyright:   2007-2008 CodeRage, LLC * Author:      Jonathan Turkanis * Contact:     turkanis at coderage dot com */#include <boost/iostreams/detail/execute.hpp>#include <boost/test/test_tools.hpp>#include <boost/test/unit_test.hpp>  using namespace std;using namespace boost;using namespace boost::iostreams;using namespace boost::iostreams::detail;using boost::unit_test::test_suite;// Function object that sets a boolean flag and returns a value// specified at constructiontemplate<typename Result>class operation {public:    typedef Result result_type;    explicit operation(Result r, bool& executed)         : r_(r), executed_(executed)         { }    Result operator()() const     {         executed_ = true;        return r_;     }private:    operation& operator=(const operation&);    Result  r_;    bool&   executed_;  };// Specialization for void returntemplate<>class operation<void> {public:    typedef void result_type;    explicit operation(bool& executed) : executed_(executed) { }    void operator()() const { executed_ = true; }private:    operation& operator=(const operation&);    bool& executed_; };// Simple exception class with error code built in to typetemplate<int Code>struct error { };// Function object that sets a boolean flag and throws an exceptiontemplate<int Code>class thrower {public:    typedef void result_type;    explicit thrower(bool& executed) : executed_(executed) { }    void operator()() const     {         executed_ = true;        throw error<Code>();     }private:    thrower& operator=(const thrower&);    bool& executed_; };// Function object for use by foreach_testclass foreach_func {public:    typedef void result_type;    explicit foreach_func(int& count) : count_(count) { }    void operator()(int x) const    {        ++count_;        switch (x) {        case 0: throw error<0>();        case 1: throw error<1>();        case 2: throw error<2>();        case 3: throw error<3>();        case 4: throw error<4>();        case 5: throw error<5>();        case 6: throw error<6>();        case 7: throw error<7>();        case 8: throw error<8>();        case 9: throw error<9>();        default:            break;        }    }private:    foreach_func& operator=(const foreach_func&);    int&  count_; // Number of times operator() has been called};void success_test(){    // Test returning an int    {        bool executed = false;        BOOST_CHECK(execute_all(operation<int>(9, executed)) == 9);        BOOST_CHECK(executed);    }    // Test returning void    {        bool executed = false;        execute_all(operation<void>(executed));        BOOST_CHECK(executed);    }    // Test returning an int with one cleanup operation    {        bool executed = false, cleaned_up = false;        BOOST_CHECK(            execute_all(                operation<int>(9, executed),                operation<void>(cleaned_up)            ) == 9        );        BOOST_CHECK(executed && cleaned_up);    }    // Test returning void with one cleanup operation    {        bool executed = false, cleaned_up = false;        execute_all(            operation<void>(executed),            operation<void>(cleaned_up)        );        BOOST_CHECK(executed && cleaned_up);    }    // Test returning an int with two cleanup operations    {        bool executed = false, cleaned_up1 = false, cleaned_up2 = false;        BOOST_CHECK(            execute_all(                operation<int>(9, executed),                operation<void>(cleaned_up1),                operation<void>(cleaned_up2)            ) == 9        );        BOOST_CHECK(executed && cleaned_up1 && cleaned_up2);    }    // Test returning void with two cleanup operations    {        bool executed = false, cleaned_up1 = false, cleaned_up2 = false;        execute_all(            operation<void>(executed),            operation<void>(cleaned_up1),            operation<void>(cleaned_up2)        );        BOOST_CHECK(executed && cleaned_up1 && cleaned_up2);    }    // Test returning an int with three cleanup operations    {        bool executed = false, cleaned_up1 = false,              cleaned_up2 = false, cleaned_up3 = false;        BOOST_CHECK(            execute_all(                operation<int>(9, executed),                operation<void>(cleaned_up1),                operation<void>(cleaned_up2),                operation<void>(cleaned_up3)            ) == 9        );        BOOST_CHECK(executed && cleaned_up1 && cleaned_up2 && cleaned_up3);    }    // Test returning void with three cleanup operations    {        bool executed = false, cleaned_up1 = false,              cleaned_up2 = false, cleaned_up3 = false;        execute_all(            operation<void>(executed),            operation<void>(cleaned_up1),            operation<void>(cleaned_up2),            operation<void>(cleaned_up3)        );        BOOST_CHECK(executed && cleaned_up1 && cleaned_up2 && cleaned_up3);    }}void operation_throws_test(){    // Test primary operation throwing with no cleanup operations    {        bool executed = false;        BOOST_CHECK_THROW(            execute_all(thrower<0>(executed)),            error<0>        );        BOOST_CHECK(executed);    }    // Test primary operation throwing with one cleanup operation    {        bool executed = false, cleaned_up = false;        BOOST_CHECK_THROW(            execute_all(                thrower<0>(executed),                operation<void>(cleaned_up)            ),            error<0>        );        BOOST_CHECK(executed && cleaned_up);    }    // Test primary operation throwing with two cleanup operations    {        bool executed = false, cleaned_up1 = false, cleaned_up2 = false;        BOOST_CHECK_THROW(            execute_all(                thrower<0>(executed),                operation<void>(cleaned_up1),                operation<void>(cleaned_up2)            ),             error<0>        );        BOOST_CHECK(executed && cleaned_up1 && cleaned_up2);    }    // Test primary operation throwing with three cleanup operations    {        bool executed = false, cleaned_up1 = false,              cleaned_up2 = false, cleaned_up3 = false;        BOOST_CHECK_THROW(            execute_all(                thrower<0>(executed),                operation<void>(cleaned_up1),                operation<void>(cleaned_up2),                operation<void>(cleaned_up3)            ),             error<0>        );        BOOST_CHECK(executed && cleaned_up1 && cleaned_up2 && cleaned_up3);    }}void cleanup_throws_test(){    // Test single cleanup operation that throws    {        bool executed = false, cleaned_up = false;        BOOST_CHECK_THROW(            execute_all(                operation<void>(executed),                thrower<1>(cleaned_up)            ),            error<1>        );        BOOST_CHECK(executed && cleaned_up);    }    // Test fist of two cleanup operations throwing    {        bool executed = false, cleaned_up1 = false, cleaned_up2 = false;        BOOST_CHECK_THROW(            execute_all(                operation<void>(executed),                thrower<1>(cleaned_up1),                operation<void>(cleaned_up2)            ),            error<1>        );        BOOST_CHECK(executed && cleaned_up1 && cleaned_up2);    }    // Test second of two cleanup operations throwing    {        bool executed = false, cleaned_up1 = false, cleaned_up2 = false;        BOOST_CHECK_THROW(            execute_all(                operation<void>(executed),                operation<void>(cleaned_up1),                thrower<2>(cleaned_up2)            ),            error<2>        );        BOOST_CHECK(executed && cleaned_up1 && cleaned_up2);    }    // Test first of three cleanup operations throwing    {        bool executed = false, cleaned_up1 = false,              cleaned_up2 = false, cleaned_up3 = false;        BOOST_CHECK_THROW(            execute_all(                operation<void>(executed),                thrower<1>(cleaned_up1),                operation<void>(cleaned_up2),                operation<void>(cleaned_up3)            ),             error<1>        );        BOOST_CHECK(executed && cleaned_up1 && cleaned_up2 && cleaned_up3);    }    // Test second of three cleanup operations throwing    {        bool executed = false, cleaned_up1 = false,              cleaned_up2 = false, cleaned_up3 = false;        BOOST_CHECK_THROW(            execute_all(                operation<void>(executed),                operation<void>(cleaned_up1),                thrower<2>(cleaned_up2),                operation<void>(cleaned_up3)            ),             error<2>        );        BOOST_CHECK(executed && cleaned_up1 && cleaned_up2 && cleaned_up3);    }    // Test third of three cleanup operations throwing    {        bool executed = false, cleaned_up1 = false, 

⌨️ 快捷键说明

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