]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/leaf/test/exception_to_result_test.cpp
import quincy beta 17.1.0
[ceph.git] / ceph / src / boost / libs / leaf / test / exception_to_result_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 #ifdef BOOST_LEAF_NO_EXCEPTIONS
8
9 #include <iostream>
10
11 int main()
12 {
13 std::cout << "Unit test not applicable." << std::endl;
14 return 0;
15 }
16
17 #else
18
19 #include <boost/leaf/capture.hpp>
20 #include <boost/leaf/result.hpp>
21 #include <boost/leaf/handle_errors.hpp>
22 #include "lightweight_test.hpp"
23
24 namespace leaf = boost::leaf;
25
26 template <int> struct my_exception: std::exception { };
27
28 int main()
29 {
30 {
31 int r = leaf::try_handle_all(
32 []
33 {
34 return leaf::exception_to_result<my_exception<1>,my_exception<2>>(
35 []() -> int
36 {
37 throw my_exception<1>();
38 } );
39 },
40 []( my_exception<1> const &, std::exception_ptr const & ep )
41 {
42 try
43 {
44 std::rethrow_exception(ep);
45 }
46 catch( my_exception<1> const & )
47 {
48 }
49 return 1;
50 },
51 []( my_exception<2> const & )
52 {
53 return 2;
54 },
55 []
56 {
57 return 3;
58 } );
59 BOOST_TEST_EQ(r, 1);
60 }
61 {
62 int r = leaf::try_handle_all(
63 []
64 {
65 return leaf::exception_to_result<my_exception<1>,my_exception<2>>(
66 []() -> int
67 {
68 throw my_exception<2>();
69 } );
70 },
71 []( my_exception<1> const & )
72 {
73 return 1;
74 },
75 []( my_exception<2> const &, std::exception_ptr const & ep )
76 {
77 try
78 {
79 std::rethrow_exception(ep);
80 }
81 catch( my_exception<2> const & )
82 {
83 }
84 return 2;
85 },
86 []
87 {
88 return 3;
89 } );
90 BOOST_TEST_EQ(r, 2);
91 }
92 {
93 int r = leaf::try_handle_all(
94 []
95 {
96 return leaf::exception_to_result<std::exception,my_exception<1>>(
97 []() -> int
98 {
99 throw my_exception<1>();
100 } );
101 },
102 []( std::exception const &, std::exception_ptr const & ep )
103 {
104 try
105 {
106 std::rethrow_exception(ep);
107 }
108 catch( my_exception<1> const & )
109 {
110 }
111 return 1;
112 },
113 []( my_exception<1> const & )
114 {
115 return 2;
116 },
117 []
118 {
119 return 3;
120 } );
121 BOOST_TEST_EQ(r, 1);
122 }
123 return boost::report_errors();
124 }
125
126 #endif