Debian Codegraph @

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

raw
1 // { dg-do run { target c++23 } }
2
3 #include <expected>
4 #include <type_traits>
5 #include <testsuite_hooks.h>
6
7 int dtor_count;
8 constexpr void reset_dtor_count()
9 {
10 if (!std::is_constant_evaluated())
11 dtor_count = 0;
12 }
13 constexpr void inc_dtor_count()
14 {
15 if (!std::is_constant_evaluated())
16 ++dtor_count;
17 }
18 constexpr bool check_dtor_count(int c)
19 {
20 if (std::is_constant_evaluated())
21 return true;
22 return dtor_count == c;
23 }
24
25 struct X
26 {
27 constexpr X(int i, int j = 0) noexcept : n(i+j) { }
28 constexpr X(std::initializer_list<int> l, void*) noexcept : n(l.size()) { }
29
30 constexpr X(const X&) = default;
31 constexpr X(X&& x) noexcept : n(x.n) { x.n = -1; }
32
33 constexpr X& operator=(const X&) = default;
34 constexpr X& operator=(X&& x) noexcept { n = x.n; x.n = -1; return *this; }
35
36 constexpr ~X()
37 {
38 inc_dtor_count();
39 }
40
41 constexpr bool operator==(const X&) const = default;
42 constexpr bool operator==(int i) const { return n == i; }
43
44 int n;
45 };
46
47 constexpr bool
48 test_copy(bool = true)
49 {
50 reset_dtor_count();
51
52 std::expected<int, int> e1(1), e2(2), e3(std::unexpect, 3);
53
54 e1 = e1;
55 e1 = e2; // T = T
56 VERIFY( e1.value() == e2.value() );
57 e1 = e3; // T = E
58 VERIFY( ! e1.has_value() );
59 VERIFY( e1.error() == e3.error() );
60 e1 = e3; // E = E
61 VERIFY( ! e1.has_value() );
62 VERIFY( e1.error() == e3.error() );
63 e1 = e2; // E = T
64 VERIFY( e1.value() == e2.value() );
65
66 e1 = std::move(e1);
67 e1 = std::move(e2); // T = T
68 VERIFY( e1.value() == e2.value() );
69 e1 = std::move(e3); // T = E
70 VERIFY( ! e1.has_value() );
71 VERIFY( e1.error() == e3.error() );
72 e1 = std::move(e3); // E = E
73 VERIFY( ! e1.has_value() );
74 VERIFY( e1.error() == e3.error() );
75 e1 = std::move(e2); // E = T
76 VERIFY( e1.value() == e2.value() );
77
78 std::expected<X, X> x1(1), x2(2), x3(std::unexpect, 3);
79
80 x1 = x1;
81
82 x1 = x2; // T = T
83 VERIFY( check_dtor_count(0) );
84 VERIFY( x1.value() == x2.value() );
85 x1 = x3; // T = E
86 VERIFY( check_dtor_count(1) );
87 VERIFY( ! x1.has_value() );
88 x1 = x3; // E = E
89 VERIFY( check_dtor_count(1) );
90 VERIFY( ! x1.has_value() );
91 x1 = x2; // E = T
92 VERIFY( check_dtor_count(2) );
93 VERIFY( x1.value() == x2.value() );
94
95 reset_dtor_count();
96
97 x1 = std::move(x1);
98 VERIFY( x1.value() == -1 );
99
100 x1 = std::move(x2); // T = T
101 VERIFY( check_dtor_count(0) );
102 VERIFY( x1.value() == 2 );
103 VERIFY( x2.value() == -1 );
104 x1 = std::move(x3); // T = E
105 VERIFY( check_dtor_count(1) );
106 VERIFY( ! x1.has_value() );
107 VERIFY( x1.error() == 3 );
108 VERIFY( x3.error() == -1 );
109 x3.error().n = 33;
110 x1 = std::move(x3); // E = E
111 VERIFY( check_dtor_count(1) );
112 VERIFY( ! x1.has_value() );
113 VERIFY( x1.error() == 33 );
114 VERIFY( x3.error() == -1 );
115 x2.value().n = 22;
116 x1 = std::move(x2); // E = T
117 VERIFY( check_dtor_count(2) );
118 VERIFY( x1.value() == 22 );
119 VERIFY( x2.value() == -1 );
120
121 std::expected<void, int> ev1, ev2, ev3(std::unexpect, 3);
122
123 ev1 = ev2; // T = T
124 VERIFY( ev1.has_value() );
125 ev1 = ev3; // T = E
126 VERIFY( ! ev1.has_value() );
127 VERIFY( ev1.error() == ev3.error() );
128 ev1 = ev3; // E = E
129 VERIFY( ! ev1.has_value() );
130 VERIFY( ev1.error() == ev3.error() );
131 ev1 = ev2; // E = T
132 VERIFY( ev1.has_value() );
133
134 reset_dtor_count();
135 std::expected<void, X> xv1, xv2, xv3(std::unexpect, 3);
136
137 xv1 = std::move(xv2); // T = T
138 VERIFY( check_dtor_count(0) );
139 VERIFY( xv1.has_value() );
140 xv1 = std::move(xv3); // T = E
141 VERIFY( check_dtor_count(0) );
142 VERIFY( ! xv1.has_value() );
143 VERIFY( xv1.error() == 3 );
144 VERIFY( xv3.error() == -1 );
145 xv3.error().n = 33;
146 xv1 = std::move(xv3); // E = E
147 VERIFY( check_dtor_count(0) );
148 VERIFY( xv1.error() == 33 );
149 VERIFY( xv3.error() == -1 );
150 xv1 = std::move(xv2); // E = T
151 VERIFY( check_dtor_count(1) );
152 VERIFY( xv1.has_value() );
153
154 return true;
155 }
156
157 constexpr bool
158 test_converting(bool = true)
159 {
160 std::expected<int, int> e1(1);
161 std::expected<unsigned, long> e2(2U), e3(std::unexpect, 3L);
162 e1 = e2;
163 VERIFY( e1.value() == e2.value() );
164 e1 = e3;
165 VERIFY( ! e1.has_value() );
166 VERIFY( e1.error() == e3.error() );
167 e1 = e2;
168 VERIFY( e1.value() == e2.value() );
169
170 e1 = std::move(e3);
171 VERIFY( ! e1.has_value() );
172 VERIFY( e1.error() == e3.error() );
173 e1 = std::move(e2);
174 VERIFY( e1.value() == e2.value() );
175
176 std::expected<void, int> ev4;
177 std::expected<const void, long> ev5(std::unexpect, 5);
178 ev4 = ev5;
179 VERIFY( ! ev4.has_value() );
180 VERIFY( ev4.error() == 5 );
181 ev4 = std::expected<volatile void, unsigned>();
182 VERIFY( ev4.has_value() );
183 ev4 = std::move(ev5);
184 VERIFY( ! ev4.has_value() );
185 VERIFY( ev4.error() == 5 );
186
187 return true;
188 }
189
190 constexpr bool
191 test_unexpected(bool = true)
192 {
193 reset_dtor_count();
194
195 std::expected<X, int> e1(0);
196
197 e1 = std::unexpected<int>(5);
198 VERIFY( ! e1.has_value() );
199 VERIFY( e1.error() == 5 );
200 VERIFY( check_dtor_count(1) );
201
202 e1 = std::unexpected<int>(6);
203 VERIFY( check_dtor_count(1) );
204
205 std::expected<int, X> e2;
206
207 std::unexpected<X> x(std::in_place, 1, 2);
208 e2 = x;
209 VERIFY( check_dtor_count(1) );
210
211 e2 = 1;
212 VERIFY( e2.value() == 1 );
213 VERIFY( check_dtor_count(2) );
214
215 return true;
216 }
217
218 constexpr bool
219 test_emplace(bool = true)
220 {
221 reset_dtor_count();
222
223 std::expected<int, int> e1(1);
224 e1.emplace(2);
225 VERIFY( e1.value() == 2 );
226
227 std::expected<void, int> ev2;
228 ev2.emplace();
229 VERIFY( ev2.has_value() );
230
231 std::expected<X, int> e3(std::in_place, 0, 0);
232
233 e3.emplace({1,2,3}, nullptr);
234 VERIFY( e3.value() == 3 );
235 VERIFY( check_dtor_count(1) );
236
237 e3.emplace(2, 2);
238 VERIFY( e3.value() == 4 );
239 VERIFY( check_dtor_count(2) );
240
241 std::expected<int, X> ev4(std::unexpect, 4);
242
243 ev4.emplace(5);
244 VERIFY( ev4.value() == 5 );
245 VERIFY( check_dtor_count(3) );
246
247 ev4.emplace(6);
248 VERIFY( ev4.value() == 6 );
249 VERIFY( check_dtor_count(3) );
250
251 return true;
252 }
253
254 void
255 test_exception_safety()
256 {
257 struct CopyThrows
258 {
259 CopyThrows(int i) noexcept : x(i) { }
260 CopyThrows(const CopyThrows&) { throw 1; }
261 CopyThrows(CopyThrows&&) = default;
262 CopyThrows& operator=(const CopyThrows&) = default;
263 CopyThrows& operator=(CopyThrows&&) = default;
264 int x;
265
266 bool operator==(int i) const { return x == i; }
267 };
268
269 struct MoveThrows
270 {
271 MoveThrows(int i) noexcept : x(i) { }
272 MoveThrows(const MoveThrows&) = default;
273 MoveThrows(MoveThrows&&) { throw 1L; }
274 MoveThrows& operator=(const MoveThrows&) = default;
275 MoveThrows& operator=(MoveThrows&&) = default;
276 int x;
277
278 bool operator==(int i) const { return x == i; }
279 };
280
281 std::expected<CopyThrows, MoveThrows> c(std::unexpect, 1);
282
283 // operator=(U&&)
284 try {
285 CopyThrows x(2);
286 c = x;
287 VERIFY( false );
288 } catch (int) {
289 VERIFY( ! c.has_value() );
290 VERIFY( c.error() == 1 );
291 }
292
293 c = CopyThrows(2);
294
295 try {
296 c = std::unexpected<MoveThrows>(3);
297 VERIFY( false );
298 } catch (long) {
299 VERIFY( c.value() == 2 );
300 }
301 }
302
303 int main(int argc, char**)
304 {
305 bool non_constant = argc == 1; // force non-constant evaluation
306
307 static_assert( test_copy() );
308 test_copy(non_constant);
309 static_assert( test_converting() );
310 test_converting(non_constant);
311 static_assert( test_unexpected() );
312 test_unexpected(non_constant);
313 static_assert( test_emplace() );
314 test_emplace(non_constant);
315
316 test_exception_safety();
317
318 // Ensure the non-constexpr tests actually ran:
319 VERIFY( dtor_count != 0 );
320 }