]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
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 >( a); | |
59 | } | |
60 | ||
61 | void test_async_1() { | |
62 | boost::fibers::future< void > f1 = boost::fibers::async( boost::fibers::launch::dispatch, 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( boost::fibers::launch::dispatch, fn2, 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( boost::fibers::launch::dispatch, fn3, std::ref( 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( boost::fibers::launch::dispatch, fn4, 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 = {0}; | |
96 | BOOST_CHECK( 0 == x.value); | |
97 | boost::fibers::future< void > f1 = boost::fibers::async( | |
98 | boost::fibers::launch::dispatch, | |
99 | std::bind( & X::foo, std::ref( x), 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 = {0}; | |
108 | BOOST_CHECK( 0 == x.value); | |
109 | boost::fibers::future< void > f1 = boost::fibers::async( | |
110 | boost::fibers::launch::dispatch, | |
111 | std::bind( & X::foo, std::ref( x), std::placeholders::_1), 3); | |
112 | BOOST_CHECK( f1.valid() ); | |
113 | ||
114 | f1.get(); | |
115 | BOOST_CHECK( 3 == x.value); | |
116 | } | |
117 | ||
b32b8144 FG |
118 | void test_async_stack_alloc() { |
119 | boost::fibers::future< void > f1 = boost::fibers::async( | |
120 | boost::fibers::launch::dispatch, | |
121 | std::allocator_arg, | |
122 | boost::fibers::fixedsize_stack{}, | |
123 | fn1); | |
124 | BOOST_CHECK( f1.valid() ); | |
125 | ||
126 | f1.get(); | |
127 | } | |
128 | ||
129 | void test_async_std_alloc() { | |
130 | boost::fibers::future< void > f1 = boost::fibers::async( | |
131 | boost::fibers::launch::dispatch, | |
132 | std::allocator_arg, | |
133 | boost::fibers::fixedsize_stack{}, | |
134 | std::allocator< void >{}, | |
135 | fn1); | |
136 | BOOST_CHECK( f1.valid() ); | |
137 | ||
138 | f1.get(); | |
139 | } | |
140 | ||
7c673cae FG |
141 | |
142 | boost::unit_test_framework::test_suite* init_unit_test_suite(int, char*[]) { | |
143 | boost::unit_test_framework::test_suite* test = | |
144 | BOOST_TEST_SUITE("Boost.Fiber: async test suite"); | |
145 | ||
146 | test->add(BOOST_TEST_CASE(test_async_1)); | |
147 | test->add(BOOST_TEST_CASE(test_async_2)); | |
148 | test->add(BOOST_TEST_CASE(test_async_3)); | |
149 | test->add(BOOST_TEST_CASE(test_async_4)); | |
150 | test->add(BOOST_TEST_CASE(test_async_5)); | |
151 | test->add(BOOST_TEST_CASE(test_async_6)); | |
b32b8144 FG |
152 | test->add(BOOST_TEST_CASE(test_async_stack_alloc)); |
153 | test->add(BOOST_TEST_CASE(test_async_std_alloc)); | |
7c673cae FG |
154 | |
155 | return test; | |
156 | } |