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 | |
8 | using namespace boost::system; |
9 | |
10 | struct X |
11 | { |
12 | int v_; |
13 | }; |
14 | |
15 | struct 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&& ) = delete; |
31 | }; |
32 | |
33 | struct E |
34 | { |
35 | }; |
36 | |
37 | int main() |
38 | { |
39 | { |
40 | result<int> r( 1 ); |
41 | |
42 | int x = r | 2; |
43 | |
44 | BOOST_TEST_EQ( x, 1 ); |
45 | } |
46 | |
47 | { |
48 | result<int> const r( 1 ); |
49 | |
50 | int x = r | 2; |
51 | |
52 | BOOST_TEST_EQ( x, 1 ); |
53 | } |
54 | |
55 | { |
56 | int x = result<int>( 1 ) | 2; |
57 | |
58 | BOOST_TEST_EQ( x, 1 ); |
59 | } |
60 | |
61 | { |
62 | result<int, E> r( in_place_error ); |
63 | |
64 | int x = r | 2; |
65 | |
66 | BOOST_TEST_EQ( x, 2 ); |
67 | } |
68 | |
69 | { |
70 | result<int, E> const r( in_place_error ); |
71 | |
72 | int x = r | 2; |
73 | |
74 | BOOST_TEST_EQ( x, 2 ); |
75 | } |
76 | |
77 | { |
78 | int x = result<int, E>( in_place_error ) | 2; |
79 | |
80 | BOOST_TEST_EQ( x, 2 ); |
81 | } |
82 | |
83 | { |
84 | Y y = result<Y>( in_place_value, 1 ) | Y{2}; |
85 | |
86 | BOOST_TEST_EQ( y.v_, 1 ); |
87 | } |
88 | |
89 | { |
90 | Y y = result<Y, E>( in_place_error ) | Y{2}; |
91 | |
92 | BOOST_TEST_EQ( y.v_, 2 ); |
93 | } |
94 | |
95 | { |
96 | Y y = result<Y>( in_place_value, 1 ) | X{.v_: 2}; |
97 | |
98 | BOOST_TEST_EQ( y.v_, 1 ); |
99 | } |
100 | |
101 | { |
102 | Y y = result<Y, E>( in_place_error ) | X{.v_: 2}; |
103 | |
104 | BOOST_TEST_EQ( y.v_, 2 ); |
105 | } |
106 | |
107 | { |
108 | int x1 = 1; |
109 | int x2 = 2; |
110 | |
111 | result<int&> r( x1 ); |
112 | |
113 | int& x = r | x2; |
114 | |
115 | BOOST_TEST_EQ( &x, &x1 ); |
116 | } |
117 | |
118 | { |
119 | int x1 = 1; |
120 | int x2 = 2; |
121 | |
122 | result<int&> const r( x1 ); |
123 | |
124 | int& x = r | x2; |
125 | |
126 | BOOST_TEST_EQ( &x, &x1 ); |
127 | } |
128 | |
129 | { |
130 | int x1 = 1; |
131 | int x2 = 2; |
132 | |
133 | int& x = result<int&>( x1 ) | x2; |
134 | |
135 | BOOST_TEST_EQ( &x, &x1 ); |
136 | } |
137 | |
138 | { |
139 | int x2 = 2; |
140 | |
141 | result<int&, E> r( in_place_error ); |
142 | |
143 | int& x = r | x2; |
144 | |
145 | BOOST_TEST_EQ( &x, &x2 ); |
146 | } |
147 | |
148 | { |
149 | int x2 = 2; |
150 | |
151 | result<int&, E> const r( in_place_error ); |
152 | |
153 | int& x = r | x2; |
154 | |
155 | BOOST_TEST_EQ( &x, &x2 ); |
156 | } |
157 | |
158 | { |
159 | int x2 = 2; |
160 | |
161 | int& x = result<int&, E>( in_place_error ) | x2; |
162 | |
163 | BOOST_TEST_EQ( &x, &x2 ); |
164 | } |
165 | |
166 | return boost::report_errors(); |
167 | } |
168 | |