• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Unit testing for outcomes
2 (C) 2013-2020 Niall Douglas <http://www.nedproductions.biz/> (4 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/experimental/status_outcome.hpp>
31 
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;
35 
36 #include <boost/test/unit_test.hpp>
37 #include <boost/test/unit_test_monitor.hpp>
38 
39 #include <iostream>
40 
41 #ifdef _MSC_VER
42 #pragma warning(disable : 4702)  // unreachable code
43 #endif
44 
45 BOOST_OUTCOME_AUTO_TEST_CASE(works_status_code_outcome, "Tests that the outcome with status_code works as intended")
46 {
47   using namespace BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE;
48 
49   {  // errored int
50     outcome<int> m(generic_code{errc::bad_address});
51     BOOST_CHECK(!m);
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);
58   }
59   {  // errored void
60     outcome<void> m(generic_code{errc::bad_address});
61     BOOST_CHECK(!m);
62     BOOST_CHECK(!m.has_value());
63     BOOST_CHECK(m.has_error());
64     BOOST_CHECK(!m.has_exception());
__anon1ecb3a5c0102() 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);
68   }
69   {  // valued int
70     outcome<int> m(5);
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(m.value() == 5);
76     m.value() = 6;
77     BOOST_CHECK(m.value() == 6);
78     BOOST_CHECK(!m.failure());
79   }
80   {  // moves do not clear state
81     outcome<std::string> m("niall");
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(m.value() == "niall");
87     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
92   }
93   {  // valued void
94     outcome<void> m(in_place_type<void>);
95     BOOST_CHECK(m);
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());
101   }
102   {  // errored
103     error ec(errc::no_link);
104     outcome<int> m(ec.clone());
105     BOOST_CHECK(!m);
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());
113     try
114     {
115       BOOST_OUTCOME_PREVENT_CONVERSION_WORKAROUND::rethrow_exception(m.failure());
116     }
117     catch(const generic_error &ex)
118     {
119       BOOST_CHECK(ex.code() == ec);
120       BOOST_CHECK(ex.code().value() == errc::no_link);
121     }
122 #endif
123   }
124 #if !defined(__APPLE__) || defined(__cpp_exceptions)
125   {  // excepted
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
128     outcome<int> m(e);
129     BOOST_CHECK(!m);
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());
137     try
138     {
139       BOOST_OUTCOME_PREVENT_CONVERSION_WORKAROUND::rethrow_exception(m.failure());
140     }
141     catch(const BOOST_OUTCOME_PREVENT_CONVERSION_WORKAROUND::system_error &ex)
142     {
143       BOOST_CHECK(ex.code() == ec);
144       BOOST_CHECK(ex.code().value() == 5);
145     }
146 #endif
147   }
148   {  // custom error type
149     struct Foo
150     {
151     };
152     auto e = BOOST_OUTCOME_PREVENT_CONVERSION_WORKAROUND::make_exception_ptr(Foo());
153     outcome<int> m(e);
154     BOOST_CHECK(!m);
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);
160   }
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);
165     BOOST_CHECK(!m);
166     BOOST_CHECK(!m.has_value());
167     BOOST_CHECK(!m.has_error());
168     BOOST_CHECK(m.has_exception());
169   }
170 #endif
171 
172 
173   {
174     outcome<int> a(5);
175     outcome<int> b(generic_code{errc::invalid_argument});
176     std::cout << sizeof(a) << std::endl;  // 40 bytes
177     a.assume_value();
178     b.assume_error();
179 #ifndef BOOST_NO_EXCEPTIONS
180     try
181     {
182       b.value();
183       std::cerr << "fail" << std::endl;
184       std::terminate();
185     }
186     catch(const generic_error & /*unused*/)
187     {
188     }
189 #endif
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, "");
200 
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>);
205   }
206 
207   {
208     // Can only be constructed via multiple args
209     struct udt3
210     {
211       udt3() = delete;
212       udt3(udt3 &&) = delete;
213       udt3(const udt3 &) = delete;
214       udt3 &operator=(udt3 &&) = delete;
215       udt3 &operator=(const udt3 &) = delete;
udt3udt3216       explicit udt3(int /*unused*/, const char * /*unused*/, std::nullptr_t /*unused*/) {}
217       ~udt3() = default;
218     };
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());
224   }
225 }
226