/* Unit testing for outcomes (C) 2013-2019 Niall Douglas (30 commits) Boost Software License - Version 1.0 - August 17th, 2003 Permission is hereby granted, free of charge, to any person or organization obtaining a copy of the software and accompanying documentation covered by this license (the "Software") to use, reproduce, display, distribute, execute, and transmit the Software, and to prepare derivative works of the Software, and to permit third-parties to whom the Software is furnished to do so, all subject to the following: The copyright notices in the Software and this entire statement, including the above license grant, this restriction and the following disclaimer, must be included in all copies of the Software, in whole or in part, and all derivative works of the Software, unless such copies or derivative works are solely in the form of machine-executable object code generated by a source language processor. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #ifdef TESTING_WG21_EXPERIMENTAL_RESULT #include #define BOOST_OUTCOME_AUTO_TEST_CASE(...) BOOST_AUTO_TEST_CASE(__VA_ARGS__) #else #include #endif #include #include #include #ifndef BOOST_NO_EXCEPTIONS // Custom error type with payload struct payload { boost::system::error_code ec; const char *str{nullptr}; payload() = default; payload(boost::system::errc::errc_t _ec, const char *_str) : ec(make_error_code(_ec)) , str(_str) { } }; struct payload_exception : std::runtime_error { explicit payload_exception(const char *what) : std::runtime_error(what) { } }; inline const boost::system::error_code &make_error_code(const payload &p) { return p.ec; } inline void outcome_throw_as_system_error_with_payload(const payload &p) { throw payload_exception(p.str); } #endif BOOST_OUTCOME_AUTO_TEST_CASE(works_result, "Tests that the result works as intended") { #ifdef TESTING_WG21_EXPERIMENTAL_RESULT using namespace std::experimental; using std::in_place_type; #else using namespace BOOST_OUTCOME_V2_NAMESPACE; #endif static_assert(std::is_constructible, int>::value, "Sanity check that monad can be constructed from a value_type"); static_assert(!std::is_constructible>, int>::value, "Sanity check that outer monad can be constructed from an inner monad's value_type"); static_assert(!std::is_constructible>>, int>::value, "Sanity check that outer monad can be constructed from an inner inner monad's value_type"); static_assert(!std::is_constructible>>>, int>::value, "Sanity check that outer monad can be constructed from an inner inner monad's value_type"); static_assert(std::is_constructible, result>::value, "Sanity check that compatible monads can be constructed from one another"); static_assert(std::is_constructible>, result>::value, "Sanity check that outer monad can be constructed from a compatible monad"); static_assert(!std::is_constructible>>, result>::value, "Sanity check that outer monad can be constructed from a compatible monad up to two nestings deep"); static_assert(!std::is_constructible>>>, result>::value, "Sanity check that outer monad can be constructed from a compatible monad three or more nestings deep"); static_assert(!std::is_constructible, result>::value, "Sanity check that incompatible monads cannot be constructed from one another"); #ifndef TESTING_WG21_EXPERIMENTAL_RESULT static_assert(std::is_constructible, result>::value, "Sanity check that all monads can be constructed from a void monad"); static_assert(std::is_constructible>, result>::value, "Sanity check that outer monad can be constructed from a compatible monad"); static_assert(std::is_constructible>>, result>::value, "Sanity check that outer monad can be constructed from a compatible monad up to two nestings deep"); static_assert(!std::is_constructible, result>::value, "Sanity check that incompatible monads cannot be constructed from one another"); #endif static_assert(std::is_void::value_type>::value, "Sanity check that result has a void value_type"); #ifndef TESTING_WG21_EXPERIMENTAL_RESULT static_assert(std::is_void::error_type>::value, "Sanity check that result has a void error_type"); #endif static_assert(std::is_same::value_type, int>::value, "Sanity check that result has a int value_type"); static_assert(std::is_same::error_type, boost::system::error_code>::value, "Sanity check that result has a error_code error_type"); { // errored int result m(boost::system::errc::bad_address); BOOST_CHECK(!m); BOOST_CHECK(!m.has_value()); BOOST_CHECK(m.has_error()); // BOOST_CHECK(!m.has_exception()); BOOST_CHECK_THROW(m.value(), boost::system::system_error); BOOST_CHECK_NO_THROW(m.error()); } { // errored void result m(boost::system::errc::bad_address); BOOST_CHECK(!m); BOOST_CHECK(!m.has_value()); BOOST_CHECK(m.has_error()); // BOOST_CHECK(!m.has_exception()); #ifndef TESTING_WG21_EXPERIMENTAL_RESULT BOOST_CHECK_THROW(([&m]() -> void { return m.value(); }()), boost::system::system_error); #endif BOOST_CHECK_NO_THROW(m.error()); } { // valued int result m(5); BOOST_CHECK(m); BOOST_CHECK(m.has_value()); BOOST_CHECK(!m.has_error()); // BOOST_CHECK(!m.has_exception()); BOOST_CHECK(m.value() == 5); m.value() = 6; BOOST_CHECK(m.value() == 6); BOOST_CHECK_THROW(m.error(), bad_result_access); } { // valued bool result m(false); BOOST_CHECK(m); BOOST_CHECK(m.has_value()); BOOST_CHECK(!m.has_error()); // BOOST_CHECK(!m.has_exception()); BOOST_CHECK(m.value() == false); m.value() = true; BOOST_CHECK(m.value() == true); BOOST_CHECK_THROW(m.error(), bad_result_access); } { // moves do not clear state result m("niall"); BOOST_CHECK(m); BOOST_CHECK(m.has_value()); BOOST_CHECK(!m.has_error()); // BOOST_CHECK(!m.has_exception()); BOOST_CHECK(m.value() == "niall"); m.value() = "NIALL"; BOOST_CHECK(m.value() == "NIALL"); auto temp(std::move(m).value()); BOOST_CHECK(temp == "NIALL"); BOOST_CHECK(m.value().empty()); // NOLINT } { // valued void result m(in_place_type); BOOST_CHECK(m); BOOST_CHECK(m.has_value()); BOOST_CHECK(!m.has_error()); // BOOST_CHECK(!m.has_exception()); BOOST_CHECK_NO_THROW(m.value()); // works, but type returned is unusable BOOST_CHECK_THROW(m.error(), bad_result_access); } { // errored boost::system::error_code ec(5, boost::system::system_category()); result m(ec); BOOST_CHECK(!m); BOOST_CHECK(!m.has_value()); BOOST_CHECK(m.has_error()); // BOOST_CHECK(!m.has_exception()); BOOST_CHECK_THROW(m.value(), boost::system::system_error); BOOST_CHECK(m.error() == ec); } #if !defined(__APPLE__) || defined(__cpp_exceptions) { // errored, custom boost::system::error_code ec(5, boost::system::system_category()); auto e = boost::copy_exception(boost::system::system_error(ec)); // NOLINT result m(e); BOOST_CHECK(!m); BOOST_CHECK(!m.has_value()); BOOST_CHECK(m.has_error()); // BOOST_CHECK(!m.has_exception()); BOOST_CHECK_THROW(m.value(), boost::system::system_error); BOOST_CHECK(m.error() == e); } #endif #ifndef TESTING_WG21_EXPERIMENTAL_RESULT { // custom error type struct Foo { }; result m(in_place_type); BOOST_CHECK(!m); BOOST_CHECK(!m.has_value()); BOOST_CHECK(m.has_error()); // BOOST_CHECK(!m.has_exception()); // BOOST_CHECK_NO_THROW(m.value()); // BOOST_CHECK_NO_THROW(m.error()); } if(false) // NOLINT { // void, void is permitted, but is not constructible result *m = nullptr; m->value(); m->error(); } #endif { // Deliberately define non-trivial operations struct udt { int _v{0}; udt() = default; udt(udt &&o) noexcept : _v(o._v) {} udt(const udt &o) // NOLINT : _v(o._v) { } udt &operator=(udt &&o) noexcept { _v = o._v; return *this; } udt &operator=(const udt &o) // NOLINT { _v = o._v; return *this; } ~udt() { _v = 0; } }; // No default construction, no copy nor move struct udt2 { udt2() = delete; udt2(udt2 &&) = delete; udt2(const udt2 &) = delete; udt2 &operator=(udt2 &&) = delete; udt2 &operator=(const udt2 &) = delete; explicit udt2(int /*unused*/) {} ~udt2() = default; }; // Can only be constructed via multiple args struct udt3 { udt3() = delete; udt3(udt3 &&) = delete; udt3(const udt3 &) = delete; udt3 &operator=(udt3 &&) = delete; udt3 &operator=(const udt3 &) = delete; explicit udt3(int /*unused*/, const char * /*unused*/, std::nullptr_t /*unused*/) {} ~udt3() = default; }; result a(5); result b(make_error_code(boost::system::errc::invalid_argument)); std::cout << sizeof(a) << std::endl; // 32 bytes if(false) // NOLINT { b.assume_value(); a.assume_error(); } #ifndef BOOST_NO_EXCEPTIONS try { b.value(); std::cerr << "fail" << std::endl; std::terminate(); } catch(const boost::system::system_error & /*unused*/) { } #endif static_assert(!std::is_default_constructible::value, ""); static_assert(!std::is_nothrow_default_constructible::value, ""); static_assert(std::is_copy_constructible::value, ""); // Quality of implementation of std::optional is poor :( #ifndef TESTING_WG21_EXPERIMENTAL_RESULT static_assert(std::is_trivially_copy_constructible::value, ""); static_assert(std::is_nothrow_copy_constructible::value, ""); static_assert(std::is_copy_assignable::value, ""); static_assert(std::is_trivially_copy_assignable::value, ""); static_assert(std::is_nothrow_copy_assignable::value, ""); #endif static_assert(std::is_trivially_destructible::value, ""); static_assert(std::is_nothrow_destructible::value, ""); // Test void compiles result c(in_place_type); result c2(c); (void) c2; // Test a standard udt compiles result d(in_place_type); result d2(d); static_assert(!std::is_default_constructible::value, ""); static_assert(!std::is_nothrow_default_constructible::value, ""); static_assert(std::is_copy_constructible::value, ""); static_assert(!std::is_trivially_copy_constructible::value, ""); static_assert(!std::is_nothrow_copy_constructible::value, ""); static_assert(std::is_copy_assignable::value, ""); static_assert(!std::is_trivially_copy_assignable::value, ""); static_assert(!std::is_nothrow_copy_assignable::value, ""); static_assert(std::is_move_assignable::value, ""); static_assert(!std::is_trivially_move_assignable::value, ""); static_assert(std::is_nothrow_move_assignable::value, ""); static_assert(!std::is_trivially_destructible::value, ""); static_assert(std::is_nothrow_destructible::value, ""); // Test a highly pathological udt compiles result e(in_place_type, 5); // result e2(e); static_assert(!std::is_default_constructible::value, ""); static_assert(!std::is_nothrow_default_constructible::value, ""); static_assert(!std::is_copy_constructible::value, ""); static_assert(!std::is_trivially_copy_constructible::value, ""); static_assert(!std::is_nothrow_copy_constructible::value, ""); static_assert(!std::is_copy_assignable::value, ""); static_assert(!std::is_trivially_copy_assignable::value, ""); static_assert(!std::is_nothrow_copy_assignable::value, ""); static_assert(!std::is_move_assignable::value, ""); static_assert(!std::is_trivially_move_assignable::value, ""); static_assert(!std::is_nothrow_move_assignable::value, ""); // Test a udt which can only be constructed in place compiles result g(in_place_type, 5, static_cast("niall"), nullptr); // Does converting inplace construction also work? result h(5, static_cast("niall"), nullptr); result i(ENOMEM, boost::system::generic_category()); BOOST_CHECK(h.has_value()); BOOST_CHECK(i.has_error()); } // Test direct use of error code enum works { constexpr result a(5), b(boost::system::errc::invalid_argument); static_assert(a.value() == 5, "a is not 5"); static_assert(b.error() == boost::system::errc::invalid_argument, "b is not errored"); BOOST_CHECK_THROW(b.value(), boost::system::system_error); } #ifndef TESTING_WG21_EXPERIMENTAL_RESULT #ifndef BOOST_NO_EXCEPTIONS // Test payload facility { const char *niall = "niall"; result b{boost::system::errc::invalid_argument, niall}; try { b.value(); BOOST_CHECK(false); } catch(const payload_exception &e) { BOOST_CHECK(!strcmp(e.what(), niall)); } catch(...) { BOOST_CHECK(false); } } #endif #endif }