]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/leaf/test/handle_basic_test.cpp
import quincy beta 17.1.0
[ceph.git] / ceph / src / boost / libs / leaf / test / handle_basic_test.cpp
1 // Copyright (c) 2018-2020 Emil Dotchevski and Reverge Studios, Inc.
2
3 // Distributed under the Boost Software License, Version 1.0. (See accompanying
4 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5
6 #include <boost/leaf/detail/config.hpp>
7 #include <boost/leaf/handle_errors.hpp>
8 #include <boost/leaf/pred.hpp>
9 #include <boost/leaf/result.hpp>
10 #include "lightweight_test.hpp"
11
12 namespace leaf = boost::leaf;
13
14 template <int Tag> struct info { int value;};
15
16 enum class error_code
17 {
18 error1=1,
19 error2,
20 error3
21 };
22
23 struct error1_tag { };
24 struct error2_tag { };
25 struct error3_tag { };
26
27 leaf::result<int> compute_answer( int what_to_do ) noexcept
28 {
29 switch( what_to_do )
30 {
31 case 0:
32 return 42;
33 case 1:
34 return leaf::new_error(error_code::error1);
35 case 2:
36 return leaf::new_error(error_code::error2);
37 case 3:
38 return leaf::new_error(error_code::error3);
39 case 4:
40 return leaf::new_error(error1_tag{}, error_code::error1);
41 case 5:
42 return leaf::new_error(error2_tag{}, error_code::error2);
43 default:
44 BOOST_LEAF_ASSERT(what_to_do==6);
45 return leaf::new_error(error3_tag{}, error_code::error3);
46 }
47 }
48
49 leaf::result<int> handle_some_errors( int what_to_do )
50 {
51 return leaf::try_handle_some(
52 [=]
53 {
54 return compute_answer(what_to_do);
55 },
56 []( error1_tag, leaf::match<error_code, error_code::error1> )
57 {
58 return -1;
59 },
60 []( leaf::match<error_code, error_code::error1> )
61 {
62 return -2;
63 } );
64 }
65
66 leaf::result<float> handle_some_errors_float( int what_to_do )
67 {
68 return leaf::try_handle_some(
69 [=]() -> leaf::result<float>
70 {
71 return compute_answer(what_to_do);
72 },
73 []( error2_tag, leaf::match<error_code, error_code::error2> )
74 {
75 return -1.0f;
76 },
77 []( leaf::match<error_code, error_code::error2> )
78 {
79 return -2.0f;
80 } );
81 }
82
83 leaf::result<void> handle_some_errors_void( int what_to_do )
84 {
85 return leaf::try_handle_some(
86 [=]() -> leaf::result<void>
87 {
88 BOOST_LEAF_AUTO(answer, compute_answer(what_to_do));
89 (void) answer;
90 return { };
91 },
92 []( leaf::match<error_code, error_code::error3> )
93 {
94 } );
95 }
96
97 int main()
98 {
99 {
100 int r = leaf::try_handle_all(
101 []() -> leaf::result<int>
102 {
103 return leaf::try_handle_some(
104 []() -> leaf::result<int>
105 {
106 return leaf::try_handle_some(
107 []() -> leaf::result<int>
108 {
109 return leaf::new_error(40);
110 },
111 []( leaf::error_info const & ei, int & v )
112 {
113 ++v;
114 return ei.error();
115 });
116 },
117 []( leaf::error_info const & ei, int & v )
118 {
119 ++v;
120 return ei.error();
121 });
122 },
123 []( int v )
124 {
125 BOOST_TEST_EQ(v, 42);
126 return 1;
127 },
128 []
129 {
130 return 2;
131 });
132 BOOST_TEST_EQ(r, 1);
133 }
134 {
135 int r = leaf::try_handle_all(
136 []() -> leaf::result<int>
137 {
138 return leaf::try_handle_some(
139 []() -> leaf::result<int>
140 {
141 return leaf::try_handle_some(
142 []() -> leaf::result<int>
143 {
144 return leaf::new_error(40);
145 },
146 []( leaf::error_info const & ei, int * v )
147 {
148 ++*v;
149 return ei.error();
150 });
151 },
152 []( leaf::error_info const & ei, int * v )
153 {
154 ++*v;
155 return ei.error();
156 });
157 },
158 []( int v )
159 {
160 BOOST_TEST_EQ(v, 42);
161 return 1;
162 },
163 []
164 {
165 return 2;
166 });
167 BOOST_TEST_EQ(r, 1);
168 }
169
170 ///////////////////////////
171
172 BOOST_TEST_EQ(handle_some_errors(0).value(), 42);
173 BOOST_TEST_EQ(handle_some_errors(1).value(), -2);
174 BOOST_TEST_EQ(handle_some_errors(4).value(), -1);
175 {
176 int r = leaf::try_handle_all(
177 []() -> leaf::result<int>
178 {
179 BOOST_LEAF_AUTO(answer, handle_some_errors(3));
180 (void) answer;
181 return 0;
182 },
183 []( leaf::match<error_code, error_code::error3> )
184 {
185 return 1;
186 },
187 []
188 {
189 return 2;
190 } );
191 BOOST_TEST_EQ(r, 1);
192 }
193
194 ///////////////////////////
195
196 BOOST_TEST_EQ(handle_some_errors_float(0).value(), 42.0f);
197 BOOST_TEST_EQ(handle_some_errors_float(2).value(), -2.0f);
198 BOOST_TEST_EQ(handle_some_errors_float(5).value(), -1.0f);
199 {
200 int r = leaf::try_handle_all(
201 []() -> leaf::result<int>
202 {
203 BOOST_LEAF_AUTO(answer, handle_some_errors_float(1));
204 (void) answer;
205 return 0;
206 },
207 []( leaf::match<error_code, error_code::error1> )
208 {
209 return 1;
210 },
211 []
212 {
213 return 2;
214 } );
215 BOOST_TEST_EQ(r, 1);
216 }
217
218 ///////////////////////////
219
220 BOOST_TEST(handle_some_errors_void(0));
221 BOOST_TEST(handle_some_errors_void(3));
222 {
223 int r = leaf::try_handle_all(
224 []() -> leaf::result<int>
225 {
226 BOOST_LEAF_CHECK(handle_some_errors_void(2));
227 return 0;
228 },
229 []( leaf::match<error_code, error_code::error2> )
230 {
231 return 1;
232 },
233 []
234 {
235 return 2;
236 } );
237 BOOST_TEST_EQ(r, 1);
238 }
239
240 ///////////////////////////
241
242 #ifndef BOOST_LEAF_NO_EXCEPTIONS
243 {
244 int r = leaf::try_handle_all(
245 []() -> leaf::result<int>
246 {
247 BOOST_LEAF_CHECK(handle_some_errors_void(2));
248 return 0;
249 },
250 []( std::exception const & )
251 {
252 return 1;
253 },
254 []
255 {
256 return 2;
257 } );
258 BOOST_TEST_EQ(r, 2);
259 }
260 #endif
261
262 ///////////////////////////
263
264 {
265 int r = leaf::try_handle_all(
266 []() -> leaf::result<int>
267 {
268 return leaf::new_error( info<1>{42} );
269 },
270 []( info<1> const & i1 )
271 {
272 BOOST_TEST_EQ(i1.value, 42);
273 int r = leaf::try_handle_all(
274 []() -> leaf::result<int>
275 {
276 return leaf::new_error( info<1>{43} );
277 },
278 []()
279 {
280 return -1;
281 } );
282 BOOST_TEST_EQ(r, -1);
283 BOOST_TEST_EQ(i1.value, 42);
284 return 0;
285 },
286 []()
287 {
288 return -1;
289 } );
290 BOOST_TEST_EQ(r, 0);
291 }
292
293 ///////////////////////////
294
295 {
296 int r = leaf::try_handle_all(
297 []() -> leaf::result<int>
298 {
299 return leaf::new_error( info<1>{42} );
300 },
301 []( info<1> const & i1 )
302 {
303 BOOST_TEST_EQ(i1.value, 42);
304 int r = leaf::try_handle_all(
305 []() -> leaf::result<int>
306 {
307 return leaf::new_error( info<1>{43} );
308 },
309 []( info<1> const & i1 )
310 {
311 BOOST_TEST_EQ(i1.value, 43);
312 return -1;
313 },
314 []()
315 {
316 return -2;
317 } );
318 BOOST_TEST_EQ(r, -1);
319 BOOST_TEST_EQ(i1.value, 42);
320 return 0;
321 },
322 []()
323 {
324 return -1;
325 } );
326 BOOST_TEST_EQ(r, 0);
327 }
328
329 ///////////////////////////
330
331 {
332 int r = leaf::try_handle_all(
333 []() -> leaf::result<int>
334 {
335 return leaf::try_handle_some(
336 []() -> leaf::result<int>
337 {
338 return leaf::new_error( info<1>{1} );
339 },
340 []( leaf::error_info const & err, info<1> const & i1, info<2> const * i2 )
341 {
342 //We have space for info<2> in the context but i2 is null.
343 BOOST_TEST_EQ(i1.value, 1);
344 BOOST_TEST(!i2);
345 return err.error().load(info<2>{2});
346 } );
347 },
348 []( info<1> const & i1, info<2> const & i2 )
349 {
350 BOOST_TEST_EQ(i1.value, 1);
351 BOOST_TEST_EQ(i2.value, 2);
352 return 0;
353 },
354 []()
355 {
356 return -1;
357 } );
358 BOOST_TEST_EQ(r, 0);
359 }
360
361 ///////////////////////////
362
363 {
364 int r = leaf::try_handle_all(
365 []() -> leaf::result<int>
366 {
367 return leaf::try_handle_some(
368 []() -> leaf::result<int>
369 {
370 return leaf::new_error( info<1>{1}, info<2>{-2} );
371 },
372 []( leaf::error_info const & err, info<1> const & i1, info<2> const & i2 )
373 {
374 BOOST_TEST_EQ(i1.value, 1);
375 BOOST_TEST_EQ(i2.value, -2);
376 return err.error().load(info<2>{2});
377 } );
378 },
379 []( info<1> const & i1, info<2> const & i2 )
380 {
381 BOOST_TEST_EQ(i1.value, 1);
382 BOOST_TEST_EQ(i2.value, 2);
383 return 0;
384 },
385 []()
386 {
387 return -1;
388 } );
389 BOOST_TEST_EQ(r, 0);
390 }
391
392 ///////////////////////////
393
394 return boost::report_errors();
395 }