]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/leaf/test/diagnostic_info_test.cpp
import quincy beta 17.1.0
[ceph.git] / ceph / src / boost / libs / leaf / test / diagnostic_info_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 #include <boost/leaf/handle_errors.hpp>
8 #include <boost/leaf/result.hpp>
9 #include <boost/leaf/common.hpp>
10 #include "lightweight_test.hpp"
11 #include <sstream>
12
13 namespace leaf = boost::leaf;
14
15 template <int A>
16 struct unexpected_test
17 {
18 int value;
19 };
20
21 struct my_exception:
22 virtual std::exception
23 {
24 char const * what() const noexcept
25 {
26 return "my_exception";
27 }
28 };
29
30 struct printable_payload
31 {
32 friend std::ostream & operator<<( std::ostream & os, printable_payload const & x )
33 {
34 return os << "printed printable_payload";
35 }
36 };
37
38 struct non_printable_payload
39 {
40 };
41
42 struct printable_info_printable_payload
43 {
44 printable_payload value;
45
46 friend std::ostream & operator<<( std::ostream & os, printable_info_printable_payload const & x )
47 {
48 return os << "*** printable_info_printable_payload " << x.value << " ***";
49 }
50 };
51
52 struct printable_info_non_printable_payload
53 {
54 non_printable_payload value;
55
56 friend std::ostream & operator<<( std::ostream & os, printable_info_non_printable_payload const & x )
57 {
58 return os << "*** printable_info_non_printable_payload ***";
59 }
60 };
61
62 struct non_printable_info_printable_payload
63 {
64 printable_payload value;
65 };
66
67 struct non_printable_info_non_printable_payload
68 {
69 non_printable_payload value;
70 };
71
72 int main()
73 {
74 leaf::try_handle_all(
75 []() -> leaf::result<void>
76 {
77 return BOOST_LEAF_NEW_ERROR(
78 printable_info_printable_payload(),
79 printable_info_non_printable_payload(),
80 non_printable_info_printable_payload(),
81 non_printable_info_non_printable_payload(),
82 unexpected_test<1>{1},
83 unexpected_test<2>{2},
84 leaf::e_errno{ENOENT} );
85 },
86 [](
87 leaf::e_source_location,
88 printable_info_printable_payload,
89 printable_info_non_printable_payload,
90 non_printable_info_printable_payload,
91 non_printable_info_non_printable_payload,
92 leaf::e_errno,
93 leaf::error_info const & unmatched )
94 {
95 std::ostringstream st;
96 st << unmatched;
97 std::string s = st.str();
98 BOOST_TEST_NE(s.find("leaf::error_info: Error ID = "), s.npos);
99 std::cout << s;
100 },
101 []()
102 {
103 BOOST_ERROR("Bad error dispatch");
104 } );
105
106 std::cout << __LINE__ << " ----\n";
107
108 leaf::try_handle_all(
109 []() -> leaf::result<void>
110 {
111 return BOOST_LEAF_NEW_ERROR(
112 printable_info_printable_payload(),
113 printable_info_non_printable_payload(),
114 non_printable_info_printable_payload(),
115 non_printable_info_non_printable_payload(),
116 unexpected_test<1>{1},
117 unexpected_test<2>{2},
118 leaf::e_errno{ENOENT} );
119 },
120 [](
121 leaf::e_source_location,
122 printable_info_printable_payload,
123 printable_info_non_printable_payload,
124 non_printable_info_printable_payload,
125 non_printable_info_non_printable_payload,
126 leaf::e_errno,
127 leaf::diagnostic_info const & unmatched )
128 {
129 std::ostringstream st;
130 st << unmatched;
131 std::string s = st.str();
132 #if BOOST_LEAF_DIAGNOSTICS
133 BOOST_TEST_NE(s.find("leaf::diagnostic_info for Error ID = "), s.npos);
134 BOOST_TEST_NE(s.find("e_source_location"), s.npos);
135 BOOST_TEST_NE(s.find("*** printable_info_printable_payload printed printable_payload ***"), s.npos);
136 BOOST_TEST_NE(s.find("*** printable_info_non_printable_payload ***"), s.npos);
137 BOOST_TEST_NE(s.find(": printed printable_payload"), s.npos);
138 BOOST_TEST_NE(s.find(": {Non-Printable}"), s.npos);
139 BOOST_TEST_NE(s.find("Detected 2 attempts"), s.npos);
140 BOOST_TEST_NE(s.find("unexpected_test<1>"), s.npos);
141 BOOST_TEST_EQ(s.find("unexpected_test<2>"), s.npos);
142 #else
143 BOOST_TEST_NE(s.find("leaf::diagnostic_info requires #define BOOST_LEAF_DIAGNOSTICS 1"), s.npos);
144 BOOST_TEST_NE(s.find("leaf::error_info: Error ID = "), s.npos);
145 #endif
146 std::cout << s;
147 },
148 []()
149 {
150 BOOST_ERROR("Bad error dispatch");
151 } );
152
153 std::cout << __LINE__ << " ----\n";
154
155 leaf::try_handle_all(
156 []() -> leaf::result<void>
157 {
158 return BOOST_LEAF_NEW_ERROR(
159 printable_info_printable_payload(),
160 printable_info_non_printable_payload(),
161 non_printable_info_printable_payload(),
162 non_printable_info_non_printable_payload(),
163 unexpected_test<1>{1},
164 unexpected_test<2>{2},
165 leaf::e_errno{ENOENT} );
166 },
167 [](
168 leaf::e_source_location,
169 printable_info_printable_payload,
170 printable_info_non_printable_payload,
171 non_printable_info_printable_payload,
172 non_printable_info_non_printable_payload,
173 leaf::e_errno,
174 leaf::verbose_diagnostic_info const & di )
175 {
176 std::ostringstream st;
177 st << di;
178 std::string s = st.str();
179 #if BOOST_LEAF_DIAGNOSTICS
180 BOOST_TEST_NE(s.find("leaf::verbose_diagnostic_info for Error ID = "), s.npos);
181 BOOST_TEST_NE(s.find("e_source_location"), s.npos);
182 BOOST_TEST_NE(s.find("*** printable_info_printable_payload printed printable_payload ***"), s.npos);
183 BOOST_TEST_NE(s.find("*** printable_info_non_printable_payload ***"), s.npos);
184 BOOST_TEST_NE(s.find(": printed printable_payload"), s.npos);
185 BOOST_TEST_NE(s.find(": {Non-Printable}"), s.npos);
186 BOOST_TEST_NE(s.find("Unhandled error objects:"), s.npos);
187 BOOST_TEST_NE(s.find("unexpected_test<1>"), s.npos);
188 BOOST_TEST_NE(s.find("unexpected_test<2>"), s.npos);
189 BOOST_TEST_NE(s.find(": 1"), s.npos);
190 BOOST_TEST_NE(s.find(": 2"), s.npos);
191 #else
192 BOOST_TEST_NE(s.find("leaf::verbose_diagnostic_info requires #define BOOST_LEAF_DIAGNOSTICS 1"), s.npos);
193 BOOST_TEST_NE(s.find("leaf::error_info: Error ID = "), s.npos);
194 #endif
195 std::cout << s;
196 },
197 []()
198 {
199 BOOST_ERROR("Bad error dispatch");
200 } );
201
202 std::cout << __LINE__ << " ----\n";
203
204 ///////////////////////////////////
205
206 #ifndef BOOST_LEAF_NO_EXCEPTIONS
207
208 leaf::try_catch(
209 []
210 {
211 BOOST_LEAF_THROW_EXCEPTION( my_exception(),
212 printable_info_printable_payload(),
213 printable_info_non_printable_payload(),
214 non_printable_info_printable_payload(),
215 non_printable_info_non_printable_payload(),
216 unexpected_test<1>{1},
217 unexpected_test<2>{2},
218 leaf::e_errno{ENOENT} );
219 },
220 [](
221 leaf::e_source_location,
222 printable_info_printable_payload,
223 printable_info_non_printable_payload,
224 non_printable_info_printable_payload,
225 non_printable_info_non_printable_payload,
226 leaf::e_errno,
227 leaf::error_info const & unmatched )
228 {
229 std::ostringstream st;
230 st << unmatched;
231 std::string s = st.str();
232 BOOST_TEST_NE(s.find("leaf::error_info: Error ID = "), s.npos);
233 BOOST_TEST_NE(s.find("Exception dynamic type: "), s.npos);
234 BOOST_TEST_NE(s.find("std::exception::what(): my_exception"), s.npos);
235 std::cout << s;
236 } );
237
238 std::cout << __LINE__ << " ----\n";
239
240 leaf::try_catch(
241 []
242 {
243 BOOST_LEAF_THROW_EXCEPTION( my_exception(),
244 printable_info_printable_payload(),
245 printable_info_non_printable_payload(),
246 non_printable_info_printable_payload(),
247 non_printable_info_non_printable_payload(),
248 unexpected_test<1>{1},
249 unexpected_test<2>{2},
250 leaf::e_errno{ENOENT} );
251 },
252 [](
253 leaf::e_source_location,
254 printable_info_printable_payload,
255 printable_info_non_printable_payload,
256 non_printable_info_printable_payload,
257 non_printable_info_non_printable_payload,
258 leaf::e_errno,
259 leaf::diagnostic_info const & unmatched )
260 {
261 std::ostringstream st;
262 st << unmatched;
263 std::string s = st.str();
264 #if BOOST_LEAF_DIAGNOSTICS
265 BOOST_TEST_NE(s.find("leaf::diagnostic_info for Error ID = "), s.npos);
266 BOOST_TEST_NE(s.find("Exception dynamic type: "), s.npos);
267 BOOST_TEST_NE(s.find("std::exception::what(): my_exception"), s.npos);
268 BOOST_TEST_NE(s.find("e_source_location"), s.npos);
269 BOOST_TEST_NE(s.find("*** printable_info_printable_payload printed printable_payload ***"), s.npos);
270 BOOST_TEST_NE(s.find("*** printable_info_non_printable_payload ***"), s.npos);
271 BOOST_TEST_NE(s.find(": printed printable_payload"), s.npos);
272 BOOST_TEST_NE(s.find(": {Non-Printable}"), s.npos);
273 BOOST_TEST_NE(s.find("Detected 2 attempts"), s.npos);
274 BOOST_TEST_NE(s.find("unexpected_test<1>"), s.npos);
275 BOOST_TEST_EQ(s.find("unexpected_test<2>"), s.npos);
276 #else
277 BOOST_TEST_NE(s.find("leaf::diagnostic_info requires #define BOOST_LEAF_DIAGNOSTICS 1"), s.npos);
278 BOOST_TEST_NE(s.find("leaf::error_info: Error ID = "), s.npos);
279 BOOST_TEST_NE(s.find("Exception dynamic type: "), s.npos);
280 BOOST_TEST_NE(s.find("std::exception::what(): my_exception"), s.npos);
281 #endif
282 std::cout << s;
283 } );
284
285 std::cout << __LINE__ << " ----\n";
286
287 leaf::try_catch(
288 []
289 {
290 BOOST_LEAF_THROW_EXCEPTION( my_exception(),
291 printable_info_printable_payload(),
292 printable_info_non_printable_payload(),
293 non_printable_info_printable_payload(),
294 non_printable_info_non_printable_payload(),
295 unexpected_test<1>{1},
296 unexpected_test<2>{2},
297 leaf::e_errno{ENOENT} );
298 },
299 [](
300 leaf::e_source_location,
301 printable_info_printable_payload,
302 printable_info_non_printable_payload,
303 non_printable_info_printable_payload,
304 non_printable_info_non_printable_payload,
305 leaf::e_errno,
306 leaf::verbose_diagnostic_info const & di )
307 {
308 std::ostringstream st;
309 st << di;
310 std::string s = st.str();
311 #if BOOST_LEAF_DIAGNOSTICS
312 BOOST_TEST_NE(s.find("leaf::verbose_diagnostic_info for Error ID = "), s.npos);
313 BOOST_TEST_NE(s.find("Exception dynamic type: "), s.npos);
314 BOOST_TEST_NE(s.find("std::exception::what(): my_exception"), s.npos);
315 BOOST_TEST_NE(s.find("e_source_location"), s.npos);
316 BOOST_TEST_NE(s.find("*** printable_info_printable_payload printed printable_payload ***"), s.npos);
317 BOOST_TEST_NE(s.find("*** printable_info_non_printable_payload ***"), s.npos);
318 BOOST_TEST_NE(s.find(": printed printable_payload"), s.npos);
319 BOOST_TEST_NE(s.find(": {Non-Printable}"), s.npos);
320 BOOST_TEST_NE(s.find("Unhandled error objects:"), s.npos);
321 BOOST_TEST_NE(s.find("unexpected_test<1>"), s.npos);
322 BOOST_TEST_NE(s.find("unexpected_test<2>"), s.npos);
323 BOOST_TEST_NE(s.find(": 1"), s.npos);
324 BOOST_TEST_NE(s.find(": 2"), s.npos);
325 #else
326 BOOST_TEST_NE(s.find("leaf::verbose_diagnostic_info requires #define BOOST_LEAF_DIAGNOSTICS 1"), s.npos);
327 BOOST_TEST_NE(s.find("leaf::error_info: Error ID = "), s.npos);
328 BOOST_TEST_NE(s.find("Exception dynamic type: "), s.npos);
329 BOOST_TEST_NE(s.find("std::exception::what(): my_exception"), s.npos);
330 #endif
331 std::cout << s;
332 } );
333
334 #endif
335
336 return boost::report_errors();
337 }