Imported Upstream version 1.64.0
[platform/upstream/boost.git] / libs / asio / test / coroutine.cpp
1 //
2 // coroutine.cpp
3 // ~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2017 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 // Test that header file is self-contained.
17 #include <boost/asio/coroutine.hpp>
18
19 #include "unit_test.hpp"
20
21 // Must come after all other headers.
22 #include <boost/asio/yield.hpp>
23
24 //------------------------------------------------------------------------------
25
26 // Coroutine completes via yield break.
27
28 void yield_break_coro(boost::asio::coroutine& coro)
29 {
30   reenter (coro)
31   {
32     yield return;
33     yield break;
34   }
35 }
36
37 void yield_break_test()
38 {
39   boost::asio::coroutine coro;
40   BOOST_ASIO_CHECK(!coro.is_complete());
41   yield_break_coro(coro);
42   BOOST_ASIO_CHECK(!coro.is_complete());
43   yield_break_coro(coro);
44   BOOST_ASIO_CHECK(coro.is_complete());
45 }
46
47 //------------------------------------------------------------------------------
48
49 // Coroutine completes via return.
50
51 void return_coro(boost::asio::coroutine& coro)
52 {
53   reenter (coro)
54   {
55     return;
56   }
57 }
58
59 void return_test()
60 {
61   boost::asio::coroutine coro;
62   return_coro(coro);
63   BOOST_ASIO_CHECK(coro.is_complete());
64 }
65
66 //------------------------------------------------------------------------------
67
68 // Coroutine completes via exception.
69
70 void exception_coro(boost::asio::coroutine& coro)
71 {
72   reenter (coro)
73   {
74     throw 1;
75   }
76 }
77
78 void exception_test()
79 {
80   boost::asio::coroutine coro;
81   try { exception_coro(coro); } catch (int) {}
82   BOOST_ASIO_CHECK(coro.is_complete());
83 }
84
85 //------------------------------------------------------------------------------
86
87 // Coroutine completes by falling off the end.
88
89 void fall_off_end_coro(boost::asio::coroutine& coro)
90 {
91   reenter (coro)
92   {
93   }
94 }
95
96 void fall_off_end_test()
97 {
98   boost::asio::coroutine coro;
99   fall_off_end_coro(coro);
100   BOOST_ASIO_CHECK(coro.is_complete());
101 }
102
103 //------------------------------------------------------------------------------
104
105 BOOST_ASIO_TEST_SUITE
106 (
107   "coroutine",
108   BOOST_ASIO_TEST_CASE(yield_break_test)
109   BOOST_ASIO_TEST_CASE(return_test)
110   BOOST_ASIO_TEST_CASE(exception_test)
111   BOOST_ASIO_TEST_CASE(fall_off_end_test)
112 )