]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/leaf/test/try_exception_and_result_test.cpp
1 // Copyright (c) 2018-2020 Emil Dotchevski and Reverge Studios, Inc.
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)
6 #include <boost/leaf/detail/config.hpp>
7 #ifdef BOOST_LEAF_NO_EXCEPTIONS
13 std::cout
<< "Unit test not applicable." << std::endl
;
19 #include <boost/leaf/handle_errors.hpp>
20 #include <boost/leaf/pred.hpp>
21 #include <boost/leaf/result.hpp>
22 #include "lightweight_test.hpp"
24 namespace leaf
= boost::leaf
;
26 template <int> struct info
{ int value
; };
28 struct my_exception
: std::exception
46 leaf::result
<int> r
= leaf::try_handle_some(
47 []() -> leaf::result
<int>
56 BOOST_TEST_EQ(r
.value(), 42);
59 leaf::result
<int> r
= leaf::try_handle_some(
60 []() -> leaf::result
<int>
62 throw leaf::exception( my_exception(), info
<1>{1} );
64 []( my_exception
const &, info
<1> const & x
)
66 BOOST_TEST_EQ(x
.value
, 1);
70 BOOST_TEST_EQ(r
.value(), 1);
73 leaf::result
<int> r
= leaf::try_handle_some(
74 []() -> leaf::result
<int>
76 throw leaf::exception( info
<1>{1} );
78 []( info
<1> const & x
)
80 BOOST_TEST_EQ(x
.value
, 1);
84 BOOST_TEST_EQ(r
.value(), 1);
87 leaf::result
<int> r
= leaf::try_handle_some(
88 []() -> leaf::result
<int>
90 return leaf::new_error( info
<1>{1} );
92 []( info
<1> const & x
)
94 BOOST_TEST_EQ(x
.value
, 1);
98 BOOST_TEST_EQ(r
.value(), 1);
101 ///////////////////////////
104 auto error_handlers
= std::make_tuple(
105 []( my_exception
const &, info
<1> const & x
) -> leaf::result
<int>
107 BOOST_TEST_EQ(x
.value
, 1);
110 []( info
<1> const & x
) -> leaf::result
<int>
112 BOOST_TEST_EQ(x
.value
, 1);
116 leaf::result
<int> r
= leaf::try_handle_some(
117 []() -> leaf::result
<int>
123 BOOST_TEST_EQ(r
.value(), 42);
126 leaf::result
<int> r
= leaf::try_handle_some(
127 []() -> leaf::result
<int>
129 throw leaf::exception( my_exception(), info
<1>{1} );
133 BOOST_TEST_EQ(r
.value(), 1);
136 leaf::result
<int> r
= leaf::try_handle_some(
137 []() -> leaf::result
<int>
139 throw leaf::exception( info
<1>{1} );
143 BOOST_TEST_EQ(r
.value(), 2);
146 leaf::result
<int> r
= leaf::try_handle_some(
147 []() -> leaf::result
<int>
149 return leaf::new_error( info
<1>{1} );
153 BOOST_TEST_EQ(r
.value(), 2);
157 ///////////////////////////
160 int r
= leaf::try_handle_all(
161 []() -> leaf::result
<int>
169 BOOST_TEST_EQ(r
, 42);
172 int r
= leaf::try_handle_all(
173 []() -> leaf::result
<int>
175 throw leaf::exception( my_exception(), info
<1>{1} );
177 []( my_exception
const &, info
<1> const & x
)
179 BOOST_TEST_EQ(x
.value
, 1);
189 int r
= leaf::try_handle_all(
190 []() -> leaf::result
<int>
192 throw leaf::exception( info
<1>{1} );
194 []( info
<1> const & x
)
196 BOOST_TEST_EQ(x
.value
, 1);
206 int r
= leaf::try_handle_all(
207 []() -> leaf::result
<int>
209 return leaf::new_error( info
<1>{1} );
211 []( info
<1> const & x
)
213 BOOST_TEST_EQ(x
.value
, 1);
223 ///////////////////////////
226 auto error_handlers
= std::make_tuple(
227 []( my_exception
const &, info
<1> const & x
)
229 BOOST_TEST_EQ(x
.value
, 1);
232 []( info
<1> const & x
)
234 BOOST_TEST_EQ(x
.value
, 1);
242 int r
= leaf::try_handle_all(
243 []() -> leaf::result
<int>
248 BOOST_TEST_EQ(r
, 42);
251 int r
= leaf::try_handle_all(
252 []() -> leaf::result
<int>
254 throw leaf::exception( my_exception(), info
<1>{1} );
260 int r
= leaf::try_handle_all(
261 []() -> leaf::result
<int>
263 throw leaf::exception( info
<1>{1} );
269 int r
= leaf::try_handle_all(
270 []() -> leaf::result
<int>
272 return leaf::new_error( info
<1>{1} );
279 ///////////////////////////
282 int r
= leaf::try_handle_all(
283 []() -> leaf::result
<int>
285 return leaf::try_handle_all(
286 []() -> leaf::result
<int>
288 return leaf::new_error( info
<1>{1} );
290 []( info
<1> const & ) -> int
292 BOOST_LEAF_THROW_EXCEPTION(my_exception());
299 []( my_exception
const &, info
<1> )
303 []( my_exception
const & )
315 int r
= leaf::try_handle_all(
316 []() -> leaf::result
<int>
318 return leaf::try_handle_all(
319 []() -> leaf::result
<int>
321 return leaf::new_error( info
<1>{1} );
323 []( info
<1> const & x
) -> int
325 BOOST_TEST_EQ(x
.value
, 1);
326 BOOST_LEAF_THROW_EXCEPTION();
333 []( my_exception
const &, info
<1> )
337 []( my_exception
const & )
349 ///////////////////////////
352 auto error_handlers
= std::make_tuple(
353 []( info
<1> const & x
) -> int
355 BOOST_TEST_EQ(x
.value
, 1);
356 BOOST_LEAF_THROW_EXCEPTION(my_exception());
362 int r
= leaf::try_handle_all(
363 [&]() -> leaf::result
<int>
365 return leaf::try_handle_all(
366 [&]() -> leaf::result
<int>
368 return leaf::new_error( info
<1>{1} );
372 []( my_exception
const &, info
<1> )
376 []( my_exception
const & )
388 auto error_handlers
= std::make_tuple(
389 []( info
<1> const & x
) -> int
391 BOOST_TEST_EQ(x
.value
, 1);
392 BOOST_LEAF_THROW_EXCEPTION();
398 int r
= leaf::try_handle_all(
399 [&]() -> leaf::result
<int>
401 return leaf::try_handle_all(
402 [&]() -> leaf::result
<int>
404 return leaf::new_error( info
<1>{1} );
408 []( my_exception
const &, info
<1> )
412 []( my_exception
const & )
424 ///////////////////////////
427 int r
= leaf::try_handle_all(
428 []() -> leaf::result
<int>
430 return leaf::try_handle_some(
431 []() -> leaf::result
<int>
433 return leaf::new_error( info
<1>{1} );
435 []( info
<1> const & x
) -> int
437 BOOST_TEST_EQ(x
.value
, 1);
438 BOOST_LEAF_THROW_EXCEPTION(my_exception());
445 []( my_exception
const &, info
<1> )
449 []( my_exception
const & )
461 int r
= leaf::try_handle_all(
462 []() -> leaf::result
<int>
464 return leaf::try_handle_some(
465 []() -> leaf::result
<int>
467 return leaf::new_error( info
<1>{1} );
469 []( info
<1> const & x
) -> int
471 BOOST_TEST_EQ(x
.value
, 1);
472 BOOST_LEAF_THROW_EXCEPTION();
479 []( my_exception
const &, info
<1> )
483 []( my_exception
const & )
495 ///////////////////////////
498 auto error_handlers
= std::make_tuple(
499 []( info
<1> const & x
) -> leaf::result
<int>
501 BOOST_TEST_EQ(x
.value
, 1);
502 BOOST_LEAF_THROW_EXCEPTION(my_exception());
504 []() -> leaf::result
<int>
508 int r
= leaf::try_handle_all(
509 [&]() -> leaf::result
<int>
511 return leaf::try_handle_some(
512 [&]() -> leaf::result
<int>
514 return leaf::new_error( info
<1>{1} );
518 []( my_exception
const &, info
<1> )
522 []( my_exception
const & )
534 auto error_handlers
= std::make_tuple(
535 []( info
<1> const & x
) -> leaf::result
<int>
537 BOOST_TEST_EQ(x
.value
, 1);
538 BOOST_LEAF_THROW_EXCEPTION();
540 []() -> leaf::result
<int>
544 int r
= leaf::try_handle_all(
545 [&]() -> leaf::result
<int>
547 return leaf::try_handle_some(
548 [&]() -> leaf::result
<int>
550 return leaf::new_error( info
<1>{1} );
554 []( my_exception
const &, info
<1> )
558 []( my_exception
const & )
570 //////////////////////////////////////
572 // match_value<> with exceptions, try_handle_some
574 leaf::result
<int> r
= leaf::try_handle_some(
575 []() -> leaf::result
<int>
577 throw leaf::exception( my_exception(42) );
579 []( leaf::match_value
<my_exception
, 42> m
)
581 return m
.matched
.value
;
584 BOOST_TEST_EQ(r
.value(), 42);
587 leaf::result
<int> r
= leaf::try_handle_some(
588 []() -> leaf::result
<int>
590 throw my_exception(42);
592 []( leaf::match_value
<my_exception
, 42> m
)
594 return m
.matched
.value
;
597 BOOST_TEST_EQ(r
.value(), 42);
600 leaf::result
<int> r
= leaf::try_handle_some(
601 []() -> leaf::result
<int>
603 throw leaf::exception( my_exception(42) );
605 []( leaf::match_value
<my_exception
, 41> m
)
607 return m
.matched
.value
;
609 []( leaf::error_info
const & unmatched
)
611 return unmatched
.error();
616 leaf::result
<int> r
= leaf::try_handle_some(
617 []() -> leaf::result
<int>
619 throw my_exception(42);
621 []( leaf::match_value
<my_exception
, 41> m
)
623 return m
.matched
.value
;
625 []( leaf::error_info
const & unmatched
)
627 return unmatched
.error();
632 //////////////////////////////////////
634 // match_value<> with exceptions, try_handle_all
636 int r
= leaf::try_handle_all(
637 []() -> leaf::result
<int>
639 throw leaf::exception( my_exception(42) );
641 []( leaf::match_value
<my_exception
, 42> m
)
643 return m
.matched
.value
;
649 BOOST_TEST_EQ(r
, 42);
652 int r
= leaf::try_handle_all(
653 []() -> leaf::result
<int>
655 throw my_exception(42);
657 []( leaf::match_value
<my_exception
, 42> m
)
659 return m
.matched
.value
;
665 BOOST_TEST_EQ(r
, 42);
668 int r
= leaf::try_handle_all(
669 []() -> leaf::result
<int>
671 throw leaf::exception( my_exception(42) );
673 []( leaf::match_value
<my_exception
, 41> m
)
675 return m
.matched
.value
;
681 BOOST_TEST_EQ(r
, -1);
684 int r
= leaf::try_handle_all(
685 []() -> leaf::result
<int>
687 throw my_exception(42);
689 []( leaf::match_value
<my_exception
, 41> m
)
691 return m
.matched
.value
;
697 BOOST_TEST_EQ(r
, -1);
700 return boost::report_errors();