]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/leaf/test/preload_exception_test.cpp
import quincy beta 17.1.0
[ceph.git] / ceph / src / boost / libs / leaf / test / preload_exception_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 #if defined(BOOST_LEAF_NO_EXCEPTIONS) || defined(BOOST_LEAF_NO_THREADS)
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/on_error.hpp>
20 #include <boost/leaf/handle_errors.hpp>
21 #include <boost/leaf/result.hpp>
22 #include "lightweight_test.hpp"
23
24 namespace leaf = boost::leaf;
25
26 template <int>
27 struct info
28 {
29 int value;
30 };
31
32 template <class Thrower>
33 void g1( Thrower th )
34 {
35 auto load = leaf::on_error( info<1>{} );
36 th();
37 }
38
39 template <class Thrower>
40 void g2( Thrower th )
41 {
42 auto load = leaf::on_error(info<3>{}, info<2>{} );
43 th();
44 }
45
46 template <class Thrower>
47 void f1( Thrower th )
48 {
49 return g1(th);
50 }
51
52 template <class Thrower>
53 void f2( Thrower th )
54 {
55 return g2(th);
56 }
57
58 int main()
59 {
60 BOOST_TEST_EQ(1,
61 leaf::try_catch(
62 []
63 {
64 f1( [] { throw leaf::exception(); } );
65 return 0;
66 },
67 []( leaf::error_info const & err, info<1> )
68 {
69 BOOST_TEST_EQ(err.error().value(), 1);
70 return 1;
71 },
72 []( info<2> )
73 {
74 return 2;
75 },
76 []( info<1>, info<2> )
77 {
78 return 3;
79 } ));
80
81 BOOST_TEST_EQ(2,
82 leaf::try_catch(
83 []
84 {
85 f2( [] { throw leaf::exception(); } );
86 return 0;
87 },
88 []( info<1> )
89 {
90 return 1;
91 },
92 []( leaf::error_info const & err, info<2>, info<3> )
93 {
94 BOOST_TEST_EQ(err.error().value(), 9);
95 return 2;
96 },
97 []( info<1>, info<2> )
98 {
99 return 3;
100 } ));
101
102 BOOST_TEST_EQ(1,
103 leaf::try_catch(
104 []
105 {
106 f1( [] { throw std::exception(); } );
107 return 0;
108 },
109 []( leaf::error_info const & err, info<1> )
110 {
111 BOOST_TEST_EQ(err.error().value(), 17);
112 return 1;
113 },
114 []( info<2> )
115 {
116 return 2;
117 },
118 []( info<1>, info<2> )
119 {
120 return 3;
121 } ) );
122
123 BOOST_TEST_EQ(2,
124 leaf::try_catch(
125 []
126 {
127 f2( [] { throw std::exception(); } );
128 return 0;
129 },
130 []( info<1> )
131 {
132 return 1;
133 },
134 []( leaf::error_info const & err, info<2>, info<3> )
135 {
136 BOOST_TEST_EQ(err.error().value(), 21);
137 return 2;
138 },
139 []( info<1>, info<2> )
140 {
141 return 3;
142 } ));
143
144 return boost::report_errors();
145 }
146
147 #endif