]>
Commit | Line | Data |
---|---|---|
1e59de90 TL |
1 | // |
2 | // experimental/awaitable_operators.cpp | |
3 | // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
4 | // | |
5 | // Copyright (c) 2003-2022 Christopher M. Kohlhoff (chris at kohlhoff dot com) | |
6 | // | |
7 | // Distributed under the Boost Software License, Version 1.0. (See accompanying | |
8 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | |
9 | // | |
10 | ||
11 | // Disable autolinking for unit tests. | |
12 | #if !defined(BOOST_ALL_NO_LIB) | |
13 | #define BOOST_ALL_NO_LIB 1 | |
14 | #endif // !defined(BOOST_ALL_NO_LIB) | |
15 | ||
16 | // Prevent link dependency on the Boost.System library. | |
17 | #if !defined(BOOST_SYSTEM_NO_DEPRECATED) | |
18 | #define BOOST_SYSTEM_NO_DEPRECATED | |
19 | #endif // !defined(BOOST_SYSTEM_NO_DEPRECATED) | |
20 | ||
21 | // Test that header file is self-contained. | |
22 | #include <boost/asio/experimental/awaitable_operators.hpp> | |
23 | ||
24 | #include <boost/asio/co_spawn.hpp> | |
25 | #include <boost/asio/detached.hpp> | |
26 | #include <boost/asio/io_context.hpp> | |
27 | #include <boost/asio/post.hpp> | |
28 | #include "../unit_test.hpp" | |
29 | ||
30 | using boost::asio::awaitable; | |
31 | using namespace boost::asio::experimental::awaitable_operators; | |
32 | ||
33 | struct move_only | |
34 | { | |
35 | move_only(int i) : value(i) {} | |
36 | move_only(const move_only&) = delete; | |
37 | move_only(move_only&&) {} | |
38 | move_only& operator=(const move_only&) = delete; | |
39 | move_only& operator=(move_only&&) { return *this; } | |
40 | int value; | |
41 | }; | |
42 | ||
43 | awaitable<void> void_ok() | |
44 | { | |
45 | co_return; | |
46 | } | |
47 | ||
48 | awaitable<void> void_ex() | |
49 | { | |
50 | throw std::runtime_error("exception"); | |
51 | co_return; | |
52 | } | |
53 | ||
54 | awaitable<void> void_post_loop() | |
55 | { | |
56 | for (;;) | |
57 | co_await boost::asio::post(boost::asio::use_awaitable); | |
58 | } | |
59 | ||
60 | awaitable<int> int_0() | |
61 | { | |
62 | co_return 0; | |
63 | } | |
64 | ||
65 | awaitable<int> int_1() | |
66 | { | |
67 | co_return 1; | |
68 | } | |
69 | ||
70 | awaitable<int> int_2() | |
71 | { | |
72 | co_return 2; | |
73 | } | |
74 | ||
75 | awaitable<int> int_ex() | |
76 | { | |
77 | throw std::runtime_error("exception"); | |
78 | co_return -1; | |
79 | } | |
80 | ||
81 | awaitable<move_only> move_only_0() | |
82 | { | |
83 | co_return move_only(0); | |
84 | } | |
85 | ||
86 | awaitable<move_only> move_only_1() | |
87 | { | |
88 | co_return move_only(1); | |
89 | } | |
90 | ||
91 | awaitable<move_only> move_only_2() | |
92 | { | |
93 | co_return move_only(2); | |
94 | } | |
95 | ||
96 | awaitable<move_only> move_only_ex() | |
97 | { | |
98 | throw std::runtime_error("exception"); | |
99 | co_return move_only(-1); | |
100 | } | |
101 | ||
102 | awaitable<void> do_test_and_awaitable_operator() | |
103 | { | |
104 | int i; | |
105 | std::tuple<int, int> ii; | |
106 | std::tuple<int, int, int> iii; | |
107 | ||
108 | co_await (void_ok() && void_ok()); | |
109 | co_await (void_ok() && void_ok() && void_ok()); | |
110 | try { co_await (void_ex() && void_ok()); } catch (...) {} | |
111 | try { co_await (void_ex() && void_ok() && void_ok()); } catch (...) {} | |
112 | try { co_await (void_ex() && void_post_loop()); } catch (...) {} | |
113 | try { co_await (void_ex() && void_ok() && void_post_loop()); } catch (...) {} | |
114 | ||
115 | i = co_await (void_ok() && int_0()); | |
116 | BOOST_ASIO_CHECK(i == 0); | |
117 | ||
118 | i = co_await (void_ok() && int_1()); | |
119 | BOOST_ASIO_CHECK(i == 1); | |
120 | ||
121 | i = co_await (int_0() && void_ok()); | |
122 | BOOST_ASIO_CHECK(i == 0); | |
123 | ||
124 | ii = co_await (int_0() && int_1()); | |
125 | BOOST_ASIO_CHECK(std::get<0>(ii) == 0); | |
126 | BOOST_ASIO_CHECK(std::get<1>(ii) == 1); | |
127 | ||
128 | i = co_await (void_ok() && void_ok() && int_0()); | |
129 | BOOST_ASIO_CHECK(i == 0); | |
130 | ||
131 | i = co_await (void_ok() && int_0() && void_ok()); | |
132 | BOOST_ASIO_CHECK(i == 0); | |
133 | ||
134 | ii = co_await (void_ok() && int_0() && int_1()); | |
135 | BOOST_ASIO_CHECK(std::get<0>(ii) == 0); | |
136 | BOOST_ASIO_CHECK(std::get<1>(ii) == 1); | |
137 | ||
138 | ii = co_await (int_0() && void_ok() && int_1()); | |
139 | BOOST_ASIO_CHECK(std::get<0>(ii) == 0); | |
140 | BOOST_ASIO_CHECK(std::get<1>(ii) == 1); | |
141 | ||
142 | iii = co_await (int_0() && int_1() && int_2()); | |
143 | BOOST_ASIO_CHECK(std::get<0>(iii) == 0); | |
144 | BOOST_ASIO_CHECK(std::get<1>(iii) == 1); | |
145 | BOOST_ASIO_CHECK(std::get<2>(iii) == 2); | |
146 | } | |
147 | ||
148 | void test_and_awaitable_operator() | |
149 | { | |
150 | boost::asio::io_context ctx; | |
151 | co_spawn(ctx, do_test_and_awaitable_operator(), boost::asio::detached); | |
152 | ctx.run(); | |
153 | } | |
154 | ||
155 | awaitable<void> do_test_or_awaitable_operator() | |
156 | { | |
157 | std::variant<int, int> ii; | |
158 | std::variant<int, std::monostate> iv; | |
159 | std::variant<std::monostate, int> vi; | |
160 | std::variant<std::monostate, std::monostate> vv; | |
161 | std::variant<int, int, int> iii; | |
162 | std::variant<int, int, std::monostate> iiv; | |
163 | std::variant<int, std::monostate, int> ivi; | |
164 | std::variant<int, std::monostate, std::monostate> ivv; | |
165 | std::variant<std::monostate, int, int> vii; | |
166 | std::variant<std::monostate, int, std::monostate> viv; | |
167 | std::variant<std::monostate, std::monostate, int> vvi; | |
168 | std::variant<std::monostate, std::monostate, std::monostate> vvv; | |
169 | ||
170 | vv = co_await (void_ok() || void_ok()); | |
171 | BOOST_ASIO_CHECK(vv.index() == 0); | |
172 | ||
173 | vv = co_await (void_ex() || void_ok()); | |
174 | BOOST_ASIO_CHECK(vv.index() == 1); | |
175 | ||
176 | vi = co_await (void_ok() || int_0()); | |
177 | BOOST_ASIO_CHECK(vi.index() == 0); | |
178 | ||
179 | vi = co_await (void_ex() || int_0()); | |
180 | BOOST_ASIO_CHECK(vi.index() == 1); | |
181 | ||
182 | iv = co_await (int_0() || void_ex()); | |
183 | BOOST_ASIO_CHECK(iv.index() == 0); | |
184 | BOOST_ASIO_CHECK(std::get<0>(iv) == 0); | |
185 | ||
186 | ii = co_await (int_0() || int_1()); | |
187 | BOOST_ASIO_CHECK(ii.index() == 0); | |
188 | BOOST_ASIO_CHECK(std::get<0>(ii) == 0); | |
189 | ||
190 | ii = co_await (int_ex() || int_1()); | |
191 | BOOST_ASIO_CHECK(ii.index() == 1); | |
192 | BOOST_ASIO_CHECK(std::get<1>(ii) == 1); | |
193 | ||
194 | vvv = co_await (void_ok() || void_ok() || void_ok()); | |
195 | BOOST_ASIO_CHECK(vvv.index() == 0); | |
196 | ||
197 | vvv = co_await (void_ex() || void_ok() || void_ok()); | |
198 | BOOST_ASIO_CHECK(vvv.index() == 1); | |
199 | ||
200 | vvv = co_await (void_ex() || void_ex() || void_ok()); | |
201 | BOOST_ASIO_CHECK(vvv.index() == 2); | |
202 | ||
203 | vvi = co_await (void_ok() || void_ok() || int_0()); | |
204 | BOOST_ASIO_CHECK(vvi.index() == 0); | |
205 | ||
206 | viv = co_await (void_ok() || int_0() || void_ok()); | |
207 | BOOST_ASIO_CHECK(viv.index() == 0); | |
208 | ||
209 | viv = co_await (void_ex() || int_0() || void_ok()); | |
210 | BOOST_ASIO_CHECK(viv.index() == 1); | |
211 | BOOST_ASIO_CHECK(std::get<1>(viv) == 0); | |
212 | ||
213 | vii = co_await (void_ex() || int_0() || int_1()); | |
214 | BOOST_ASIO_CHECK(vii.index() == 1); | |
215 | BOOST_ASIO_CHECK(std::get<1>(vii) == 0); | |
216 | ||
217 | ivv = co_await (int_0() || void_ok() || void_ok()); | |
218 | BOOST_ASIO_CHECK(ivv.index() == 0); | |
219 | BOOST_ASIO_CHECK(std::get<0>(ivv) == 0); | |
220 | ||
221 | ivv = co_await (int_ex() || void_ok() || void_ok()); | |
222 | BOOST_ASIO_CHECK(ivv.index() == 1); | |
223 | ||
224 | ivi = co_await (int_0() || void_ok() || int_1()); | |
225 | BOOST_ASIO_CHECK(ivi.index() == 0); | |
226 | BOOST_ASIO_CHECK(std::get<0>(ivi) == 0); | |
227 | ||
228 | ivi = co_await (int_ex() || void_ok() || int_1()); | |
229 | BOOST_ASIO_CHECK(ivi.index() == 1); | |
230 | ||
231 | iiv = co_await (int_0() || int_1() || void_ok()); | |
232 | BOOST_ASIO_CHECK(iiv.index() == 0); | |
233 | BOOST_ASIO_CHECK(std::get<0>(iiv) == 0); | |
234 | ||
235 | iiv = co_await (int_ex() || int_1() || void_ok()); | |
236 | BOOST_ASIO_CHECK(iiv.index() == 1); | |
237 | BOOST_ASIO_CHECK(std::get<0>(iiv) == 1); | |
238 | ||
239 | iiv = co_await (int_ex() || int_ex() || void_ok()); | |
240 | BOOST_ASIO_CHECK(iiv.index() == 2); | |
241 | ||
242 | iii = co_await (int_0() || int_1() || int_2()); | |
243 | BOOST_ASIO_CHECK(iii.index() == 0); | |
244 | BOOST_ASIO_CHECK(std::get<0>(iii) == 0); | |
245 | ||
246 | iii = co_await (int_ex() || int_1() || int_2()); | |
247 | BOOST_ASIO_CHECK(iii.index() == 1); | |
248 | BOOST_ASIO_CHECK(std::get<1>(iii) == 1); | |
249 | ||
250 | iii = co_await (int_ex() || int_ex() || int_2()); | |
251 | BOOST_ASIO_CHECK(iii.index() == 2); | |
252 | BOOST_ASIO_CHECK(std::get<2>(iii) == 2); | |
253 | ||
254 | std::variant<move_only, int> mi = co_await (move_only_0() || int_1()); | |
255 | BOOST_ASIO_CHECK(mi.index() == 0); | |
256 | BOOST_ASIO_CHECK(std::get<0>(mi).value == 0); | |
257 | ||
258 | mi = co_await (move_only_ex() || int_1()); | |
259 | BOOST_ASIO_CHECK(mi.index() == 1); | |
260 | BOOST_ASIO_CHECK(std::get<1>(mi) == 1); | |
261 | ||
262 | std::variant<move_only, move_only> mm = | |
263 | co_await (move_only_0() || move_only_1()); | |
264 | BOOST_ASIO_CHECK(mm.index() == 0); | |
265 | BOOST_ASIO_CHECK(std::get<0>(mm).value == 0); | |
266 | ||
267 | mm = co_await (move_only_ex() || move_only_1()); | |
268 | BOOST_ASIO_CHECK(mm.index() == 1); | |
269 | BOOST_ASIO_CHECK(std::get<1>(mm).value == 1); | |
270 | ||
271 | std::variant<move_only, move_only, move_only> mmm = | |
272 | co_await (move_only_0() || move_only_1() || move_only_2()); | |
273 | BOOST_ASIO_CHECK(mmm.index() == 0); | |
274 | BOOST_ASIO_CHECK(std::get<0>(mmm).value == 0); | |
275 | ||
276 | mmm = co_await (move_only_ex() || move_only_1() || move_only_2()); | |
277 | BOOST_ASIO_CHECK(mmm.index() == 1); | |
278 | BOOST_ASIO_CHECK(std::get<1>(mmm).value == 1); | |
279 | ||
280 | mmm = co_await (move_only_ex() || move_only_ex() || move_only_2()); | |
281 | BOOST_ASIO_CHECK(mmm.index() == 2); | |
282 | BOOST_ASIO_CHECK(std::get<2>(mmm).value == 2); | |
283 | } | |
284 | ||
285 | void test_or_awaitable_operator() | |
286 | { | |
287 | boost::asio::io_context ctx; | |
288 | co_spawn(ctx, do_test_or_awaitable_operator(), boost::asio::detached); | |
289 | ctx.run(); | |
290 | } | |
291 | ||
292 | BOOST_ASIO_TEST_SUITE | |
293 | ( | |
294 | "experimental/awaitable_operators", | |
295 | BOOST_ASIO_TEST_CASE(test_and_awaitable_operator) | |
296 | BOOST_ASIO_TEST_CASE(test_or_awaitable_operator) | |
297 | ) |