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