1 | // |
---|---|
2 | // as_tuple.cpp |
3 | // ~~~~~~~~~~~~ |
4 | // |
5 | // Copyright (c) 2003-2024 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/as_tuple.hpp> |
18 | |
19 | #include <boost/asio/bind_executor.hpp> |
20 | #include <boost/asio/deferred.hpp> |
21 | #include <boost/asio/io_context.hpp> |
22 | #include <boost/asio/post.hpp> |
23 | #include <boost/asio/system_timer.hpp> |
24 | #include <boost/asio/use_future.hpp> |
25 | #include "unit_test.hpp" |
26 | |
27 | void as_tuple_test() |
28 | { |
29 | boost::asio::io_context io1; |
30 | boost::asio::io_context io2; |
31 | boost::asio::system_timer timer1(io1); |
32 | int count = 0; |
33 | |
34 | timer1.expires_after(expiry_time: boost::asio::chrono::seconds(0)); |
35 | timer1.async_wait( |
36 | token: boost::asio::as_tuple( |
37 | completion_token: boost::asio::bind_executor(ex: io2.get_executor(), |
38 | t: [&count](std::tuple<boost::system::error_code>) |
39 | { |
40 | ++count; |
41 | }))); |
42 | |
43 | BOOST_ASIO_CHECK(count == 0); |
44 | |
45 | io1.run(); |
46 | |
47 | BOOST_ASIO_CHECK(count == 0); |
48 | |
49 | io2.run(); |
50 | |
51 | BOOST_ASIO_CHECK(count == 1); |
52 | |
53 | timer1.async_wait( |
54 | token: boost::asio::as_tuple( |
55 | completion_token: boost::asio::bind_executor(ex: io2.get_executor(), |
56 | t: boost::asio::deferred)))( |
57 | [&count](std::tuple<boost::system::error_code>) |
58 | { |
59 | ++count; |
60 | }); |
61 | |
62 | BOOST_ASIO_CHECK(count == 1); |
63 | |
64 | io1.restart(); |
65 | io1.run(); |
66 | |
67 | BOOST_ASIO_CHECK(count == 1); |
68 | |
69 | io2.restart(); |
70 | io2.run(); |
71 | |
72 | BOOST_ASIO_CHECK(count == 2); |
73 | |
74 | # if defined(BOOST_ASIO_HAS_STD_FUTURE_CLASS) |
75 | std::future<std::tuple<boost::system::error_code> > f = timer1.async_wait( |
76 | token: boost::asio::as_tuple( |
77 | completion_token: boost::asio::bind_executor(ex: io2.get_executor(), |
78 | t: boost::asio::use_future))); |
79 | |
80 | BOOST_ASIO_CHECK(f.wait_for(std::chrono::seconds(0)) |
81 | == std::future_status::timeout); |
82 | |
83 | io1.restart(); |
84 | io1.run(); |
85 | |
86 | BOOST_ASIO_CHECK(f.wait_for(std::chrono::seconds(0)) |
87 | == std::future_status::timeout); |
88 | |
89 | io2.restart(); |
90 | io2.run(); |
91 | |
92 | BOOST_ASIO_CHECK(f.wait_for(std::chrono::seconds(0)) |
93 | == std::future_status::ready); |
94 | # endif // defined(BOOST_ASIO_HAS_STD_FUTURE_CLASS) |
95 | } |
96 | |
97 | void as_tuple_constness_test() |
98 | { |
99 | # if defined(BOOST_ASIO_HAS_STD_FUTURE_CLASS) |
100 | boost::asio::io_context io1; |
101 | boost::asio::system_timer timer1(io1); |
102 | |
103 | auto tok1 = boost::asio::as_tuple(completion_token: boost::asio::use_future); |
104 | (void)timer1.async_wait(token&: tok1); |
105 | (void)timer1.async_wait(token: std::move(tok1)); |
106 | |
107 | const auto tok2 = boost::asio::as_tuple(completion_token: boost::asio::use_future); |
108 | (void)timer1.async_wait(token: tok2); |
109 | (void)timer1.async_wait(token: std::move(tok2)); |
110 | |
111 | constexpr auto tok3 = boost::asio::as_tuple(completion_token: boost::asio::use_future); |
112 | (void)timer1.async_wait(token: tok3); |
113 | (void)timer1.async_wait(token: std::move(tok3)); |
114 | # endif // defined(BOOST_ASIO_HAS_STD_FUTURE_CLASS) |
115 | } |
116 | |
117 | BOOST_ASIO_TEST_SUITE |
118 | ( |
119 | "as_tuple", |
120 | BOOST_ASIO_TEST_CASE(as_tuple_test) |
121 | BOOST_ASIO_COMPILE_TEST_CASE(as_tuple_constness_test) |
122 | ) |
123 |