| 1 | // (C) Copyright 2008-10 Anthony Williams |
|---|---|
| 2 | // |
| 3 | // Distributed under the Boost Software License, Version 1.0. (See |
| 4 | // accompanying file LICENSE_1_0.txt or copy at |
| 5 | // http://www.boost.org/LICENSE_1_0.txt) |
| 6 | |
| 7 | #include <utility> |
| 8 | #include <memory> |
| 9 | #include <stdexcept> |
| 10 | #include <string> |
| 11 | #include <thread> |
| 12 | |
| 13 | #include <boost/fiber/all.hpp> |
| 14 | #include <boost/test/unit_test.hpp> |
| 15 | |
| 16 | int fn( int i) { |
| 17 | return i; |
| 18 | } |
| 19 | |
| 20 | void test_async() { |
| 21 | for ( int i = 0; i < 10; ++i) { |
| 22 | int n = 3; |
| 23 | boost::fibers::packaged_task< int( int) > pt( fn); |
| 24 | boost::fibers::future< int > f( pt.get_future() ); |
| 25 | std::thread t( |
| 26 | std::bind( |
| 27 | f: [n](boost::fibers::packaged_task< int( int) > & pt) mutable -> void { |
| 28 | boost::fibers::fiber( boost::fibers::launch::dispatch, std::move( pt), n).join(); |
| 29 | }, |
| 30 | args: std::move( pt) ) ); |
| 31 | int result = f.get(); |
| 32 | BOOST_CHECK_EQUAL( n, result); |
| 33 | t.join(); |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | void test_dummy() {} |
| 38 | |
| 39 | boost::unit_test_framework::test_suite* init_unit_test_suite(int, char*[]) { |
| 40 | boost::unit_test_framework::test_suite* test = |
| 41 | BOOST_TEST_SUITE("Boost.Fiber: futures-mt test suite"); |
| 42 | |
| 43 | #if ! defined(BOOST_FIBERS_NO_ATOMICS) |
| 44 | test->add(BOOST_TEST_CASE(test_async)); |
| 45 | #else |
| 46 | test->add(BOOST_TEST_CASE(test_dummy)); |
| 47 | #endif |
| 48 | |
| 49 | return test; |
| 50 | } |
| 51 |
