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 defined(BOOST_LEAF_NO_EXCEPTIONS) || !BOOST_LEAF_CFG_CAPTURE
9
10#include <iostream>
11
12int main()
13{
14 std::cout << "Unit test not applicable." << std::endl;
15 return 0;
16}
17
18#else
19
20#ifdef BOOST_LEAF_TEST_SINGLE_HEADER
21# include "leaf.hpp"
22#else
23# include <boost/leaf/result.hpp>
24# include <boost/leaf/handle_errors.hpp>
25# include <boost/leaf/on_error.hpp>
26# include <boost/leaf/exception.hpp>
27#endif
28
29#include "_test_ec.hpp"
30#include "lightweight_test.hpp"
31
32namespace leaf = boost::leaf;
33
34template <int> struct info { int value; };
35
36template <class F>
37void test( F f )
38{
39 {
40 int c=0;
41 auto r = f();
42 leaf::try_handle_all(
43 [&r]() -> leaf::result<void>
44 {
45 BOOST_LEAF_CHECK(std::move(r));
46 return { };
47 },
48 [&c]( info<1> const & x )
49 {
50 BOOST_TEST_EQ(x.value, 1);
51 BOOST_TEST_EQ(c, 0);
52 c = 1;
53 },
54 [&c]
55 {
56 BOOST_TEST_EQ(c, 0);
57 c = 2;
58 } );
59 BOOST_TEST_EQ(c, 1);
60 }
61
62 {
63 int c=0;
64 auto r = f();
65 leaf::try_handle_all(
66 [&r]() -> leaf::result<void>
67 {
68 BOOST_LEAF_CHECK(std::move(r));
69 return { };
70 },
71 [&c]( info<2> const & x )
72 {
73 BOOST_TEST_EQ(x.value, 2);
74 BOOST_TEST_EQ(c, 0);
75 c = 1;
76 },
77 [&c]
78 {
79 BOOST_TEST_EQ(c, 0);
80 c = 2;
81 } );
82 BOOST_TEST_EQ(c, 2);
83 }
84
85 {
86 auto r = f();
87 int what = leaf::try_handle_all(
88 [&r]() -> leaf::result<int>
89 {
90 BOOST_LEAF_CHECK(std::move(r));
91 return 0;
92 },
93 []( info<1> const & x )
94 {
95 BOOST_TEST_EQ(x.value, 1);
96 return 1;
97 },
98 []
99 {
100 return 2;
101 } );
102 BOOST_TEST_EQ(what, 1);
103 }
104
105 {
106 auto r = f();
107 int what = leaf::try_handle_all(
108 [&r]() -> leaf::result<int>
109 {
110 BOOST_LEAF_CHECK(std::move(r));
111 return 0;
112 },
113 []( info<2> const & x )
114 {
115 BOOST_TEST_EQ(x.value, 2);
116 return 1;
117 },
118 []
119 {
120 return 2;
121 } );
122 BOOST_TEST_EQ(what, 2);
123 }
124}
125
126int main()
127{
128 test( f: []
129 {
130 return leaf::try_capture_all(
131 try_block: []() -> int
132 {
133 leaf::throw_exception(e: errc_a::a0, e: info<1>{.value: 1}, e: info<3>{.value: 3});
134 } );
135 } );
136
137 test( f: []
138 {
139 return leaf::try_capture_all(
140 try_block: []() -> void
141 {
142 leaf::throw_exception(e: errc_a::a0, e: info<1>{.value: 1}, e: info<3>{.value: 3});
143 } );
144 } );
145
146 test( f: []
147 {
148 return leaf::try_capture_all(
149 try_block: []() -> int
150 {
151 auto load = leaf::on_error(i: errc_a::a0, i: info<1>{.value: 1}, i: info<3>{.value: 3});
152 leaf::throw_exception();
153 } );
154 } );
155
156 test( f: []
157 {
158 return leaf::try_capture_all(
159 try_block: []() -> void
160 {
161 auto load = leaf::on_error(i: errc_a::a0, i: info<1>{.value: 1}, i: info<3>{.value: 3});
162 leaf::throw_exception();
163 } );
164 } );
165
166 test( f: []
167 {
168 return leaf::try_capture_all(
169 try_block: []() -> int
170 {
171 auto load = leaf::on_error(i: errc_a::a0, i: info<1>{.value: 1}, i: info<3>{.value: 3});
172 throw std::exception();
173 } );
174 } );
175
176 test( f: []
177 {
178 return leaf::try_capture_all(
179 try_block: []() -> void
180 {
181 auto load = leaf::on_error(i: errc_a::a0, i: info<1>{.value: 1}, i: info<3>{.value: 3});
182 throw std::exception();
183 } );
184 } );
185
186 return boost::report_errors();
187}
188
189#endif
190

source code of boost/libs/leaf/test/capture_exception_result_unload_test.cpp