| 1 | // Copyright 2018-2023 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/config.hpp> |
| 7 | |
| 8 | #if BOOST_LEAF_CFG_STD_STRING |
| 9 | # include <sstream> |
| 10 | # include <iostream> |
| 11 | #endif |
| 12 | |
| 13 | #if !BOOST_LEAF_CFG_CAPTURE |
| 14 | |
| 15 | #include <iostream> |
| 16 | |
| 17 | int main() |
| 18 | { |
| 19 | std::cout << "Unit test not applicable." << std::endl; |
| 20 | return 0; |
| 21 | } |
| 22 | |
| 23 | #else |
| 24 | |
| 25 | #ifdef BOOST_LEAF_TEST_SINGLE_HEADER |
| 26 | # include "leaf.hpp" |
| 27 | #else |
| 28 | # include <boost/leaf/result.hpp> |
| 29 | # include <boost/leaf/handle_errors.hpp> |
| 30 | #endif |
| 31 | |
| 32 | #include "lightweight_test.hpp" |
| 33 | |
| 34 | namespace leaf = boost::leaf; |
| 35 | |
| 36 | namespace |
| 37 | { |
| 38 | template <int> |
| 39 | struct err |
| 40 | { |
| 41 | static int count; |
| 42 | static int move_count; |
| 43 | |
| 44 | err() |
| 45 | { |
| 46 | ++count; |
| 47 | } |
| 48 | |
| 49 | err( err const & ) |
| 50 | { |
| 51 | ++count; |
| 52 | } |
| 53 | |
| 54 | err( err && ) |
| 55 | { |
| 56 | ++count; |
| 57 | ++move_count; |
| 58 | } |
| 59 | |
| 60 | ~err() |
| 61 | { |
| 62 | --count; |
| 63 | } |
| 64 | }; |
| 65 | template <int N> int err<N>::count = 0; |
| 66 | template <int N> int err<N>::move_count = 0; |
| 67 | } |
| 68 | |
| 69 | int main() |
| 70 | { |
| 71 | { |
| 72 | leaf::result<void> r = leaf::try_capture_all( |
| 73 | try_block: []() -> leaf::result<void> |
| 74 | { |
| 75 | return leaf::new_error(item: err<1>{}, item: err<2>{}); |
| 76 | }); |
| 77 | BOOST_TEST_EQ(err<1>::count, 1); |
| 78 | BOOST_TEST_EQ(err<2>::count, 1); |
| 79 | BOOST_TEST(!r); |
| 80 | int r1 = leaf::try_handle_all( |
| 81 | try_block: [&]() -> leaf::result<int> |
| 82 | { |
| 83 | BOOST_LEAF_CHECK(r); |
| 84 | return 0; |
| 85 | }, |
| 86 | h: [](err<1>, err<2>) |
| 87 | { |
| 88 | return 1; |
| 89 | }, |
| 90 | h: [] |
| 91 | { |
| 92 | return 2; |
| 93 | } ); |
| 94 | BOOST_TEST_EQ(r1, 1); |
| 95 | } |
| 96 | BOOST_TEST_EQ(err<1>::count, 0); |
| 97 | BOOST_TEST_EQ(err<2>::count, 0); |
| 98 | |
| 99 | { |
| 100 | leaf::result<int> r = leaf::try_capture_all( |
| 101 | try_block: []() -> leaf::result<int> |
| 102 | { |
| 103 | return leaf::new_error(item: err<1>{}, item: err<2>{}); |
| 104 | }); |
| 105 | BOOST_TEST(!r); |
| 106 | BOOST_TEST_EQ(err<1>::count, 1); |
| 107 | BOOST_TEST_EQ(err<2>::count, 1); |
| 108 | int r1 = leaf::try_handle_all( |
| 109 | try_block: [&]() -> leaf::result<int> |
| 110 | { |
| 111 | BOOST_LEAF_CHECK(r); |
| 112 | return 0; |
| 113 | }, |
| 114 | h: [](err<1>, err<2>) |
| 115 | { |
| 116 | return 1; |
| 117 | }, |
| 118 | h: [] |
| 119 | { |
| 120 | return 2; |
| 121 | } ); |
| 122 | BOOST_TEST_EQ(r1, 1); |
| 123 | } |
| 124 | BOOST_TEST_EQ(err<1>::count, 0); |
| 125 | BOOST_TEST_EQ(err<2>::count, 0); |
| 126 | |
| 127 | return boost::report_errors(); |
| 128 | } |
| 129 | |
| 130 | #endif |
| 131 | |