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
17struct 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
38struct X {
39 int value;
40
41 void foo( int i) {
42 value = i;
43 }
44};
45
46void fn1() {
47}
48
49int fn2( int i) {
50 return i;
51}
52
53int & fn3( int & i) {
54 return i;
55}
56
57A fn4( A && a) {
58 return std::forward< A >( t&: a);
59}
60
61void test_async_1() {
62 boost::fibers::future< void > f1 = boost::fibers::async( policy: boost::fibers::launch::post, fn&: fn1);
63 BOOST_CHECK( f1.valid() );
64
65 f1.get();
66}
67
68void test_async_2() {
69 int i = 3;
70 boost::fibers::future< int > f1 = boost::fibers::async( policy: boost::fibers::launch::post, fn&: fn2, args: i);
71 BOOST_CHECK( f1.valid() );
72
73 BOOST_CHECK( i == f1.get());
74}
75
76void test_async_3() {
77 int i = 7;
78 boost::fibers::future< int& > f1 = boost::fibers::async( policy: boost::fibers::launch::post, fn&: fn3, args: std::ref( t&: i) );
79 BOOST_CHECK( f1.valid() );
80
81 BOOST_CHECK( & i == & f1.get());
82}
83
84void test_async_4() {
85 A a1;
86 a1.value = 7;
87 boost::fibers::future< A > f1 = boost::fibers::async( policy: boost::fibers::launch::post, 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
94void 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::post,
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
106void 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::post,
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
119boost::unit_test_framework::test_suite* init_unit_test_suite(int, char*[]) {
120 boost::unit_test_framework::test_suite* test =
121 BOOST_TEST_SUITE("Boost.Fiber: async test suite");
122
123 test->add(BOOST_TEST_CASE(test_async_1));
124 test->add(BOOST_TEST_CASE(test_async_2));
125 test->add(BOOST_TEST_CASE(test_async_3));
126 test->add(BOOST_TEST_CASE(test_async_4));
127 test->add(BOOST_TEST_CASE(test_async_5));
128 test->add(BOOST_TEST_CASE(test_async_6));
129
130 return test;
131}
132

source code of boost/libs/fiber/test/test_async_post.cpp