1// Copyright 2017, 2021, 2022 Peter Dimov.
2// Distributed under the Boost Software License, Version 1.0.
3// https://www.boost.org/LICENSE_1_0.txt
4
5#include <boost/system/result.hpp>
6#include <boost/core/lightweight_test.hpp>
7
8using namespace boost::system;
9
10struct X
11{
12 int v_;
13};
14
15struct Y
16{
17 int v_;
18
19 explicit Y( int v ): v_( v ) {}
20 Y( X x ): v_( x.v_) {}
21
22 Y( Y const& ) = delete;
23 Y& operator=( Y const& ) = delete;
24
25 Y( Y&& r ): v_( r.v_ )
26 {
27 r.v_ = 0;
28 }
29
30 Y& operator=( Y&& r )
31 {
32 if( &r != this )
33 {
34 v_ = r.v_;
35 r.v_ = 0;
36 }
37
38 return *this;
39 }
40};
41
42struct E
43{
44};
45
46int f( int x )
47{
48 return x * 2 + 1;
49}
50
51X g( Y y )
52{
53 return X{ .v_: y.v_ * 2 + 1 };
54}
55
56int& h( int& )
57{
58 static int x = 2;
59 return x;
60}
61
62int main()
63{
64 {
65 result<int> r( 1 );
66
67 r &= f;
68
69 BOOST_TEST( r.has_value() ) && BOOST_TEST_EQ( *r, 3 );
70 }
71
72 {
73 result<int, E> r( in_place_error );
74
75 r &= f;
76
77 BOOST_TEST( r.has_error() );
78 }
79
80 {
81 result<Y> r( in_place_value, 1 );
82
83 r &= g;
84
85 BOOST_TEST( r.has_value() ) && BOOST_TEST_EQ( r->v_, 3 );
86 }
87
88 {
89 result<Y, E> r( in_place_error );
90
91 r &= g;
92
93 BOOST_TEST( r.has_error() );
94 }
95
96 {
97 int x1 = 1;
98 result<int&> r( x1 );
99
100 r &= h;
101
102 BOOST_TEST( r.has_value() ) && BOOST_TEST_EQ( &*r, &h( x1 ) );
103 }
104
105 {
106 result<int&, E> r( in_place_error );
107
108 r &= h;
109
110 BOOST_TEST( r.has_error() );
111 }
112
113 return boost::report_errors();
114}
115

source code of boost/libs/system/test/result_and_eq_fn1v.cpp