1 /* Unit testing for outcomes
2 (C) 2013-2019 Niall Douglas <http://www.nedproductions.biz/> (4 commits)
5 Boost Software License - Version 1.0 - August 17th, 2003
7 Permission is hereby granted, free of charge, to any person or organization
8 obtaining a copy of the software and accompanying documentation covered by
9 this license (the "Software") to use, reproduce, display, distribute,
10 execute, and transmit the Software, and to prepare derivative works of the
11 Software, and to permit third-parties to whom the Software is furnished to
12 do so, all subject to the following:
14 The copyright notices in the Software and this entire statement, including
15 the above license grant, this restriction and the following disclaimer,
16 must be included in all copies of the Software, in whole or in part, and
17 all derivative works of the Software, unless such copies or derivative
18 works are solely in the form of machine-executable object code generated by
19 a source language processor.
21 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
24 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
25 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
26 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27 DEALINGS IN THE SOFTWARE.
30 #include <boost/outcome/experimental/status_outcome.hpp>
32 #define BOOST_OUTCOME_PREVENT_CONVERSION_WORKAROUND std
33 template <class T
, class S
= BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE::system_code
, class P
= BOOST_OUTCOME_PREVENT_CONVERSION_WORKAROUND::exception_ptr
> using outcome
= BOOST_OUTCOME_V2_NAMESPACE::experimental::status_outcome
<T
, S
, P
>;
34 using BOOST_OUTCOME_V2_NAMESPACE::in_place_type
;
36 #include <boost/test/unit_test.hpp>
37 #include <boost/test/unit_test_monitor.hpp>
42 #pragma warning(disable : 4702) // unreachable code
45 BOOST_OUTCOME_AUTO_TEST_CASE(works_status_code_outcome
, "Tests that the outcome with status_code works as intended")
47 using namespace BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE
;
50 outcome
<int> m(generic_code
{errc::bad_address
});
52 BOOST_CHECK(!m
.has_value());
53 BOOST_CHECK(m
.has_error());
54 BOOST_CHECK(!m
.has_exception());
55 BOOST_CHECK_THROW(m
.value(), status_error
<void>);
56 BOOST_CHECK_NO_THROW(m
.error());
57 BOOST_CHECK_THROW(BOOST_OUTCOME_PREVENT_CONVERSION_WORKAROUND::rethrow_exception(m
.failure()), generic_error
);
60 outcome
<void> m(generic_code
{errc::bad_address
});
62 BOOST_CHECK(!m
.has_value());
63 BOOST_CHECK(m
.has_error());
64 BOOST_CHECK(!m
.has_exception());
65 BOOST_CHECK_THROW(([&m
]() -> void { return m
.value(); }()), generic_error
);
66 BOOST_CHECK_NO_THROW(m
.error());
67 BOOST_CHECK_THROW(BOOST_OUTCOME_PREVENT_CONVERSION_WORKAROUND::rethrow_exception(m
.failure()), generic_error
);
72 BOOST_CHECK(m
.has_value());
73 BOOST_CHECK(!m
.has_error());
74 BOOST_CHECK(!m
.has_exception());
75 BOOST_CHECK(m
.value() == 5);
77 BOOST_CHECK(m
.value() == 6);
78 BOOST_CHECK(!m
.failure());
80 { // moves do not clear state
81 outcome
<std::string
> m("niall");
83 BOOST_CHECK(m
.has_value());
84 BOOST_CHECK(!m
.has_error());
85 BOOST_CHECK(!m
.has_exception());
86 BOOST_CHECK(m
.value() == "niall");
88 BOOST_CHECK(m
.value() == "NIALL");
89 auto temp(std::move(m
).value());
90 BOOST_CHECK(temp
== "NIALL");
91 BOOST_CHECK(m
.value().empty()); // NOLINT
94 outcome
<void> m(in_place_type
<void>);
96 BOOST_CHECK(m
.has_value());
97 BOOST_CHECK(!m
.has_error());
98 BOOST_CHECK(!m
.has_exception());
99 BOOST_CHECK_NO_THROW(m
.value()); // works, but type returned is unusable
100 BOOST_CHECK(!m
.failure());
103 error
ec(errc::no_link
);
104 outcome
<int> m(ec
.clone());
106 BOOST_CHECK(!m
.has_value());
107 BOOST_CHECK(m
.has_error());
108 BOOST_CHECK(!m
.has_exception());
109 BOOST_CHECK_THROW(m
.value(), generic_error
);
110 BOOST_CHECK(m
.error() == ec
);
111 #ifndef BOOST_NO_EXCEPTIONS
112 BOOST_CHECK(m
.failure());
115 BOOST_OUTCOME_PREVENT_CONVERSION_WORKAROUND::rethrow_exception(m
.failure());
117 catch(const generic_error
&ex
)
119 BOOST_CHECK(ex
.code() == ec
);
120 BOOST_CHECK(ex
.code().value() == errc::no_link
);
124 #if !defined(__APPLE__) || defined(__cpp_exceptions)
126 BOOST_OUTCOME_PREVENT_CONVERSION_WORKAROUND::error_code
ec(5, BOOST_OUTCOME_PREVENT_CONVERSION_WORKAROUND::system_category());
127 auto e
= BOOST_OUTCOME_PREVENT_CONVERSION_WORKAROUND::make_exception_ptr(BOOST_OUTCOME_PREVENT_CONVERSION_WORKAROUND::system_error(ec
)); // NOLINT
130 BOOST_CHECK(!m
.has_value());
131 BOOST_CHECK(!m
.has_error());
132 BOOST_CHECK(m
.has_exception());
133 BOOST_CHECK_THROW(m
.value(), BOOST_OUTCOME_PREVENT_CONVERSION_WORKAROUND::system_error
);
134 BOOST_CHECK(m
.exception() == e
);
135 #ifndef BOOST_NO_EXCEPTIONS
136 BOOST_CHECK(m
.failure());
139 BOOST_OUTCOME_PREVENT_CONVERSION_WORKAROUND::rethrow_exception(m
.failure());
141 catch(const BOOST_OUTCOME_PREVENT_CONVERSION_WORKAROUND::system_error
&ex
)
143 BOOST_CHECK(ex
.code() == ec
);
144 BOOST_CHECK(ex
.code().value() == 5);
148 { // custom error type
152 auto e
= BOOST_OUTCOME_PREVENT_CONVERSION_WORKAROUND::make_exception_ptr(Foo());
155 BOOST_CHECK(!m
.has_value());
156 BOOST_CHECK(!m
.has_error());
157 BOOST_CHECK(m
.has_exception());
158 BOOST_CHECK_THROW(m
.value(), Foo
);
159 BOOST_CHECK(m
.exception() == e
);
161 { // outcome<void, void> should work
162 BOOST_OUTCOME_PREVENT_CONVERSION_WORKAROUND::error_code
ec(5, BOOST_OUTCOME_PREVENT_CONVERSION_WORKAROUND::system_category());
163 auto e
= BOOST_OUTCOME_PREVENT_CONVERSION_WORKAROUND::make_exception_ptr(BOOST_OUTCOME_PREVENT_CONVERSION_WORKAROUND::system_error(ec
));
164 outcome
<void, void> m(e
);
166 BOOST_CHECK(!m
.has_value());
167 BOOST_CHECK(!m
.has_error());
168 BOOST_CHECK(m
.has_exception());
175 outcome
<int> b(generic_code
{errc::invalid_argument
});
176 std::cout
<< sizeof(a
) << std::endl
; // 40 bytes
179 #ifndef BOOST_NO_EXCEPTIONS
183 std::cerr
<< "fail" << std::endl
;
186 catch(const generic_error
& /*unused*/)
190 static_assert(!std::is_default_constructible
<decltype(a
)>::value
, "");
191 static_assert(!std::is_nothrow_default_constructible
<decltype(a
)>::value
, "");
192 static_assert(!std::is_copy_constructible
<decltype(a
)>::value
, "");
193 static_assert(!std::is_trivially_copy_constructible
<decltype(a
)>::value
, "");
194 static_assert(!std::is_nothrow_copy_constructible
<decltype(a
)>::value
, "");
195 static_assert(!std::is_copy_assignable
<decltype(a
)>::value
, "");
196 static_assert(!std::is_trivially_copy_assignable
<decltype(a
)>::value
, "");
197 static_assert(!std::is_nothrow_copy_assignable
<decltype(a
)>::value
, "");
198 static_assert(!std::is_trivially_destructible
<decltype(a
)>::value
, "");
199 static_assert(std::is_nothrow_destructible
<decltype(a
)>::value
, "");
201 // Test void compiles
202 outcome
<void> c(in_place_type
<void>);
203 // Test int, void compiles
204 outcome
<int, void> d(in_place_type
<BOOST_OUTCOME_PREVENT_CONVERSION_WORKAROUND::exception_ptr
>);
208 // Can only be constructed via multiple args
212 udt3(udt3
&&) = delete;
213 udt3(const udt3
&) = delete;
214 udt3
&operator=(udt3
&&) = delete;
215 udt3
&operator=(const udt3
&) = delete;
216 explicit udt3(int /*unused*/, const char * /*unused*/, std::nullptr_t
/*unused*/) {}
219 // Test a udt which can only be constructed in place compiles
220 outcome
<udt3
> g(in_place_type
<udt3
>, 5, static_cast<const char *>("niall"), nullptr);
221 // Does converting inplace construction also work?
222 outcome
<udt3
> h(5, static_cast<const char *>("niall"), nullptr);
223 BOOST_CHECK(h
.has_value());