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