| 1 | // (C) Copyright 2008-10 Anthony Williams |
| 2 | // 2015 Oliver Kowalke |
| 3 | // |
| 4 | // Distributed under the Boost Software License, Version 1.0. (See |
| 5 | // accompanying file LICENSE_1_0.txt or copy at |
| 6 | // http://www.boost.org/LICENSE_1_0.txt) |
| 7 | |
| 8 | #include <utility> |
| 9 | #include <memory> |
| 10 | #include <stdexcept> |
| 11 | #include <string> |
| 12 | |
| 13 | #include <boost/test/unit_test.hpp> |
| 14 | |
| 15 | #include <boost/fiber/all.hpp> |
| 16 | |
| 17 | struct A { |
| 18 | A() = default; |
| 19 | |
| 20 | A( A const&) = delete; |
| 21 | A & operator=( A const&) = delete; |
| 22 | |
| 23 | A( A && other) : |
| 24 | value{ other.value } { |
| 25 | other.value = 0; |
| 26 | } |
| 27 | |
| 28 | A & operator=( A && other) { |
| 29 | if ( this == & other) return * this; |
| 30 | value = other.value; |
| 31 | other.value = 0; |
| 32 | return * this; |
| 33 | } |
| 34 | |
| 35 | int value{ 0 }; |
| 36 | }; |
| 37 | |
| 38 | struct X { |
| 39 | int value; |
| 40 | |
| 41 | void foo( int i) { |
| 42 | value = i; |
| 43 | } |
| 44 | }; |
| 45 | |
| 46 | void fn1() { |
| 47 | } |
| 48 | |
| 49 | int fn2( int i) { |
| 50 | return i; |
| 51 | } |
| 52 | |
| 53 | int & fn3( int & i) { |
| 54 | return i; |
| 55 | } |
| 56 | |
| 57 | A fn4( A && a) { |
| 58 | return std::forward< A >( t&: a); |
| 59 | } |
| 60 | |
| 61 | void test_async_1() { |
| 62 | boost::fibers::future< void > f1 = boost::fibers::async( policy: boost::fibers::launch::dispatch, fn&: fn1); |
| 63 | BOOST_CHECK( f1.valid() ); |
| 64 | |
| 65 | f1.get(); |
| 66 | } |
| 67 | |
| 68 | void test_async_2() { |
| 69 | int i = 3; |
| 70 | boost::fibers::future< int > f1 = boost::fibers::async( policy: boost::fibers::launch::dispatch, fn&: fn2, args: i); |
| 71 | BOOST_CHECK( f1.valid() ); |
| 72 | |
| 73 | BOOST_CHECK( i == f1.get()); |
| 74 | } |
| 75 | |
| 76 | void test_async_3() { |
| 77 | int i = 7; |
| 78 | boost::fibers::future< int& > f1 = boost::fibers::async( policy: boost::fibers::launch::dispatch, fn&: fn3, args: std::ref( t&: i) ); |
| 79 | BOOST_CHECK( f1.valid() ); |
| 80 | |
| 81 | BOOST_CHECK( & i == & f1.get()); |
| 82 | } |
| 83 | |
| 84 | void test_async_4() { |
| 85 | A a1; |
| 86 | a1.value = 7; |
| 87 | boost::fibers::future< A > f1 = boost::fibers::async( policy: boost::fibers::launch::dispatch, fn&: fn4, args: std::move( a1) ); |
| 88 | BOOST_CHECK( f1.valid() ); |
| 89 | |
| 90 | A a2 = f1.get(); |
| 91 | BOOST_CHECK( 7 == a2.value); |
| 92 | } |
| 93 | |
| 94 | void test_async_5() { |
| 95 | X x = {.value: 0}; |
| 96 | BOOST_CHECK( 0 == x.value); |
| 97 | boost::fibers::future< void > f1 = boost::fibers::async( |
| 98 | policy: boost::fibers::launch::dispatch, |
| 99 | fn: std::bind( f: & X::foo, args: std::ref( t&: x), args: 3) ); |
| 100 | BOOST_CHECK( f1.valid() ); |
| 101 | |
| 102 | f1.get(); |
| 103 | BOOST_CHECK( 3 == x.value); |
| 104 | } |
| 105 | |
| 106 | void test_async_6() { |
| 107 | X x = {.value: 0}; |
| 108 | BOOST_CHECK( 0 == x.value); |
| 109 | boost::fibers::future< void > f1 = boost::fibers::async( |
| 110 | policy: boost::fibers::launch::dispatch, |
| 111 | fn: std::bind( f: & X::foo, args: std::ref( t&: x), args: std::placeholders::_1), args: 3); |
| 112 | BOOST_CHECK( f1.valid() ); |
| 113 | |
| 114 | f1.get(); |
| 115 | BOOST_CHECK( 3 == x.value); |
| 116 | } |
| 117 | |
| 118 | void test_async_stack_alloc() { |
| 119 | boost::fibers::future< void > f1 = boost::fibers::async( |
| 120 | policy: boost::fibers::launch::dispatch, |
| 121 | std::allocator_arg, |
| 122 | salloc: boost::fibers::fixedsize_stack{}, |
| 123 | fn&: fn1); |
| 124 | BOOST_CHECK( f1.valid() ); |
| 125 | |
| 126 | f1.get(); |
| 127 | } |
| 128 | |
| 129 | void test_async_std_alloc() { |
| 130 | struct none {}; |
| 131 | boost::fibers::future< void > f1 = boost::fibers::async( |
| 132 | policy: boost::fibers::launch::dispatch, |
| 133 | std::allocator_arg, |
| 134 | salloc: boost::fibers::fixedsize_stack{}, |
| 135 | alloc: std::allocator< none >{}, |
| 136 | fn&: fn1); |
| 137 | BOOST_CHECK( f1.valid() ); |
| 138 | |
| 139 | f1.get(); |
| 140 | } |
| 141 | |
| 142 | |
| 143 | boost::unit_test_framework::test_suite* init_unit_test_suite(int, char*[]) { |
| 144 | boost::unit_test_framework::test_suite* test = |
| 145 | BOOST_TEST_SUITE("Boost.Fiber: async test suite" ); |
| 146 | |
| 147 | test->add(BOOST_TEST_CASE(test_async_1)); |
| 148 | test->add(BOOST_TEST_CASE(test_async_2)); |
| 149 | test->add(BOOST_TEST_CASE(test_async_3)); |
| 150 | test->add(BOOST_TEST_CASE(test_async_4)); |
| 151 | test->add(BOOST_TEST_CASE(test_async_5)); |
| 152 | test->add(BOOST_TEST_CASE(test_async_6)); |
| 153 | test->add(BOOST_TEST_CASE(test_async_stack_alloc)); |
| 154 | test->add(BOOST_TEST_CASE(test_async_std_alloc)); |
| 155 | |
| 156 | return test; |
| 157 | } |
| 158 | |