]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/outcome/test/tests/core-outcome.cpp
import new upstream nautilus stable release 14.2.8
[ceph.git] / ceph / src / boost / libs / outcome / test / tests / core-outcome.cpp
1 /* Unit testing for outcomes
2 (C) 2013-2019 Niall Douglas <http://www.nedproductions.biz/> (18 commits)
3
4
5 Boost Software License - Version 1.0 - August 17th, 2003
6
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:
13
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.
20
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.
28 */
29
30 #include <boost/outcome/outcome.hpp>
31 #include <boost/test/unit_test.hpp>
32 #include <boost/test/unit_test_monitor.hpp>
33
34 #include <iostream>
35
36 #ifdef _MSC_VER
37 #pragma warning(disable : 4702) // unreachable code
38 #endif
39
40 BOOST_OUTCOME_AUTO_TEST_CASE(works_outcome, "Tests that the outcome works as intended")
41 {
42 using namespace BOOST_OUTCOME_V2_NAMESPACE;
43
44 static_assert(std::is_constructible<outcome<long>, int>::value, "Sanity check that monad can be constructed from a value_type");
45 static_assert(!std::is_constructible<outcome<outcome<long>>, int>::value, "Sanity check that outer monad can be constructed from an inner monad's value_type");
46 static_assert(!std::is_constructible<outcome<outcome<outcome<long>>>, int>::value, "Sanity check that outer monad can be constructed from an inner inner monad's value_type");
47 static_assert(!std::is_constructible<outcome<outcome<outcome<outcome<long>>>>, int>::value, "Sanity check that outer monad can be constructed from an inner inner monad's value_type");
48
49 static_assert(std::is_constructible<outcome<int>, outcome<long>>::value, "Sanity check that compatible monads can be constructed from one another");
50 static_assert(std::is_constructible<outcome<outcome<int>>, outcome<long>>::value, "Sanity check that outer monad can be constructed from a compatible monad");
51 static_assert(!std::is_constructible<outcome<outcome<outcome<int>>>, outcome<long>>::value, "Sanity check that outer monad can be constructed from a compatible monad up to two nestings deep");
52 static_assert(!std::is_constructible<outcome<outcome<outcome<outcome<int>>>>, outcome<long>>::value, "Sanity check that outer monad can be constructed from a compatible monad three or more nestings deep");
53 static_assert(!std::is_constructible<outcome<std::string>, outcome<int>>::value, "Sanity check that incompatible monads cannot be constructed from one another");
54
55 static_assert(std::is_constructible<outcome<int>, outcome<void>>::value, "Sanity check that all monads can be constructed from a void monad");
56 static_assert(std::is_constructible<outcome<outcome<int>>, outcome<void>>::value, "Sanity check that outer monad can be constructed from a compatible monad");
57 static_assert(std::is_constructible<outcome<outcome<outcome<int>>>, outcome<void>>::value, "Sanity check that outer monad can be constructed from a compatible monad up to two nestings deep");
58 static_assert(!std::is_constructible<outcome<void>, outcome<int>>::value, "Sanity check that incompatible monads cannot be constructed from one another");
59
60 static_assert(std::is_void<result<void>::value_type>::value, "Sanity check that result<void> has a void value_type");
61 static_assert(std::is_void<result<void, void>::error_type>::value, "Sanity check that result<void, void> has a void error_type");
62 // static_assert(std::is_void<outcome<void, void, void>::exception_type>::value, "Sanity check that outcome<void, void, void> has a void exception_type");
63
64 static_assert(std::is_same<outcome<int>::value_type, int>::value, "Sanity check that outcome<int> has a int value_type");
65 static_assert(std::is_same<outcome<int>::error_type, boost::system::error_code>::value, "Sanity check that outcome<int> has a error_code error_type");
66 static_assert(std::is_same<outcome<int>::exception_type, boost::exception_ptr>::value, "Sanity check that outcome<int> has a exception_ptr exception_type");
67
68
69 { // errored int
70 outcome<int> m(boost::system::errc::bad_address);
71 BOOST_CHECK(!m);
72 BOOST_CHECK(!m.has_value());
73 BOOST_CHECK(m.has_error());
74 BOOST_CHECK(!m.has_exception());
75 BOOST_CHECK_THROW(m.value(), boost::system::system_error);
76 BOOST_CHECK_NO_THROW(m.error());
77 BOOST_CHECK_THROW(m.exception(), bad_outcome_access);
78 BOOST_CHECK_THROW(boost::rethrow_exception(m.failure()), boost::system::system_error);
79 }
80 { // errored void
81 outcome<void> m(boost::system::errc::bad_address);
82 BOOST_CHECK(!m);
83 BOOST_CHECK(!m.has_value());
84 BOOST_CHECK(m.has_error());
85 BOOST_CHECK(!m.has_exception());
86 BOOST_CHECK_THROW(([&m]() -> void { return m.value(); }()), boost::system::system_error);
87 BOOST_CHECK_NO_THROW(m.error());
88 BOOST_CHECK_THROW(m.exception(), bad_outcome_access);
89 BOOST_CHECK_THROW(boost::rethrow_exception(m.failure()), boost::system::system_error);
90 }
91 { // valued int
92 outcome<int> m(5);
93 BOOST_CHECK(m);
94 BOOST_CHECK(m.has_value());
95 BOOST_CHECK(!m.has_error());
96 BOOST_CHECK(!m.has_exception());
97 BOOST_CHECK(m.value() == 5);
98 m.value() = 6;
99 BOOST_CHECK(m.value() == 6);
100 BOOST_CHECK_THROW(m.error(), bad_outcome_access);
101 BOOST_CHECK_THROW(m.exception(), bad_outcome_access);
102 BOOST_CHECK(!m.failure());
103 }
104 { // moves do not clear state
105 outcome<std::string> m("niall");
106 BOOST_CHECK(m);
107 BOOST_CHECK(m.has_value());
108 BOOST_CHECK(!m.has_error());
109 BOOST_CHECK(!m.has_exception());
110 BOOST_CHECK(m.value() == "niall");
111 m.value() = "NIALL";
112 BOOST_CHECK(m.value() == "NIALL");
113 auto temp(std::move(m).value());
114 BOOST_CHECK(temp == "NIALL");
115 BOOST_CHECK(m.value().empty()); // NOLINT
116 }
117 { // valued void
118 outcome<void> m(in_place_type<void>);
119 BOOST_CHECK(m);
120 BOOST_CHECK(m.has_value());
121 BOOST_CHECK(!m.has_error());
122 BOOST_CHECK(!m.has_exception());
123 BOOST_CHECK_NO_THROW(m.value()); // works, but type returned is unusable
124 BOOST_CHECK_THROW(m.error(), bad_outcome_access);
125 BOOST_CHECK_THROW(m.exception(), bad_outcome_access);
126 BOOST_CHECK(!m.failure());
127 }
128 { // errored
129 boost::system::error_code ec(5, boost::system::system_category());
130 outcome<int> m(ec);
131 BOOST_CHECK(!m);
132 BOOST_CHECK(!m.has_value());
133 BOOST_CHECK(m.has_error());
134 BOOST_CHECK(!m.has_exception());
135 BOOST_CHECK_THROW(m.value(), boost::system::system_error);
136 BOOST_CHECK(m.error() == ec);
137 BOOST_CHECK_THROW(m.exception(), bad_outcome_access);
138 #ifndef BOOST_NO_EXCEPTIONS
139 BOOST_CHECK(m.failure());
140 try
141 {
142 boost::rethrow_exception(m.failure());
143 }
144 catch(const boost::system::system_error &ex)
145 {
146 BOOST_CHECK(ex.code() == ec);
147 BOOST_CHECK(ex.code().value() == 5);
148 }
149 #endif
150 }
151 #if !defined(__APPLE__) || defined(__cpp_exceptions)
152 { // excepted
153 boost::system::error_code ec(5, boost::system::system_category());
154 auto e = boost::copy_exception(boost::system::system_error(ec)); // NOLINT
155 outcome<int> m(e);
156 BOOST_CHECK(!m);
157 BOOST_CHECK(!m.has_value());
158 BOOST_CHECK(!m.has_error());
159 BOOST_CHECK(m.has_exception());
160 BOOST_CHECK_THROW(m.value(), boost::system::system_error);
161 BOOST_CHECK_THROW(m.error(), bad_outcome_access);
162 BOOST_CHECK(m.exception() == e);
163 #ifndef BOOST_NO_EXCEPTIONS
164 BOOST_CHECK(m.failure());
165 try
166 {
167 boost::rethrow_exception(m.failure());
168 }
169 catch(const boost::system::system_error &ex)
170 {
171 BOOST_CHECK(ex.code() == ec);
172 BOOST_CHECK(ex.code().value() == 5);
173 }
174 #endif
175 }
176 { // custom error type
177 struct Foo
178 {
179 };
180 auto e = boost::copy_exception(Foo());
181 outcome<int> m(e);
182 BOOST_CHECK(!m);
183 BOOST_CHECK(!m.has_value());
184 BOOST_CHECK(!m.has_error());
185 BOOST_CHECK(m.has_exception());
186 BOOST_CHECK_THROW(m.value(), Foo);
187 BOOST_CHECK_THROW(m.error(), bad_outcome_access);
188 BOOST_CHECK(m.exception() == e);
189 }
190 { // outcome<void, void> should work
191 boost::system::error_code ec(5, boost::system::system_category());
192 auto e = boost::copy_exception(boost::system::system_error(ec));
193 outcome<void, void> m(e);
194 BOOST_CHECK(!m);
195 BOOST_CHECK(!m.has_value());
196 BOOST_CHECK(!m.has_error());
197 BOOST_CHECK(m.has_exception());
198 }
199 #endif
200
201
202 {
203 outcome<int> a(5);
204 outcome<int> b(make_error_code(boost::system::errc::invalid_argument));
205 std::cout << sizeof(a) << std::endl; // 40 bytes
206 a.assume_value();
207 b.assume_error();
208 #ifndef BOOST_NO_EXCEPTIONS
209 try
210 {
211 b.value();
212 std::cerr << "fail" << std::endl;
213 std::terminate();
214 }
215 catch(const boost::system::system_error & /*unused*/)
216 {
217 }
218 #endif
219 static_assert(!std::is_default_constructible<decltype(a)>::value, "");
220 static_assert(!std::is_nothrow_default_constructible<decltype(a)>::value, "");
221 static_assert(std::is_copy_constructible<decltype(a)>::value, "");
222 static_assert(!std::is_trivially_copy_constructible<decltype(a)>::value, "");
223 static_assert(std::is_nothrow_copy_constructible<decltype(a)>::value, "");
224 static_assert(std::is_copy_assignable<decltype(a)>::value, "");
225 static_assert(!std::is_trivially_copy_assignable<decltype(a)>::value, "");
226 static_assert(std::is_nothrow_copy_assignable<decltype(a)>::value, "");
227 static_assert(!std::is_trivially_destructible<decltype(a)>::value, "");
228 static_assert(std::is_nothrow_destructible<decltype(a)>::value, "");
229
230 // Test void compiles
231 outcome<void> c(in_place_type<void>);
232 outcome<void> c2(c);
233 (void) c2;
234 // Test int, void compiles
235 outcome<int, void> d(in_place_type<boost::exception_ptr>);
236 }
237
238 {
239 // Can only be constructed via multiple args
240 struct udt3
241 {
242 udt3() = delete;
243 udt3(udt3 &&) = delete;
244 udt3(const udt3 &) = delete;
245 udt3 &operator=(udt3 &&) = delete;
246 udt3 &operator=(const udt3 &) = delete;
247 explicit udt3(int /*unused*/, const char * /*unused*/, std::nullptr_t /*unused*/) {}
248 ~udt3() = default;
249 };
250 // Test a udt which can only be constructed in place compiles
251 outcome<udt3> g(in_place_type<udt3>, 5, static_cast<const char *>("niall"), nullptr);
252 // Does converting inplace construction also work?
253 outcome<udt3> h(5, static_cast<const char *>("niall"), nullptr);
254 outcome<udt3> i(ENOMEM, boost::system::generic_category());
255 BOOST_CHECK(h.has_value());
256 BOOST_CHECK(i.has_error());
257 }
258 }