5 // Copyright (c) 2003-2022 Christopher M. Kohlhoff (chris at kohlhoff dot com)
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)
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)
16 // Test that header file is self-contained.
17 #include <boost/asio/execution/set_error.hpp>
19 #include <boost/system/error_code.hpp>
20 #include "../unit_test.hpp"
22 namespace exec
= boost::asio::execution
;
24 static int call_count
= 0;
30 struct const_member_set_error
33 void set_error(BOOST_ASIO_MOVE_ARG(E
) e
) const BOOST_ASIO_NOEXCEPT
35 typename
boost::asio::decay
<E
>::type
tmp(BOOST_ASIO_MOVE_CAST(E
)(e
));
41 #if !defined(BOOST_ASIO_HAS_DEDUCED_SET_ERROR_MEMBER_TRAIT)
48 struct set_error_member
<const const_member_set_error
, E
>
50 BOOST_ASIO_STATIC_CONSTEXPR(bool, is_valid
= true);
51 BOOST_ASIO_STATIC_CONSTEXPR(bool, is_noexcept
= true);
52 typedef void result_type
;
59 #endif // !defined(BOOST_ASIO_HAS_DEDUCED_SET_ERROR_MEMBER_TRAIT)
61 struct free_set_error_const_receiver
64 friend void set_error(const free_set_error_const_receiver
&,
65 BOOST_ASIO_MOVE_ARG(E
) e
) BOOST_ASIO_NOEXCEPT
67 typename
boost::asio::decay
<E
>::type
tmp(BOOST_ASIO_MOVE_CAST(E
)(e
));
73 #if !defined(BOOST_ASIO_HAS_DEDUCED_SET_ERROR_FREE_TRAIT)
80 struct set_error_free
<const free_set_error_const_receiver
, E
>
82 BOOST_ASIO_STATIC_CONSTEXPR(bool, is_valid
= true);
83 BOOST_ASIO_STATIC_CONSTEXPR(bool, is_noexcept
= true);
84 typedef void result_type
;
91 #endif // !defined(BOOST_ASIO_HAS_DEDUCED_SET_ERROR_FREE_TRAIT)
93 struct non_const_member_set_error
96 void set_error(BOOST_ASIO_MOVE_ARG(E
) e
) BOOST_ASIO_NOEXCEPT
98 typename
boost::asio::decay
<E
>::type
tmp(BOOST_ASIO_MOVE_CAST(E
)(e
));
104 #if !defined(BOOST_ASIO_HAS_DEDUCED_SET_ERROR_MEMBER_TRAIT)
110 template <typename E
>
111 struct set_error_member
<non_const_member_set_error
, E
>
113 BOOST_ASIO_STATIC_CONSTEXPR(bool, is_valid
= true);
114 BOOST_ASIO_STATIC_CONSTEXPR(bool, is_noexcept
= true);
115 typedef void result_type
;
118 } // namespace traits
122 #endif // !defined(BOOST_ASIO_HAS_DEDUCED_SET_ERROR_MEMBER_TRAIT)
124 struct free_set_error_non_const_receiver
126 template <typename E
>
127 friend void set_error(free_set_error_non_const_receiver
&,
128 BOOST_ASIO_MOVE_ARG(E
) e
) BOOST_ASIO_NOEXCEPT
130 typename
boost::asio::decay
<E
>::type
tmp(BOOST_ASIO_MOVE_CAST(E
)(e
));
136 #if !defined(BOOST_ASIO_HAS_DEDUCED_SET_ERROR_FREE_TRAIT)
142 template <typename E
>
143 struct set_error_free
<free_set_error_non_const_receiver
, E
>
145 BOOST_ASIO_STATIC_CONSTEXPR(bool, is_valid
= true);
146 BOOST_ASIO_STATIC_CONSTEXPR(bool, is_noexcept
= true);
147 typedef void result_type
;
150 } // namespace traits
154 #endif // !defined(BOOST_ASIO_HAS_DEDUCED_SET_ERROR_FREE_TRAIT)
156 void test_can_set_error()
158 BOOST_ASIO_CONSTEXPR
bool b1
= exec::can_set_error
<
159 no_set_error
&, boost::system::error_code
>::value
;
160 BOOST_ASIO_CHECK(b1
== false);
162 BOOST_ASIO_CONSTEXPR
bool b2
= exec::can_set_error
<
163 const no_set_error
&, boost::system::error_code
>::value
;
164 BOOST_ASIO_CHECK(b2
== false);
166 BOOST_ASIO_CONSTEXPR
bool b3
= exec::can_set_error
<
167 const_member_set_error
&, boost::system::error_code
>::value
;
168 BOOST_ASIO_CHECK(b3
== true);
170 BOOST_ASIO_CONSTEXPR
bool b4
= exec::can_set_error
<
171 const const_member_set_error
&, boost::system::error_code
>::value
;
172 BOOST_ASIO_CHECK(b4
== true);
174 BOOST_ASIO_CONSTEXPR
bool b5
= exec::can_set_error
<
175 free_set_error_const_receiver
&, boost::system::error_code
>::value
;
176 BOOST_ASIO_CHECK(b5
== true);
178 BOOST_ASIO_CONSTEXPR
bool b6
= exec::can_set_error
<
179 const free_set_error_const_receiver
&, boost::system::error_code
>::value
;
180 BOOST_ASIO_CHECK(b6
== true);
182 BOOST_ASIO_CONSTEXPR
bool b7
= exec::can_set_error
<
183 non_const_member_set_error
&, boost::system::error_code
>::value
;
184 BOOST_ASIO_CHECK(b7
== true);
186 BOOST_ASIO_CONSTEXPR
bool b8
= exec::can_set_error
<
187 const non_const_member_set_error
&, boost::system::error_code
>::value
;
188 BOOST_ASIO_CHECK(b8
== false);
190 BOOST_ASIO_CONSTEXPR
bool b9
= exec::can_set_error
<
191 free_set_error_non_const_receiver
&, boost::system::error_code
>::value
;
192 BOOST_ASIO_CHECK(b9
== true);
194 BOOST_ASIO_CONSTEXPR
bool b10
= exec::can_set_error
<
195 const free_set_error_non_const_receiver
&, boost::system::error_code
>::value
;
196 BOOST_ASIO_CHECK(b10
== false);
199 void increment(int* count
)
204 void test_set_error()
206 boost::system::error_code ec
;
209 const_member_set_error ex1
= {};
210 exec::set_error(ex1
, ec
);
211 BOOST_ASIO_CHECK(call_count
== 1);
214 const const_member_set_error ex2
= {};
215 exec::set_error(ex2
, ec
);
216 BOOST_ASIO_CHECK(call_count
== 1);
219 exec::set_error(const_member_set_error(), ec
);
220 BOOST_ASIO_CHECK(call_count
== 1);
223 free_set_error_const_receiver ex3
= {};
224 exec::set_error(ex3
, ec
);
225 BOOST_ASIO_CHECK(call_count
== 1);
228 const free_set_error_const_receiver ex4
= {};
229 exec::set_error(ex4
, ec
);
230 BOOST_ASIO_CHECK(call_count
== 1);
233 exec::set_error(free_set_error_const_receiver(), ec
);
234 BOOST_ASIO_CHECK(call_count
== 1);
237 non_const_member_set_error ex5
= {};
238 exec::set_error(ex5
, ec
);
239 BOOST_ASIO_CHECK(call_count
== 1);
242 free_set_error_non_const_receiver ex6
= {};
243 exec::set_error(ex6
, ec
);
244 BOOST_ASIO_CHECK(call_count
== 1);
247 BOOST_ASIO_TEST_SUITE
250 BOOST_ASIO_TEST_CASE(test_can_set_error
)
251 BOOST_ASIO_TEST_CASE(test_set_error
)