Debian Codegraph @

libstdc++-v3/testsuite/20_util/expected/observers.cc

raw
1 // { dg-do run { target c++23 } }
2
3 #include <expected>
4 #include <expected>
5 #include <testsuite_hooks.h>
6
7 struct X
8 {
9 constexpr int f() & { return 1; }
10 constexpr int f() const & { return 2; }
11 constexpr int f() && { return 3; }
12 constexpr int f() const && { return 4; }
13 };
14
15 constexpr bool
16 test_arrow()
17 {
18 std::expected<X, int> e1;
19 VERIFY( e1->f() == 1 );
20 const auto& e2 = e1;
21 VERIFY( e2->f() == 2 );
22
23 return true;
24 }
25
26 constexpr bool
27 test_star()
28 {
29 std::expected<X, int> e1;
30 VERIFY( (*e1).f() == 1 );
31 VERIFY( std::move(*e1).f() == 3 );
32 const auto& e2 = e1;
33 VERIFY( (*e2).f() == 2 );
34 VERIFY( std::move(*e2).f() == 4 );
35
36 std::expected<void, int> v;
37 *v;
38
39 return true;
40 }
41
42 constexpr bool
43 test_has_value()
44 {
45 std::expected<int, int> e;
46 VERIFY( e.has_value() );
47 VERIFY( e );
48 e = std::unexpected(1);
49 VERIFY( ! e.has_value() );
50 VERIFY( ! e );
51
52 std::expected<void, int> v;
53 VERIFY( v.has_value() );
54 VERIFY( v );
55 v = std::unexpected(1);
56 VERIFY( ! v.has_value() );
57 VERIFY( ! v );
58
59 return true;
60 }
61
62 constexpr bool
63 test_value()
64 {
65 std::expected<X, int> e1;
66
67 VERIFY( e1.value().f() == 1 );
68 VERIFY( std::move(e1).value().f() == 3 );
69 const auto& e2 = e1;
70 VERIFY( e2.value().f() == 2 );
71 VERIFY( std::move(e2).value().f() == 4 );
72
73 std::expected<void, int> v1;
74 v1.value();
75 std::move(v1).value();
76
77 return true;
78 }
79
80 void
81 test_value_throw()
82 {
83 std::expected<int, int> e1 = std::unexpected(9);
84
85 try {
86 e1.value();
87 VERIFY( false );
88 } catch (const std::bad_expected_access<int>& e) {
89 VERIFY( e.error() == 9 );
90 }
91 try {
92 std::move(e1).value();
93 VERIFY( false );
94 } catch (const std::bad_expected_access<int>& e) {
95 VERIFY( e.error() == 9 );
96 }
97
98 const auto& e2 = e1;
99 try {
100 e2.value();
101 VERIFY( false );
102 } catch (const std::bad_expected_access<int>& e) {
103 VERIFY( e.error() == 9 );
104 }
105 try {
106 std::move(e2).value();
107 VERIFY( false );
108 } catch (const std::bad_expected_access<int>& e) {
109 VERIFY( e.error() == 9 );
110 }
111
112 std::expected<void, int> v1 = std::unexpected(8);
113 try {
114 v1.value();
115 VERIFY( false );
116 } catch (const std::bad_expected_access<int>& e) {
117 VERIFY( e.error() == 8 );
118 }
119 try {
120 std::move(v1).value();
121 VERIFY( false );
122 } catch (const std::bad_expected_access<int>& e) {
123 VERIFY( e.error() == 8 );
124 }
125 }
126
127 constexpr bool
128 test_error()
129 {
130 std::expected<int, X> e1(std::unexpect);
131
132 VERIFY( e1.error().f() == 1 );
133 VERIFY( std::move(e1).error().f() == 3 );
134 const auto& e2 = e1;
135 VERIFY( e2.error().f() == 2 );
136 VERIFY( std::move(e2).error().f() == 4 );
137
138 std::expected<void, X> v1(std::unexpect);
139
140 VERIFY( v1.error().f() == 1 );
141 VERIFY( std::move(v1).error().f() == 3 );
142 const auto& v2 = v1;
143 VERIFY( v2.error().f() == 2 );
144 VERIFY( std::move(v2).error().f() == 4 );
145
146 return true;
147 }
148
149 constexpr bool
150 test_value_or()
151 {
152 struct Movable
153 {
154 constexpr Movable(int i) : x(i) { }
155 constexpr Movable(const Movable&) = default;
156 constexpr Movable(Movable&& m) : x(m.x) { m.x = -1; }
157 int x;
158
159 constexpr bool operator==(int i) const { return x == i; }
160 };
161
162 std::expected<Movable, int> e1(1);
163
164 Movable m2(2);
165 VERIFY( e1.value_or(2) == 1 );
166 VERIFY( e1.value_or(m2) == 1 );
167 VERIFY( e1.value_or(std::move(m2)) == 1 );
168 VERIFY( m2 == 2 );
169
170 VERIFY( std::move(e1).value_or(m2) == 1 );
171 VERIFY( *e1 == -1 ); // moved
172 VERIFY( m2 == 2 );
173
174 e1 = std::unexpected(3);
175 VERIFY( e1.value_or(m2) == 2 );
176 VERIFY( m2 == 2 );
177 VERIFY( std::move(e1).value_or(m2) == 2 );
178 VERIFY( m2 == 2 );
179
180 VERIFY( e1.value_or(std::move(m2)) == 2 );
181 VERIFY( m2 == -1 );
182
183 m2.x = 4;
184 VERIFY( std::move(e1).value_or(std::move(m2)) == 4 );
185 VERIFY( m2 == -1 );
186
187 VERIFY( e1.value_or(5) == 5 );
188 VERIFY( std::move(e1).value_or(6) == 6 );
189
190 return true;
191 }
192
193 constexpr bool
194 test_error_or()
195 {
196 std::expected<int, int> e1(1), e2(std::unexpect, 3);
197 VERIFY( e1.error_or(2) == 2 );
198 VERIFY( std::move(e1).error_or(2) == 2 );
199 VERIFY( e2.error_or(2) == 3 );
200 VERIFY( std::move(e2).error_or(2) == 3 );
201
202 std::expected<void, int> e3, e4(std::unexpect, 3);
203 VERIFY( e3.error_or(2) == 2 );
204 VERIFY( std::move(e3).error_or(2) == 2 );
205 VERIFY( e4.error_or(2) == 3 );
206 VERIFY( std::move(e4).error_or(2) == 3 );
207
208 return true;
209 }
210
211 int main()
212 {
213 static_assert( test_arrow() );
214 test_arrow();
215 static_assert( test_star() );
216 test_star();
217 static_assert( test_has_value() );
218 test_has_value();
219 static_assert( test_value() );
220 test_value();
221 test_value_throw();
222 static_assert( test_error() );
223 test_error();
224 static_assert( test_value_or() );
225 test_value_or();
226 static_assert( test_error_or() );
227 test_error_or();
228 }