123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679 |
- // (C) Copyright 2008-10 Anthony Williams
- //
- // 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)
- #include <utility>
- #include <memory>
- #include <stdexcept>
- #include <string>
- #include <boost/test/unit_test.hpp>
- #include <boost/fiber/all.hpp>
- int gi = 7;
- struct my_exception : public std::runtime_error {
- my_exception() :
- std::runtime_error("my_exception") {
- }
- };
- struct A {
- A() = default;
- A( A const&) = delete;
- A( A &&) = default;
- A & operator=( A const&) = delete;
- A & operator=( A &&) = default;
- int value;
- };
- struct B {
- bool bset{ false };
- B() = default;
- B( bool set) :
- bset{ set } {
- gi = 3;
- }
- ~B() {
- if ( bset) {
- gi = -1;
- }
- }
- B( B && other) :
- bset{ other.bset } {
- other.bset = false;
- }
- B & operator=( B && other) {
- if ( this == & other) return * this;
- bset = other.bset;
- other.bset = false;
- return * this;
- }
- B( B const&) = delete;
- B & operator=( B const&) = delete;
- };
- void fn1( boost::fibers::promise< int > * p, int i) {
- boost::this_fiber::yield();
- p->set_value( i);
- }
- void fn2() {
- boost::fibers::promise< int > p;
- boost::fibers::future< int > f( p.get_future() );
- boost::this_fiber::yield();
- boost::fibers::fiber( boost::fibers::launch::post, fn1, & p, 7).detach();
- boost::this_fiber::yield();
- BOOST_CHECK( 7 == f.get() );
- }
- int fn3() {
- return 3;
- }
- void fn4() {
- }
- int fn5() {
- boost::throw_exception( my_exception() );
- return 3;
- }
- void fn6() {
- boost::throw_exception( my_exception() );
- }
- int & fn7() {
- return gi;
- }
- int fn8( int i) {
- return i;
- }
- A fn9() {
- A a;
- a.value = 3;
- return a;
- }
- A fn10() {
- boost::throw_exception( my_exception() );
- return A();
- }
- B fn11( bool set) {
- B b( set);
- return b;
- }
- // packaged_task
- void test_packaged_task_create() {
- // default constructed packaged_task is not valid
- boost::fibers::packaged_task< int() > t1;
- BOOST_CHECK( ! t1.valid() );
- // packaged_task from function
- boost::fibers::packaged_task< int() > t2( fn3);
- BOOST_CHECK( t2.valid() );
- }
- // packaged_task
- void test_packaged_task_create_move() {
- // default constructed packaged_task is not valid
- boost::fibers::packaged_task< A() > t1;
- BOOST_CHECK( ! t1.valid() );
- // packaged_task from function
- boost::fibers::packaged_task< A() > t2( fn9);
- BOOST_CHECK( t2.valid() );
- }
- void test_packaged_task_create_void() {
- // default constructed packaged_task is not valid
- boost::fibers::packaged_task< void() > t1;
- BOOST_CHECK( ! t1.valid() );
- // packaged_task from function
- boost::fibers::packaged_task< void() > t2( fn4);
- BOOST_CHECK( t2.valid() );
- }
- void test_packaged_task_move() {
- boost::fibers::packaged_task< int() > t1( fn3);
- BOOST_CHECK( t1.valid() );
- // move construction
- boost::fibers::packaged_task< int() > t2( std::move( t1) );
- BOOST_CHECK( ! t1.valid() );
- BOOST_CHECK( t2.valid() );
- // move assignment
- t1 = std::move( t2);
- BOOST_CHECK( t1.valid() );
- BOOST_CHECK( ! t2.valid() );
- }
- void test_packaged_task_move_move() {
- boost::fibers::packaged_task< A() > t1( fn9);
- BOOST_CHECK( t1.valid() );
- // move construction
- boost::fibers::packaged_task< A() > t2( std::move( t1) );
- BOOST_CHECK( ! t1.valid() );
- BOOST_CHECK( t2.valid() );
- // move assignment
- t1 = std::move( t2);
- BOOST_CHECK( t1.valid() );
- BOOST_CHECK( ! t2.valid() );
- }
- void test_packaged_task_move_void() {
- boost::fibers::packaged_task< void() > t1( fn4);
- BOOST_CHECK( t1.valid() );
- // move construction
- boost::fibers::packaged_task< void() > t2( std::move( t1) );
- BOOST_CHECK( ! t1.valid() );
- BOOST_CHECK( t2.valid() );
- // move assignment
- t1 = std::move( t2);
- BOOST_CHECK( t1.valid() );
- BOOST_CHECK( ! t2.valid() );
- }
- void test_packaged_task_swap() {
- boost::fibers::packaged_task< int() > t1( fn3);
- BOOST_CHECK( t1.valid() );
- boost::fibers::packaged_task< int() > t2;
- BOOST_CHECK( ! t2.valid() );
- // swap
- t1.swap( t2);
- BOOST_CHECK( ! t1.valid() );
- BOOST_CHECK( t2.valid() );
- }
- void test_packaged_task_swap_move() {
- boost::fibers::packaged_task< A() > t1( fn9);
- BOOST_CHECK( t1.valid() );
- boost::fibers::packaged_task< A() > t2;
- BOOST_CHECK( ! t2.valid() );
- // swap
- t1.swap( t2);
- BOOST_CHECK( ! t1.valid() );
- BOOST_CHECK( t2.valid() );
- }
- void test_packaged_task_swap_void() {
- boost::fibers::packaged_task< void() > t1( fn4);
- BOOST_CHECK( t1.valid() );
- boost::fibers::packaged_task< void() > t2;
- BOOST_CHECK( ! t2.valid() );
- // swap
- t1.swap( t2);
- BOOST_CHECK( ! t1.valid() );
- BOOST_CHECK( t2.valid() );
- }
- void test_packaged_task_reset() {
- {
- boost::fibers::packaged_task< int() > p( fn3);
- boost::fibers::future< int > f( p.get_future() );
- BOOST_CHECK( p.valid() );
- p();
- BOOST_CHECK( 3 == f.get() );
- // reset
- p.reset();
- p();
- f = p.get_future();
- BOOST_CHECK( 3 == f.get() );
- }
- {
- boost::fibers::packaged_task< int() > p;
- bool thrown = false;
- try {
- p.reset();
- } catch ( boost::fibers::packaged_task_uninitialized const&) {
- thrown = true;
- }
- BOOST_CHECK( thrown);
- }
- }
- void test_packaged_task_reset_destruction() {
- gi = 0;
- boost::fibers::packaged_task< B( bool) > p( fn11);
- BOOST_CHECK( p.valid() );
- BOOST_CHECK( 0 == gi);
- p( true);
- BOOST_CHECK( 3 == gi);
- // reset
- p.reset();
- BOOST_CHECK( -1 == gi);
- p( false);
- BOOST_CHECK( 3 == gi);
- // reset
- p.reset();
- BOOST_CHECK( 3 == gi);
- }
- void test_packaged_task_reset_move() {
- {
- boost::fibers::packaged_task< A() > p( fn9);
- boost::fibers::future< A > f( p.get_future() );
- BOOST_CHECK( p.valid() );
- p();
- BOOST_CHECK( 3 == f.get().value);
- // reset
- p.reset();
- p();
- f = p.get_future();
- BOOST_CHECK( 3 == f.get().value);
- }
- {
- boost::fibers::packaged_task< A() > p;
- bool thrown = false;
- try {
- p.reset();
- } catch ( boost::fibers::packaged_task_uninitialized const&) {
- thrown = true;
- }
- BOOST_CHECK( thrown);
- }
- }
- void test_packaged_task_reset_void() {
- {
- boost::fibers::packaged_task< void() > p( fn4);
- boost::fibers::future< void > f( p.get_future() );
- BOOST_CHECK( p.valid() );
- p();
- f.get();
- // reset
- p.reset();
- p();
- f = p.get_future();
- f.get();
- }
- {
- boost::fibers::packaged_task< void() > p;
- bool thrown = false;
- try {
- p.reset();
- } catch ( boost::fibers::packaged_task_uninitialized const&) {
- thrown = true;
- }
- BOOST_CHECK( thrown);
- }
- }
- void test_packaged_task_get_future() {
- boost::fibers::packaged_task< int() > t1( fn3);
- BOOST_CHECK( t1.valid() );
- // retrieve future
- boost::fibers::future< int > f1 = t1.get_future();
- BOOST_CHECK( f1.valid() );
- // retrieve future a second time
- bool thrown = false;
- try {
- f1 = t1.get_future();
- } catch ( boost::fibers::future_already_retrieved const&) {
- thrown = true;
- }
- BOOST_CHECK( thrown);
- // move construction
- boost::fibers::packaged_task< int() > t2( std::move( t1) );
- BOOST_CHECK( ! t1.valid() );
- BOOST_CHECK( t2.valid() );
- // retrieve future from uninitialized
- thrown = false;
- try {
- f1 = t1.get_future();
- } catch ( boost::fibers::packaged_task_uninitialized const&) {
- thrown = true;
- }
- BOOST_CHECK( thrown);
- }
- void test_packaged_task_get_future_move() {
- boost::fibers::packaged_task< A() > t1( fn9);
- BOOST_CHECK( t1.valid() );
- // retrieve future
- boost::fibers::future< A > f1 = t1.get_future();
- BOOST_CHECK( f1.valid() );
- // retrieve future a second time
- bool thrown = false;
- try {
- f1 = t1.get_future();
- } catch ( boost::fibers::future_already_retrieved const&) {
- thrown = true;
- }
- BOOST_CHECK( thrown);
- // move construction
- boost::fibers::packaged_task< A() > t2( std::move( t1) );
- BOOST_CHECK( ! t1.valid() );
- BOOST_CHECK( t2.valid() );
- // retrieve future from uninitialized
- thrown = false;
- try {
- f1 = t1.get_future();
- } catch ( boost::fibers::packaged_task_uninitialized const&) {
- thrown = true;
- }
- BOOST_CHECK( thrown);
- }
- void test_packaged_task_get_future_void() {
- boost::fibers::packaged_task< void() > t1( fn4);
- BOOST_CHECK( t1.valid() );
- // retrieve future
- boost::fibers::future< void > f1 = t1.get_future();
- BOOST_CHECK( f1.valid() );
- // retrieve future a second time
- bool thrown = false;
- try {
- f1 = t1.get_future();
- } catch ( boost::fibers::future_already_retrieved const&) {
- thrown = true;
- }
- BOOST_CHECK( thrown);
- // move construction
- boost::fibers::packaged_task< void() > t2( std::move( t1) );
- BOOST_CHECK( ! t1.valid() );
- BOOST_CHECK( t2.valid() );
- // retrieve future from uninitialized
- thrown = false;
- try {
- f1 = t1.get_future();
- } catch ( boost::fibers::packaged_task_uninitialized const&) {
- thrown = true;
- }
- BOOST_CHECK( thrown);
- }
- void test_packaged_task_exec() {
- // promise takes a copyable as return type
- boost::fibers::packaged_task< int() > t1( fn3);
- BOOST_CHECK( t1.valid() );
- boost::fibers::future< int > f1 = t1.get_future();
- BOOST_CHECK( f1.valid() );
- // exec
- t1();
- BOOST_CHECK( 3 == f1.get() );
- // exec a second time
- bool thrown = false;
- try {
- t1();
- } catch ( boost::fibers::promise_already_satisfied const&) {
- thrown = true;
- }
- BOOST_CHECK( thrown);
- }
- void test_packaged_task_exec_move() {
- // promise takes a copyable as return type
- boost::fibers::packaged_task< A() > t1( fn9);
- BOOST_CHECK( t1.valid() );
- boost::fibers::future< A > f1 = t1.get_future();
- BOOST_CHECK( f1.valid() );
- // exec
- t1();
- BOOST_CHECK( 3 == f1.get().value);
- // exec a second time
- bool thrown = false;
- try {
- t1();
- } catch ( boost::fibers::promise_already_satisfied const&) {
- thrown = true;
- }
- BOOST_CHECK( thrown);
- }
- void test_packaged_task_exec_param() {
- // promise takes a copyable as return type
- boost::fibers::packaged_task< int( int) > t1( fn8);
- BOOST_CHECK( t1.valid() );
- boost::fibers::future< int > f1 = t1.get_future();
- BOOST_CHECK( f1.valid() );
- // exec
- t1( 3);
- BOOST_CHECK( 3 == f1.get() );
- // exec a second time
- bool thrown = false;
- try {
- t1( 7);
- } catch ( boost::fibers::promise_already_satisfied const&) {
- thrown = true;
- }
- BOOST_CHECK( thrown);
- //TODO: packaged_task returns a moveable-only as return type
- }
- void test_packaged_task_exec_ref() {
- // promise takes a copyable as return type
- boost::fibers::packaged_task< int&() > t1( fn7);
- BOOST_CHECK( t1.valid() );
- boost::fibers::future< int& > f1 = t1.get_future();
- BOOST_CHECK( f1.valid() );
- // exec
- t1();
- int & i = f1.get();
- BOOST_CHECK( &gi == &i);
- // exec a second time
- bool thrown = false;
- try {
- t1();
- } catch ( boost::fibers::promise_already_satisfied const&) {
- thrown = true;
- }
- BOOST_CHECK( thrown);
- //TODO: packaged_task returns a moveable-only as return type
- }
- void test_packaged_task_exec_void() {
- // promise takes a copyable as return type
- boost::fibers::packaged_task< void() > t1( fn4);
- BOOST_CHECK( t1.valid() );
- boost::fibers::future< void > f1 = t1.get_future();
- BOOST_CHECK( f1.valid() );
- // set void
- t1();
- f1.get();
- // exec a second time
- bool thrown = false;
- try {
- t1();
- } catch ( boost::fibers::promise_already_satisfied const&) {
- thrown = true;
- }
- BOOST_CHECK( thrown);
- }
- void test_packaged_task_exception() {
- // promise takes a copyable as return type
- boost::fibers::packaged_task< int() > t1( fn5);
- BOOST_CHECK( t1.valid() );
- boost::fibers::future< int > f1 = t1.get_future();
- BOOST_CHECK( f1.valid() );
- // exec
- t1();
- bool thrown = false;
- try {
- f1.get();
- } catch ( my_exception const&) {
- thrown = true;
- }
- BOOST_CHECK( thrown);
- boost::fibers::packaged_task< int() > t2( fn5);
- BOOST_CHECK( t2.valid() );
- boost::fibers::future< int > f2 = t2.get_future();
- BOOST_CHECK( f2.valid() );
- // exec
- t2();
- BOOST_CHECK( f2.get_exception_ptr() );
- thrown = false;
- try
- { std::rethrow_exception( f2.get_exception_ptr() ); }
- catch ( my_exception const&)
- { thrown = true; }
- BOOST_CHECK( thrown);
- }
- void test_packaged_task_exception_move() {
- // promise takes a moveable as return type
- boost::fibers::packaged_task< A() > t1( fn10);
- BOOST_CHECK( t1.valid() );
- boost::fibers::future< A > f1 = t1.get_future();
- BOOST_CHECK( f1.valid() );
- // exec
- t1();
- bool thrown = false;
- try {
- f1.get();
- } catch ( my_exception const&) {
- thrown = true;
- }
- BOOST_CHECK( thrown);
- boost::fibers::packaged_task< A() > t2( fn10);
- BOOST_CHECK( t2.valid() );
- boost::fibers::future< A > f2 = t2.get_future();
- BOOST_CHECK( f2.valid() );
- // exec
- t2();
- BOOST_CHECK( f2.get_exception_ptr() );
- thrown = false;
- try
- { std::rethrow_exception( f2.get_exception_ptr() ); }
- catch ( my_exception const&)
- { thrown = true; }
- BOOST_CHECK( thrown);
- }
- void test_packaged_task_exception_void() {
- // promise takes a copyable as return type
- boost::fibers::packaged_task< void() > t1( fn6);
- BOOST_CHECK( t1.valid() );
- boost::fibers::future< void > f1 = t1.get_future();
- BOOST_CHECK( f1.valid() );
- // set void
- t1();
- bool thrown = false;
- try {
- f1.get();
- } catch ( my_exception const&) {
- thrown = true;
- }
- BOOST_CHECK( thrown);
-
- boost::fibers::packaged_task< void() > t2( fn6);
- BOOST_CHECK( t2.valid() );
- boost::fibers::future< void > f2 = t2.get_future();
- BOOST_CHECK( f2.valid() );
- // exec
- t2();
- BOOST_CHECK( f2.get_exception_ptr() );
- thrown = false;
- try {
- std::rethrow_exception( f2.get_exception_ptr() );
- } catch ( my_exception const&) {
- thrown = true;
- }
- BOOST_CHECK( thrown);
- }
- boost::unit_test_framework::test_suite* init_unit_test_suite(int, char*[]) {
- boost::unit_test_framework::test_suite* test =
- BOOST_TEST_SUITE("Boost.Fiber: packaged_task test suite");
- test->add(BOOST_TEST_CASE(test_packaged_task_create));
- test->add(BOOST_TEST_CASE(test_packaged_task_create_move));
- test->add(BOOST_TEST_CASE(test_packaged_task_create_void));
- test->add(BOOST_TEST_CASE(test_packaged_task_move));
- test->add(BOOST_TEST_CASE(test_packaged_task_move_move));
- test->add(BOOST_TEST_CASE(test_packaged_task_move_void));
- test->add(BOOST_TEST_CASE(test_packaged_task_swap));
- test->add(BOOST_TEST_CASE(test_packaged_task_swap_move));
- test->add(BOOST_TEST_CASE(test_packaged_task_swap_void));
- test->add(BOOST_TEST_CASE(test_packaged_task_reset));
- test->add(BOOST_TEST_CASE(test_packaged_task_reset_destruction));
- test->add(BOOST_TEST_CASE(test_packaged_task_reset_move));
- test->add(BOOST_TEST_CASE(test_packaged_task_reset_void));
- test->add(BOOST_TEST_CASE(test_packaged_task_get_future));
- test->add(BOOST_TEST_CASE(test_packaged_task_get_future_move));
- test->add(BOOST_TEST_CASE(test_packaged_task_get_future_void));
- test->add(BOOST_TEST_CASE(test_packaged_task_exec));
- test->add(BOOST_TEST_CASE(test_packaged_task_exec_move));
- test->add(BOOST_TEST_CASE(test_packaged_task_exec_param));
- test->add(BOOST_TEST_CASE(test_packaged_task_exec_ref));
- test->add(BOOST_TEST_CASE(test_packaged_task_exec_void));
- test->add(BOOST_TEST_CASE(test_packaged_task_exception));
- test->add(BOOST_TEST_CASE(test_packaged_task_exception_move));
- test->add(BOOST_TEST_CASE(test_packaged_task_exception_void));
- return test;
- }
|