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

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