]>
Commit | Line | Data |
---|---|---|
20effc67 TL |
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 | #ifdef BOOST_LEAF_NO_EXCEPTIONS | |
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/handle_errors.hpp> | |
20 | #include <boost/leaf/pred.hpp> | |
21 | #include <boost/leaf/result.hpp> | |
22 | #include "lightweight_test.hpp" | |
23 | ||
24 | namespace leaf = boost::leaf; | |
25 | ||
26 | template <int> struct info { int value; }; | |
27 | ||
28 | struct my_exception: std::exception | |
29 | { | |
30 | int value; | |
31 | ||
32 | my_exception(): | |
33 | value(0) | |
34 | { | |
35 | } | |
36 | ||
37 | my_exception(int v): | |
38 | value(v) | |
39 | { | |
40 | } | |
41 | }; | |
42 | ||
43 | int main() | |
44 | { | |
45 | { | |
46 | leaf::result<int> r = leaf::try_handle_some( | |
47 | []() -> leaf::result<int> | |
48 | { | |
49 | return 42; | |
50 | }, | |
51 | [] | |
52 | { | |
53 | return 1; | |
54 | } ); | |
55 | BOOST_TEST(r); | |
56 | BOOST_TEST_EQ(r.value(), 42); | |
57 | } | |
58 | { | |
59 | leaf::result<int> r = leaf::try_handle_some( | |
60 | []() -> leaf::result<int> | |
61 | { | |
62 | throw leaf::exception( my_exception(), info<1>{1} ); | |
63 | }, | |
64 | []( my_exception const &, info<1> const & x ) | |
65 | { | |
66 | BOOST_TEST_EQ(x.value, 1); | |
67 | return 1; | |
68 | } ); | |
69 | BOOST_TEST(r); | |
70 | BOOST_TEST_EQ(r.value(), 1); | |
71 | } | |
72 | { | |
73 | leaf::result<int> r = leaf::try_handle_some( | |
74 | []() -> leaf::result<int> | |
75 | { | |
76 | throw leaf::exception( info<1>{1} ); | |
77 | }, | |
78 | []( info<1> const & x ) | |
79 | { | |
80 | BOOST_TEST_EQ(x.value, 1); | |
81 | return 1; | |
82 | } ); | |
83 | BOOST_TEST(r); | |
84 | BOOST_TEST_EQ(r.value(), 1); | |
85 | } | |
86 | { | |
87 | leaf::result<int> r = leaf::try_handle_some( | |
88 | []() -> leaf::result<int> | |
89 | { | |
90 | return leaf::new_error( info<1>{1} ); | |
91 | }, | |
92 | []( info<1> const & x ) | |
93 | { | |
94 | BOOST_TEST_EQ(x.value, 1); | |
95 | return 1; | |
96 | } ); | |
97 | BOOST_TEST(r); | |
98 | BOOST_TEST_EQ(r.value(), 1); | |
99 | } | |
100 | ||
101 | /////////////////////////// | |
102 | ||
103 | { | |
104 | auto error_handlers = std::make_tuple( | |
105 | []( my_exception const &, info<1> const & x ) -> leaf::result<int> | |
106 | { | |
107 | BOOST_TEST_EQ(x.value, 1); | |
108 | return 1; | |
109 | }, | |
110 | []( info<1> const & x ) -> leaf::result<int> | |
111 | { | |
112 | BOOST_TEST_EQ(x.value, 1); | |
113 | return 2; | |
114 | } ); | |
115 | { | |
116 | leaf::result<int> r = leaf::try_handle_some( | |
117 | []() -> leaf::result<int> | |
118 | { | |
119 | return 42; | |
120 | }, | |
121 | error_handlers ); | |
122 | BOOST_TEST(r); | |
123 | BOOST_TEST_EQ(r.value(), 42); | |
124 | } | |
125 | { | |
126 | leaf::result<int> r = leaf::try_handle_some( | |
127 | []() -> leaf::result<int> | |
128 | { | |
129 | throw leaf::exception( my_exception(), info<1>{1} ); | |
130 | }, | |
131 | error_handlers ); | |
132 | BOOST_TEST(r); | |
133 | BOOST_TEST_EQ(r.value(), 1); | |
134 | } | |
135 | { | |
136 | leaf::result<int> r = leaf::try_handle_some( | |
137 | []() -> leaf::result<int> | |
138 | { | |
139 | throw leaf::exception( info<1>{1} ); | |
140 | }, | |
141 | error_handlers ); | |
142 | BOOST_TEST(r); | |
143 | BOOST_TEST_EQ(r.value(), 2); | |
144 | } | |
145 | { | |
146 | leaf::result<int> r = leaf::try_handle_some( | |
147 | []() -> leaf::result<int> | |
148 | { | |
149 | return leaf::new_error( info<1>{1} ); | |
150 | }, | |
151 | error_handlers ); | |
152 | BOOST_TEST(r); | |
153 | BOOST_TEST_EQ(r.value(), 2); | |
154 | } | |
155 | } | |
156 | ||
157 | /////////////////////////// | |
158 | ||
159 | { | |
160 | int r = leaf::try_handle_all( | |
161 | []() -> leaf::result<int> | |
162 | { | |
163 | return 42; | |
164 | }, | |
165 | [] | |
166 | { | |
167 | return 1; | |
168 | } ); | |
169 | BOOST_TEST_EQ(r, 42); | |
170 | } | |
171 | { | |
172 | int r = leaf::try_handle_all( | |
173 | []() -> leaf::result<int> | |
174 | { | |
175 | throw leaf::exception( my_exception(), info<1>{1} ); | |
176 | }, | |
177 | []( my_exception const &, info<1> const & x ) | |
178 | { | |
179 | BOOST_TEST_EQ(x.value, 1); | |
180 | return 1; | |
181 | }, | |
182 | [] | |
183 | { | |
184 | return 2; | |
185 | } ); | |
186 | BOOST_TEST_EQ(r, 1); | |
187 | } | |
188 | { | |
189 | int r = leaf::try_handle_all( | |
190 | []() -> leaf::result<int> | |
191 | { | |
192 | throw leaf::exception( info<1>{1} ); | |
193 | }, | |
194 | []( info<1> const & x ) | |
195 | { | |
196 | BOOST_TEST_EQ(x.value, 1); | |
197 | return 1; | |
198 | }, | |
199 | [] | |
200 | { | |
201 | return 2; | |
202 | } ); | |
203 | BOOST_TEST_EQ(r, 1); | |
204 | } | |
205 | { | |
206 | int r = leaf::try_handle_all( | |
207 | []() -> leaf::result<int> | |
208 | { | |
209 | return leaf::new_error( info<1>{1} ); | |
210 | }, | |
211 | []( info<1> const & x ) | |
212 | { | |
213 | BOOST_TEST_EQ(x.value, 1); | |
214 | return 1; | |
215 | }, | |
216 | [] | |
217 | { | |
218 | return 2; | |
219 | } ); | |
220 | BOOST_TEST_EQ(r, 1); | |
221 | } | |
222 | ||
223 | /////////////////////////// | |
224 | ||
225 | { | |
226 | auto error_handlers = std::make_tuple( | |
227 | []( my_exception const &, info<1> const & x ) | |
228 | { | |
229 | BOOST_TEST_EQ(x.value, 1); | |
230 | return 1; | |
231 | }, | |
232 | []( info<1> const & x ) | |
233 | { | |
234 | BOOST_TEST_EQ(x.value, 1); | |
235 | return 2; | |
236 | }, | |
237 | [] | |
238 | { | |
239 | return 1; | |
240 | } ); | |
241 | { | |
242 | int r = leaf::try_handle_all( | |
243 | []() -> leaf::result<int> | |
244 | { | |
245 | return 42; | |
246 | }, | |
247 | error_handlers ); | |
248 | BOOST_TEST_EQ(r, 42); | |
249 | } | |
250 | { | |
251 | int r = leaf::try_handle_all( | |
252 | []() -> leaf::result<int> | |
253 | { | |
254 | throw leaf::exception( my_exception(), info<1>{1} ); | |
255 | }, | |
256 | error_handlers ); | |
257 | BOOST_TEST_EQ(r, 1); | |
258 | } | |
259 | { | |
260 | int r = leaf::try_handle_all( | |
261 | []() -> leaf::result<int> | |
262 | { | |
263 | throw leaf::exception( info<1>{1} ); | |
264 | }, | |
265 | error_handlers ); | |
266 | BOOST_TEST_EQ(r, 2); | |
267 | } | |
268 | { | |
269 | int r = leaf::try_handle_all( | |
270 | []() -> leaf::result<int> | |
271 | { | |
272 | return leaf::new_error( info<1>{1} ); | |
273 | }, | |
274 | error_handlers ); | |
275 | BOOST_TEST_EQ(r, 2); | |
276 | } | |
277 | } | |
278 | ||
279 | /////////////////////////// | |
280 | ||
281 | { | |
282 | int r = leaf::try_handle_all( | |
283 | []() -> leaf::result<int> | |
284 | { | |
285 | return leaf::try_handle_all( | |
286 | []() -> leaf::result<int> | |
287 | { | |
288 | return leaf::new_error( info<1>{1} ); | |
289 | }, | |
290 | []( info<1> const & ) -> int | |
291 | { | |
292 | BOOST_LEAF_THROW_EXCEPTION(my_exception()); | |
293 | }, | |
294 | [] | |
295 | { | |
296 | return 1; | |
297 | } ); | |
298 | }, | |
299 | []( my_exception const &, info<1> ) | |
300 | { | |
301 | return 2; | |
302 | }, | |
303 | []( my_exception const & ) | |
304 | { | |
305 | return 3; | |
306 | }, | |
307 | [] | |
308 | { | |
309 | return 4; | |
310 | } ); | |
311 | ||
312 | BOOST_TEST_EQ(r, 3); | |
313 | } | |
314 | { | |
315 | int r = leaf::try_handle_all( | |
316 | []() -> leaf::result<int> | |
317 | { | |
318 | return leaf::try_handle_all( | |
319 | []() -> leaf::result<int> | |
320 | { | |
321 | return leaf::new_error( info<1>{1} ); | |
322 | }, | |
323 | []( info<1> const & x ) -> int | |
324 | { | |
325 | BOOST_TEST_EQ(x.value, 1); | |
326 | BOOST_LEAF_THROW_EXCEPTION(); | |
327 | }, | |
328 | [] | |
329 | { | |
330 | return 1; | |
331 | } ); | |
332 | }, | |
333 | []( my_exception const &, info<1> ) | |
334 | { | |
335 | return 2; | |
336 | }, | |
337 | []( my_exception const & ) | |
338 | { | |
339 | return 3; | |
340 | }, | |
341 | [] | |
342 | { | |
343 | return 4; | |
344 | } ); | |
345 | ||
346 | BOOST_TEST_EQ(r, 4); | |
347 | } | |
348 | ||
349 | /////////////////////////// | |
350 | ||
351 | { | |
352 | auto error_handlers = std::make_tuple( | |
353 | []( info<1> const & x ) -> int | |
354 | { | |
355 | BOOST_TEST_EQ(x.value, 1); | |
356 | BOOST_LEAF_THROW_EXCEPTION(my_exception()); | |
357 | }, | |
358 | [] | |
359 | { | |
360 | return 1; | |
361 | } ); | |
362 | int r = leaf::try_handle_all( | |
363 | [&]() -> leaf::result<int> | |
364 | { | |
365 | return leaf::try_handle_all( | |
366 | [&]() -> leaf::result<int> | |
367 | { | |
368 | return leaf::new_error( info<1>{1} ); | |
369 | }, | |
370 | error_handlers ); | |
371 | }, | |
372 | []( my_exception const &, info<1> ) | |
373 | { | |
374 | return 2; | |
375 | }, | |
376 | []( my_exception const & ) | |
377 | { | |
378 | return 3; | |
379 | }, | |
380 | [] | |
381 | { | |
382 | return 4; | |
383 | } ); | |
384 | ||
385 | BOOST_TEST_EQ(r, 3); | |
386 | } | |
387 | { | |
388 | auto error_handlers = std::make_tuple( | |
389 | []( info<1> const & x ) -> int | |
390 | { | |
391 | BOOST_TEST_EQ(x.value, 1); | |
392 | BOOST_LEAF_THROW_EXCEPTION(); | |
393 | }, | |
394 | [] | |
395 | { | |
396 | return 1; | |
397 | } ); | |
398 | int r = leaf::try_handle_all( | |
399 | [&]() -> leaf::result<int> | |
400 | { | |
401 | return leaf::try_handle_all( | |
402 | [&]() -> leaf::result<int> | |
403 | { | |
404 | return leaf::new_error( info<1>{1} ); | |
405 | }, | |
406 | error_handlers ); | |
407 | }, | |
408 | []( my_exception const &, info<1> ) | |
409 | { | |
410 | return 2; | |
411 | }, | |
412 | []( my_exception const & ) | |
413 | { | |
414 | return 3; | |
415 | }, | |
416 | [] | |
417 | { | |
418 | return 4; | |
419 | } ); | |
420 | ||
421 | BOOST_TEST_EQ(r, 4); | |
422 | } | |
423 | ||
424 | /////////////////////////// | |
425 | ||
426 | { | |
427 | int r = leaf::try_handle_all( | |
428 | []() -> leaf::result<int> | |
429 | { | |
430 | return leaf::try_handle_some( | |
431 | []() -> leaf::result<int> | |
432 | { | |
433 | return leaf::new_error( info<1>{1} ); | |
434 | }, | |
435 | []( info<1> const & x ) -> int | |
436 | { | |
437 | BOOST_TEST_EQ(x.value, 1); | |
438 | BOOST_LEAF_THROW_EXCEPTION(my_exception()); | |
439 | }, | |
440 | [] | |
441 | { | |
442 | return 1; | |
443 | } ); | |
444 | }, | |
445 | []( my_exception const &, info<1> ) | |
446 | { | |
447 | return 3; | |
448 | }, | |
449 | []( my_exception const & ) | |
450 | { | |
451 | return 4; | |
452 | }, | |
453 | [] | |
454 | { | |
455 | return 5; | |
456 | } ); | |
457 | ||
458 | BOOST_TEST_EQ(r, 4); | |
459 | } | |
460 | { | |
461 | int r = leaf::try_handle_all( | |
462 | []() -> leaf::result<int> | |
463 | { | |
464 | return leaf::try_handle_some( | |
465 | []() -> leaf::result<int> | |
466 | { | |
467 | return leaf::new_error( info<1>{1} ); | |
468 | }, | |
469 | []( info<1> const & x ) -> int | |
470 | { | |
471 | BOOST_TEST_EQ(x.value, 1); | |
472 | BOOST_LEAF_THROW_EXCEPTION(); | |
473 | }, | |
474 | [] | |
475 | { | |
476 | return 1; | |
477 | } ); | |
478 | }, | |
479 | []( my_exception const &, info<1> ) | |
480 | { | |
481 | return 3; | |
482 | }, | |
483 | []( my_exception const & ) | |
484 | { | |
485 | return 4; | |
486 | }, | |
487 | [] | |
488 | { | |
489 | return 5; | |
490 | } ); | |
491 | ||
492 | BOOST_TEST_EQ(r, 5); | |
493 | } | |
494 | ||
495 | /////////////////////////// | |
496 | ||
497 | { | |
498 | auto error_handlers = std::make_tuple( | |
499 | []( info<1> const & x ) -> leaf::result<int> | |
500 | { | |
501 | BOOST_TEST_EQ(x.value, 1); | |
502 | BOOST_LEAF_THROW_EXCEPTION(my_exception()); | |
503 | }, | |
504 | []() -> leaf::result<int> | |
505 | { | |
506 | return 1; | |
507 | } ); | |
508 | int r = leaf::try_handle_all( | |
509 | [&]() -> leaf::result<int> | |
510 | { | |
511 | return leaf::try_handle_some( | |
512 | [&]() -> leaf::result<int> | |
513 | { | |
514 | return leaf::new_error( info<1>{1} ); | |
515 | }, | |
516 | error_handlers ); | |
517 | }, | |
518 | []( my_exception const &, info<1> ) | |
519 | { | |
520 | return 3; | |
521 | }, | |
522 | []( my_exception const & ) | |
523 | { | |
524 | return 4; | |
525 | }, | |
526 | [] | |
527 | { | |
528 | return 5; | |
529 | } ); | |
530 | ||
531 | BOOST_TEST_EQ(r, 4); | |
532 | } | |
533 | { | |
534 | auto error_handlers = std::make_tuple( | |
535 | []( info<1> const & x ) -> leaf::result<int> | |
536 | { | |
537 | BOOST_TEST_EQ(x.value, 1); | |
538 | BOOST_LEAF_THROW_EXCEPTION(); | |
539 | }, | |
540 | []() -> leaf::result<int> | |
541 | { | |
542 | return 1; | |
543 | } ); | |
544 | int r = leaf::try_handle_all( | |
545 | [&]() -> leaf::result<int> | |
546 | { | |
547 | return leaf::try_handle_some( | |
548 | [&]() -> leaf::result<int> | |
549 | { | |
550 | return leaf::new_error( info<1>{1} ); | |
551 | }, | |
552 | error_handlers ); | |
553 | }, | |
554 | []( my_exception const &, info<1> ) | |
555 | { | |
556 | return 3; | |
557 | }, | |
558 | []( my_exception const & ) | |
559 | { | |
560 | return 4; | |
561 | }, | |
562 | [] | |
563 | { | |
564 | return 5; | |
565 | } ); | |
566 | ||
567 | BOOST_TEST_EQ(r, 5); | |
568 | } | |
569 | ||
570 | ////////////////////////////////////// | |
571 | ||
572 | // match_value<> with exceptions, try_handle_some | |
573 | { | |
574 | leaf::result<int> r = leaf::try_handle_some( | |
575 | []() -> leaf::result<int> | |
576 | { | |
577 | throw leaf::exception( my_exception(42) ); | |
578 | }, | |
579 | []( leaf::match_value<my_exception, 42> m ) | |
580 | { | |
581 | return m.matched.value; | |
582 | } ); | |
583 | BOOST_TEST(r); | |
584 | BOOST_TEST_EQ(r.value(), 42); | |
585 | } | |
586 | { | |
587 | leaf::result<int> r = leaf::try_handle_some( | |
588 | []() -> leaf::result<int> | |
589 | { | |
590 | throw my_exception(42); | |
591 | }, | |
592 | []( leaf::match_value<my_exception, 42> m ) | |
593 | { | |
594 | return m.matched.value; | |
595 | } ); | |
596 | BOOST_TEST(r); | |
597 | BOOST_TEST_EQ(r.value(), 42); | |
598 | } | |
599 | { | |
600 | leaf::result<int> r = leaf::try_handle_some( | |
601 | []() -> leaf::result<int> | |
602 | { | |
603 | throw leaf::exception( my_exception(42) ); | |
604 | }, | |
605 | []( leaf::match_value<my_exception, 41> m ) | |
606 | { | |
607 | return m.matched.value; | |
608 | }, | |
609 | []( leaf::error_info const & unmatched ) | |
610 | { | |
611 | return unmatched.error(); | |
612 | } ); | |
613 | BOOST_TEST(!r); | |
614 | } | |
615 | { | |
616 | leaf::result<int> r = leaf::try_handle_some( | |
617 | []() -> leaf::result<int> | |
618 | { | |
619 | throw my_exception(42); | |
620 | }, | |
621 | []( leaf::match_value<my_exception, 41> m ) | |
622 | { | |
623 | return m.matched.value; | |
624 | }, | |
625 | []( leaf::error_info const & unmatched ) | |
626 | { | |
627 | return unmatched.error(); | |
628 | } ); | |
629 | BOOST_TEST(!r); | |
630 | } | |
631 | ||
632 | ////////////////////////////////////// | |
633 | ||
634 | // match_value<> with exceptions, try_handle_all | |
635 | { | |
636 | int r = leaf::try_handle_all( | |
637 | []() -> leaf::result<int> | |
638 | { | |
639 | throw leaf::exception( my_exception(42) ); | |
640 | }, | |
641 | []( leaf::match_value<my_exception, 42> m ) | |
642 | { | |
643 | return m.matched.value; | |
644 | }, | |
645 | [] | |
646 | { | |
647 | return -1; | |
648 | } ); | |
649 | BOOST_TEST_EQ(r, 42); | |
650 | } | |
651 | { | |
652 | int r = leaf::try_handle_all( | |
653 | []() -> leaf::result<int> | |
654 | { | |
655 | throw my_exception(42); | |
656 | }, | |
657 | []( leaf::match_value<my_exception, 42> m ) | |
658 | { | |
659 | return m.matched.value; | |
660 | }, | |
661 | [] | |
662 | { | |
663 | return -1; | |
664 | } ); | |
665 | BOOST_TEST_EQ(r, 42); | |
666 | } | |
667 | { | |
668 | int r = leaf::try_handle_all( | |
669 | []() -> leaf::result<int> | |
670 | { | |
671 | throw leaf::exception( my_exception(42) ); | |
672 | }, | |
673 | []( leaf::match_value<my_exception, 41> m ) | |
674 | { | |
675 | return m.matched.value; | |
676 | }, | |
677 | [] | |
678 | { | |
679 | return -1; | |
680 | } ); | |
681 | BOOST_TEST_EQ(r, -1); | |
682 | } | |
683 | { | |
684 | int r = leaf::try_handle_all( | |
685 | []() -> leaf::result<int> | |
686 | { | |
687 | throw my_exception(42); | |
688 | }, | |
689 | []( leaf::match_value<my_exception, 41> m ) | |
690 | { | |
691 | return m.matched.value; | |
692 | }, | |
693 | [] | |
694 | { | |
695 | return -1; | |
696 | } ); | |
697 | BOOST_TEST_EQ(r, -1); | |
698 | } | |
699 | ||
700 | return boost::report_errors(); | |
701 | } | |
702 | ||
703 | #endif |