]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/leaf/test/match_test.cpp
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / boost / libs / leaf / test / match_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 #ifdef BOOST_LEAF_TEST_SINGLE_HEADER
7 # include "leaf.hpp"
8 #else
9 # include <boost/leaf/handle_errors.hpp>
10 # include <boost/leaf/pred.hpp>
11 # include <boost/leaf/result.hpp>
12 #endif
13
14 #include "_test_ec.hpp"
15 #include "lightweight_test.hpp"
16 #include <exception>
17
18 namespace leaf = boost::leaf;
19
20 enum class my_error { e1=1, e2, e3 };
21
22 struct e_my_error { int value; };
23
24 #if __cplusplus >= 201703L
25 template <my_error value>
26 constexpr bool cmp_my_error( my_error const & e ) noexcept
27 {
28 return e == value;
29 };
30
31 template <int S>
32 constexpr bool e_my_error_gt( e_my_error const & e ) noexcept
33 {
34 return e.value > S;
35 }
36 #endif
37
38 struct my_exception: std::exception
39 {
40 int value;
41 bool operator==(int);
42 };
43
44 template <class M, class E>
45 bool test(E const & e )
46 {
47 if( M::evaluate(e) )
48 {
49 M m{e};
50 BOOST_TEST(e == m.matched);
51 return true;
52 }
53 else
54 return false;
55 }
56
57 int main()
58 {
59 {
60 int e = 42;
61
62 BOOST_TEST(( test<leaf::match<int, 42>>(e) ));
63 BOOST_TEST(( !test<leaf::match<int, 41>>(e) ));
64 BOOST_TEST(( test<leaf::match<int, 42, 41>>(e) ));
65
66 BOOST_TEST(( !test<leaf::if_not<leaf::match<int, 42>>>(e) ));
67 BOOST_TEST(( test<leaf::if_not<leaf::match<int, 41>>>(e) ));
68 BOOST_TEST(( !test<leaf::if_not<leaf::match<int, 42, 41>>>(e) ));
69 }
70
71 {
72 my_error e = my_error::e1;
73
74 BOOST_TEST(( test<leaf::match<my_error, my_error::e1>>(e) ));
75 BOOST_TEST(( !test<leaf::match<my_error, my_error::e2>>(e) ));
76 BOOST_TEST(( test<leaf::match<my_error, my_error::e2, my_error::e1>>(e) ));
77
78 BOOST_TEST(( !test<leaf::if_not<leaf::match<my_error, my_error::e1>>>(e) ));
79 BOOST_TEST(( test<leaf::if_not<leaf::match<my_error, my_error::e2>>>(e) ));
80 BOOST_TEST(( !test<leaf::if_not<leaf::match<my_error, my_error::e2, my_error::e1>>>(e) ));
81 }
82
83 #if BOOST_LEAF_CFG_STD_SYSTEM_ERROR
84 {
85 std::error_code e = errc_a::a0;
86
87 BOOST_TEST(( test<leaf::match<leaf::condition<cond_x>, cond_x::x00>>(e) ));
88 BOOST_TEST(( !test<leaf::match<leaf::condition<cond_x>, cond_x::x11>>(e) ));
89 BOOST_TEST(( test<leaf::match<leaf::condition<cond_x>, cond_x::x11, cond_x::x00>>(e) ));
90
91
92 BOOST_TEST(( !test<leaf::if_not<leaf::match<leaf::condition<cond_x>, cond_x::x00>>>(e) ));
93 BOOST_TEST(( test<leaf::if_not<leaf::match<leaf::condition<cond_x>, cond_x::x11>>>(e) ));
94 BOOST_TEST(( !test<leaf::if_not<leaf::match<leaf::condition<cond_x>, cond_x::x11, cond_x::x00>>>(e) ));
95
96 #if __cplusplus >= 201703L
97 BOOST_TEST(( test<leaf::match<std::error_code, errc_a::a0>>(e) ));
98 BOOST_TEST(( !test<leaf::match<std::error_code, errc_a::a2>>(e) ));
99 BOOST_TEST(( test<leaf::match<std::error_code, errc_a::a2, errc_a::a0>>(e) ));
100
101 BOOST_TEST(( !test<leaf::if_not<leaf::match<std::error_code, errc_a::a0>>>(e) ));
102 BOOST_TEST(( test<leaf::if_not<leaf::match<std::error_code, errc_a::a2>>>(e) ));
103 BOOST_TEST(( !test<leaf::if_not<leaf::match<std::error_code, errc_a::a2, errc_a::a0>>>(e) ));
104 #endif
105 }
106 #endif
107
108 #if __cplusplus >= 201703L
109 {
110 my_error e = my_error::e1;
111
112 BOOST_TEST(( test<leaf::match<my_error, cmp_my_error<my_error::e1>>>(e) ));
113 BOOST_TEST(( !test<leaf::match<my_error, cmp_my_error<my_error::e2>>>(e) ));
114 }
115 #endif
116
117 {
118 int r = leaf::try_handle_all(
119 []() -> leaf::result<int>
120 {
121 return leaf::new_error(my_error::e1);
122 },
123
124 []( leaf::match<my_error, my_error::e1> )
125 {
126 return 1;
127 },
128
129 []
130 {
131 return 2;
132 } );
133 BOOST_TEST_EQ(r, 1);
134 }
135
136 {
137 int r = leaf::try_handle_all(
138 []() -> leaf::result<int>
139 {
140 return leaf::new_error(my_error::e1);
141 },
142
143 []( leaf::match<my_error, my_error::e2> )
144 {
145 return 1;
146 },
147
148 []
149 {
150 return 2;
151 } );
152 BOOST_TEST_EQ(r, 2);
153 }
154
155 {
156 int r = leaf::try_handle_all(
157 []() -> leaf::result<int>
158 {
159 return leaf::new_error(my_error::e1);
160 },
161
162 []( leaf::if_not<leaf::match<my_error, my_error::e1>> )
163 {
164 return 1;
165 },
166
167 []( my_error e )
168 {
169 return 2;
170 },
171
172 []
173 {
174 return 3;
175 } );
176 BOOST_TEST_EQ(r, 2);
177 }
178
179 {
180 int r = leaf::try_handle_all(
181 []() -> leaf::result<int>
182 {
183 return leaf::new_error();
184 },
185
186 []( leaf::if_not<leaf::match<my_error, my_error::e1>> )
187 {
188 return 1;
189 },
190
191 []( my_error e )
192 {
193 return 2;
194 },
195
196 []
197 {
198 return 3;
199 } );
200 BOOST_TEST_EQ(r, 3);
201 }
202
203 #if __cplusplus >= 201703L
204 {
205 int r = leaf::try_handle_all(
206 []() -> leaf::result<int>
207 {
208 return leaf::new_error(my_error::e1);
209 },
210
211 []( leaf::match<my_error, cmp_my_error<my_error::e1>> )
212 {
213 return 1;
214 },
215
216 []
217 {
218 return 2;
219 } );
220 BOOST_TEST_EQ(r, 1);
221 }
222
223 {
224 int r = leaf::try_handle_all(
225 []() -> leaf::result<int>
226 {
227 return leaf::new_error(my_error::e1);
228 },
229
230 []( leaf::match<my_error, cmp_my_error<my_error::e2>> )
231 {
232 return 1;
233 },
234
235 []
236 {
237 return 2;
238 } );
239 BOOST_TEST_EQ(r, 2);
240 }
241
242 {
243 int r = leaf::try_handle_all(
244 []() -> leaf::result<int>
245 {
246 return leaf::new_error(e_my_error{42});
247 },
248
249 []( leaf::match<e_my_error, e_my_error_gt<41>> m )
250 {
251 return 1;
252 },
253
254 []
255 {
256 return 2;
257 } );
258 BOOST_TEST_EQ(r, 1);
259 }
260
261 {
262 int r = leaf::try_handle_all(
263 []() -> leaf::result<int>
264 {
265 return leaf::new_error(e_my_error{42});
266 },
267
268 []( leaf::match<e_my_error, e_my_error_gt<42>> m )
269 {
270 return 1;
271 },
272
273 []
274 {
275 return 2;
276 } );
277 BOOST_TEST_EQ(r, 2);
278 }
279 #endif
280
281 return boost::report_errors();
282 }