| 1 | // Copyright 2023 Peter Dimov. |
| 2 | // Distributed under the Boost Software License, Version 1.0. |
| 3 | // https://www.boost.org/LICENSE_1_0.txt |
| 4 | |
| 5 | #if defined(_MSC_VER) && _MSC_VER < 1910 |
| 6 | # pragma warning( disable: 4800 ) // forcing value to bool 'true' or 'false' |
| 7 | #endif |
| 8 | |
| 9 | #include <boost/system/result.hpp> |
| 10 | #include <boost/core/lightweight_test.hpp> |
| 11 | #include <boost/core/lightweight_test_trait.hpp> |
| 12 | #include <utility> |
| 13 | #include <type_traits> |
| 14 | |
| 15 | using namespace boost::system; |
| 16 | |
| 17 | // Tricky mixed construction cases |
| 18 | // https://github.com/boostorg/system/issues/104 |
| 19 | // https://brevzin.github.io//c++/2023/01/18/optional-construction/ |
| 20 | |
| 21 | template<class R1, class R2> void test() |
| 22 | { |
| 23 | { |
| 24 | R1 r1( make_error_code( e: errc::invalid_argument ) ); |
| 25 | R2 r2( r1 ); |
| 26 | |
| 27 | BOOST_TEST( !r2.has_value() ); |
| 28 | } |
| 29 | |
| 30 | { |
| 31 | R1 r1( 0 ); |
| 32 | R2 r2( r1 ); |
| 33 | |
| 34 | BOOST_TEST( r2.has_value() ) && BOOST_TEST_EQ( r2.value(), false ); |
| 35 | } |
| 36 | |
| 37 | { |
| 38 | R1 r1( 1 ); |
| 39 | R2 r2( r1 ); |
| 40 | |
| 41 | BOOST_TEST( r2.has_value() ) && BOOST_TEST_EQ( r2.value(), true ); |
| 42 | } |
| 43 | |
| 44 | { |
| 45 | R1 r1( make_error_code( e: errc::invalid_argument ) ); |
| 46 | R2 r2( std::move( r1 ) ); |
| 47 | |
| 48 | BOOST_TEST( !r2.has_value() ); |
| 49 | } |
| 50 | |
| 51 | { |
| 52 | R1 r1( 0 ); |
| 53 | R2 r2( std::move( r1 ) ); |
| 54 | |
| 55 | BOOST_TEST( r2.has_value() ) && BOOST_TEST_EQ( r2.value(), false ); |
| 56 | } |
| 57 | |
| 58 | { |
| 59 | R1 r1( 1 ); |
| 60 | R2 r2( std::move( r1 ) ); |
| 61 | |
| 62 | BOOST_TEST( r2.has_value() ) && BOOST_TEST_EQ( r2.value(), true ); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | struct X |
| 67 | { |
| 68 | }; |
| 69 | |
| 70 | int main() |
| 71 | { |
| 72 | test< result<int>, result<bool> >(); |
| 73 | test< result<int> const, result<bool> >(); |
| 74 | |
| 75 | BOOST_TEST_TRAIT_FALSE((std::is_constructible<result<bool>, result<X>&>)); |
| 76 | BOOST_TEST_TRAIT_FALSE((std::is_constructible<result<bool>, result<X> const&>)); |
| 77 | BOOST_TEST_TRAIT_FALSE((std::is_constructible<result<bool>, result<X>&&>)); |
| 78 | BOOST_TEST_TRAIT_FALSE((std::is_constructible<result<bool>, result<X> const&&>)); |
| 79 | |
| 80 | BOOST_TEST_TRAIT_FALSE((std::is_constructible<result<bool const>, result<X>&>)); |
| 81 | BOOST_TEST_TRAIT_FALSE((std::is_constructible<result<bool const>, result<X> const&>)); |
| 82 | BOOST_TEST_TRAIT_FALSE((std::is_constructible<result<bool const>, result<X>&&>)); |
| 83 | BOOST_TEST_TRAIT_FALSE((std::is_constructible<result<bool const>, result<X> const&&>)); |
| 84 | |
| 85 | return boost::report_errors(); |
| 86 | } |
| 87 | |