]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/leaf/test/exception_test.cpp
1 // Copyright 2018-2022 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/config.hpp>
8 #ifdef BOOST_LEAF_NO_EXCEPTIONS
14 std::cout
<< "Unit test not applicable." << std::endl
;
20 #ifdef BOOST_LEAF_TEST_SINGLE_HEADER
23 # include <boost/leaf/handle_errors.hpp>
24 # include <boost/leaf/pred.hpp>
25 # include <boost/leaf/exception.hpp>
26 # include <boost/leaf/on_error.hpp>
29 #include "lightweight_test.hpp"
31 namespace leaf
= boost::leaf
;
33 struct info
{ int value
; };
35 struct abstract_base_exception
37 virtual ~abstract_base_exception() { }
38 virtual int get_val() const = 0;
43 abstract_base_exception
46 explicit my_exception(int val
): val
{val
} { }
47 int get_val() const { return val
; }
50 int get_val( abstract_base_exception
const & ex
)
55 int get_val( my_exception
const & ex
)
60 int get_val( leaf::catch_
<abstract_base_exception
> const & ex
)
62 return ex
.matched
.get_val();
65 int get_val( leaf::catch_
<my_exception
> const & ex
)
67 return ex
.matched
.val
;
70 template <class Ex
, class F
>
73 return leaf::try_catch(
80 []( Ex ex
, leaf::match_value
<info
,42>, leaf::e_source_location
)
82 BOOST_TEST_EQ(get_val(ex
), 42);
85 []( Ex ex
, leaf::match_value
<info
,42>, info x
)
87 BOOST_TEST_EQ(get_val(ex
), 42);
90 []( Ex ex
, leaf::e_source_location
)
92 BOOST_TEST_EQ(get_val(ex
), 42);
97 BOOST_TEST_EQ(get_val(ex
), 42);
100 []( leaf::match_value
<info
,42>, leaf::e_source_location
)
104 []( leaf::match_value
<info
,42>, info x
)
108 []( leaf::e_source_location
)
120 BOOST_TEST_EQ(20, test
<leaf::catch_
<my_exception
>>([]{ BOOST_LEAF_THROW_EXCEPTION(my_exception(42), info
{42}); }));
121 BOOST_TEST_EQ(20, test
<leaf::catch_
<my_exception
>>([]{ throw BOOST_LEAF_EXCEPTION(my_exception(42), info
{42}); }));
122 BOOST_TEST_EQ(21, test
<leaf::catch_
<my_exception
>>([]{ throw leaf::exception(my_exception(42), info
{42}); }));
123 BOOST_TEST_EQ(21, test
<leaf::catch_
<my_exception
>>([]{ my_exception
exc(42); throw leaf::exception(exc
, info
{42}); }));
124 BOOST_TEST_EQ(21, test
<leaf::catch_
<my_exception
>>([]{ my_exception
const exc(42); throw leaf::exception(exc
, info
{42}); }));
125 BOOST_TEST_EQ(22, test
<leaf::catch_
<my_exception
>>([]{ BOOST_LEAF_THROW_EXCEPTION(my_exception(42)); }));
126 BOOST_TEST_EQ(22, test
<leaf::catch_
<my_exception
>>([]{ throw BOOST_LEAF_EXCEPTION(my_exception(42)); }));
127 BOOST_TEST_EQ(23, test
<leaf::catch_
<my_exception
>>([]{ throw leaf::exception(my_exception(42)); }));
128 BOOST_TEST_EQ(23, test
<leaf::catch_
<my_exception
>>([]{ my_exception
exc(42); throw leaf::exception(exc
); }));
129 BOOST_TEST_EQ(23, test
<leaf::catch_
<my_exception
>>([]{ my_exception
const exc(42); throw leaf::exception(exc
); }));
131 BOOST_TEST_EQ(20, test
<my_exception
const &>([]{ BOOST_LEAF_THROW_EXCEPTION(my_exception(42), info
{42}); }));
132 BOOST_TEST_EQ(20, test
<my_exception
const &>([]{ throw BOOST_LEAF_EXCEPTION(my_exception(42), info
{42}); }));
133 BOOST_TEST_EQ(21, test
<my_exception
const &>([]{ throw leaf::exception(my_exception(42), info
{42}); }));
134 BOOST_TEST_EQ(21, test
<my_exception
const &>([]{ my_exception
exc(42); throw leaf::exception(exc
, info
{42}); }));
135 BOOST_TEST_EQ(21, test
<my_exception
const &>([]{ my_exception
const exc(42); throw leaf::exception(exc
, info
{42}); }));
136 BOOST_TEST_EQ(22, test
<my_exception
const &>([]{ BOOST_LEAF_THROW_EXCEPTION(my_exception(42)); }));
137 BOOST_TEST_EQ(22, test
<my_exception
const &>([]{ throw BOOST_LEAF_EXCEPTION(my_exception(42)); }));
138 BOOST_TEST_EQ(23, test
<my_exception
const &>([]{ throw leaf::exception(my_exception(42)); }));
139 BOOST_TEST_EQ(23, test
<my_exception
const &>([]{ my_exception
exc(42); throw leaf::exception(exc
); }));
140 BOOST_TEST_EQ(23, test
<my_exception
const &>([]{ my_exception
const exc(42); throw leaf::exception(exc
); }));
142 BOOST_TEST_EQ(40, test
<my_exception
&>([]{ BOOST_LEAF_THROW_EXCEPTION(info
{42}); }));
143 BOOST_TEST_EQ(40, test
<my_exception
&>([]{ throw BOOST_LEAF_EXCEPTION(info
{42}); }));
144 BOOST_TEST_EQ(41, test
<my_exception
&>([]{ throw leaf::exception(info
{42}); }));
145 BOOST_TEST_EQ(41, test
<my_exception
&>([]{ info inf
{42}; throw leaf::exception(inf
); }));
146 BOOST_TEST_EQ(41, test
<my_exception
&>([]{ info
const inf
{42}; throw leaf::exception(inf
); }));
147 BOOST_TEST_EQ(42, test
<my_exception
&>([]{ BOOST_LEAF_THROW_EXCEPTION(); }));
148 BOOST_TEST_EQ(42, test
<my_exception
&>([]{ throw BOOST_LEAF_EXCEPTION(); }));
149 BOOST_TEST_EQ(43, test
<my_exception
&>([]{ throw leaf::exception(); }));
150 BOOST_TEST_EQ(23, test
<my_exception
&>([]{ my_exception
exc(42); throw leaf::exception(exc
); }));
151 BOOST_TEST_EQ(23, test
<my_exception
&>([]{ my_exception
const exc(42); throw leaf::exception(exc
); }));
153 BOOST_TEST_EQ(20, test
<my_exception
const>([]{ BOOST_LEAF_THROW_EXCEPTION(my_exception(42), info
{42}); }));
154 BOOST_TEST_EQ(20, test
<my_exception
const>([]{ throw BOOST_LEAF_EXCEPTION(my_exception(42), info
{42}); }));
155 BOOST_TEST_EQ(21, test
<my_exception
const>([]{ throw leaf::exception(my_exception(42), info
{42}); }));
156 BOOST_TEST_EQ(21, test
<my_exception
const>([]{ my_exception
exc(42); throw leaf::exception(exc
, info
{42}); }));
157 BOOST_TEST_EQ(21, test
<my_exception
const>([]{ my_exception
const exc(42); throw leaf::exception(exc
, info
{42}); }));
158 BOOST_TEST_EQ(22, test
<my_exception
const>([]{ BOOST_LEAF_THROW_EXCEPTION(my_exception(42)); }));
159 BOOST_TEST_EQ(22, test
<my_exception
const>([]{ throw BOOST_LEAF_EXCEPTION(my_exception(42)); }));
160 BOOST_TEST_EQ(23, test
<my_exception
const>([]{ throw leaf::exception(my_exception(42)); }));
161 BOOST_TEST_EQ(23, test
<my_exception
const>([]{ my_exception
exc(42); throw leaf::exception(exc
); }));
162 BOOST_TEST_EQ(23, test
<my_exception
const>([]{ my_exception
const exc(42); throw leaf::exception(exc
); }));
164 BOOST_TEST_EQ(40, test
<my_exception
>([]{ BOOST_LEAF_THROW_EXCEPTION(info
{42}); }));
165 BOOST_TEST_EQ(40, test
<my_exception
>([]{ throw BOOST_LEAF_EXCEPTION(info
{42}); }));
166 BOOST_TEST_EQ(41, test
<my_exception
>([]{ throw leaf::exception(info
{42}); }));
167 BOOST_TEST_EQ(41, test
<my_exception
>([]{ info inf
{42}; throw leaf::exception(inf
); }));
168 BOOST_TEST_EQ(41, test
<my_exception
>([]{ info
const inf
{42}; throw leaf::exception(inf
); }));
169 BOOST_TEST_EQ(42, test
<my_exception
>([]{ BOOST_LEAF_THROW_EXCEPTION(); }));
170 BOOST_TEST_EQ(42, test
<my_exception
>([]{ throw BOOST_LEAF_EXCEPTION(); }));
171 BOOST_TEST_EQ(43, test
<my_exception
>([]{ throw leaf::exception(); }));
172 BOOST_TEST_EQ(23, test
<my_exception
>([]{ my_exception
exc(42); throw leaf::exception(exc
); }));
173 BOOST_TEST_EQ(23, test
<my_exception
>([]{ my_exception
const exc(42); throw leaf::exception(exc
); }));
175 BOOST_TEST_EQ(20, test
<leaf::catch_
<abstract_base_exception
>>([]{ BOOST_LEAF_THROW_EXCEPTION(my_exception(42), info
{42}); }));
176 BOOST_TEST_EQ(20, test
<leaf::catch_
<abstract_base_exception
>>([]{ throw BOOST_LEAF_EXCEPTION(my_exception(42), info
{42}); }));
177 BOOST_TEST_EQ(21, test
<leaf::catch_
<abstract_base_exception
>>([]{ throw leaf::exception(my_exception(42), info
{42}); }));
178 BOOST_TEST_EQ(21, test
<leaf::catch_
<abstract_base_exception
>>([]{ my_exception
exc(42); throw leaf::exception(exc
, info
{42}); }));
179 BOOST_TEST_EQ(21, test
<leaf::catch_
<abstract_base_exception
>>([]{ my_exception
const exc(42); throw leaf::exception(exc
, info
{42}); }));
180 BOOST_TEST_EQ(22, test
<leaf::catch_
<abstract_base_exception
>>([]{ BOOST_LEAF_THROW_EXCEPTION(my_exception(42)); }));
181 BOOST_TEST_EQ(22, test
<leaf::catch_
<abstract_base_exception
>>([]{ throw BOOST_LEAF_EXCEPTION(my_exception(42)); }));
182 BOOST_TEST_EQ(23, test
<leaf::catch_
<abstract_base_exception
>>([]{ throw leaf::exception(my_exception(42)); }));
183 BOOST_TEST_EQ(23, test
<leaf::catch_
<abstract_base_exception
>>([]{ my_exception
exc(42); throw leaf::exception(exc
); }));
184 BOOST_TEST_EQ(23, test
<leaf::catch_
<abstract_base_exception
>>([]{ my_exception
const exc(42); throw leaf::exception(exc
); }));
186 BOOST_TEST_EQ(20, test
<abstract_base_exception
const &>([]{ BOOST_LEAF_THROW_EXCEPTION(my_exception(42), info
{42}); }));
187 BOOST_TEST_EQ(20, test
<abstract_base_exception
const &>([]{ throw BOOST_LEAF_EXCEPTION(my_exception(42), info
{42}); }));
188 BOOST_TEST_EQ(21, test
<abstract_base_exception
const &>([]{ throw leaf::exception(my_exception(42), info
{42}); }));
189 BOOST_TEST_EQ(21, test
<abstract_base_exception
const &>([]{ my_exception
exc(42); throw leaf::exception(exc
, info
{42}); }));
190 BOOST_TEST_EQ(21, test
<abstract_base_exception
const &>([]{ my_exception
const exc(42); throw leaf::exception(exc
, info
{42}); }));
191 BOOST_TEST_EQ(22, test
<abstract_base_exception
const &>([]{ BOOST_LEAF_THROW_EXCEPTION(my_exception(42)); }));
192 BOOST_TEST_EQ(22, test
<abstract_base_exception
const &>([]{ throw BOOST_LEAF_EXCEPTION(my_exception(42)); }));
193 BOOST_TEST_EQ(23, test
<abstract_base_exception
const &>([]{ throw leaf::exception(my_exception(42)); }));
194 BOOST_TEST_EQ(23, test
<abstract_base_exception
const &>([]{ my_exception
exc(42); throw leaf::exception(exc
); }));
195 BOOST_TEST_EQ(23, test
<abstract_base_exception
const &>([]{ my_exception
const exc(42); throw leaf::exception(exc
); }));
197 BOOST_TEST_EQ(40, test
<abstract_base_exception
&>([]{ BOOST_LEAF_THROW_EXCEPTION(info
{42}); }));
198 BOOST_TEST_EQ(40, test
<abstract_base_exception
&>([]{ throw BOOST_LEAF_EXCEPTION(info
{42}); }));
199 BOOST_TEST_EQ(41, test
<abstract_base_exception
&>([]{ throw leaf::exception(info
{42}); }));
200 BOOST_TEST_EQ(41, test
<abstract_base_exception
&>([]{ info inf
{42}; throw leaf::exception(inf
); }));
201 BOOST_TEST_EQ(41, test
<abstract_base_exception
&>([]{ info
const inf
{42}; throw leaf::exception(inf
); }));
202 BOOST_TEST_EQ(42, test
<abstract_base_exception
&>([]{ BOOST_LEAF_THROW_EXCEPTION(); }));
203 BOOST_TEST_EQ(42, test
<abstract_base_exception
&>([]{ throw BOOST_LEAF_EXCEPTION(); }));
204 BOOST_TEST_EQ(43, test
<abstract_base_exception
&>([]{ throw leaf::exception(); }));
205 BOOST_TEST_EQ(23, test
<abstract_base_exception
&>([]{ my_exception
exc(42); throw leaf::exception(exc
); }));
206 BOOST_TEST_EQ(23, test
<abstract_base_exception
&>([]{ my_exception
const exc(42); throw leaf::exception(exc
); }));
213 throw std::runtime_error("Test");
215 [&]( std::exception
const & ex
)
219 BOOST_TEST(wh
!=0 || !strcmp(wh
,"Test"));
223 int const id
= leaf::leaf_detail::current_id();
224 BOOST_TEST_EQ( 21, test
<my_exception
const &>( []
226 auto load
= leaf::on_error(info
{42});
227 throw my_exception(42);
229 BOOST_TEST_NE(id
, leaf::leaf_detail::current_id());
233 int const id
= leaf::leaf_detail::current_id();
234 BOOST_TEST_EQ( 21, test
<my_exception
&>( []
236 auto load
= leaf::on_error(info
{42});
237 throw my_exception(42);
239 BOOST_TEST_NE(id
, leaf::leaf_detail::current_id());
243 BOOST_TEST_EQ( 23, test
<my_exception
const &>( []
245 int const id
= leaf::leaf_detail::current_id();
251 throw my_exception(42);
256 BOOST_TEST_EQ(id
, leaf::leaf_detail::current_id());
263 BOOST_TEST_EQ( 23, test
<my_exception
&>( []
265 int const id
= leaf::leaf_detail::current_id();
271 throw my_exception(42);
276 BOOST_TEST_EQ(id
, leaf::leaf_detail::current_id());
286 throw leaf::exception( info
{42} );
290 BOOST_TEST_EQ(x
.value
, 42);
292 int r
= leaf::try_catch(
295 throw std::exception();
308 return boost::report_errors();