From 19fcec84d8d7d21e796c7624e521b60d28ee21ed Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 7 Apr 2024 20:45:59 +0200 Subject: Adding upstream version 16.2.11+ds. Signed-off-by: Daniel Baumann --- src/boost/libs/outcome/test/Jamfile.v2 | 67 + .../outcome/test/compile-fail/issue0071-fail.cpp | 37 + .../test/compile-fail/outcome-int-int-1.cpp | 33 + .../outcome/test/compile-fail/result-int-int-1.cpp | 33 + .../outcome/test/compile-fail/result-int-int-2.cpp | 33 + src/boost/libs/outcome/test/expected-pass.cpp | 2015 ++++++++++++++++++++ src/boost/libs/outcome/test/tests/comparison.cpp | 102 + src/boost/libs/outcome/test/tests/constexpr.cpp | 118 ++ src/boost/libs/outcome/test/tests/containers.cpp | 58 + src/boost/libs/outcome/test/tests/core-outcome.cpp | 258 +++ src/boost/libs/outcome/test/tests/core-result.cpp | 370 ++++ .../libs/outcome/test/tests/coroutine-support.cpp | 120 ++ .../outcome/test/tests/default-construction.cpp | 57 + .../tests/experimental-core-outcome-status.cpp | 225 +++ .../test/tests/experimental-core-result-status.cpp | 357 ++++ .../outcome/test/tests/experimental-p0709a.cpp | 169 ++ src/boost/libs/outcome/test/tests/fileopen.cpp | 87 + src/boost/libs/outcome/test/tests/hooks.cpp | 114 ++ src/boost/libs/outcome/test/tests/issue0007.cpp | 81 + src/boost/libs/outcome/test/tests/issue0009.cpp | 52 + src/boost/libs/outcome/test/tests/issue0010.cpp | 88 + src/boost/libs/outcome/test/tests/issue0012.cpp | 59 + src/boost/libs/outcome/test/tests/issue0016.cpp | 56 + src/boost/libs/outcome/test/tests/issue0059.cpp | 60 + src/boost/libs/outcome/test/tests/issue0061.cpp | 128 ++ src/boost/libs/outcome/test/tests/issue0064.cpp | 50 + src/boost/libs/outcome/test/tests/issue0065.cpp | 66 + src/boost/libs/outcome/test/tests/issue0071.cpp | 45 + src/boost/libs/outcome/test/tests/issue0095.cpp | 58 + src/boost/libs/outcome/test/tests/issue0115.cpp | 57 + src/boost/libs/outcome/test/tests/issue0116.cpp | 45 + src/boost/libs/outcome/test/tests/issue0140.cpp | 96 + src/boost/libs/outcome/test/tests/issue0182.cpp | 42 + src/boost/libs/outcome/test/tests/issue0203.cpp | 103 + src/boost/libs/outcome/test/tests/issue0210.cpp | 78 + src/boost/libs/outcome/test/tests/issue0220.cpp | 55 + .../outcome/test/tests/noexcept-propagation.cpp | 84 + src/boost/libs/outcome/test/tests/propagate.cpp | 90 + .../libs/outcome/test/tests/serialisation.cpp | 49 + .../libs/outcome/test/tests/success-failure.cpp | 69 + src/boost/libs/outcome/test/tests/swap.cpp | 432 +++++ src/boost/libs/outcome/test/tests/udts.cpp | 154 ++ .../libs/outcome/test/tests/value-or-error.cpp | 54 + 43 files changed, 6404 insertions(+) create mode 100644 src/boost/libs/outcome/test/Jamfile.v2 create mode 100644 src/boost/libs/outcome/test/compile-fail/issue0071-fail.cpp create mode 100644 src/boost/libs/outcome/test/compile-fail/outcome-int-int-1.cpp create mode 100644 src/boost/libs/outcome/test/compile-fail/result-int-int-1.cpp create mode 100644 src/boost/libs/outcome/test/compile-fail/result-int-int-2.cpp create mode 100644 src/boost/libs/outcome/test/expected-pass.cpp create mode 100644 src/boost/libs/outcome/test/tests/comparison.cpp create mode 100644 src/boost/libs/outcome/test/tests/constexpr.cpp create mode 100644 src/boost/libs/outcome/test/tests/containers.cpp create mode 100644 src/boost/libs/outcome/test/tests/core-outcome.cpp create mode 100644 src/boost/libs/outcome/test/tests/core-result.cpp create mode 100644 src/boost/libs/outcome/test/tests/coroutine-support.cpp create mode 100644 src/boost/libs/outcome/test/tests/default-construction.cpp create mode 100644 src/boost/libs/outcome/test/tests/experimental-core-outcome-status.cpp create mode 100644 src/boost/libs/outcome/test/tests/experimental-core-result-status.cpp create mode 100644 src/boost/libs/outcome/test/tests/experimental-p0709a.cpp create mode 100644 src/boost/libs/outcome/test/tests/fileopen.cpp create mode 100644 src/boost/libs/outcome/test/tests/hooks.cpp create mode 100644 src/boost/libs/outcome/test/tests/issue0007.cpp create mode 100644 src/boost/libs/outcome/test/tests/issue0009.cpp create mode 100644 src/boost/libs/outcome/test/tests/issue0010.cpp create mode 100644 src/boost/libs/outcome/test/tests/issue0012.cpp create mode 100644 src/boost/libs/outcome/test/tests/issue0016.cpp create mode 100644 src/boost/libs/outcome/test/tests/issue0059.cpp create mode 100644 src/boost/libs/outcome/test/tests/issue0061.cpp create mode 100644 src/boost/libs/outcome/test/tests/issue0064.cpp create mode 100644 src/boost/libs/outcome/test/tests/issue0065.cpp create mode 100644 src/boost/libs/outcome/test/tests/issue0071.cpp create mode 100644 src/boost/libs/outcome/test/tests/issue0095.cpp create mode 100644 src/boost/libs/outcome/test/tests/issue0115.cpp create mode 100644 src/boost/libs/outcome/test/tests/issue0116.cpp create mode 100644 src/boost/libs/outcome/test/tests/issue0140.cpp create mode 100644 src/boost/libs/outcome/test/tests/issue0182.cpp create mode 100644 src/boost/libs/outcome/test/tests/issue0203.cpp create mode 100644 src/boost/libs/outcome/test/tests/issue0210.cpp create mode 100644 src/boost/libs/outcome/test/tests/issue0220.cpp create mode 100644 src/boost/libs/outcome/test/tests/noexcept-propagation.cpp create mode 100644 src/boost/libs/outcome/test/tests/propagate.cpp create mode 100644 src/boost/libs/outcome/test/tests/serialisation.cpp create mode 100644 src/boost/libs/outcome/test/tests/success-failure.cpp create mode 100644 src/boost/libs/outcome/test/tests/swap.cpp create mode 100644 src/boost/libs/outcome/test/tests/udts.cpp create mode 100644 src/boost/libs/outcome/test/tests/value-or-error.cpp (limited to 'src/boost/libs/outcome/test') diff --git a/src/boost/libs/outcome/test/Jamfile.v2 b/src/boost/libs/outcome/test/Jamfile.v2 new file mode 100644 index 000000000..4b11a3805 --- /dev/null +++ b/src/boost/libs/outcome/test/Jamfile.v2 @@ -0,0 +1,67 @@ +# Boost.Outcome Library test Jamfile +# +# Copyright (C) 2017-2019 Niall Douglas +# +# Use, modification, and distribution is subject to the Boost Software +# License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at +# http://www.boost.org/LICENSE_1_0.txt) +# +# See http://www.boost.org/libs/outcome for documentation. + +import testing ; +import ../../config/checks/config : requires ; +import ../../predef/tools/check/predef : check require : predef-check predef-require ; + +project + : requirements + [ requires cxx14_variable_templates cxx14_constexpr ] + [ predef-require "!BOOST_COMP_GNUC" or "BOOST_COMP_GNUC >= 6.0" ] + [ predef-require "!BOOST_COMP_CLANG" or "BOOST_COMP_CLANG >= 4.0" ] + BOOST_TEST_MODULE=Outcome + ../../test/build//boost_unit_test_framework + ; + +{ + test-suite outcome : + + [ compile-fail compile-fail/outcome-int-int-1.cpp ] + [ compile-fail compile-fail/result-int-int-1.cpp ] + [ compile-fail compile-fail/result-int-int-2.cpp ] + + [ run tests/comparison.cpp ] + [ run tests/constexpr.cpp ] + [ run tests/containers.cpp ] + [ run tests/core-outcome.cpp ] + [ run tests/core-result.cpp ] + [ run tests/default-construction.cpp ] + [ run tests/experimental-core-outcome-status.cpp ] + [ run tests/experimental-core-result-status.cpp ] + [ run tests/experimental-p0709a.cpp ] + [ run tests/fileopen.cpp ] + [ run tests/hooks.cpp ] + [ run tests/issue0007.cpp ] + [ run tests/issue0009.cpp ] + [ run tests/issue0010.cpp ] + [ run tests/issue0012.cpp ] + [ run tests/issue0016.cpp ] + [ run tests/issue0059.cpp ] + [ run tests/issue0061.cpp ] + [ run tests/issue0064.cpp ] + [ run tests/issue0065.cpp ] + [ run tests/issue0071.cpp ] + [ run tests/issue0095.cpp ] + [ run tests/issue0115.cpp ] + [ run tests/issue0116.cpp ] + [ run tests/issue0140.cpp ] + [ run tests/noexcept-propagation.cpp ] + [ run tests/propagate.cpp ] + [ run tests/serialisation.cpp ] + [ run tests/success-failure.cpp ] + [ run tests/swap.cpp ] + [ run tests/udts.cpp ] + [ run tests/value-or-error.cpp ] + + [ run expected-pass.cpp ] + + ; +} diff --git a/src/boost/libs/outcome/test/compile-fail/issue0071-fail.cpp b/src/boost/libs/outcome/test/compile-fail/issue0071-fail.cpp new file mode 100644 index 000000000..ac94c7138 --- /dev/null +++ b/src/boost/libs/outcome/test/compile-fail/issue0071-fail.cpp @@ -0,0 +1,37 @@ +/* clang-format off +(error: no matching function for call to .+::basic_result|error: no matching constructor for initialization of 'result'|cannot convert argument 1 from 'int') +clang-format on + + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License in the accompanying file +Licence.txt or at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. + + +Distributed under the Boost Software License, Version 1.0. +(See accompanying file Licence.txt or copy at +http://www.boost.org/LICENSE_1_0.txt) +*/ + +#include "../../include/boost/outcome/result.hpp" + +int main() +{ + using namespace BOOST_OUTCOME_V2_NAMESPACE; + struct udt + { + explicit udt(int /*unused*/) {} + }; + // Must not be possible to implicitly initialise a result + result m(5); + return 0; +} diff --git a/src/boost/libs/outcome/test/compile-fail/outcome-int-int-1.cpp b/src/boost/libs/outcome/test/compile-fail/outcome-int-int-1.cpp new file mode 100644 index 000000000..4ee66b746 --- /dev/null +++ b/src/boost/libs/outcome/test/compile-fail/outcome-int-int-1.cpp @@ -0,0 +1,33 @@ +/* clang-format off +(use of deleted function|call to deleted constructor|attempting to reference a deleted function) +clang-format on + + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License in the accompanying file +Licence.txt or at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. + + +Distributed under the Boost Software License, Version 1.0. +(See accompanying file Licence.txt or copy at +http://www.boost.org/LICENSE_1_0.txt) +*/ + +#include "../../include/boost/outcome/outcome.hpp" + +int main() +{ + using namespace BOOST_OUTCOME_V2_NAMESPACE; + // Must not be possible to initialise an outcome with same R, S and P types + outcome m(5); + return 0; +} diff --git a/src/boost/libs/outcome/test/compile-fail/result-int-int-1.cpp b/src/boost/libs/outcome/test/compile-fail/result-int-int-1.cpp new file mode 100644 index 000000000..e4eb92031 --- /dev/null +++ b/src/boost/libs/outcome/test/compile-fail/result-int-int-1.cpp @@ -0,0 +1,33 @@ +/* clang-format off +(use of deleted function|call to deleted constructor|attempting to reference a deleted function) +clang-format on + + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License in the accompanying file +Licence.txt or at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. + + +Distributed under the Boost Software License, Version 1.0. +(See accompanying file Licence.txt or copy at +http://www.boost.org/LICENSE_1_0.txt) +*/ + +#include "../../include/boost/outcome/result.hpp" + +int main() +{ + using namespace BOOST_OUTCOME_V2_NAMESPACE; + // Must not be possible to initialise a result with same R and S types + result m(5); + return 0; +} diff --git a/src/boost/libs/outcome/test/compile-fail/result-int-int-2.cpp b/src/boost/libs/outcome/test/compile-fail/result-int-int-2.cpp new file mode 100644 index 000000000..35c999d0a --- /dev/null +++ b/src/boost/libs/outcome/test/compile-fail/result-int-int-2.cpp @@ -0,0 +1,33 @@ +/* clang-format off +(use of deleted function|call to deleted constructor|attempting to reference a deleted function) +clang-format on + + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License in the accompanying file +Licence.txt or at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. + + +Distributed under the Boost Software License, Version 1.0. +(See accompanying file Licence.txt or copy at +http://www.boost.org/LICENSE_1_0.txt) +*/ + +#include "../../include/boost/outcome/result.hpp" + +int main() +{ + using namespace BOOST_OUTCOME_V2_NAMESPACE; + // Must not be possible to initialise a result with same R and S types + result m(in_place_type); + return 0; +} diff --git a/src/boost/libs/outcome/test/expected-pass.cpp b/src/boost/libs/outcome/test/expected-pass.cpp new file mode 100644 index 000000000..5c63b9118 --- /dev/null +++ b/src/boost/libs/outcome/test/expected-pass.cpp @@ -0,0 +1,2015 @@ +//! \file test_expected.cpp + +// Copyright Pierre Talbot 2013. +// Copyright Vicente J. Botet Escriba 2013,2014. + +// Use, modification and distribution are subject to the +// Boost Software License, Version 1.0. +//(See accompanying file LICENSE_1_0.txt +// or copy at http://www.boost.org/LICENSE_1_0.txt) + + +// Notes by ned: +// Original is at https://github.com/viboes/std-make/blob/master/test/expected/expected_pass.cpp +// This edition modified to use result with throw_bad_result_access policy +// Quite a lot of the test suite I had to disable, not because our Expected implementation is +// incorrect, but because the reference test suite is testing an Expected quite far away from +// the latest WG21 proposal paper, and we're implementing that latest edition. + +#if !defined(__GNUC__) || defined(__clang__) || __GNUC__ >= 7 + +#include + +#include +#include + +#define QUICKCPPLIB_BOOST_UNIT_TEST_CUSTOM_MAIN_DEFINED +#include +#include + +#define JASEL_NORETURN +#ifndef BOOST_TEST +#define BOOST_TEST(expr) BOOST_CHECK(expr) +#endif +#ifndef BOOST_TEST_EQ +#define BOOST_TEST_EQ(a, b) BOOST_CHECK_EQUAL((a), (b)) +#endif +#ifndef BOOST_TEST_THROWS +#define BOOST_TEST_THROWS(expr, ex) BOOST_CHECK_THROW((expr), ex) +#endif +#ifndef BOOST_CONSTEXPR +#define BOOST_CONSTEXPR constexpr +#endif + +#ifdef _MSC_VER +#pragma warning(disable : 4127) // conditional expression is constant +#pragma warning(disable : 4244) // conversion from int to short +#endif + +namespace stde +{ +#if __cplusplus >= 201700 || _HAS_CXX17 + using in_place_t = std::in_place_t; + using std::in_place; +#else + struct in_place_t + { + explicit in_place_t() = default; + }; + constexpr in_place_t in_place{}; +#endif + + //! [expected_implementation] + /* Here is a fairly conforming implementation of P0323R3 `expected` using `checked`. + It passes the reference test suite for P0323R3 at + https://github.com/viboes/std-make/blob/master/test/expected/expected_pass.cpp with modifications + only to move the test much closer to the P0323R3 Expected, as the reference test suite is for a + much older proposed Expected. + + Known differences from P0323R3 in this implementation: + - `T` and `E` cannot be the same type. + - No variant storage is implemented. + */ + + namespace detail + { + template using expected_result = BOOST_OUTCOME_V2_NAMESPACE::checked; + template struct enable_default_constructor : public expected_result + { + using base = expected_result; + using base::base; + constexpr enable_default_constructor() + : base{BOOST_OUTCOME_V2_NAMESPACE::in_place_type} + { + } + }; + template using select_expected_base = std::conditional_t::value, enable_default_constructor, expected_result>; + } // namespace detail + template class expected : public detail::select_expected_base + { + static_assert(!std::is_same::value, "T and E cannot be the same in this expected implementation"); + using base = detail::select_expected_base; + + public: + // Inherit base's constructors + using base::base; + expected() = default; + + // Expected takes in_place not in_place_type + template + constexpr explicit expected(in_place_t /*unused*/, Args &&... args) + : base{BOOST_OUTCOME_V2_NAMESPACE::in_place_type, std::forward(args)...} + { + } + + // Expected always accepts a T even if ambiguous + BOOST_OUTCOME_TEMPLATE(class U) + BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(std::is_constructible::value)) + constexpr expected(U &&v) // NOLINT + : base{BOOST_OUTCOME_V2_NAMESPACE::in_place_type, std::forward(v)} + { + } + + // Expected has an emplace() modifier + template void emplace(Args &&... args) { *static_cast(this) = base{BOOST_OUTCOME_V2_NAMESPACE::in_place_type, std::forward(args)...}; } + + // Expected has a narrow operator* and operator-> + constexpr const T &operator*() const & { return base::assume_value(); } + constexpr T &operator*() & { return base::assume_value(); } + constexpr const T &&operator*() const && { return base::assume_value(); } + constexpr T &&operator*() && { return base::assume_value(); } + constexpr const T *operator->() const { return &base::assume_value(); } + constexpr T *operator->() { return &base::assume_value(); } + + // Expected has a narrow error() observer + constexpr const E &error() const & { return base::assume_error(); } + constexpr E &error() & { return base::assume_error(); } + constexpr const E &&error() const && { return base::assume_error(); } + constexpr E &error() && { return base::assume_error(); } + }; + template class expected : public BOOST_OUTCOME_V2_NAMESPACE::result> + { + using base = BOOST_OUTCOME_V2_NAMESPACE::result>; + + public: + // Inherit base constructors + using base::base; + + // Expected has a narrow operator* and operator-> + constexpr void operator*() const { base::assume_value(); } + constexpr void operator->() const { base::assume_value(); } + }; + template using unexpected = BOOST_OUTCOME_V2_NAMESPACE::failure_type; + template unexpected make_unexpected(E &&arg) { return BOOST_OUTCOME_V2_NAMESPACE::failure(std::forward(arg)); } + template unexpected make_unexpected(Args &&... args) { return BOOST_OUTCOME_V2_NAMESPACE::failure(std::forward(args)...); } + template using bad_expected_access = BOOST_OUTCOME_V2_NAMESPACE::bad_result_access_with; + //! [expected_implementation] + + // Not actually part of the Expected proposal, but needed to pass the test + template using exception_or = expected; +} // namespace stde + +template using expected_sc = stde::expected; + +struct NoDefaultConstructible +{ + NoDefaultConstructible() = delete; + NoDefaultConstructible(int /*unused*/) {} // NOLINT +}; + +struct NoCopyConstructible +{ + NoCopyConstructible() = default; + NoCopyConstructible(NoCopyConstructible const &) = delete; + NoCopyConstructible(NoCopyConstructible &&) noexcept = default; +}; +struct NoMoveConstructible +{ + NoMoveConstructible() = default; + NoMoveConstructible(NoMoveConstructible const &) noexcept = default; + NoMoveConstructible(NoMoveConstructible &&) = delete; + NoMoveConstructible &operator=(NoMoveConstructible const &) noexcept = default; + NoMoveConstructible &operator=(NoMoveConstructible &&) = delete; +}; + +enum State +{ + sDefaultConstructed, + sValueCopyConstructed, + sValueMoveConstructed, + sCopyConstructed, + sMoveConstructed, + sMoveAssigned, + sCopyAssigned, + sValueCopyAssigned, + sValueMoveAssigned, + sMovedFrom, + sValueConstructed +}; + +struct OracleVal +{ + State s{sValueConstructed}; + int i; + constexpr OracleVal(int i_ = 0) // NOLINT + : i(i_) + { + } +}; + +struct Oracle +{ + State s{sDefaultConstructed}; + OracleVal val; + + Oracle() = default; + Oracle(const OracleVal &v) // NOLINT + : s(sValueCopyConstructed), + val(v) + { + } + Oracle(OracleVal &&v) noexcept : s(sValueMoveConstructed), val(v) { v.s = sMovedFrom; } // NOLINT + Oracle(const Oracle &o) + : s(sCopyConstructed) + , val(o.val) + { + } + Oracle(Oracle &&o) noexcept : s(sMoveConstructed), val(std::move(o.val)) { o.s = sMovedFrom; } // NOLINT + + Oracle &operator=(const OracleVal &v) + { + s = sValueCopyConstructed; + val = v; + return *this; + } + Oracle &operator=(OracleVal &&v) noexcept + { + s = sValueMoveConstructed; + val = std::move(v); // NOLINT + v.s = sMovedFrom; + return *this; + } + Oracle &operator=(const Oracle &o) + { + s = sCopyConstructed; + val = o.val; + return *this; + } + Oracle &operator=(Oracle &&o) noexcept + { + s = sMoveConstructed; + val = std::move(o.val); // NOLINT + o.s = sMovedFrom; + return *this; + } +}; + +struct Guard +{ + std::string val; + Guard() = default; + explicit Guard(std::string s, int /*unused*/ = 0) + : val(std::move(s)) + { + } + Guard(const Guard &) = delete; + Guard(Guard &&) = delete; + void operator=(const Guard &) = delete; + void operator=(Guard &&) = delete; +}; + +struct ExplicitStr +{ + std::string s; + explicit ExplicitStr(const char *chp) + : s(chp) + { + } +}; + +struct Date +{ + int i; + Date() = delete; + Date(int i_) noexcept : i{i_} {} // NOLINT + Date(Date &&d) noexcept : i(d.i) { d.i = 0; } + Date(const Date &) = delete; + Date &operator=(const Date &) = delete; + Date &operator=(Date &&d) noexcept + { + i = d.i; + d.i = 0; + return *this; + } +}; + +struct TExcept +{ + int i; + TExcept() = delete; + TExcept(int i_) // NOLINT + : i{i_} + { + } + TExcept(TExcept &&d) + : i(d.i) + { + d.i = 0; + } + TExcept(const TExcept &) = delete; + TExcept &operator=(const TExcept &) = delete; + TExcept &operator=(TExcept &&d) + { + i = d.i; + d.i = 0; + return *this; + } +}; + +template struct MoveAware +{ + T val; + bool moved; + MoveAware(T val_) // NOLINT + : val(val_), + moved(false) + { + } + MoveAware(MoveAware const &) = delete; + MoveAware(MoveAware &&rhs) + : val(rhs.val) + , moved(rhs.moved) + { + rhs.moved = true; + } + MoveAware &operator=(MoveAware const &) = delete; + MoveAware &operator=(MoveAware &&rhs) + { + val = (rhs.val); + moved = (rhs.moved); + rhs.moved = true; + return *this; + } +}; + +struct OverloadedAddressOf +{ + OverloadedAddressOf() = default; + OverloadedAddressOf *operator&() const { return nullptr; } +}; + +// using namespace boost; +// using namespace boost::functional; + +class test_exception : public std::exception +{ +}; + +int throwing_fun() +{ + throw test_exception(); +} +int nothrowing_fun() +{ + return 4; +} + +JASEL_NORETURN void void_throwing_fun() +{ + throw test_exception(); +} +void do_nothing_fun() +{ +} + +void except_default_constructor() +{ + // From value constructor. + expected_sc e{}; + try + { + int i = e.value(); + (void) i; + BOOST_TEST(true); + } + catch(...) + { + BOOST_TEST(false); + }; + BOOST_TEST(e.has_value()); + BOOST_TEST(e); + BOOST_TEST(static_cast(e)); +} + + +void except_default_constructor_error_code() +{ + // From value constructor. + stde::expected e; + BOOST_TEST(e.has_value()); + BOOST_TEST(e); + BOOST_TEST(static_cast(e)); +} + +void except_default_constructor_constexpr() +{ + // From value constructor. + BOOST_CONSTEXPR stde::expected e; + BOOST_TEST(e.has_value()); +} + +void expected_from_value() +{ + // using T = int; + using E = std::error_code; + + // static_assert(noexcept(stde::adl::swap_impl(std::declval(), std::declval())), ""); + static_assert(std::is_nothrow_copy_constructible::value, ""); + // static_assert(noexcept(stde::adl::swap_impl(std::declval(), std::declval())), ""); + + static_assert(std::is_nothrow_copy_constructible>::value, ""); + static_assert(std::is_nothrow_copy_assignable>::value, ""); + static_assert(std::is_nothrow_move_constructible>::value, ""); + static_assert(std::is_nothrow_move_assignable>::value, ""); + + // From value constructor. + expected_sc e(5); + // BOOST_REQUIRE_NO_THROW(e.value()); + BOOST_TEST_EQ(e.value(), 5); + BOOST_TEST_EQ(*e, 5); + BOOST_TEST(e.has_value()); + BOOST_TEST(static_cast(e)); +} + +void expected_from_value2() +{ + // From value constructor. + expected_sc e(5); + e = {}; + BOOST_TEST(e.has_value()); + BOOST_TEST_EQ(e.value(), 0); +} + +void expected_from_cnv_value() +{ + OracleVal v; + expected_sc e(v); + // BOOST_REQUIRE_NO_THROW(e.value()); + BOOST_TEST(!!e); + BOOST_TEST(e.has_value()); + BOOST_TEST(bool(e)); + BOOST_TEST_EQ(e.value().s, sValueCopyConstructed); + BOOST_TEST_EQ(v.s, sValueConstructed); + + expected_sc e2(std::move(v)); // NOLINT + // BOOST_REQUIRE_NO_THROW(e2.value()); + BOOST_TEST(!!e2); + BOOST_TEST(e2.has_value()); + BOOST_TEST(bool(e2)); + BOOST_TEST_EQ(e2.value().s, sValueMoveConstructed); + BOOST_TEST_EQ(v.s, sMovedFrom); +} + +struct NDCE // no default constructor +{ // (no default date exists) + explicit NDCE(int /*unused*/) {} +}; + +void except_constructor_NDCE() +{ + expected_sc e{NDCE{1}}; + BOOST_TEST(e.has_value()); +} +struct NDC // no default constructor +{ // (no default date exists) + NDC(int /*unused*/) {} // NOLINT +}; + +void except_constructor_NDC() +{ + static_assert(std::is_nothrow_copy_constructible>::value, ""); + static_assert(std::is_nothrow_copy_assignable>::value, ""); + static_assert(std::is_nothrow_move_constructible>::value, ""); + static_assert(std::is_nothrow_move_assignable>::value, ""); + expected_sc e{1}; + BOOST_TEST(e.has_value()); +} + +void except_constructor_Date() +{ + static_assert(std::is_nothrow_move_constructible>::value, ""); + static_assert(std::is_nothrow_move_assignable>::value, ""); + expected_sc e{Date{1}}; + BOOST_TEST(e.has_value()); +} + +void except_constructor_TExcept() +{ + static_assert(!std::is_nothrow_move_constructible>::value, ""); + static_assert(!std::is_nothrow_move_assignable>::value, ""); + expected_sc e{TExcept{1}}; + BOOST_TEST(e.has_value()); +} + +void expected_from_in_place_value() +{ + OracleVal v; + expected_sc e{stde::in_place, v}; + // BOOST_REQUIRE_NO_THROW(e.value()); + BOOST_TEST(!!e); + BOOST_TEST(e.has_value()); + BOOST_TEST(bool(e)); + BOOST_TEST_EQ(e.value().s, sValueCopyConstructed); + BOOST_TEST_EQ(v.s, sValueConstructed); + + expected_sc e2{stde::in_place, std::move(v)}; // NOLINT + // BOOST_REQUIRE_NO_THROW(e2.value()); + BOOST_TEST(!!e2); + BOOST_TEST(e2.has_value()); + BOOST_TEST(bool(e2)); + BOOST_TEST_EQ(e2.value().s, sValueMoveConstructed); + BOOST_TEST_EQ(v.s, sMovedFrom); +} + +#if 0 +void expected_from_exception() +{ + // From stde::unexpected constructor. + stde::exception_or e(stde::make_unexpected(test_exception())); + BOOST_TEST_THROWS(e.value(), test_exception); + BOOST_TEST_EQ(e.has_value(), false); + BOOST_TEST_EQ(static_cast(e), false); +} +#endif + +void expected_from_copy_value() +{ + // From copy constructor. + expected_sc ef(5); + expected_sc e(ef); + // BOOST_REQUIRE_NO_THROW(e.value()); + BOOST_TEST_EQ(e.value(), 5); + BOOST_TEST_EQ(*e, 5); + BOOST_TEST(e.has_value()); + BOOST_TEST(static_cast(e)); +} + +#if 0 +void expected_from_copy_exception() +{ + // From stde::unexpected constructor. + stde::exception_or ef(stde::make_unexpected(test_exception())); + stde::exception_or e(ef); + BOOST_TEST_THROWS(e.value(), test_exception); + BOOST_TEST_EQ(e.has_value(), false); + BOOST_TEST_EQ(static_cast(e), false); +} +#endif + +void expected_from_in_place() +{ + // From stde::in_place constructor. + expected_sc e(stde::in_place, "stde::in_place"); + // BOOST_REQUIRE_NO_THROW(e.value()); + BOOST_TEST_EQ(e.value(), "stde::in_place"); + BOOST_TEST_EQ(*e, "stde::in_place"); + BOOST_TEST(e.has_value()); + BOOST_TEST(static_cast(e)); +} + +#if 0 +void expected_from_exception_ptr() +{ + // From exception_ptr constructor. + stde::exception_or e(stde::make_unexpected(std::make_exception_ptr(test_exception()))); + BOOST_TEST_THROWS(e.value(), test_exception); + BOOST_TEST_EQ(e.has_value(), false); + BOOST_TEST_EQ(static_cast(e), false); +} +#endif + +void expected_from_moved_value() +{ + // From move value constructor. + std::string value = "my value"; + expected_sc e = std::move(value); + // BOOST_REQUIRE_NO_THROW(e.value()); + BOOST_TEST_EQ(e.value(), "my value"); + BOOST_TEST_EQ(*e, "my value"); + BOOST_TEST(e.has_value()); + BOOST_TEST(static_cast(e)); +} + +void expected_from_catch_block() +{ + // From catch block + try + { + throw test_exception(); + } + catch(...) + { + stde::exception_or e(stde::make_unexpected(std::current_exception())); + + BOOST_TEST_THROWS(e.value(), std::exception); + BOOST_TEST_EQ(e.has_value(), false); + BOOST_TEST_EQ(static_cast(e), false); + } +} + + +void make_expected_E_from_value() +{ + // auto e = stde::make_expected( 5 ); + // BOOST_TEST_EQ(e.has_value(), false); +} +void make_expected_const_from_value() +{ +#if defined __clang__ && __clang_major__ >= 4 && __cplusplus > 201402L + const int i = 0; + auto e = expected_sc(i); + (void) e; +// static_assert(std::is_same>::value, ""); +#endif +} +void make_expected_from_U_value() +{ + expected_sc e = expected_sc(short(5)); + static_assert(std::is_same>{}, ""); + BOOST_TEST_EQ(e.has_value(), true); +} +void make_expected_from_U_value2() +{ + expected_sc e = expected_sc("aa"); + static_assert(std::is_same>{}, ""); + BOOST_TEST_EQ(e.has_value(), true); +} + +void expected_from_value_error_condition() +{ + // From value constructor. + stde::expected e(5); + // BOOST_REQUIRE_NO_THROW(e.value()); + BOOST_TEST_EQ(e.value(), 5); + BOOST_TEST_EQ(*e, 5); + BOOST_TEST(e.has_value()); + BOOST_TEST(static_cast(e)); +} + +void expected_from_error_error_condition() +{ + // From stde::unexpected constructor. + stde::expected e(stde::make_unexpected(std::make_error_condition(std::errc::invalid_argument))); + auto error_from_except_check = [](const stde::bad_expected_access &except) { return std::errc(except.error().value()) == std::errc::invalid_argument; }; + try + { + (void) e.value(); + } + catch(stde::bad_expected_access &ex) + { + BOOST_TEST(error_from_except_check(ex)); + } + BOOST_TEST_EQ(e.has_value(), false); + BOOST_TEST_EQ(static_cast(e), false); +} + + +void expected_from_error_convertible() +{ + { + stde::expected e1 = stde::make_unexpected(1); + stde::expected e2(e1); + BOOST_TEST_EQ(e2.has_value(), false); + BOOST_TEST_EQ(static_cast(e2), false); + BOOST_TEST_EQ(e2.error(), 1); + } + { + stde::expected e1 = stde::make_unexpected(1); + stde::expected e2(e1); + BOOST_TEST_EQ(e2.has_value(), false); + BOOST_TEST_EQ(static_cast(e2), false); + BOOST_TEST_EQ(e2.error(), 1); + } +} + +void except_valid_constexpr_int() +{ + // From value constructor. + BOOST_CONSTEXPR stde::expected e; + BOOST_CONSTEXPR bool b = e.has_value(); + BOOST_TEST(b); +} +void except_value_constexpr_int() +{ + // From value constructor. + BOOST_CONSTEXPR stde::expected e(1); + BOOST_CONSTEXPR int x = e.value(); + BOOST_TEST_EQ(x, 1); +} + +void expected_from_value3() +{ + expected_sc e(5); + BOOST_TEST_EQ(e.value(), 5); + + // From value assignment. + e = 8; + // BOOST_REQUIRE_NO_THROW(e.value()); + BOOST_TEST_EQ(e.value(), 8); + BOOST_TEST_EQ(*e, 8); + BOOST_TEST(e.has_value()); + BOOST_TEST(static_cast(e)); +} + +void expected_from_copy_expected() +{ + expected_sc e(5); + expected_sc e2(8); + + // From value assignment. + e = e2; + // BOOST_REQUIRE_NO_THROW(e.value()); + BOOST_TEST_EQ(e.value(), 8); + BOOST_TEST_EQ(*e, 8); + BOOST_TEST(e.has_value()); + BOOST_TEST(static_cast(e)); +} + +void expected_from_moved_expected() +{ + expected_sc e("e"); + expected_sc e2("e2"); + + // From value assignment. + e = std::move(e2); + // BOOST_REQUIRE_NO_THROW(e.value()); + BOOST_TEST_EQ(e.value(), "e2"); + BOOST_TEST_EQ(*e, "e2"); + BOOST_TEST(e.has_value()); + BOOST_TEST(static_cast(e)); + +// BOOST_REQUIRE_NO_THROW(e2.value()); +#ifndef __GLIBBOOST_OUTCOME_C__ + BOOST_TEST_EQ(e2.value(), ""); + BOOST_TEST_EQ(*e2, ""); +#endif + BOOST_TEST(e2.has_value()); + BOOST_TEST(static_cast(e2)); +} + +void expected_from_in_place2() +{ + // From stde::in_place constructor. + expected_sc e(stde::in_place, "stde::in_place"); + BOOST_TEST_EQ(e.value(), "stde::in_place"); + + // From emplace method. + e.emplace("emplace method"); + // BOOST_REQUIRE_NO_THROW(e.value()); + BOOST_TEST_EQ(e.value(), "emplace method"); + BOOST_TEST_EQ(*e, "emplace method"); + BOOST_TEST(e.has_value()); + BOOST_TEST(static_cast(e)); +} + +void expected_from_move_value() +{ + expected_sc e("v"); + + std::string value = "my value"; + // From assignment operator. + e = std::move(value); + // BOOST_REQUIRE_NO_THROW(e.value()); + BOOST_TEST_EQ(e.value(), "my value"); + BOOST_TEST_EQ(*e, "my value"); + BOOST_TEST(e.has_value()); + BOOST_TEST(static_cast(e)); +} + + +void expected_from_in_place3() +{ + // From stde::in_place factory. + // auto e = stde::make_expected("stde::in_place"); + auto e = expected_sc("stde::in_place"); + // BOOST_REQUIRE_NO_THROW(e.value()); + BOOST_TEST_EQ(e.value(), "stde::in_place"); + BOOST_TEST_EQ(*e, "stde::in_place"); + BOOST_TEST(e.has_value()); + BOOST_TEST(static_cast(e)); +} +void expected_from_in_place_error() +{ + // From stde::in_place factory. + auto e = stde::expected("stde::in_place"); + // BOOST_REQUIRE_NO_THROW(e.value()); + BOOST_TEST_EQ(e.value(), "stde::in_place"); + BOOST_TEST_EQ(*e, "stde::in_place"); + BOOST_TEST(e.has_value()); + BOOST_TEST(static_cast(e)); +} + +void expected_from_exception_catch() +{ + // From catch block + try + { + throw test_exception(); + } + catch(...) + { + stde::exception_or e = stde::make_unexpected(std::current_exception()); + + BOOST_TEST_THROWS(e.value(), std::exception); + BOOST_TEST_EQ(e.has_value(), false); + BOOST_TEST_EQ(static_cast(e), false); + } +} + +#if 0 +void expected_from_error() +{ + // From stde::unexpected constructor. + auto e = stde::make_expected_from_error(std::make_error_condition(std::errc::invalid_argument)); + auto error_from_except_check = [](const stde::bad_expected_access &except) { return std::errc(except.error().value()) == std::errc::invalid_argument; }; + try + { + (void) e.value(); + } + catch(stde::bad_expected_access &ex) + { + BOOST_TEST(error_from_except_check(ex)); + } + BOOST_TEST_EQ(e.has_value(), false); + BOOST_TEST_EQ(static_cast(e), false); +} + +void expected_from_error_U() +{ + // From stde::unexpected constructor. + auto e = stde::make_expected_from_error(42); + static_assert(std::is_same>{}, ""); + BOOST_TEST_EQ(e.has_value(), false); + BOOST_TEST_EQ(static_cast(e), false); +} + +void expected_from_exception2() +{ + // From stde::unexpected constructor. + auto e = stde::make_expected_from_exception(test_exception()); + // auto e = expected_sc(stde::unexpected<>(test_exception())); + BOOST_TEST_THROWS(e.value(), test_exception ); + BOOST_TEST_EQ(e.has_value(), false); + BOOST_TEST_EQ(static_cast(e), false); +} + +void expected_from_exception_ptr2() +{ + // From exception_ptr constructor. + auto e = stde::exception_or(stde::make_unexpected(test_exception())); + BOOST_TEST_THROWS(e.value(), test_exception ); + BOOST_TEST_EQ(e.has_value(), false); + BOOST_TEST_EQ(static_cast(e), false); +} + +void make_expected_from_call_fun() +{ + try + { + stde::make_expected_from_call(throwing_fun); + BOOST_TEST(true); + } + catch(...) + { + BOOST_TEST(false); + } + stde::exception_or e = stde::make_expected_from_call(throwing_fun); + BOOST_TEST_THROWS(e.value(), std::exception ); + BOOST_TEST_EQ(e.has_value(), false); + BOOST_TEST_EQ(static_cast(e), false); + + e = stde::make_expected_from_call(nothrowing_fun); + try + { + (void) e.value(); + BOOST_TEST(true); + } + catch(...) + { + BOOST_TEST(false); + } + BOOST_TEST_EQ(e.value(), 4); + BOOST_TEST_EQ(*e, 4); + BOOST_TEST_EQ(e.has_value(), true); + BOOST_TEST_EQ(static_cast(e), true); + +#if 0 + BOOST_TEST_THROWS(stde::make_expected_from_call(throwing_fun), test_exception); + + BOOST_TEST_NO_THROW(stde::make_expected_from_call(nothrowing_fun)); + stde::expected e2 = stde::make_expected_from_call(nothrowing_fun); + BOOST_TEST_NO_THROW(e2.value()); + BOOST_TEST_EQ(e2.value(), 4); + BOOST_TEST_EQ(*e2, 4); + BOOST_TEST_EQ(e2.has_value(), true); + BOOST_TEST_EQ(static_cast(e2), true); +#endif +} + +void make_expected_from_call_void_fun() +{ +#if 0 + BOOST_TEST_NO_THROW(stde::make_expected_from_call(void_throwing_fun)); + expected_sc e = stde::make_expected_from_call(void_throwing_fun); + BOOST_TEST_THROWS(e.value(), std::exception); + BOOST_TEST_EQ(e.has_value(), false); + BOOST_TEST_EQ(static_cast(e), false); + + e = stde::make_expected_from_call(do_nothing_fun); + BOOST_TEST_NO_THROW(e.value()); + BOOST_TEST_EQ(e.has_value(), true); + BOOST_TEST_EQ(static_cast(e), true); + + BOOST_TEST_THROWS(stde::make_expected_from_call(void_throwing_fun), test_exception); + + try { + stde::make_expected_from_call(do_nothing_fun); + BOOST_TEST(true); + } + catch (...) + { + BOOST_TEST(false); + } + stde::expected e2 = stde::make_expected_from_call(do_nothing_fun); + try { + (void)e2.value(); + BOOST_TEST(true); + } + catch (...) + { + BOOST_TEST(false); + } + //BOOST_TEST_NO_THROW(e2.value()); + BOOST_TEST_EQ(e2.has_value(), true); + BOOST_TEST_EQ(static_cast(e2), true); +#endif +} +#endif + +void expected_swap_value() +{ + // From value constructor. + expected_sc e(5); + expected_sc e2(8); + + e.swap(e2); + + BOOST_TEST_EQ(e.value(), 8); + BOOST_TEST_EQ(e2.value(), 5); + + e2.swap(e); + + BOOST_TEST_EQ(e.value(), 5); + BOOST_TEST_EQ(e2.value(), 8); +} + +#if 0 +void expected_swap_exception() +{ + // From value constructor. + stde::exception_or e = stde::make_unexpected(std::invalid_argument("e")); + stde::exception_or e2 = stde::make_unexpected(std::invalid_argument("e2")); + + e.swap(e2); + + auto equal_to_e = [](const std::invalid_argument &except) { return std::string(except.what()) == "e"; }; + auto equal_to_e2 = [](const std::invalid_argument &except) { return std::string(except.what()) == "e2"; }; + + try + { + (void) e.value(); + BOOST_TEST(true); + } + catch(std::invalid_argument &ex) + { + BOOST_TEST(equal_to_e2(ex)); + } + try + { + (void) e2.value(); + BOOST_TEST(true); + } + catch(std::invalid_argument &ex) + { + BOOST_TEST(equal_to_e(ex)); + } + + e2.swap(e); + + try + { + (void) e.value(); + BOOST_TEST(true); + } + catch(std::invalid_argument &ex) + { + BOOST_TEST(equal_to_e(ex)); + } + try + { + (void) e2.value(); + BOOST_TEST(true); + } + catch(std::invalid_argument &ex) + { + BOOST_TEST(equal_to_e2(ex)); + } +} +#endif + +void expected_swap_function_value() +{ + // From value constructor. + expected_sc e(5); + expected_sc e2(8); + + swap(e, e2); + + BOOST_TEST_EQ(e.value(), 8); + BOOST_TEST_EQ(e2.value(), 5); + + swap(e, e2); + + BOOST_TEST_EQ(e.value(), 5); + BOOST_TEST_EQ(e2.value(), 8); +} + + +#ifdef QUICKCPPLIB_BOOST_UNIT_TEST_HPP +int main() +#else +BOOST_AUTO_TEST_CASE(expected_pass) +#endif +{ + + static_assert(!std::is_default_constructible::value, ""); + static_assert(!std::is_default_constructible>::value, ""); + + static_assert(!std::is_copy_constructible::value, ""); + static_assert(!std::is_constructible, NoCopyConstructible const &>::value, ""); + static_assert(!std::is_constructible, stde::exception_or const &>::value, ""); + static_assert(!std::is_copy_constructible>::value, ""); + +#if 0 + // fixme + { + NoMoveConstructible nmc; + // NoMoveConstructible nmc2 = std::move(nmc); // FAILS as expected + + expected_sc x{std::move(nmc)}; // DOESN'T FAIL as copy is selected instead + (void) x; + } +// fixme +#if defined __clang__ && __clang_major__ >= 4 && __cplusplus > 201402L + { + NoMoveConstructible nmc; + // NoMoveConstructible nmc2 = std::move(nmc); // FAILS as expected + + expected_sc x = std::move(nmc); // DOESN'T FAIL as copy is selected instead + (void) x; + } +#endif +#endif + + static_assert(!std::is_move_constructible::value, ""); + static_assert(!std::is_constructible, NoMoveConstructible &&>::value, ""); + static_assert(std::is_move_constructible>::value, ""); + + except_default_constructor(); + except_default_constructor_error_code(); + except_default_constructor_constexpr(); + expected_from_value(); + expected_from_value2(); + expected_from_cnv_value(); + except_constructor_NDCE(); + except_constructor_NDC(); + except_constructor_Date(); + expected_from_in_place_value(); + // expected_from_exception(); + expected_from_copy_value(); + // expected_from_copy_exception(); + expected_from_in_place(); + // expected_from_exception_ptr(); + expected_from_moved_value(); + expected_from_catch_block(); + make_expected_E_from_value(); + make_expected_const_from_value(); + make_expected_from_U_value2(); + expected_from_value_error_condition(); + expected_from_error_error_condition(); + expected_from_error_convertible(); + except_valid_constexpr_int(); + except_value_constexpr_int(); + expected_from_value3(); + expected_from_copy_expected(); + expected_from_moved_expected(); + expected_from_in_place2(); + expected_from_move_value(); + expected_from_in_place3(); + expected_from_in_place_error(); + expected_from_exception_catch(); + // expected_from_error(); + // expected_from_error_U(); + // expected_from_exception2(); + // expected_from_exception_ptr2(); + // make_expected_from_call_fun(); + // make_expected_from_call_void_fun(); + expected_swap_value(); + // expected_swap_exception(); + expected_swap_function_value(); + +#ifdef QUICKCPPLIB_BOOST_UNIT_TEST_HPP + return QUICKCPPLIB_NAMESPACE::unit_test::current_test_case()->fails != 0; +#endif +} + +#if 0 +/////////////////////////////////////////////////////////////////////////////////////////////// +/////////////////////////////////////////////////////////////////////////////////////////////// +/////////////////////////////////////////////////////////////////////////////////////////////// +/////////////////////////////////////////////////////////////////////////////////////////////// + + +//void expected_from_error_catch_exception) +//{ +// // From catch block +// try +// { +// throw test_exception(); +// } +// catch(...) +// { +// auto throw_lambda = [](){ return stde::make_expected_from_error();}; +// +// //BOOST_TEST_THROWS(throw_lambda(), test_exception); +// } +//} + + + +//////////////////////////////////// +BOOST_AUTO_TEST_SUITE(expected_map) + +void expected_map() +{ + auto fun = [](bool b) -> expected_sc + { + if (b) + return stde::make_expected(5); + else + return stde::make_unexpected(test_exception()); + }; + + auto add_five = [](int sum) -> int + { + return sum + 5; + }; + + auto launch_except = [](int sum) -> int + { + throw test_exception(); + }; + + expected_sc e = fun(true).map(add_five); + BOOST_TEST_NO_THROW(e.value()); + BOOST_TEST_EQ(*e, 10); + + e = fun(true).map(add_five).map(add_five); + BOOST_TEST_NO_THROW(e.value()); + BOOST_TEST_EQ(*e, 15); + + e = fun(false).map(add_five).map(add_five); + BOOST_TEST_THROWS(e.value(), test_exception); + + BOOST_TEST_THROWS(fun(true).map(launch_except), test_exception); + +} + +void expected_void_map() +{ + auto fun = [](bool b) + { + if (b) + return stde::make_expected(); + else + return expected_sc(stde::make_unexpected(test_exception())); + }; + + auto launch_except = []() -> void + { + throw test_exception(); + }; + + auto do_nothing = []() {}; + + expected_sc e = fun(true).map(do_nothing); + BOOST_TEST_NO_THROW(e.value()); + + e = fun(false).map(do_nothing); + BOOST_TEST_THROWS(e.value(), test_exception); + + BOOST_TEST_THROWS(fun(true).map(launch_except), test_exception); + +} + +BOOST_AUTO_TEST_SUITE_END() + +//////////////////////////////////// +BOOST_AUTO_TEST_SUITE(expected_bind) + +void expected_bind() +{ + auto fun = [](bool b) -> expected_sc + { + if (b) + return stde::make_expected(5); + else + return stde::make_unexpected(test_exception()); + }; + + auto add_five = [](int sum) -> expected_sc + { + return stde::make_expected(sum + 5); + }; + + auto launch_except = [](int sum) -> expected_sc + { + throw test_exception(); + }; + + expected_sc e = fun(true).bind(add_five); + BOOST_TEST_NO_THROW(e.value()); + BOOST_TEST_EQ(*e, 10); + + e = fun(true).bind(add_five).bind(add_five); + BOOST_TEST_NO_THROW(e.value()); + BOOST_TEST_EQ(*e, 15); + + e = fun(false).bind(add_five).bind(add_five); + BOOST_TEST_THROWS(e.value(), test_exception); + + BOOST_TEST_THROWS(fun(true).bind(launch_except), test_exception); + +} + +void expected_void_bind() +{ + auto fun = [](bool b) + { + if (b) + return stde::make_expected(); + else + return expected_sc(stde::make_unexpected(test_exception())); + }; + + auto launch_except = []() -> expected_sc + { + throw test_exception(); + }; + + auto do_nothing = []() { + return stde::make_expected(); + }; + + expected_sc e = fun(true).bind(do_nothing); + BOOST_TEST_NO_THROW(e.value()); + + e = fun(false).bind(do_nothing); + BOOST_TEST_THROWS(e.value(), test_exception); + + BOOST_TEST_THROWS(fun(true).bind(launch_except), test_exception); + +} + +BOOST_AUTO_TEST_SUITE_END() + + +//////////////////////////////////// +BOOST_AUTO_TEST_SUITE(expected_then) + +void expected_non_void_then() +{ + auto fun = [](bool b) -> expected_sc + { + if (b) + return stde::make_expected(5); + else + return stde::make_unexpected(test_exception()); + }; + + + auto add_five = [](int sum) -> int + { + return sum + 5; + }; + auto six = []() -> int + { + return 6; + }; + + auto pair = [](int a) -> bool + { + return (a % 2) == 0; + }; + + auto launch_except = [](int sum) -> int + { + throw test_exception(); + }; + + auto then_launch_except = [](expected) -> int + { + throw test_exception(); + }; + + expected_sc e = fun(true).then(if_valued(add_five)); + BOOST_TEST_NO_THROW(e.value()); + BOOST_TEST_EQ(*e, 10); + + e = fun(true).then(if_valued(ident(six))); + BOOST_TEST_NO_THROW(e.value()); + BOOST_TEST_EQ(*e, 6); + + e = fun(false).then(if_unexpected(ident(six))); + BOOST_TEST_NO_THROW(e.value()); + BOOST_TEST_EQ(*e, 6); + + expected_sc e1 = fun(true).then(if_valued(pair)); + BOOST_TEST_NO_THROW(e1.value()); + BOOST_TEST_EQ(*e1, false); + + + + e = fun(true).then(if_valued(add_five)).then(if_valued(add_five)); + BOOST_TEST_NO_THROW(e.value()); + BOOST_TEST_EQ(*e, 15); + + e = fun(false).then(if_valued(add_five)).then(if_valued(add_five)); + BOOST_TEST_THROWS(e.value(), test_exception); + + BOOST_TEST_THROWS(fun(true).then(if_valued(launch_except)), test_exception); + + e = fun(false).then(catch_all(then_launch_except)); + BOOST_TEST_THROWS(e.value(), test_exception); + +} + +void expected_void_then() +{ + auto fun = [](bool b) -> expected_sc + { + if (b) + return stde::make_expected(); + else + return stde::make_unexpected(test_exception()); + }; + + auto launch_except = []() + { + throw test_exception(); + }; + + auto do_nothing = []() {}; + + BOOST_TEST(true); + expected_sc e = fun(true).then(if_valued(do_nothing)); + BOOST_TEST_NO_THROW(e.value()); + + e = fun(false).then(if_valued(do_nothing)); + BOOST_TEST_THROWS(e.value(), test_exception); + + BOOST_TEST_THROWS(fun(true).then(if_valued(launch_except)), test_exception); + +} + +BOOST_AUTO_TEST_SUITE_END() + +BOOST_AUTO_TEST_SUITE(expected_recover) + +void expected_recover() +{ + auto fun = [](bool b) + { + if (b) + return expected_sc(5); + else + return expected_sc(stde::make_unexpected(test_exception())); + }; + + auto add_five = [](int sum) -> expected_sc + { + return stde::make_expected(sum + 5); + }; + + auto recover_error = [](std::exception_ptr p) + { + return stde::make_expected(0); + }; + + auto recover_error_silent_failure = [](std::exception_ptr p) + { + return expected_sc(stde::make_unexpected(p)); + }; + + auto recover_error_failure = [](std::exception_ptr p) -> expected_sc + { + return expected_sc(stde::make_unexpected(test_exception())); + }; + + auto recover_error_throws = [](std::exception_ptr p) -> expected_sc + { + throw test_exception(); + }; + + BOOST_TEST_EQ(fun(false).catch_error(recover_error).has_value(), true); + BOOST_TEST_EQ(fun(false).catch_error(recover_error).value(), 0); + BOOST_TEST_EQ(fun(true).catch_error(recover_error).value(), 5); + BOOST_TEST_EQ(fun(false).catch_error(recover_error_silent_failure).has_value(), false); + BOOST_TEST_EQ(fun(false).catch_error(recover_error_failure).has_value(), false); + + BOOST_TEST_EQ(fun(true).bind(add_five).value(), 10); + BOOST_TEST_EQ(fun(true).bind(add_five).catch_error(recover_error).value(), 10); + BOOST_TEST_EQ(fun(true).bind(add_five).catch_error(recover_error_silent_failure).value(), 10); + BOOST_TEST_EQ(fun(true).bind(add_five).catch_error(recover_error_failure).value(), 10); + + BOOST_TEST_EQ(fun(false).catch_error(recover_error).bind(add_five).value(), 5); + BOOST_TEST_EQ(fun(false).catch_error(recover_error).bind(add_five).bind(add_five).value(), 10); + BOOST_TEST_EQ(fun(false).catch_error(recover_error_failure).bind(add_five).has_value(), false); + BOOST_TEST_EQ(fun(false).bind(add_five).catch_error(recover_error_failure).bind(add_five).has_value(), false); + BOOST_TEST_EQ(fun(false).bind(add_five).catch_error(recover_error_silent_failure).bind(add_five).has_value(), false); + + BOOST_TEST_THROWS(fun(false).catch_error(recover_error_throws), test_exception); + +} + +void expected_void_recover() +{ + auto fun = [](bool b) + { + if (b) + return stde::make_expected(); + else + return expected_sc(boost::stde::make_unexpected(test_exception())); + }; + + auto do_nothing = []() { + return stde::make_expected(); + }; + + auto recover_error = [](std::exception_ptr p) + { + return stde::make_expected(); + }; + + auto recover_error_silent_failure = [](std::exception_ptr p) + { + return expected_sc(boost::stde::make_unexpected(p)); + }; + + auto recover_error_failure = [](std::exception_ptr p) -> expected_sc + { + throw test_exception(); + }; + + // The catch_error doesn't alter the stde::expected if it's valid. + BOOST_TEST_EQ(fun(true).catch_error(recover_error_failure).has_value(), true); + + // Simple catch_error tests. + BOOST_TEST_EQ(fun(false).catch_error(recover_error).has_value(), true); + BOOST_TEST_THROWS(fun(false).catch_error(recover_error_failure), test_exception); + BOOST_TEST_EQ(fun(false).catch_error(recover_error_silent_failure).has_value(), false); + + // With a bind between. + BOOST_TEST_THROWS(fun(false).bind(do_nothing).catch_error(recover_error_failure), test_exception); + +} +BOOST_AUTO_TEST_SUITE_END() + +BOOST_AUTO_TEST_SUITE(proposal) + +void proposal_concept() +{ + using namespace std; + + { + stde::expected ei = 0; + stde::expected ej = 1; + stde::expected ek = stde::make_unexpected(string()); + + ei = 1; + ej = stde::make_unexpected(string());; + ek = 0; + + ei = stde::make_unexpected(string());; + ej = 0; + ek = 1; + } +} +void proposal_init() +{ + using namespace std; + { + string s{ "STR" }; + + stde::expected ep{ stde::make_unexpected(-1) }; // unexpected value, requires Movable + stde::expected eq = { stde::make_unexpected(-1) }; // unexpected value, requires Movable + + expected_sc es{ s }; // requires Copyable + expected_sc et = s; // requires Copyable + expected_sc ev = string{ "STR" }; // requires Movable + + expected_sc ew; // unexpected value + expected_sc ex{}; // unexpected value + expected_sc ey = {}; // unexpected value + expected_sc ez = expected_sc{}; // unexpected value + } + + { + stde::expected eg; // unexpected value + stde::expected eh{}; // unexpected value + stde::expected ei{ stde::in_place }; // calls Guard{} in place + stde::expected ej{ stde::in_place, "arg" }; // calls Guard{"arg"} in place + } + + { + stde::expected ei{ unexpect }; // unexpected value, calls string{} in place + stde::expected ej{ unexpect, "arg" }; // unexpected value, calls string{"arg"} in place + } +} +void proposal_make_unexpected_fact() +{ + using namespace std; + { + stde::expected opt1 = stde::make_unexpected(1); + stde::expected opt2 = { unexpect, 1 }; + + opt1 = stde::make_unexpected(1); + opt2 = { unexpect, 1 }; + } +} +void proposal_error_exception_ts() +{ + using namespace std; + { + stde::expected > e = + stde::make_unexpected(make_error_code(errc::invalid_argument)); +#if !defined BOOST_MSVC || BOOST_MSVC >= 1900 + BOOST_TEST(e.error() == make_error_code(errc::invalid_argument)); +#else + // VS2013 doesn't match operator==(boost::error_exception, std::error_code) + BOOST_TEST(e.error() == (error_exception(make_error_code(errc::invalid_argument)))); +#endif + try { + e.value(); + BOOST_TEST(false); + } + catch (std::system_error const& ex) { + + } + catch (...) { + BOOST_TEST(false); + } + stde::expected > e2 = stde::make_unexpected(e.error()); +#if !defined BOOST_MSVC || BOOST_MSVC >= 1900 + BOOST_TEST(e2.error() == make_error_code(errc::invalid_argument)); +#else + // VS2013 doesn't match operator==(boost::error_exception, std::error_code) + BOOST_TEST(e2.error() == (error_exception(make_error_code(errc::invalid_argument)))); +#endif + try { + e2.value(); + BOOST_TEST(false); + } + catch (std::system_error const& ex) { + + } + catch (...) { + BOOST_TEST(false); + } + + } +} +void proposal_ensured_read_ts() +{ + using namespace std; + { + ensured_read e = make_ensured_read(1); + BOOST_TEST(e == 1); + } + { + ensured_read e = make_ensured_read(1); + stde::unexpected> ue1 = stde::make_unexpected(std::move(e)); + BOOST_TEST(ue1.value() == 1); + } + // { + // stde::make_unexpected(make_ensured_read(1)); + // // calls to terminate. + // } + // { + // stde::expected > e = stde::make_unexpected(make_ensured_read(1)); + // // calls to terminate. + // } + { + stde::expected > e{ 1 }; + BOOST_TEST(e.value() == 1); + } + { + stde::expected > e = stde::make_unexpected(make_ensured_read(1)); + BOOST_TEST(e.error() == 1); + } + { + stde::expected > e{ unexpect, 1 }; + BOOST_TEST(e.error() == 1); + } + { + ensured_read e = make_ensured_read(std::make_exception_ptr(1)); + BOOST_TEST_THROWS(std::rethrow_exception(e.value()), int); + } + { + stde::expected > e = stde::make_unexpected(make_ensured_read(std::make_exception_ptr(1))); + BOOST_TEST_THROWS(std::rethrow_exception(e.error().value()), int); + } +} +void proposal_relational_operators() +{ + using namespace std; + { + stde::expected e0{ 0 }; + stde::expected e1{ 1 }; + stde::expected eN{ unexpect, -1 }; + + BOOST_TEST(eN < e0); + BOOST_TEST(e0 < e1); + BOOST_TEST(!(e0 < eN)); + BOOST_TEST(eN <= e0); + BOOST_TEST(e0 <= e1); + + BOOST_TEST(e0 > eN); + BOOST_TEST(e1 > e0); + BOOST_TEST(e0 >= eN); + BOOST_TEST(e1 >= e0); + + BOOST_TEST(!(eN < eN)); + BOOST_TEST(!(e1 < e1)); + BOOST_TEST(eN <= eN); + BOOST_TEST(e1 <= e1); + + BOOST_TEST(eN != e0); + BOOST_TEST(e0 != e1); + BOOST_TEST(eN == eN); + BOOST_TEST(e0 == e0); + + ////// + + BOOST_TEST(eN == stde::make_unexpected(-1)); + BOOST_TEST(e0 != stde::make_unexpected(1)); + BOOST_TEST(eN != 1u); + BOOST_TEST(e1 == 1u); + + BOOST_TEST(eN < 1u); + BOOST_TEST(eN <= 1u); + BOOST_TEST(1u > eN); + BOOST_TEST(!(1u < eN)); + BOOST_TEST(1u >= eN); + BOOST_TEST(stde::make_unexpected(1) < e0); + BOOST_TEST(stde::make_unexpected(1) <= e0); + BOOST_TEST(!(stde::make_unexpected(1) > e0)); + BOOST_TEST(!(stde::make_unexpected(1) >= e0)); + + + BOOST_TEST(!(e0 < stde::make_unexpected(1))); + BOOST_TEST(!(e0 <= stde::make_unexpected(1))); + BOOST_TEST(e0 > stde::make_unexpected(1)); + BOOST_TEST(e0 >= stde::make_unexpected(1)); + } + { + stde::expected e0{ boost::expect }; + stde::expected eN{ unexpect, -1 }; + + BOOST_TEST(!(e0 < e0)); + BOOST_TEST(eN < e0); + BOOST_TEST(!(e0 < eN)); + BOOST_TEST(!(eN < eN)); + BOOST_TEST(e0 <= e0); + BOOST_TEST(eN <= e0); + BOOST_TEST(!(e0 <= eN)); + BOOST_TEST(eN <= eN); + + BOOST_TEST(!(e0 > e0)); + BOOST_TEST(e0 > eN); + BOOST_TEST(!(eN > e0)); + BOOST_TEST(!(eN > eN)); + BOOST_TEST(e0 >= e0); + BOOST_TEST(e0 >= eN); + BOOST_TEST(!(eN >= e0)); + BOOST_TEST(eN >= eN); + + BOOST_TEST(!(e0 != e0)); + BOOST_TEST(eN != e0); + BOOST_TEST(e0 != eN); + BOOST_TEST(!(eN != eN)); + BOOST_TEST(e0 == e0); + BOOST_TEST(!(eN == e0)); + BOOST_TEST(!(e0 == eN)); + BOOST_TEST(eN == eN); + + ////// + + BOOST_TEST(eN == stde::make_unexpected(-1)); + BOOST_TEST(e0 != stde::make_unexpected(1)); + + BOOST_TEST(stde::make_unexpected(1) < e0); + BOOST_TEST(stde::make_unexpected(1) <= e0); + BOOST_TEST(!(stde::make_unexpected(1) > e0)); + BOOST_TEST(!(stde::make_unexpected(1) >= e0)); + + BOOST_TEST(!(stde::make_unexpected(1) < eN)); + BOOST_TEST(!(stde::make_unexpected(1) <= eN)); + BOOST_TEST(stde::make_unexpected(1) > eN); + BOOST_TEST(stde::make_unexpected(1) >= eN); + + BOOST_TEST(!(eN < stde::make_unexpected(-1))); + BOOST_TEST(eN <= stde::make_unexpected(-1)); + BOOST_TEST(!(eN > stde::make_unexpected(-1))); + BOOST_TEST(eN >= stde::make_unexpected(-1)); + } +} +void proposal_dereference_operators() +{ + using namespace std; + { + const string s{ "STR" }; + + expected_sc e0{ s }; + const expected_sc e1{ s }; + BOOST_TEST(*e0.operator->() == s); + BOOST_TEST(*e1.operator->() == s); + + // Test with class which has operator&() overloaded + const OverloadedAddressOf o{}; + BOOST_TEST(&o == nullptr); + + expected_sc e2{ o }; + const expected_sc e3{ o }; + BOOST_TEST(e2.operator->() != nullptr); + BOOST_TEST(e3.operator->() != nullptr); + } +} +BOOST_AUTO_TEST_SUITE_END() + +BOOST_AUTO_TEST_SUITE(movesem) +////////////////////////////// +void movesem_moved_from_state() +{ + // first, test mock: + MoveAware i{ 1 }, j{ 2 }; + BOOST_TEST(i.val == 1); + BOOST_TEST(!i.moved); + BOOST_TEST(j.val == 2); + BOOST_TEST(!j.moved); + + MoveAware k = std::move(i); + BOOST_TEST(k.val == 1); + BOOST_TEST(!k.moved); + BOOST_TEST(i.val == 1); + BOOST_TEST(i.moved); + + k = std::move(j); + BOOST_TEST(k.val == 2); + BOOST_TEST(!k.moved); + BOOST_TEST(j.val == 2); + BOOST_TEST(j.moved); + + // now, test stde::expected + expected_sc> oi{ 1 }, oj{ 2 }; + BOOST_TEST(oi); + BOOST_TEST(!oi->moved); + BOOST_TEST(oj); + BOOST_TEST(!oj->moved); + + expected_sc> ok{ std::move(oi) }; + BOOST_TEST(ok); + BOOST_TEST(!ok->moved); + BOOST_TEST(oi); + BOOST_TEST(oi->moved); + + ok = std::move(oj); + BOOST_TEST(ok); + BOOST_TEST(!ok->moved); + BOOST_TEST(oj); + BOOST_TEST(oj->moved); + +} +void movesem_move_only_value() +{ + const auto make_int = []() { + std::unique_ptr value{ new int }; + *value = 100; + return value; + }; + const auto return_void = [](std::unique_ptr value) { + BOOST_TEST(value != nullptr); + BOOST_TEST(*value == 100); + }; + const auto return_expected = [](std::unique_ptr value) { + BOOST_TEST(value != nullptr); + BOOST_TEST(*value == 100); + return expected_sc{boost::expect}; + }; + const auto return_int = [](std::unique_ptr value) { + BOOST_TEST(value != nullptr); + BOOST_TEST(*value == 100); + return 200; + }; + + BOOST_TEST(expected>{make_int()}.map(return_void)); + BOOST_TEST(expected>{make_int()}.map(return_expected)); + BOOST_TEST(expected>{make_int()}.map(return_int)); +} +void movesem_move_only_value2() +{ + const auto make_int = []() { + std::unique_ptr value{ new int }; + *value = 100; + return value; + }; + const auto return_expected_void = [](std::unique_ptr value) { + BOOST_TEST(value != nullptr); + BOOST_TEST(*value == 100); + return stde::make_expected(); + }; + const auto return_expected = [](std::unique_ptr value) { + BOOST_TEST(value != nullptr); + BOOST_TEST(*value == 100); + return expected_sc{boost::expect}; + }; + BOOST_TEST(expected>{make_int()}.bind(return_expected_void)); + BOOST_TEST(expected>{make_int()}.bind(return_expected)); +} +void movesem_copy_move_ctor_optional_int() +{ + expected_sc oi; + expected_sc oj = oi; + + BOOST_TEST(oj); + BOOST_TEST(oj == oi); + BOOST_TEST(bool(oj)); + + oi = 1; + expected_sc ok = oi; + BOOST_TEST(!!ok); + BOOST_TEST(bool(ok)); + BOOST_TEST(ok == oi); + BOOST_TEST(ok != oj); + BOOST_TEST(*ok == 1); + + expected_sc ol = std::move(oi); + BOOST_TEST(!!ol); + BOOST_TEST(bool(ol)); + BOOST_TEST(ol == oi); + BOOST_TEST(ol != oj); + BOOST_TEST(*ol == 1); +} +void movesem_expected_expected() +{ + expected_sc> oi1 = stde::make_unexpected(-1); + BOOST_TEST(!oi1); + + { + expected_sc> oi2{ stde::expect }; + BOOST_TEST(bool(oi2)); + BOOST_TEST((*oi2)); + //std::cout << typeid(**oi2).name() << std::endl; + } + + { + expected_sc> oi2{ stde::expect, stde::make_unexpected(-1) }; + BOOST_TEST(bool(oi2)); + BOOST_TEST(!*oi2); + } + + { + stde::expected> oi2{ stde::expected{} }; + BOOST_TEST(bool(oi2)); + BOOST_TEST(*oi2); + } + + stde::expected oi; + auto ooi = stde::make_expected(oi); + static_assert(std::is_same>, decltype(ooi)>::value, ""); + +} +BOOST_AUTO_TEST_SUITE_END() + +void process() {} +void process(int) {} +void processNil() {} + +BOOST_AUTO_TEST_SUITE(Examples) +////////////////////////////// + +void example1() +{ + stde::expected oi; // create disengaged object + stde::expected oj = { unexpect }; // alternative syntax + oi = oj; // assign disengaged object + stde::expected ok = oj; // ok is disengaged + + if (oi) BOOST_TEST(false); // 'if oi is engaged...' + if (!oi) BOOST_TEST(true); // 'if oi is disengaged...' + + BOOST_TEST(oi == ok); // two disengaged optionals compare equal + + /////////////////////////////////////////////////////////////////////////// + stde::expected ol{ 1 }; // ol is engaged; its contained value is 1 + ok = 2; // ok becomes engaged; its contained value is 2 + oj = ol; // oj becomes engaged; its contained value is 1 + + BOOST_TEST(oi != ol); // disengaged != engaged + BOOST_TEST(ok != ol); // different contained values + BOOST_TEST(oj == ol); // same contained value + //BOOST_TEST(oi < ol); // disengaged < engaged + //BOOST_TEST(ol < ok); // less by contained value + + ///////////////////////////////////////////////////////////////////////////// + stde::expected om{ 1 }; // om is engaged; its contained value is 1 + stde::expected on = om; // on is engaged; its contained value is 1 + om = 2; // om is engaged; its contained value is 2 + BOOST_TEST(on != om); // on still contains 3. They are not pointers + + ///////////////////////////////////////////////////////////////////////////// + int i = *ol; // i obtains the value contained in ol + BOOST_TEST(i == 1); + *ol = 9; // the object contained in ol becomes 9 + BOOST_TEST(*ol == 9); + BOOST_TEST(ol == stde::make_expected(9)); + + /////////////////////////////////// + int p = 1; + stde::expected op = p; + BOOST_TEST(*op == 1); + p = 2; + BOOST_TEST(*op == 1); // value contained in op is separated from p + + //////////////////////////////// + if (ol) + process(*ol); // use contained value if present + else + process(); // proceed without contained value + + if (!om) + processNil(); + else + process(*om); + + ///////////////////////////////////////// + process(ol.value_or(0)); // use 0 if ol is disengaged + + //////////////////////////////////////////// + ok = { unexpect }; // if ok was engaged calls T's dtor + oj = {}; // assigns a temporary disengaged stde::expected +} +////////////////////////////////////////////////// +void ValueOr() +{ + stde::expected oi = 1; + int i = oi.value_or(0); + BOOST_TEST(i == 1); + + oi = { unexpect }; + BOOST_TEST(oi.value_or(3) == 3); + + stde::expected os{ "AAA" }; + BOOST_TEST(os.value_or("BBB") == "AAA"); + os = {}; + BOOST_TEST(os); + BOOST_TEST(os.value() == ""); + + BOOST_TEST(os.value_or(std::string("BBB")) == ""); + { + constexpr stde::expected e = 1; + static_assert(e.has_value(), ""); + static_assert(*e == 1, ""); + static_assert(e.value() == 1, ""); + } + { + constexpr std::error_code ec = std::make_error_code(std::errc(1)); + constexpr stde::expected e = stde::make_unexpected(ec); + static_assert(!e.has_value(), ""); + static_assert(e.error() == ec, ""); + } + { + constexpr stde::expected e = 1; + static_assert(e.has_value(), ""); + static_assert(*e == 1, ""); + static_assert(e.value() == 1, ""); + } + { + constexpr std::error_code ec = std::make_error_code(std::errc(1)); + constexpr stde::expected e = stde::make_unexpected(ec); + static_assert(!e.has_value(), ""); + static_assert(e.error() == ec, ""); + } +} + +////////////////////////////////////////////////// +BOOST_AUTO_TEST_SUITE_END() + +#endif + +#else +int main(void) +{ + return 0; +} +#endif diff --git a/src/boost/libs/outcome/test/tests/comparison.cpp b/src/boost/libs/outcome/test/tests/comparison.cpp new file mode 100644 index 000000000..1c093ffd2 --- /dev/null +++ b/src/boost/libs/outcome/test/tests/comparison.cpp @@ -0,0 +1,102 @@ +/* Unit testing for outcomes +(C) 2013-2020 Niall Douglas (6 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 _MSC_VER +#pragma warning(disable : 4127) // conditional expression is constant +#pragma warning(disable : 4244) // conversion from int to short +#endif + +#include +#include +#include + +BOOST_OUTCOME_AUTO_TEST_CASE(works_outcome_comparison, "Tests that the outcome can compare to compatible outcomes") +{ +#if !defined(__APPLE__) || defined(__cpp_exceptions) + using namespace BOOST_OUTCOME_V2_NAMESPACE; + auto p = boost::copy_exception(std::runtime_error("hi")); + // value comparison + { + outcome a(1), b(2), c(2); + BOOST_CHECK(a == a); + BOOST_CHECK(b == b); + BOOST_CHECK(c == c); + BOOST_CHECK(a != b); + BOOST_CHECK(b == c); + } + // homogeneous outcome comparison + { + outcome a(1), b(2), c(boost::system::errc::invalid_argument), d(p); + BOOST_CHECK(a == a); + BOOST_CHECK(a != b); + BOOST_CHECK(a != c); + BOOST_CHECK(a != d); + BOOST_CHECK(b == b); + BOOST_CHECK(b != c); + BOOST_CHECK(b != d); + BOOST_CHECK(c == c); + BOOST_CHECK(c != d); + BOOST_CHECK(d == d); + outcome e(boost::system::errc::invalid_argument), f(p); + BOOST_CHECK(c == e); + BOOST_CHECK(d == f); + } + // heterogeneous outcome comparison, so outcome to outcome etc + { + outcome a(1); + outcome b(1); + outcome c(1); + BOOST_CHECK(a == b); + BOOST_CHECK(a == c); + BOOST_CHECK(b == a); + BOOST_CHECK(b == c); + BOOST_CHECK(c == a); + BOOST_CHECK(c == b); + // outcome e(in_place_type); + outcome f(boost::system::errc::invalid_argument); + outcome g(p); + // BOOST_CHECK(a != e); + BOOST_CHECK(a != f); + BOOST_CHECK(a != g); + // BOOST_CHECK(e != f); + BOOST_CHECK(f != g); + } + // upconverting outcome comparison, so outcome==result etc + { + outcome a(1); + result b(1); + BOOST_CHECK(a == b); + BOOST_CHECK(b == a); + // result e(in_place_type), f(boost::system::errc::invalid_argument); + // BOOST_CHECK(a != e); + // BOOST_CHECK(a != f); + } + // Should I do outcome(5) == 5? Unsure if it's wise +#endif +} diff --git a/src/boost/libs/outcome/test/tests/constexpr.cpp b/src/boost/libs/outcome/test/tests/constexpr.cpp new file mode 100644 index 000000000..327b8f26a --- /dev/null +++ b/src/boost/libs/outcome/test/tests/constexpr.cpp @@ -0,0 +1,118 @@ +/* Unit testing for outcomes +(C) 2013-2020 Niall Douglas (9 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. +*/ + +#include +#include +#include + +#if __cplusplus >= 201700 || _HAS_CXX17 +// Match LiteralType, even on C++ 17 and later +template struct is_literal_type +{ + static constexpr bool value = // + std::is_void::value // + || std::is_scalar::value // + || std::is_reference::value // + // leave out is_array for simplicity + || (std::is_class::value && std::is_trivially_destructible::value + // how does one detect if a type has at least one constexpr constructor without Reflection??? + ) // leave out union for simplicity + ; +}; +#else +template using is_literal_type = std::is_literal_type; +#endif + +BOOST_OUTCOME_AUTO_TEST_CASE(works_outcome_constexpr, "Tests that outcome works as intended in a constexpr evaluation context") +{ + using namespace BOOST_OUTCOME_V2_NAMESPACE; + + static_assert(is_literal_type>::value, "result is not a literal type!"); + static_assert(is_literal_type>::value, "outcome is not a literal type!"); + + // Unfortunately result can never be a literal type as error_code can never be literal + // + // It can however be trivially destructible as error_code is trivially destructible. That + // makes possible lots of compiler optimisations + static_assert(std::is_trivially_destructible>::value, "result is not trivially destructible!"); + static_assert(std::is_trivially_destructible>::value, "result is not trivially destructible!"); + + // outcome default has no trivial operations, but if configured it can become so + static_assert(std::is_trivially_destructible>::value, + "outcome is not trivially destructible!"); + + { + // Test compatible results can be constructed from one another + constexpr result g(in_place_type, 5); + constexpr result g2(g); + static_assert(g.has_value(), ""); + static_assert(!g.has_error(), ""); + static_assert(g.assume_value() == 5, ""); // value() with UDT E won't compile + static_assert(g2.has_value(), ""); + static_assert(!g2.has_error(), ""); + static_assert(g2.assume_value() == 5, ""); // value() with UDT E won't compile + constexpr result g3(in_place_type); + constexpr result g4(g3); + constexpr result g5(in_place_type); + constexpr result g6(g5); + (void) g4; + (void) g6; + + // Test void + constexpr result h(in_place_type); + static_assert(h.has_value(), ""); + constexpr result h2(in_place_type); + static_assert(!h2.has_value(), ""); + static_assert(h2.has_error(), ""); + + // Test const + constexpr result i(5); + constexpr result i2(i); + (void) i2; + } + { + // Test compatible outcomes can be constructed from one another + constexpr outcome g(in_place_type, 5); + constexpr outcome g2(g); + static_assert(g.has_value(), ""); + static_assert(!g.has_error(), ""); + static_assert(!g.has_exception(), ""); + static_assert(g.assume_value() == 5, ""); // value() with UDT E won't compile + static_assert(g2.has_value(), ""); + static_assert(!g2.has_error(), ""); + static_assert(!g2.has_exception(), ""); + static_assert(g2.assume_value() == 5, ""); // value() with UDT E won't compile + constexpr outcome g3(in_place_type); + constexpr outcome g4(g3); + constexpr outcome g5(in_place_type); + constexpr outcome g6(g5); + (void) g4; + (void) g6; + } +} diff --git a/src/boost/libs/outcome/test/tests/containers.cpp b/src/boost/libs/outcome/test/tests/containers.cpp new file mode 100644 index 000000000..b92af4b70 --- /dev/null +++ b/src/boost/libs/outcome/test/tests/containers.cpp @@ -0,0 +1,58 @@ +/* Unit testing for outcomes +(C) 2013-2020 Niall Douglas (7 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. +*/ + +#include +#include +#include + +BOOST_OUTCOME_AUTO_TEST_CASE(works_outcome_containers, "Tests that outcome works as intended inside containers") +{ + using namespace BOOST_OUTCOME_V2_NAMESPACE; + outcome> a(std::vector{5, 6, 7, 8}); + BOOST_CHECK(a.has_value()); + BOOST_CHECK(a.value().size() == 4U); + auto b(a); + BOOST_CHECK(a.has_value()); + BOOST_CHECK(a.value().size() == 4U); + BOOST_CHECK(b.has_value()); + BOOST_CHECK(b.value().size() == 4U); + + std::vector>> vect; + vect.push_back(std::vector{5, 6, 7, 8}); + vect.push_back(std::vector{1, 2, 3, 4}); + BOOST_REQUIRE(vect.size() == 2U); // NOLINT + BOOST_CHECK(vect[0].has_value()); + BOOST_CHECK(vect[1].has_value()); + BOOST_CHECK(vect[0].value().size() == 4U); + BOOST_CHECK(vect[1].value().size() == 4U); + BOOST_CHECK(vect[0].value().front() == 5); + BOOST_CHECK(vect[0].value().back() == 8); + BOOST_CHECK(vect[1].value().front() == 1); + BOOST_CHECK(vect[1].value().back() == 4); +} diff --git a/src/boost/libs/outcome/test/tests/core-outcome.cpp b/src/boost/libs/outcome/test/tests/core-outcome.cpp new file mode 100644 index 000000000..dfa15d355 --- /dev/null +++ b/src/boost/libs/outcome/test/tests/core-outcome.cpp @@ -0,0 +1,258 @@ +/* Unit testing for outcomes +(C) 2013-2020 Niall Douglas (18 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. +*/ + +#include +#include +#include + +#include + +#ifdef _MSC_VER +#pragma warning(disable : 4702) // unreachable code +#endif + +BOOST_OUTCOME_AUTO_TEST_CASE(works_outcome, "Tests that the outcome works as intended") +{ + using namespace BOOST_OUTCOME_V2_NAMESPACE; + + 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, outcome>::value, "Sanity check that compatible monads can be constructed from one another"); + static_assert(std::is_constructible>, outcome>::value, "Sanity check that outer monad can be constructed from a compatible monad"); + static_assert(!std::is_constructible>>, outcome>::value, "Sanity check that outer monad can be constructed from a compatible monad up to two nestings deep"); + static_assert(!std::is_constructible>>>, outcome>::value, "Sanity check that outer monad can be constructed from a compatible monad three or more nestings deep"); + static_assert(!std::is_constructible, outcome>::value, "Sanity check that incompatible monads cannot be constructed from one another"); + + static_assert(std::is_constructible, outcome>::value, "Sanity check that all monads can be constructed from a void monad"); + static_assert(std::is_constructible>, outcome>::value, "Sanity check that outer monad can be constructed from a compatible monad"); + static_assert(std::is_constructible>>, outcome>::value, "Sanity check that outer monad can be constructed from a compatible monad up to two nestings deep"); + static_assert(!std::is_constructible, outcome>::value, "Sanity check that incompatible monads cannot be constructed from one another"); + + static_assert(std::is_void::value_type>::value, "Sanity check that result has a void value_type"); + static_assert(std::is_void::error_type>::value, "Sanity check that result has a void error_type"); + // static_assert(std::is_void::exception_type>::value, "Sanity check that outcome has a void exception_type"); + + static_assert(std::is_same::value_type, int>::value, "Sanity check that outcome has a int value_type"); + static_assert(std::is_same::error_type, boost::system::error_code>::value, "Sanity check that outcome has a error_code error_type"); + static_assert(std::is_same::exception_type, boost::exception_ptr>::value, "Sanity check that outcome has a exception_ptr exception_type"); + + + { // errored int + outcome 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()); + BOOST_CHECK_THROW(m.exception(), bad_outcome_access); + BOOST_CHECK_THROW(boost::rethrow_exception(m.failure()), boost::system::system_error); + } + { // errored void + outcome 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]() -> void { return m.value(); }()), boost::system::system_error); + BOOST_CHECK_NO_THROW(m.error()); + BOOST_CHECK_THROW(m.exception(), bad_outcome_access); + BOOST_CHECK_THROW(boost::rethrow_exception(m.failure()), boost::system::system_error); + } + { // valued int + outcome 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_outcome_access); + BOOST_CHECK_THROW(m.exception(), bad_outcome_access); + BOOST_CHECK(!m.failure()); + } + { // moves do not clear state + outcome 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 + outcome 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_outcome_access); + BOOST_CHECK_THROW(m.exception(), bad_outcome_access); + BOOST_CHECK(!m.failure()); + } + { // errored + boost::system::error_code ec(5, boost::system::system_category()); + outcome 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); + BOOST_CHECK_THROW(m.exception(), bad_outcome_access); +#ifndef BOOST_NO_EXCEPTIONS + BOOST_CHECK(m.failure()); + try + { + boost::rethrow_exception(m.failure()); + } + catch(const boost::system::system_error &ex) + { + BOOST_CHECK(ex.code() == ec); + BOOST_CHECK(ex.code().value() == 5); + } +#endif + } +#if !defined(__APPLE__) || defined(__cpp_exceptions) + { // excepted + boost::system::error_code ec(5, boost::system::system_category()); + auto e = boost::copy_exception(boost::system::system_error(ec)); // NOLINT + outcome 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_THROW(m.error(), bad_outcome_access); + BOOST_CHECK(m.exception() == e); +#ifndef BOOST_NO_EXCEPTIONS + BOOST_CHECK(m.failure()); + try + { + boost::rethrow_exception(m.failure()); + } + catch(const boost::system::system_error &ex) + { + BOOST_CHECK(ex.code() == ec); + BOOST_CHECK(ex.code().value() == 5); + } +#endif + } + { // custom error type + struct Foo + { + }; + auto e = boost::copy_exception(Foo()); + outcome 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(), Foo); + BOOST_CHECK_THROW(m.error(), bad_outcome_access); + BOOST_CHECK(m.exception() == e); + } + { // outcome should work + boost::system::error_code ec(5, boost::system::system_category()); + auto e = boost::copy_exception(boost::system::system_error(ec)); + outcome m(e); + BOOST_CHECK(!m); + BOOST_CHECK(!m.has_value()); + BOOST_CHECK(!m.has_error()); + BOOST_CHECK(m.has_exception()); + } +#endif + + + { + outcome a(5); + outcome b(make_error_code(boost::system::errc::invalid_argument)); + std::cout << sizeof(a) << std::endl; // 40 bytes + a.assume_value(); + b.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, ""); + 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_trivially_destructible::value, ""); + static_assert(std::is_nothrow_destructible::value, ""); + + // Test void compiles + outcome c(in_place_type); + outcome c2(c); + (void) c2; + // Test int, void compiles + outcome d(in_place_type); + } + + { + // 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; + }; + // Test a udt which can only be constructed in place compiles + outcome g(in_place_type, 5, static_cast("niall"), nullptr); + // Does converting inplace construction also work? + outcome h(5, static_cast("niall"), nullptr); + outcome i(ENOMEM, boost::system::generic_category()); + BOOST_CHECK(h.has_value()); + BOOST_CHECK(i.has_error()); + } +} diff --git a/src/boost/libs/outcome/test/tests/core-result.cpp b/src/boost/libs/outcome/test/tests/core-result.cpp new file mode 100644 index 000000000..2ded9cb6a --- /dev/null +++ b/src/boost/libs/outcome/test/tests/core-result.cpp @@ -0,0 +1,370 @@ +/* Unit testing for outcomes +(C) 2013-2020 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 +} diff --git a/src/boost/libs/outcome/test/tests/coroutine-support.cpp b/src/boost/libs/outcome/test/tests/coroutine-support.cpp new file mode 100644 index 000000000..43c46b09b --- /dev/null +++ b/src/boost/libs/outcome/test/tests/coroutine-support.cpp @@ -0,0 +1,120 @@ +/* Unit testing for outcomes +(C) 2013-2020 Niall Douglas (6 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. +*/ + +#if defined(__cpp_coroutines) + +#include +#include +#include +#include +#include + +namespace coroutines +{ + template using eager = BOOST_OUTCOME_V2_NAMESPACE::awaitables::eager; + template using lazy = BOOST_OUTCOME_V2_NAMESPACE::awaitables::lazy; + template using result = BOOST_OUTCOME_V2_NAMESPACE::result; + + inline eager> eager_int(int x) { co_return x + 1; } + inline lazy> lazy_int(int x) { co_return x + 1; } + inline eager> eager_error() { co_return boost::system::errc::not_enough_memory; } + inline lazy> lazy_error() { co_return boost::system::errc::not_enough_memory; } + inline eager> eager_void() { co_return boost::system::errc::not_enough_memory; } + inline lazy> lazy_void() { co_return boost::system::errc::not_enough_memory; } + + template inline eager> eager_coawait(U &&f, Args... args) + { + BOOST_OUTCOME_CO_TRY(co_await f(args...)); + co_return "hi"; + } + template inline lazy> lazy_coawait(U &&f, Args... args) + { + BOOST_OUTCOME_CO_TRY(co_await f(args...)); + co_return "hi"; + } + +#ifndef BOOST_NO_EXCEPTIONS + struct custom_exception_type + { + }; + inline lazy> result_exception(boost::exception_ptr e) + { + boost::rethrow_exception(e); + co_return 5; + } + + inline lazy> outcome_exception(boost::exception_ptr e) + { + boost::rethrow_exception(e); + co_return 5; + } +#endif + + inline eager eager_int2(int x) { co_return x + 1; } + inline lazy lazy_int2(int x) { co_return x + 1; } + inline eager eager_void2() { co_return; } + inline lazy lazy_void2() { co_return; } +} // namespace coroutines + +BOOST_OUTCOME_AUTO_TEST_CASE(works_result_coroutine, "Tests that results are eager and lazy awaitable") +{ + using namespace coroutines; + auto eager_await = [](auto t) { return t.await_resume(); }; + auto lazy_await = [](auto t) { + t.await_suspend({}); + return t.await_resume(); + }; + + BOOST_CHECK(eager_await(eager_int(5)).value() == 6); + BOOST_CHECK(lazy_await(lazy_int(5)).value() == 6); + BOOST_CHECK(eager_await(eager_error()).error() == boost::system::errc::not_enough_memory); + BOOST_CHECK(lazy_await(lazy_error()).error() == boost::system::errc::not_enough_memory); + BOOST_CHECK(eager_await(eager_void()).error() == boost::system::errc::not_enough_memory); + BOOST_CHECK(lazy_await(lazy_void()).error() == boost::system::errc::not_enough_memory); + + BOOST_CHECK(eager_await(eager_coawait(eager_int, 5)).value() == "hi"); + BOOST_CHECK(lazy_await(lazy_coawait(lazy_int, 5)).value() == "hi"); + +#ifndef BOOST_NO_EXCEPTIONS + auto e = boost::copy_exception(custom_exception_type()); + BOOST_CHECK_THROW(lazy_await(result_exception(e)).value(), custom_exception_type); + BOOST_CHECK_THROW(lazy_await(outcome_exception(e)).value(), custom_exception_type); +#endif + + BOOST_CHECK(eager_await(eager_int2(5)) == 6); + BOOST_CHECK(lazy_await(lazy_int2(5)) == 6); + eager_await(eager_void2()); + lazy_await(lazy_void2()); +} +#else +int main(void) +{ + return 0; +} +#endif diff --git a/src/boost/libs/outcome/test/tests/default-construction.cpp b/src/boost/libs/outcome/test/tests/default-construction.cpp new file mode 100644 index 000000000..25726abb4 --- /dev/null +++ b/src/boost/libs/outcome/test/tests/default-construction.cpp @@ -0,0 +1,57 @@ +/* Unit testing for outcomes +(C) 2013-2020 Niall Douglas (4 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. +*/ + +#include +#include +#include + +BOOST_OUTCOME_AUTO_TEST_CASE(works_outcome_default_construction, "Tests that outcome default constructs when it ought to") +{ + using namespace BOOST_OUTCOME_V2_NAMESPACE; + struct udt + { + int _v{78}; + udt() = default; + constexpr explicit udt(int v) noexcept : _v(v) {} + udt(udt &&o) = delete; + udt(const udt &) = delete; + udt &operator=(udt &&o) = delete; + udt &operator=(const udt &) = delete; + ~udt() = default; + constexpr int operator*() const noexcept { return _v; } + }; + // One path is via success_type + outcome a(success()); + BOOST_CHECK(*a.value() == 78); + + // Other path is via explicit conversion from void + outcome c = success(); + outcome d(c); + BOOST_CHECK(*d.value() == 78); +} diff --git a/src/boost/libs/outcome/test/tests/experimental-core-outcome-status.cpp b/src/boost/libs/outcome/test/tests/experimental-core-outcome-status.cpp new file mode 100644 index 000000000..b7aed1d8c --- /dev/null +++ b/src/boost/libs/outcome/test/tests/experimental-core-outcome-status.cpp @@ -0,0 +1,225 @@ +/* Unit testing for outcomes +(C) 2013-2020 Niall Douglas (4 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. +*/ + +#include + +#define BOOST_OUTCOME_PREVENT_CONVERSION_WORKAROUND std +template using outcome = BOOST_OUTCOME_V2_NAMESPACE::experimental::status_outcome; +using BOOST_OUTCOME_V2_NAMESPACE::in_place_type; + +#include +#include + +#include + +#ifdef _MSC_VER +#pragma warning(disable : 4702) // unreachable code +#endif + +BOOST_OUTCOME_AUTO_TEST_CASE(works_status_code_outcome, "Tests that the outcome with status_code works as intended") +{ + using namespace BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE; + + { // errored int + outcome m(generic_code{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(), status_error); + BOOST_CHECK_NO_THROW(m.error()); + BOOST_CHECK_THROW(BOOST_OUTCOME_PREVENT_CONVERSION_WORKAROUND::rethrow_exception(m.failure()), generic_error); + } + { // errored void + outcome m(generic_code{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]() -> void { return m.value(); }()), generic_error); + BOOST_CHECK_NO_THROW(m.error()); + BOOST_CHECK_THROW(BOOST_OUTCOME_PREVENT_CONVERSION_WORKAROUND::rethrow_exception(m.failure()), generic_error); + } + { // valued int + outcome 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(!m.failure()); + } + { // moves do not clear state + outcome 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 + outcome 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(!m.failure()); + } + { // errored + error ec(errc::no_link); + outcome m(ec.clone()); + BOOST_CHECK(!m); + BOOST_CHECK(!m.has_value()); + BOOST_CHECK(m.has_error()); + BOOST_CHECK(!m.has_exception()); + BOOST_CHECK_THROW(m.value(), generic_error); + BOOST_CHECK(m.error() == ec); +#ifndef BOOST_NO_EXCEPTIONS + BOOST_CHECK(m.failure()); + try + { + BOOST_OUTCOME_PREVENT_CONVERSION_WORKAROUND::rethrow_exception(m.failure()); + } + catch(const generic_error &ex) + { + BOOST_CHECK(ex.code() == ec); + BOOST_CHECK(ex.code().value() == errc::no_link); + } +#endif + } +#if !defined(__APPLE__) || defined(__cpp_exceptions) + { // excepted + BOOST_OUTCOME_PREVENT_CONVERSION_WORKAROUND::error_code ec(5, BOOST_OUTCOME_PREVENT_CONVERSION_WORKAROUND::system_category()); + auto e = BOOST_OUTCOME_PREVENT_CONVERSION_WORKAROUND::make_exception_ptr(BOOST_OUTCOME_PREVENT_CONVERSION_WORKAROUND::system_error(ec)); // NOLINT + outcome 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_OUTCOME_PREVENT_CONVERSION_WORKAROUND::system_error); + BOOST_CHECK(m.exception() == e); +#ifndef BOOST_NO_EXCEPTIONS + BOOST_CHECK(m.failure()); + try + { + BOOST_OUTCOME_PREVENT_CONVERSION_WORKAROUND::rethrow_exception(m.failure()); + } + catch(const BOOST_OUTCOME_PREVENT_CONVERSION_WORKAROUND::system_error &ex) + { + BOOST_CHECK(ex.code() == ec); + BOOST_CHECK(ex.code().value() == 5); + } +#endif + } + { // custom error type + struct Foo + { + }; + auto e = BOOST_OUTCOME_PREVENT_CONVERSION_WORKAROUND::make_exception_ptr(Foo()); + outcome 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(), Foo); + BOOST_CHECK(m.exception() == e); + } + { // outcome should work + BOOST_OUTCOME_PREVENT_CONVERSION_WORKAROUND::error_code ec(5, BOOST_OUTCOME_PREVENT_CONVERSION_WORKAROUND::system_category()); + auto e = BOOST_OUTCOME_PREVENT_CONVERSION_WORKAROUND::make_exception_ptr(BOOST_OUTCOME_PREVENT_CONVERSION_WORKAROUND::system_error(ec)); + outcome m(e); + BOOST_CHECK(!m); + BOOST_CHECK(!m.has_value()); + BOOST_CHECK(!m.has_error()); + BOOST_CHECK(m.has_exception()); + } +#endif + + + { + outcome a(5); + outcome b(generic_code{errc::invalid_argument}); + std::cout << sizeof(a) << std::endl; // 40 bytes + a.assume_value(); + b.assume_error(); +#ifndef BOOST_NO_EXCEPTIONS + try + { + b.value(); + std::cerr << "fail" << std::endl; + std::terminate(); + } + catch(const generic_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, ""); + 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_trivially_destructible::value, ""); + static_assert(std::is_nothrow_destructible::value, ""); + + // Test void compiles + outcome c(in_place_type); + // Test int, void compiles + outcome d(in_place_type); + } + + { + // 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; + }; + // Test a udt which can only be constructed in place compiles + outcome g(in_place_type, 5, static_cast("niall"), nullptr); + // Does converting inplace construction also work? + outcome h(5, static_cast("niall"), nullptr); + BOOST_CHECK(h.has_value()); + } +} diff --git a/src/boost/libs/outcome/test/tests/experimental-core-result-status.cpp b/src/boost/libs/outcome/test/tests/experimental-core-result-status.cpp new file mode 100644 index 000000000..085c13dd2 --- /dev/null +++ b/src/boost/libs/outcome/test/tests/experimental-core-result-status.cpp @@ -0,0 +1,357 @@ +/* Unit testing for outcomes +(C) 2013-2020 Niall Douglas (8 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. +*/ + +#include + +template > using result = BOOST_OUTCOME_V2_NAMESPACE::experimental::status_result; +using BOOST_OUTCOME_V2_NAMESPACE::in_place_type; + +#include +#include + +#include +#include + +#ifndef BOOST_NO_EXCEPTIONS +// Custom error type with payload +struct payload +{ + BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE::errc ec{BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE::errc::success}; + const char *str{nullptr}; + payload() = default; + payload(BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE::errc _ec, const char *_str) + : ec(_ec) + , str(_str) + { + } +}; +struct payload_exception : std::exception +{ + const char *_what{nullptr}; + explicit payload_exception(const char *what) + : _what(what) + { + } + virtual const char *what() const noexcept override final { return _what; } // NOLINT +}; + +class _payload_domain; +using status_code_payload = BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE::status_code<_payload_domain>; +class _payload_domain : public BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE::status_code_domain +{ + template friend class status_code; + using _base = BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE::status_code_domain; + +public: + using value_type = payload; + using string_ref = _base::string_ref; + +public: + constexpr _payload_domain() noexcept : _base(0x7b782c8f935e34ba) {} + + static inline constexpr const _payload_domain &get(); + + virtual _base::string_ref name() const noexcept override final { return string_ref("payload domain"); } // NOLINT +protected: + virtual bool _do_failure(const BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE::status_code &code) const noexcept override final // NOLINT + { + assert(code.domain() == *this); // NOLINT + return static_cast(code).value().ec != BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE::errc::success; // NOLINT + } + virtual bool _do_equivalent(const BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE::status_code &code1, const BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE::status_code &code2) const noexcept override final // NOLINT + { + assert(code1.domain() == *this); // NOLINT + const auto &c1 = static_cast(code1); // NOLINT + if(code2.domain() == *this) + { + const auto &c2 = static_cast(code2); // NOLINT + return c1.value().ec == c2.value().ec; + } + return false; + } + virtual BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE::generic_code _generic_code(const BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE::status_code &code) const noexcept override final // NOLINT + { + assert(code.domain() == *this); // NOLINT + return static_cast(code).value().ec; // NOLINT + } + virtual _base::string_ref _do_message(const BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE::status_code &code) const noexcept override final // NOLINT + { + assert(code.domain() == *this); // NOLINT + const auto &c = static_cast(code); // NOLINT + return string_ref(BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE::detail::generic_code_message(c.value().ec)); + } + virtual void _do_throw_exception(const BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE::status_code &code) const override final // NOLINT + { + assert(code.domain() == *this); // NOLINT + const auto &c = static_cast(code); // NOLINT + throw payload_exception(c.value().str); + } +}; +constexpr _payload_domain payload_domain; +inline constexpr const _payload_domain &_payload_domain::get() +{ + return payload_domain; +} +inline status_code_payload make_status_code(payload c) noexcept +{ + return status_code_payload(BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE::in_place, c); +} +#endif + +BOOST_OUTCOME_AUTO_TEST_CASE(works_status_code_result, "Tests that the result with status_code works as intended") +{ + using namespace BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE; + + { // errored int + result m(generic_code{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(), generic_error); + BOOST_CHECK_NO_THROW(m.error()); + } + { // errored void + result m(generic_code{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]() -> void { return m.value(); }()), generic_error); + 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); + } + { // 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); + } + { // 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 + } + { // errored + error ec(errc::no_link); + result m(ec.clone()); + BOOST_CHECK(!m); + BOOST_CHECK(!m.has_value()); + BOOST_CHECK(m.has_error()); + // BOOST_CHECK(!m.has_exception()); + BOOST_CHECK_THROW(m.value(), generic_error); + BOOST_CHECK(m.error() == ec); + } + if(false) // NOLINT + { // void, void is permitted, but is not constructible + result *m = nullptr; + m->value(); + m->error(); + } + + { + // 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(generic_code{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 generic_error &e) + { + BOOST_CHECK(!strcmp(e.what(), b.error().message().c_str())); + } +#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); + + // Test a standard udt compiles + result d(in_place_type); + 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(generic_code{errc::not_enough_memory}); + BOOST_CHECK(h.has_value()); + BOOST_CHECK(i.has_error()); + } + + // Test direct use of error code enum works + { + constexpr result a(5), b(errc::invalid_argument); + static_assert(a.value() == 5, "a is not 5"); + static_assert(b.error() == errc::invalid_argument, "b is not errored"); + } + +#ifndef BOOST_NO_EXCEPTIONS + // Test payload facility + { + const char *niall = "niall"; + result b{payload{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 +} diff --git a/src/boost/libs/outcome/test/tests/experimental-p0709a.cpp b/src/boost/libs/outcome/test/tests/experimental-p0709a.cpp new file mode 100644 index 000000000..8823568ae --- /dev/null +++ b/src/boost/libs/outcome/test/tests/experimental-p0709a.cpp @@ -0,0 +1,169 @@ +/* Unit testing for outcomes +(C) 2017-2020 Niall Douglas (7 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. +*/ + +#include + +#include // for INT_MAX +#include + +// status_code> +using error = BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE::error; +// Outcome's result must be told when it is dealing with an erased status code +template using result = BOOST_OUTCOME_V2_NAMESPACE::experimental::status_result; + +enum class arithmetic_errc +{ + success, + divide_by_zero, + integer_divide_overflows, + not_integer_division +}; + +class _arithmetic_errc_domain; +using arithmetic_errc_error = BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE::status_code<_arithmetic_errc_domain>; + +class _arithmetic_errc_domain : public BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE::status_code_domain +{ + using _base = BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE::status_code_domain; + +public: + using value_type = arithmetic_errc; + + constexpr explicit _arithmetic_errc_domain(typename _base::unique_id_type id = 0x290f170194f0c6c7) noexcept : _base(id) {} + static inline constexpr const _arithmetic_errc_domain &get(); + + virtual _base::string_ref name() const noexcept override final // NOLINT + { + static string_ref v("arithmetic error domain"); + return v; // NOLINT + } + +protected: + virtual bool _do_failure(const BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE::status_code &code) const noexcept override final // NOLINT + { + assert(code.domain() == *this); // NOLINT + const auto &c1 = static_cast(code); // NOLINT + return c1.value() != arithmetic_errc::success; + } + virtual bool _do_equivalent(const BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE::status_code &, const BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE::status_code &) const noexcept override final { return false; } // NOLINT + virtual BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE::generic_code _generic_code(const BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE::status_code &) const noexcept override final { return {}; } // NOLINT + virtual _base::string_ref _do_message(const BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE::status_code &code) const noexcept override final // NOLINT + { + assert(code.domain() == *this); // NOLINT + const auto &c1 = static_cast(code); // NOLINT + switch(c1.value()) + { + case arithmetic_errc::success: + return _base::string_ref("success"); + case arithmetic_errc::divide_by_zero: + return _base::string_ref("divide by zero"); + case arithmetic_errc::integer_divide_overflows: + return _base::string_ref("integer divide overflows"); + case arithmetic_errc::not_integer_division: + return _base::string_ref("not integer division"); + } + return _base::string_ref("unknown"); + } + BOOST_OUTCOME_SYSTEM_ERROR2_NORETURN virtual void _do_throw_exception(const BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE::status_code &) const override final { abort(); } // NOLINT +}; + +constexpr _arithmetic_errc_domain arithmetic_errc_domain; +inline constexpr const _arithmetic_errc_domain &_arithmetic_errc_domain::get() +{ + return arithmetic_errc_domain; +} + + +// Tell status code about the available implicit conversion +inline arithmetic_errc_error make_status_code(arithmetic_errc e) +{ + return arithmetic_errc_error(BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE::in_place, e); +} + +BOOST_OUTCOME_V2_NAMESPACE_BEGIN +namespace trait +{ + // Tell Outcome that arithmetic_errc is convertible into std::error + template <> struct is_error_type_enum + { + static constexpr bool value = true; + }; +} +BOOST_OUTCOME_V2_NAMESPACE_END +// And tell Outcome how to perform the implicit conversion +inline error make_error_code(arithmetic_errc e) +{ + return arithmetic_errc_error(BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE::in_place, e); +} + + +result safe_divide(int i, int j) +{ + if(j == 0) + { + return arithmetic_errc::divide_by_zero; + } + if(i == INT_MIN && j == -1) + { + return arithmetic_errc::integer_divide_overflows; + } + if(i % j != 0) + { + return arithmetic_errc::not_integer_division; + } + return i / j; +} + +int caller2(int i, int j) +{ + auto r = safe_divide(i, j); + if(r) + { + return r.value(); + } + if(r.error() == arithmetic_errc::divide_by_zero) + { + return 0; + } + if(r.error() == arithmetic_errc::not_integer_division) + { + return i / j; // ignore + } + if(r.error() == arithmetic_errc::integer_divide_overflows) + { + return INT_MIN; + } + return 0; +} + +int main() +{ + printf("%d\n", caller2(5, 6)); // NOLINT + return 0; +} diff --git a/src/boost/libs/outcome/test/tests/fileopen.cpp b/src/boost/libs/outcome/test/tests/fileopen.cpp new file mode 100644 index 000000000..f2634d4fd --- /dev/null +++ b/src/boost/libs/outcome/test/tests/fileopen.cpp @@ -0,0 +1,87 @@ +/* Unit testing for outcomes +(C) 2013-2020 Niall Douglas (7 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. +*/ + +#define _CRT_SECURE_NO_WARNINGS + +#include +#include +#include + +#ifndef BOOST_NO_EXCEPTIONS + +#ifdef _MSC_VER +#define BOOST_OUTCOME_POSIX_OPEN ::_open +#include +#else +#define BOOST_OUTCOME_POSIX_OPEN ::open +#include +#endif + +BOOST_OUTCOME_AUTO_TEST_CASE(works_outcome_fileopen, "Tests that the outcome semantically represents opening a file") +{ + using namespace BOOST_OUTCOME_V2_NAMESPACE; + + //! [file_open_example] + auto openfile = [](std::string path) noexcept->outcome + { + int fd; + do + { + fd = BOOST_OUTCOME_POSIX_OPEN(path.c_str(), 0); // NOLINT + } while(-1 == fd && EINTR == errno); + try + { + if(-1 == fd) + { + int code = errno; + // If a temporary failure, this is an expected unexpected outcome + if(EBUSY == code || EISDIR == code || ELOOP == code || ENOENT == code || ENOTDIR == code || EPERM == code || EACCES == code) + { + return boost::system::error_code(code, boost::system::generic_category()); + } + // If a non-temporary failure, this is an unexpected outcome + return boost::copy_exception(boost::system::system_error(code, boost::system::generic_category(), strerror(code))); + } + return fd; + } + catch(...) + { + // Any exception thrown is truly unexpected + return boost::current_exception(); + } + }; + auto a = openfile("shouldneverexistnotever"); + BOOST_CHECK(!a); + BOOST_CHECK(!a.has_value()); + BOOST_CHECK(!a.has_exception()); + BOOST_CHECK(a.has_error()); + BOOST_CHECK(a.error() == boost::system::error_code(ENOENT, boost::system::generic_category())); + //! [file_open_example] +} +#endif diff --git a/src/boost/libs/outcome/test/tests/hooks.cpp b/src/boost/libs/outcome/test/tests/hooks.cpp new file mode 100644 index 000000000..857113cde --- /dev/null +++ b/src/boost/libs/outcome/test/tests/hooks.cpp @@ -0,0 +1,114 @@ +/* Unit testing for outcomes +(C) 2013-2020 Niall Douglas (14 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. +*/ + +#define _CRT_SECURE_NO_WARNINGS + +#include +#include +#include + +#include + +namespace hook_test +{ + using BOOST_OUTCOME_V2_NAMESPACE::in_place_type; + // Use static storage to convey extended error info from result construction to outcome conversion + static char extended_error_info[256]; + + // Use the error_code type as the ADL bridge for the hooks by creating a type here + struct error_code : public boost::system::error_code + { + using boost::system::error_code::error_code; + error_code() = default; + error_code(boost::system::error_code ec) // NOLINT + : boost::system::error_code(ec) + { + } + }; + // Localise result to using the local error_code so this namespace gets looked up for the hooks + template using result = BOOST_OUTCOME_V2_NAMESPACE::result; + // Specialise the result construction hook for our localised result + template constexpr inline void hook_result_construction(result *res, U && /*unused*/) noexcept + { + // Write the value in the result into the static storage + snprintf(extended_error_info, sizeof(extended_error_info), "%d", res->assume_value()); // NOLINT + } + template constexpr inline void hook_result_construction(result *res, U && /*unused*/) noexcept + { + // Write the value in the result into the static storage + snprintf(extended_error_info, sizeof(extended_error_info), "%s", res->assume_value().c_str()); // NOLINT + } +} // namespace hook_test + +BOOST_OUTCOME_AUTO_TEST_CASE(works_result_hooks, "Tests that you can hook result's construction") +{ + using namespace hook_test; + result a(5); + BOOST_CHECK(!strcmp(extended_error_info, "5")); // NOLINT + result b("niall"); + BOOST_CHECK(!strcmp(extended_error_info, "niall")); // NOLINT +} + +//! [extended_error_coding2] +namespace hook_test +{ + // Localise outcome to using the local error_code so this namespace gets looked up for the hooks + template using outcome = BOOST_OUTCOME_V2_NAMESPACE::outcome; + + // Specialise the outcome copy and move conversion hook for our localised result + template constexpr inline void hook_outcome_copy_construction(outcome *res, const result & /*unused*/) noexcept + { + // when copy constructing from a result, place extended_error_coding::extended_error_info into the payload + std::cout << "hook_outcome_copy_construction fires" << std::endl; + BOOST_OUTCOME_V2_NAMESPACE::hooks::override_outcome_exception(res, extended_error_info); + } + template constexpr inline void hook_outcome_move_construction(outcome *res, result && /*unused*/) noexcept + { + // when move constructing from a result, place extended_error_coding::extended_error_info into the payload + std::cout << "hook_outcome_move_construction fires" << std::endl; + BOOST_OUTCOME_V2_NAMESPACE::hooks::override_outcome_exception(res, extended_error_info); + } +} // namespace hook_test + +BOOST_OUTCOME_AUTO_TEST_CASE(works_outcome_hooks, "Tests that you can hook outcome's conversion from a result") +{ + using namespace hook_test; + outcome a(result(5)); + BOOST_REQUIRE(a.has_exception()); // NOLINT + BOOST_CHECK(a.exception() == "5"); + outcome b(result("niall")); + BOOST_CHECK(b.exception() == "niall"); + + // Make sure hook does not fire for any other kind of outcome copy or move, only when converting from our custom result only + outcome c(5); + outcome d(c); // can't be the same type as source, else copy elision takes place and no ADL hook calling + BOOST_CHECK(!d.has_exception()); + outcome e(BOOST_OUTCOME_V2_NAMESPACE::result(5)); + BOOST_CHECK(!e.has_exception()); +} diff --git a/src/boost/libs/outcome/test/tests/issue0007.cpp b/src/boost/libs/outcome/test/tests/issue0007.cpp new file mode 100644 index 000000000..d890c333e --- /dev/null +++ b/src/boost/libs/outcome/test/tests/issue0007.cpp @@ -0,0 +1,81 @@ +/* Unit testing for outcomes +(C) 2013-2020 Niall Douglas (5 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. +*/ + +#include +#include +#include +#include + +BOOST_OUTCOME_AUTO_TEST_CASE(issues_0007_test, "BOOST_OUTCOME_TRYV(expr) in a function whose return outcome's type has no default constructor fails to compile") +{ + using namespace BOOST_OUTCOME_V2_NAMESPACE; + struct udt // NOLINT + { + explicit udt(int /*unused*/) {} + // udt() = delete; + udt(const udt &) = default; + udt(udt &&) = default; + }; + { + auto f = []() -> result { + auto g = [] { return result(5); }; + /* This fails because BOOST_OUTCOME_TRYV() returns a result + which if it were valued void, would implicitly convert into a + default constructed udt which is not possible, hence the compile error. + */ + BOOST_OUTCOME_TRYV(g()); + return udt(5); + }; + (void) f(); + } + { + auto f = []() -> outcome { + auto g = [] { return outcome(5); }; + /* This fails because BOOST_OUTCOME_TRYV() returns a result + which if it were valued void, would implicitly convert into a + default constructed udt which is not possible, hence the compile error. + */ + BOOST_OUTCOME_TRYV(g()); + return udt(5); + }; + (void) f(); + } + { + auto f = []() -> outcome { + auto g = [] { return result(5); }; + /* This fails because BOOST_OUTCOME_TRYV() returns a result + which if it were valued void, would implicitly convert into a + default constructed udt which is not possible, hence the compile error. + */ + BOOST_OUTCOME_TRYV(g()); + return udt(5); + }; + (void) f(); + } +} diff --git a/src/boost/libs/outcome/test/tests/issue0009.cpp b/src/boost/libs/outcome/test/tests/issue0009.cpp new file mode 100644 index 000000000..7a61cfc92 --- /dev/null +++ b/src/boost/libs/outcome/test/tests/issue0009.cpp @@ -0,0 +1,52 @@ +/* Unit testing for outcomes +(C) 2013-2020 Niall Douglas (5 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. +*/ + +#include +#include +#include +#include + +BOOST_OUTCOME_AUTO_TEST_CASE(issues_0009_test, "Alternative TRY macros?") +{ +#ifdef BOOST_OUTCOME_TRYX + using namespace BOOST_OUTCOME_V2_NAMESPACE; + struct udt // NOLINT + { + explicit udt(int /*unused*/) {} + // udt() = delete; + udt(const udt &) = default; + udt(udt &&) = default; + }; + auto f = []() -> result { + auto g = [] { return result(5); }; + return udt(BOOST_OUTCOME_TRYX(g())); + }; + (void) f(); +#endif +} diff --git a/src/boost/libs/outcome/test/tests/issue0010.cpp b/src/boost/libs/outcome/test/tests/issue0010.cpp new file mode 100644 index 000000000..84a897773 --- /dev/null +++ b/src/boost/libs/outcome/test/tests/issue0010.cpp @@ -0,0 +1,88 @@ +/* Unit testing for outcomes +(C) 2013-2020 Niall Douglas (9 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. +*/ + +#include +#include +#include + +BOOST_OUTCOME_AUTO_TEST_CASE(issues_0010_test, "Expected's operator->(), operator*() and .error() throw exceptions when they should not") +{ + using namespace BOOST_OUTCOME_V2_NAMESPACE; + const char *a = "hi", *b = "bye"; + struct udt1 // NOLINT + { + const char *_v{nullptr}; + udt1() = default; + constexpr explicit udt1(const char *v) noexcept : _v(v) {} + constexpr udt1(udt1 &&o) noexcept : _v(o._v) { o._v = nullptr; } + udt1(const udt1 &) = default; + constexpr udt1 &operator=(udt1 &&o) noexcept + { + _v = o._v; + o._v = nullptr; + return *this; + } + udt1 &operator=(const udt1 &) = delete; + constexpr const char *operator*() const noexcept { return _v; } + }; + struct udt2 // NOLINT + { + const char *_v{nullptr}; + udt2() = default; + constexpr explicit udt2(const char *v) noexcept : _v(v) {} + constexpr udt2(udt2 &&o) noexcept : _v(o._v) { o._v = nullptr; } + udt2(const udt2 &) = default; + constexpr udt2 &operator=(udt2 &&o) noexcept + { + _v = o._v; + o._v = nullptr; + return *this; + } + udt1 &operator=(const udt1 &) = delete; + constexpr const char *operator*() const noexcept { return _v; } + }; + result p(udt1{a}); + result n(udt2{b}); + // State check + BOOST_CHECK(p.has_value()); + BOOST_CHECK(!n.has_value()); + // These should behave as expected (!) + // BOOST_CHECK_NO_THROW(p.value()); + // BOOST_CHECK_NO_THROW(n.value()); + // And state is not destroyed + BOOST_CHECK(p.has_value() && *p.assume_value() == a); + BOOST_CHECK(!n.has_value() && *n.assume_error() == b); + // LEWG Expected provides rvalue ref semantics for operator*(), error() and error_or() + udt1 a1(std::move(p.assume_value())); + BOOST_CHECK(*a1 == a); + BOOST_CHECK(*p.assume_value() == nullptr); + udt2 e2(std::move(n).assume_error()); + BOOST_CHECK(*e2 == b); + BOOST_CHECK(*n.assume_error() == nullptr); // NOLINT +} diff --git a/src/boost/libs/outcome/test/tests/issue0012.cpp b/src/boost/libs/outcome/test/tests/issue0012.cpp new file mode 100644 index 000000000..ff5d3c83e --- /dev/null +++ b/src/boost/libs/outcome/test/tests/issue0012.cpp @@ -0,0 +1,59 @@ +/* Unit testing for outcomes +(C) 2013-2020 Niall Douglas (5 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. +*/ + +#include +#include +#include + +BOOST_OUTCOME_AUTO_TEST_CASE(issues_0012_test, "outcome's copy assignment gets instantiated even when type T cannot be copied") +{ + using namespace BOOST_OUTCOME_V2_NAMESPACE; + const char *s = "hi"; + struct udt // NOLINT + { + const char *_v{nullptr}; + udt() = default; + constexpr explicit udt(const char *v) noexcept : _v(v) {} + constexpr udt(udt &&o) noexcept : _v(o._v) { o._v = nullptr; } + udt(const udt &) = delete; + constexpr udt &operator=(udt &&o) noexcept + { + _v = o._v; + o._v = nullptr; + return *this; + } + udt &operator=(const udt &) = delete; + constexpr const char *operator*() const noexcept { return _v; } + }; + static_assert(std::is_move_constructible>::value, "expected is not move constructible!"); + static_assert(!std::is_copy_constructible>::value, "expected is copy constructible!"); + outcome p(udt{s}), n(boost::system::error_code(ENOMEM, boost::system::generic_category())); + n = boost::system::error_code(EINVAL, boost::system::generic_category()); + BOOST_CHECK(n.error().value() == EINVAL); +} diff --git a/src/boost/libs/outcome/test/tests/issue0016.cpp b/src/boost/libs/outcome/test/tests/issue0016.cpp new file mode 100644 index 000000000..ddf5dd158 --- /dev/null +++ b/src/boost/libs/outcome/test/tests/issue0016.cpp @@ -0,0 +1,56 @@ +/* Unit testing for outcomes +(C) 2013-2020 Niall Douglas (5 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. +*/ + +#include +#include +#include + +BOOST_OUTCOME_AUTO_TEST_CASE(issues_0016_test, "Default constructor of T is sometimes compiled when T has no default constructor") +{ + using namespace BOOST_OUTCOME_V2_NAMESPACE; + struct udt + { + const char *_v{nullptr}; + udt() = delete; + constexpr explicit udt(const char *v) noexcept : _v(v) {} + constexpr udt(udt &&o) noexcept : _v(o._v) { o._v = nullptr; } + udt(const udt &) = delete; + constexpr udt &operator=(udt &&o) noexcept + { + _v = o._v; + o._v = nullptr; + return *this; + } + udt &operator=(const udt &) = delete; + ~udt() = default; + constexpr const char *operator*() const noexcept { return _v; } + }; + result n(boost::system::error_code(ENOMEM, boost::system::generic_category())); + (void) n; +} diff --git a/src/boost/libs/outcome/test/tests/issue0059.cpp b/src/boost/libs/outcome/test/tests/issue0059.cpp new file mode 100644 index 000000000..867ec6670 --- /dev/null +++ b/src/boost/libs/outcome/test/tests/issue0059.cpp @@ -0,0 +1,60 @@ +/* Unit testing for outcomes +(C) 2013-2020 Niall Douglas (8 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. +*/ + +#include +#include +#include +#include + +BOOST_OUTCOME_AUTO_TEST_CASE(issues_0059_test, "result supported?") +{ +#if defined(_MSC_VER) || __cplusplus >= 201700 + using namespace BOOST_OUTCOME_V2_NAMESPACE; + struct udt + { + const char *_v{nullptr}; + udt() = delete; + constexpr udt(const char *v) noexcept : _v(v) {} // NOLINT + udt(udt &&o) = delete; + udt(const udt &) = delete; + udt &operator=(udt &&o) = delete; + udt &operator=(const udt &) = delete; + ~udt() = default; + constexpr const char *operator*() const noexcept { return _v; } + }; + const char *niall = "niall"; + auto f = [niall]() -> result { + auto g = [niall]() -> result { return {niall}; }; + BOOST_OUTCOME_TRY(v, g()); // this must never copy nor move + BOOST_CHECK(*v == niall); + return success(); + }; + (void) f(); +#endif +} diff --git a/src/boost/libs/outcome/test/tests/issue0061.cpp b/src/boost/libs/outcome/test/tests/issue0061.cpp new file mode 100644 index 000000000..aab9a3bff --- /dev/null +++ b/src/boost/libs/outcome/test/tests/issue0061.cpp @@ -0,0 +1,128 @@ +/* Unit testing for outcomes +(C) 2013-2020 Niall Douglas (6 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. +*/ + +#include +#include +#include + +BOOST_OUTCOME_AUTO_TEST_CASE(issues_0061_result, "result does not compare to incompatible result") +{ + using namespace BOOST_OUTCOME_V2_NAMESPACE; + struct udt1 + { + const char *_v{nullptr}; + udt1() = default; + constexpr udt1(const char *v) noexcept : _v(v) {} // NOLINT + udt1(udt1 &&o) = delete; + udt1(const udt1 &) = delete; + udt1 &operator=(udt1 &&o) = delete; + udt1 &operator=(const udt1 &) = delete; + ~udt1() = default; + constexpr const char *operator*() const noexcept { return _v; } + }; + struct udt2 + { + const char *_v{nullptr}; + udt2() = default; + constexpr explicit udt2(const char *v) noexcept : _v(v) {} + udt2(udt2 &&o) = delete; + udt2(const udt2 &) = delete; + udt2 &operator=(udt2 &&o) = delete; + udt2 &operator=(const udt2 &) = delete; + ~udt2() = default; + constexpr const char *operator*() const noexcept { return _v; } + bool operator==(const udt1 &o) const noexcept { return _v == *o; } + bool operator!=(const udt1 &o) const noexcept { return _v != *o; } + }; + using result1 = result; + using result2 = result; + + result1 a(5); + result2 b(5); + BOOST_CHECK(b == a); // udt2 will compare to udt1 + BOOST_CHECK(!(b != a)); // udt2 will compare to udt1 + + result c = success(); + result d = success(); + BOOST_CHECK(c == d); + BOOST_CHECK(!(c != d)); + + BOOST_CHECK(a == success()); + BOOST_CHECK(success() == a); + BOOST_CHECK(b != failure("foo")); + BOOST_CHECK(failure("foo") != b); +} + +BOOST_OUTCOME_AUTO_TEST_CASE(issues_0061_outcome, "outcome does not compare to incompatible outcome") +{ + using namespace BOOST_OUTCOME_V2_NAMESPACE; + struct udt1 + { + const char *_v{nullptr}; + udt1() = default; + constexpr udt1(const char *v) noexcept : _v(v) {} // NOLINT + udt1(udt1 &&o) = delete; + udt1(const udt1 &) = delete; + udt1 &operator=(udt1 &&o) = delete; + udt1 &operator=(const udt1 &) = delete; + ~udt1() = default; + constexpr const char *operator*() const noexcept { return _v; } + }; + struct udt2 + { + const char *_v{nullptr}; + udt2() = default; + constexpr explicit udt2(const char *v) noexcept : _v(v) {} + udt2(udt2 &&o) = delete; + udt2(const udt2 &) = delete; + udt2 &operator=(udt2 &&o) = delete; + udt2 &operator=(const udt2 &) = delete; + ~udt2() = default; + constexpr const char *operator*() const noexcept { return _v; } + bool operator==(const udt1 &o) const noexcept { return _v == *o; } + bool operator!=(const udt1 &o) const noexcept { return _v != *o; } + }; + using outcome1 = outcome; + using outcome2 = outcome; + + outcome1 a(5), _a(6); + outcome2 b(5); + BOOST_CHECK(b == a); // udt2 will compare to udt1 + BOOST_CHECK(!(b != a)); // udt2 will compare to udt1 + + outcome c = success(); + outcome d = success(); + BOOST_CHECK(c == d); + BOOST_CHECK(!(c != d)); + + BOOST_CHECK(a == success()); + BOOST_CHECK(success() == a); + BOOST_CHECK(b != failure("foo")); + BOOST_CHECK(failure("foo") != b); +} diff --git a/src/boost/libs/outcome/test/tests/issue0064.cpp b/src/boost/libs/outcome/test/tests/issue0064.cpp new file mode 100644 index 000000000..6dbdcd91c --- /dev/null +++ b/src/boost/libs/outcome/test/tests/issue0064.cpp @@ -0,0 +1,50 @@ +/* Unit testing for outcomes +(C) 2013-2020 Niall Douglas (4 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. +*/ + +#include +#include +#include +#include +#include + +BOOST_OUTCOME_AUTO_TEST_CASE(issues_0064_outcome, "BOOST_OUTCOME_TRY on excepted outcome is propagating a null error rather than just an exception") +{ +#ifndef BOOST_NO_EXCEPTIONS + using namespace BOOST_OUTCOME_V2_NAMESPACE; + auto f = []() -> outcome { + auto h = []() -> outcome { return boost::copy_exception(std::runtime_error("hello")); }; + BOOST_OUTCOME_TRY((h())); + return 0; + }(); + std::cout << print(f) << std::endl; + BOOST_CHECK(!f.has_value()); + BOOST_CHECK(!f.has_error()); + BOOST_CHECK(f.has_exception()); +#endif +} diff --git a/src/boost/libs/outcome/test/tests/issue0065.cpp b/src/boost/libs/outcome/test/tests/issue0065.cpp new file mode 100644 index 000000000..c537a38a7 --- /dev/null +++ b/src/boost/libs/outcome/test/tests/issue0065.cpp @@ -0,0 +1,66 @@ +/* Unit testing for outcomes +(C) 2013-2020 Niall Douglas (3 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. +*/ + +#include +#include +#include +#include + +BOOST_OUTCOME_AUTO_TEST_CASE(issues_0065_outcome, "BOOST_OUTCOME_TRY does not preserve the exception_ptr") +{ +#ifndef BOOST_NO_EXCEPTIONS + using namespace BOOST_OUTCOME_V2_NAMESPACE; + auto g = []() -> outcome { + auto f = []() -> outcome { + try + { + throw std::runtime_error{"XXX"}; + } + catch(...) + { + return boost::current_exception(); + } + }; + BOOST_OUTCOME_TRY(ans, (f())); + return ans; + }; + outcome o = g(); + BOOST_CHECK(!o); + BOOST_CHECK(o.has_exception()); + try + { + o.value(); + BOOST_CHECK(false); + } + catch(const std::runtime_error &e) + { + BOOST_CHECK(!strcmp(e.what(), "XXX")); + } +#endif +} diff --git a/src/boost/libs/outcome/test/tests/issue0071.cpp b/src/boost/libs/outcome/test/tests/issue0071.cpp new file mode 100644 index 000000000..333fb2088 --- /dev/null +++ b/src/boost/libs/outcome/test/tests/issue0071.cpp @@ -0,0 +1,45 @@ +/* Unit testing for outcomes +(C) 2013-2020 Niall Douglas (4 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. +*/ + +#include +#include +#include + +BOOST_OUTCOME_AUTO_TEST_CASE(issues_0071_outcome, "Implicit construction is over eager") +{ + using namespace BOOST_OUTCOME_V2_NAMESPACE; + struct udt + { + int v; + explicit udt(int a) + : v(a){}; + }; + result m = udt{5}; + BOOST_CHECK(m.value().v == 5); +} diff --git a/src/boost/libs/outcome/test/tests/issue0095.cpp b/src/boost/libs/outcome/test/tests/issue0095.cpp new file mode 100644 index 000000000..c1ed1d460 --- /dev/null +++ b/src/boost/libs/outcome/test/tests/issue0095.cpp @@ -0,0 +1,58 @@ +/* Unit testing for outcomes +(C) 2013-2020 Niall Douglas (6 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. +*/ + +#include +#include +#include +#include + +namespace issue0095 +{ + namespace out = BOOST_OUTCOME_V2_NAMESPACE; + + struct E + { + }; + // struct F : E {}; + struct F + { + operator E() const { return E{}; } // NOLINT + }; + + out::result f() { return F{}; } + out::result e() + { + BOOST_OUTCOME_TRY(i, (f())); + return i; + } +} // namespace issue0095 +BOOST_OUTCOME_AUTO_TEST_CASE(issues_0095_outcome, "operator conversions on E type cause TRY to fail") +{ + BOOST_CHECK(issue0095::e().has_error()); +} diff --git a/src/boost/libs/outcome/test/tests/issue0115.cpp b/src/boost/libs/outcome/test/tests/issue0115.cpp new file mode 100644 index 000000000..17d852274 --- /dev/null +++ b/src/boost/libs/outcome/test/tests/issue0115.cpp @@ -0,0 +1,57 @@ +/* Unit testing for outcomes +(C) 2013-2020 Niall Douglas (1 commit) + + +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. +*/ + +#include +#include +#include + +BOOST_OUTCOME_AUTO_TEST_CASE(issues_0115_outcome, "Initialization from `failure_type` drops default-constructed values") +{ + namespace out = BOOST_OUTCOME_V2_NAMESPACE; + + out::outcome o1 = boost::system::error_code{}; + BOOST_CHECK(o1.has_error()); + BOOST_CHECK(!o1.has_exception()); + + out::outcome o2 = out::failure(boost::system::error_code{}); + BOOST_CHECK(o2.has_error()); + BOOST_CHECK(!o2.has_exception()); + + out::outcome o3(boost::system::error_code{}, boost::exception_ptr{}); + BOOST_CHECK(o3.has_error()); + BOOST_CHECK(o3.has_exception()); + + out::outcome o4 = out::failure(boost::system::error_code{}, boost::exception_ptr{}); + BOOST_CHECK(o4.has_error()); + BOOST_CHECK(o4.has_exception()); + + out::outcome o5 = out::failure(boost::exception_ptr{}); + BOOST_CHECK(!o5.has_error()); + BOOST_CHECK(o5.has_exception()); +} diff --git a/src/boost/libs/outcome/test/tests/issue0116.cpp b/src/boost/libs/outcome/test/tests/issue0116.cpp new file mode 100644 index 000000000..168c3e5c4 --- /dev/null +++ b/src/boost/libs/outcome/test/tests/issue0116.cpp @@ -0,0 +1,45 @@ +/* Unit testing for outcomes +(C) 2013-2020 Niall Douglas (2 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. +*/ + +#include +#include +#include + +BOOST_OUTCOME_AUTO_TEST_CASE(issues_0116_outcome, "Bad implementation of outcome::operator==") +{ + namespace out = BOOST_OUTCOME_V2_NAMESPACE; + + out::outcome o1 = 1; + BOOST_CHECK(!o1.has_error()); + + out::outcome o2 = out::failure(boost::system::error_code{EINVAL, boost::system::generic_category()}); + BOOST_CHECK(o2.has_error()); + + BOOST_CHECK(o1 != o2); +} diff --git a/src/boost/libs/outcome/test/tests/issue0140.cpp b/src/boost/libs/outcome/test/tests/issue0140.cpp new file mode 100644 index 000000000..bb86f0619 --- /dev/null +++ b/src/boost/libs/outcome/test/tests/issue0140.cpp @@ -0,0 +1,96 @@ +/* Unit testing for outcomes +(C) 2013-2020 Niall Douglas (2 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. +*/ + +#include +#include +#include + +BOOST_OUTCOME_AUTO_TEST_CASE(issues_0140_result, "Construction of non copy constructible value_type fails to not compile") +{ + namespace out = BOOST_OUTCOME_V2_NAMESPACE; + + enum op + { + constructed, + moved, + copied + }; + struct NotCopyMoveConstructible + { + op v{op::constructed}; + NotCopyMoveConstructible() = default; + NotCopyMoveConstructible(const NotCopyMoveConstructible &o) = delete; + NotCopyMoveConstructible(NotCopyMoveConstructible &&) = delete; + NotCopyMoveConstructible &operator=(const NotCopyMoveConstructible &) = delete; + NotCopyMoveConstructible &operator=(NotCopyMoveConstructible &&) = delete; + ~NotCopyMoveConstructible() = default; + }; + struct NotMoveConstructible + { + op v{op::constructed}; + NotMoveConstructible() = default; + NotMoveConstructible(const NotMoveConstructible & /*unused*/) + : v(op::copied) + { + } + NotMoveConstructible(NotMoveConstructible &&) = delete; + NotMoveConstructible &operator=(const NotMoveConstructible &) = delete; + NotMoveConstructible &operator=(NotMoveConstructible &&) = delete; + ~NotMoveConstructible() = default; + }; + struct NotCopyConstructible + { + op v{op::constructed}; + NotCopyConstructible() = default; + NotCopyConstructible(NotCopyConstructible && /*unused*/) noexcept : v(op::moved) {} + NotCopyConstructible(const NotCopyConstructible & /*unused*/) = delete; + NotCopyConstructible &operator=(const NotCopyConstructible &) = delete; + NotCopyConstructible &operator=(NotCopyConstructible &&) = delete; + ~NotCopyConstructible() = default; + }; + + // Uncopyable and immovable items should be neither copyable nor moveable + static_assert(!std::is_copy_constructible>::value, "result is copy constructible!"); + static_assert(!std::is_move_constructible>::value, "result is move constructible!"); + static_assert(!std::is_convertible::value, ""); + static_assert(!std::is_convertible::value, ""); + static_assert(!std::is_constructible, const NotCopyMoveConstructible &>::value, "result is constructible from const NotCopyMoveConstructible&!"); + static_assert(!std::is_constructible, NotCopyMoveConstructible &&>::value, "result is constructible from NotCopyMoveConstructible&&!"); + + // Immovable items should fall back to copy + static_assert(!std::is_move_constructible::value, "NotMoveConstructible is move constructible!"); + static_assert(std::is_move_constructible>::value, "result is not move constructible!"); + static_assert(std::is_constructible, const NotMoveConstructible &>::value, "result is not constructible from NotMoveConstructible&&!"); + + // Uncopyable items should never move (this was the bug report) + static_assert(!std::is_copy_constructible>::value, "result is copy constructible!"); + static_assert(!std::is_constructible, const NotCopyConstructible &>::value, "result is constructible from const NotCopyConstructible&!"); + + BOOST_CHECK(true); +} diff --git a/src/boost/libs/outcome/test/tests/issue0182.cpp b/src/boost/libs/outcome/test/tests/issue0182.cpp new file mode 100644 index 000000000..7bf06d424 --- /dev/null +++ b/src/boost/libs/outcome/test/tests/issue0182.cpp @@ -0,0 +1,42 @@ +/* Unit testing for outcomes +(C) 2013-2020 Niall Douglas (1 commit) + + +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. +*/ + +#include +#include +#include + +BOOST_OUTCOME_AUTO_TEST_CASE(issues_0189_test, "result.value() compiles without tripping fail_to_compile_observers") +{ + namespace outcome = BOOST_OUTCOME_V2_NAMESPACE; + static_assert(!outcome::trait::is_error_code_available::value, "int is clearly not a source for make_error_code()"); + static_assert(!outcome::trait::is_exception_ptr_available::value, "int is clearly not a source for make_exception_ptr()"); + //outcome::result r(5); + //r.value(); + BOOST_CHECK(true); +} diff --git a/src/boost/libs/outcome/test/tests/issue0203.cpp b/src/boost/libs/outcome/test/tests/issue0203.cpp new file mode 100644 index 000000000..800fa8373 --- /dev/null +++ b/src/boost/libs/outcome/test/tests/issue0203.cpp @@ -0,0 +1,103 @@ +/* Unit testing for outcomes +(C) 2013-2020 Niall Douglas (1 commit) + + +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. +*/ + +#include +#include +#include +#include + +namespace outcome = BOOST_OUTCOME_V2_NAMESPACE; + +enum class error +{ + test, + abcde +}; + +class error_category_impl : public std::error_category +{ +public: + const char *name() const noexcept override { return "test"; } + + std::string message(int code) const noexcept override + { + switch(static_cast(code)) + { + case error::test: + return "test"; + case error::abcde: + return "abcde"; + } + return "unknown"; + } +}; +const std::error_category &error_category() noexcept +{ + static error_category_impl instance; + return instance; +} + +boost::system::error_code make_error_code(error error) noexcept +{ + return {static_cast(error), error_category()}; +} + +namespace std +{ + template <> struct is_error_code_enum : true_type + { + }; +} // namespace std + +template using enum_result = outcome::basic_result>; + +enum_result test() +{ + return 5; +} + +outcome::std_result abc() +{ + static_assert(std::is_error_code_enum::value, "custom enum is not marked convertible to error code"); + static_assert(std::is_constructible::value, "error code is not explicitly constructible from custom enum"); + static_assert(std::is_convertible::value, "error code is not implicitly constructible from custom enum"); + boost::system::error_code ec = error::test; // custom enum is definitely convertible to error code + BOOST_OUTCOME_TRY(test()); // hence this should compile, as implicit conversions work here + (void) ec; + + // But explicit conversions are required between dissimilar basic_result, implicit conversions are disabled + static_assert(std::is_constructible, enum_result>::value, "basic_result with error code is not explicitly constructible from basic_result with custom enum"); + static_assert(!std::is_convertible, outcome::std_result>::value, "basic_result with error code is implicitly constructible from basic_result with custom enum"); + return 5; +} + +BOOST_OUTCOME_AUTO_TEST_CASE(issues_0203_test, "enum convertible to error code works as designed") +{ + BOOST_CHECK(abc().value() == 5); +} diff --git a/src/boost/libs/outcome/test/tests/issue0210.cpp b/src/boost/libs/outcome/test/tests/issue0210.cpp new file mode 100644 index 000000000..0ac1b759a --- /dev/null +++ b/src/boost/libs/outcome/test/tests/issue0210.cpp @@ -0,0 +1,78 @@ +/* Unit testing for outcomes +(C) 2013-2020 Niall Douglas (1 commit) + + +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. +*/ + +#include +#include +#include +#include + +namespace issues210 +{ + namespace outcome = BOOST_OUTCOME_V2_NAMESPACE; + + struct error + { + boost::system::error_code code; + }; + + // say that my custom error code type is error code compatible + inline boost::system::error_code make_error_code(error error) noexcept { return error.code; } + + template using custom_result = outcome::basic_result>; + + // source of custom result type with error code compatible error type + inline custom_result funcA(int x) { return x; } + + // Is the custom result type explicitly constructible to an ordinary result type? + inline outcome::result funcB(int x) { return outcome::result(funcA(x)); } + + // Does the custom result type TRY-convert to an ordinary result type? + inline outcome::result func1(int x) + { + BOOST_OUTCOME_TRY(y, funcA(x)); + return funcB(y); + } + + // Is the custom result type explicitly constructible to an ordinary outcome type? + inline outcome::outcome funcC(int x) { return outcome::outcome(funcA(x)); } + + // Does the custom result type TRY-convert to an ordinary outcome type? + inline outcome::outcome func2(int x) + { + BOOST_OUTCOME_TRY(y, funcA(x)); + return funcC(y); + } +} // namespace issues210 + + +BOOST_OUTCOME_AUTO_TEST_CASE(issues_0210_test, "result with error code compatible custom E does not TRY into a result function") +{ + BOOST_CHECK(issues210::func1(5).value() == 5); + BOOST_CHECK(issues210::func2(5).value() == 5); +} diff --git a/src/boost/libs/outcome/test/tests/issue0220.cpp b/src/boost/libs/outcome/test/tests/issue0220.cpp new file mode 100644 index 000000000..0da6eaf16 --- /dev/null +++ b/src/boost/libs/outcome/test/tests/issue0220.cpp @@ -0,0 +1,55 @@ +/* Unit testing for outcomes +(C) 2013-2020 Niall Douglas (1 commit) + + +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. +*/ + +#include +#include +#include +#include + +#ifndef BOOST_OUTCOME_SYSTEM_ERROR2_NOT_POSIX +namespace issues220 +{ + namespace outcome_e = BOOST_OUTCOME_V2_NAMESPACE::experimental; + + template using Result = outcome_e::status_result; + + template using PosixResult = outcome_e::status_result; + + Result convert(const PosixResult &posix_result) { return Result(posix_result); } +} // namespace issues220 +#endif + + +BOOST_OUTCOME_AUTO_TEST_CASE(issues_0220_test, "ubsan reports reference binding to null pointer") +{ +#ifndef BOOST_OUTCOME_SYSTEM_ERROR2_NOT_POSIX + using namespace issues220; + BOOST_CHECK(convert(PosixResult(0)).value() == 0); +#endif +} diff --git a/src/boost/libs/outcome/test/tests/noexcept-propagation.cpp b/src/boost/libs/outcome/test/tests/noexcept-propagation.cpp new file mode 100644 index 000000000..3754ff44d --- /dev/null +++ b/src/boost/libs/outcome/test/tests/noexcept-propagation.cpp @@ -0,0 +1,84 @@ +/* Unit testing for outcomes +(C) 2013-2020 Niall Douglas (6 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. +*/ + +#include +#include +#include + +#ifdef _MSC_VER +#pragma warning(disable : 4127) // conditional expression is constant +#endif + +#ifndef BOOST_NO_EXCEPTIONS +// std nothrow traits seem to return random values if exceptions are disabled on MSVC +BOOST_OUTCOME_AUTO_TEST_CASE(works_outcome_noexcept, "Tests that the outcome correctly inherits noexcept from its type R") +{ + using namespace BOOST_OUTCOME_V2_NAMESPACE; + { + using type = outcome; + BOOST_CHECK(std::is_nothrow_copy_constructible::value); + BOOST_CHECK(std::is_nothrow_move_constructible::value); + BOOST_CHECK(std::is_nothrow_copy_assignable::value); + BOOST_CHECK(std::is_nothrow_move_assignable::value); + BOOST_CHECK(std::is_nothrow_destructible::value); + } + { + using type = outcome; + BOOST_CHECK(std::is_nothrow_copy_constructible::value == std::is_nothrow_copy_constructible::value); + BOOST_CHECK(std::is_nothrow_move_constructible::value == std::is_nothrow_move_constructible::value); + BOOST_CHECK(std::is_nothrow_copy_assignable::value == std::is_nothrow_copy_assignable::value); + BOOST_CHECK(std::is_nothrow_move_assignable::value == std::is_nothrow_move_assignable::value); + BOOST_CHECK(std::is_nothrow_destructible::value == std::is_nothrow_destructible::value); + } + { + struct Except + { + int n; + Except() = delete; + Except(const Except & /*unused*/) noexcept(false) + : n(0) + { + } + Except(Except && /*unused*/) noexcept(false) + : n(0) + { + } + Except &operator=(const Except & /*unused*/) noexcept(false) { return *this; } + Except &operator=(Except && /*unused*/) noexcept(false) { return *this; } + ~Except() noexcept(false) { n = 0; } + }; + using type = outcome; + BOOST_CHECK(!std::is_nothrow_copy_constructible::value); + BOOST_CHECK(!std::is_nothrow_move_constructible::value); + BOOST_CHECK(!std::is_nothrow_copy_assignable::value); + BOOST_CHECK(!std::is_nothrow_move_assignable::value); + BOOST_CHECK(!std::is_nothrow_destructible::value); + } +} +#endif diff --git a/src/boost/libs/outcome/test/tests/propagate.cpp b/src/boost/libs/outcome/test/tests/propagate.cpp new file mode 100644 index 000000000..0e3cbb545 --- /dev/null +++ b/src/boost/libs/outcome/test/tests/propagate.cpp @@ -0,0 +1,90 @@ +/* Unit testing for outcomes +(C) 2013-2020 Niall Douglas (7 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. +*/ + +#include +#include +#include +#include + +BOOST_OUTCOME_AUTO_TEST_CASE(works_outcome_propagate, "Tests that the outcome propagates errors between different editions of itself") +{ + using namespace BOOST_OUTCOME_V2_NAMESPACE; + { + auto t0 = [&](int a) { return result(boost::system::error_code(a, boost::system::generic_category())); }; + auto t1 = [&](int a) { + result f(t0(a)); // double is constructible from long + BOOST_CHECK(!f.has_value()); + BOOST_CHECK(f.has_error()); + return f; + }; + auto t2 = [&](int a) { + result f(t1(a).error()); + BOOST_CHECK(!f.has_value()); + BOOST_CHECK(f.has_error()); + return f; + }; + auto t3 = [&](int a) { + outcome f(t2(a)); + BOOST_CHECK(!f.has_value()); + BOOST_CHECK(f.has_error()); + return f; + }; + BOOST_CHECK(t3(5).error().value() == 5); + result a2{result(in_place_type)}; + result a3{result(boost::system::error_code(5, boost::system::generic_category()))}; + BOOST_CHECK(a2.has_value()); + BOOST_CHECK(!a2.has_error()); + BOOST_CHECK(!a3.has_value()); + BOOST_CHECK(a3.has_error()); + } + { + auto t0 = [&](int a) { return result(a); }; + auto t1 = [&](int a) -> result { + BOOST_OUTCOME_TRY(f, (t0(a))); + return std::to_string(f); + }; + BOOST_CHECK(t1(5).value() == "5"); + } + { + auto t0 = [&](int a) { return result(a); }; + auto t1 = [&](int a) -> outcome { + BOOST_OUTCOME_TRY(f, (t0(a))); + return std::to_string(f); + }; + BOOST_CHECK(t1(5).value() == "5"); + } + { + auto t0 = [&](int a) -> result { return result(a); }; + auto t1 = [&](int a) -> outcome { + BOOST_OUTCOME_TRY((t0(a))); + return outcome(in_place_type); + }; + (void) t1(5); + } +} diff --git a/src/boost/libs/outcome/test/tests/serialisation.cpp b/src/boost/libs/outcome/test/tests/serialisation.cpp new file mode 100644 index 000000000..55d978dd5 --- /dev/null +++ b/src/boost/libs/outcome/test/tests/serialisation.cpp @@ -0,0 +1,49 @@ +/* Unit testing for outcomes +(C) 2013-2020 Niall Douglas (7 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. +*/ + +#include +#include +#include + +BOOST_OUTCOME_AUTO_TEST_CASE(works_outcome_serialisation, "Tests that the outcome serialises and deserialises as intended") +{ +#if !defined(__APPLE__) || defined(__cpp_exceptions) + using namespace BOOST_OUTCOME_V2_NAMESPACE; + outcome a("niall"), b(boost::system::error_code(5, boost::system::generic_category())), c(boost::copy_exception(std::ios_base::failure("A test failure message"))); + std::cout << "a contains " << print(a) << " and b contains " << print(b) << " and c contains " << print(c) << std::endl; + + std::stringstream ss; + outcome d(success(5)); + ss << d; + ss.seekg(0); + outcome e(failure("")); + ss >> e; + BOOST_CHECK(d == e); +#endif +} diff --git a/src/boost/libs/outcome/test/tests/success-failure.cpp b/src/boost/libs/outcome/test/tests/success-failure.cpp new file mode 100644 index 000000000..af1c3e46d --- /dev/null +++ b/src/boost/libs/outcome/test/tests/success-failure.cpp @@ -0,0 +1,69 @@ +/* Unit testing for outcomes +(C) 2017-2020 Niall Douglas (10 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. +*/ + +#include +#include +#include + +#include + +BOOST_OUTCOME_AUTO_TEST_CASE(works_success_failure, "Tests that the success and failure type sugars work as intended") +{ + using namespace BOOST_OUTCOME_V2_NAMESPACE; +#ifdef __cpp_deduction_guides + std::cout << "__cpp_deduction_guides = 1" << std::endl; +#endif + { + auto a = success(5); + auto b = success(); + auto c = success("hello"); + static_assert(std::is_same>::value, ""); + static_assert(std::is_same>::value, ""); + static_assert(std::is_same>::value, ""); + static_assert(std::is_same::value, ""); + // static_assert(std::is_same::value, ""); + static_assert(std::is_same::value, ""); + } +#if !defined(__APPLE__) || defined(__cpp_exceptions) + { + auto e = boost::copy_exception(std::exception()); + auto a = failure(5); + auto b = failure(5, e); + auto c = failure(5, 5); + static_assert(std::is_same>::value, ""); + static_assert(std::is_same>::value, ""); + static_assert(std::is_same>::value, ""); + static_assert(std::is_same::value, ""); + static_assert(std::is_same::value, ""); + static_assert(std::is_same::value, ""); + static_assert(std::is_same::value, ""); + static_assert(std::is_same::value, ""); + } +#endif +} diff --git a/src/boost/libs/outcome/test/tests/swap.cpp b/src/boost/libs/outcome/test/tests/swap.cpp new file mode 100644 index 000000000..0bcba3802 --- /dev/null +++ b/src/boost/libs/outcome/test/tests/swap.cpp @@ -0,0 +1,432 @@ +/* Unit testing for outcomes +(C) 2013-2020 Niall Douglas (5 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. +*/ + +#include +#include +#include + +/* Should be this: + +78 move constructor count = 2 +65 move assignment count = 3 +78 move assignment count = 1 +65 move constructor count = 2 +78 move assignment count = 0 +65 move assignment count = 1 + +78 move constructor count = 1 +65 move assignment count = 2 +78 move assignment count = 0 +65 move assignment count = 1 +78 move assignment count = 0 + +78 move constructor count = 2 +65 move assignment count = 3 +78 move assignment count = 1 +65 move constructor count = 2 +78 move assignment count = 0 +65 move assignment count = 1 + +78 move constructor count = 1 +65 move assignment count = 2 +78 move assignment count = 0 +65 move assignment count = 1 +78 move assignment count = 0 + +78 move constructor count = 2 +65 move assignment count = 3 +78 move assignment count = 1 +65 move constructor count = 2 +78 move assignment count = 0 +65 move assignment count = 1 + +78 move constructor count = 1 +65 move assignment count = 2 +78 move assignment count = 0 +65 move assignment count = 1 +78 move assignment count = 0 + +78 move constructor count = 2 +65 move assignment count = 3 +78 move assignment count = 1 +65 move constructor count = 2 +78 move assignment count = 0 +65 move assignment count = 1 + +78 move constructor count = 1 +65 move assignment count = 2 +78 move assignment count = 0 +65 move assignment count = 1 +78 move assignment count = 0 +*/ + +#ifndef BOOST_NO_EXCEPTIONS +#ifdef _MSC_VER +#pragma warning(push) +#pragma warning(disable: 4297) // function assumed not to throw an exception but does +#endif +#if defined(__GNUC__) && !defined(__clang__) +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wterminate" +#endif +template struct Throwy +{ + int count{0}, inc{0}, id{0}; + Throwy() = default; + Throwy(int c, int d, int i = 1) noexcept + : count(c) + , inc(i) + , id(d) + { + } + Throwy(const Throwy &) = delete; + Throwy &operator=(const Throwy &) = delete; + Throwy(Throwy &&o) noexcept(!mc) + : count(o.count - o.inc) + , inc(o.inc) + , id(o.id) // NOLINT + { + if(mc) + { + std::cout << " " << id << " move constructor count = " << count << std::endl; + if(!count) + { + std::cout << " " << id << " move constructor throws!" << std::endl; + throw std::bad_alloc(); + } + } + o.count = 0; + o.inc = 0; + o.id = 0; + } + Throwy &operator=(Throwy &&o) noexcept(!ma) + { + count = o.count - o.inc; + if(ma) + { + std::cout << " " << o.id << " move assignment count = " << count << std::endl; + if(!count) + { + std::cout << " " << o.id << " move assignment throws!" << std::endl; + throw std::bad_alloc(); + } + } + inc = o.inc; + id = o.id; + o.count = 0; + o.inc = 0; + o.id = 0; + return *this; + } + ~Throwy() = default; +}; +#if defined(__GNUC__) && !defined(__clang__) +#pragma GCC diagnostic pop +#endif +#ifdef _MSC_VER +#pragma warning(pop) +#endif +enum class ErrorCode +{ + dummy +}; +enum class ErrorCode2 +{ + dummy +}; +template using resulty1 = BOOST_OUTCOME_V2_NAMESPACE::result, ErrorCode, BOOST_OUTCOME_V2_NAMESPACE::policy::all_narrow>; +template using resulty2 = BOOST_OUTCOME_V2_NAMESPACE::result, BOOST_OUTCOME_V2_NAMESPACE::policy::all_narrow>; +template using outcomey1 = BOOST_OUTCOME_V2_NAMESPACE::outcome, ErrorCode2, BOOST_OUTCOME_V2_NAMESPACE::policy::all_narrow>; +template using outcomey2 = BOOST_OUTCOME_V2_NAMESPACE::outcome, BOOST_OUTCOME_V2_NAMESPACE::policy::all_narrow>; +#endif + +BOOST_OUTCOME_AUTO_TEST_CASE(works_outcome_swap, "Tests that the outcome swaps as intended") +{ + using namespace BOOST_OUTCOME_V2_NAMESPACE; + { // Does swap actually swap? + outcome a("niall"), b("douglas"); + BOOST_CHECK(a.value() == "niall"); + BOOST_CHECK(b.value() == "douglas"); + swap(a, b); + BOOST_CHECK(a.value() == "douglas"); + BOOST_CHECK(b.value() == "niall"); + a = boost::system::errc::not_enough_memory; + swap(a, b); + BOOST_CHECK(a.value() == "niall"); + BOOST_CHECK(b.error() == boost::system::errc::not_enough_memory); + BOOST_CHECK(!a.has_lost_consistency()); + BOOST_CHECK(!b.has_lost_consistency()); + } +#ifndef BOOST_NO_EXCEPTIONS + { // Is noexcept propagated? + using nothrow_t = Throwy; + using nothrow = resulty1; + static_assert(std::is_nothrow_move_constructible::value, "throwy not correct!"); + static_assert(std::is_nothrow_move_assignable::value, "throwy not correct!"); + static_assert(std::is_nothrow_move_constructible::value, "type not correct!"); + static_assert(std::is_nothrow_move_assignable::value, "type not correct!"); + + static_assert(detail::is_nothrow_swappable::value, "is_nothrow_swappable is not correct!"); + + static_assert(noexcept(nothrow(0, 0)), "type has a throwing value constructor!"); + nothrow a(1, 78), b(1, 65); + a.swap(b); + static_assert(noexcept(a.swap(b)), "type has a throwing swap!"); + } + { // Is noexcept propagated? + using nothrow_t = Throwy; + using nothrow = resulty2; + static_assert(std::is_nothrow_move_constructible::value, "throwy not correct!"); + static_assert(std::is_nothrow_move_assignable::value, "throwy not correct!"); + static_assert(std::is_nothrow_move_constructible::value, "type not correct!"); + static_assert(std::is_nothrow_move_assignable::value, "type not correct!"); + + static_assert(detail::is_nothrow_swappable::value, "is_nothrow_swappable is not correct!"); + + static_assert(noexcept(nothrow(0, 0)), "type has a throwing value constructor!"); + nothrow a(1, 78), b(1, 65); + a.swap(b); + static_assert(noexcept(a.swap(b)), "type has a throwing swap!"); + } + + { // Does swap implement the strong guarantee? + using throwy_t = Throwy; + using throwy = resulty1; + static_assert(!std::is_nothrow_move_constructible::value, "throwy not correct!"); + static_assert(!std::is_nothrow_move_assignable::value, "throwy not correct!"); + static_assert(!std::is_nothrow_move_constructible::value, "type not correct!"); + static_assert(!std::is_nothrow_move_assignable::value, "type not correct!"); + + static_assert(!detail::is_nothrow_swappable::value, "is_nothrow_swappable is not correct!"); + + std::cout << "Result value first swap succeeds, second swap second move assignment throws:" << std::endl; + { + throwy a(3, 78), b(4, 65); + a.swap(b); + static_assert(!noexcept(a.swap(b)), "type has a non-throwing swap!"); + BOOST_CHECK(a.value().id == 65); + BOOST_CHECK(b.value().id == 78); + try + { + a.swap(b); // fails on first assignment + BOOST_REQUIRE(false); + } + catch(const std::bad_alloc & /*unused*/) + { + BOOST_CHECK(a.value().id == 65); // ensure it is perfectly restored + BOOST_CHECK(b.value().id == 78); + } + BOOST_CHECK(!a.has_lost_consistency()); + BOOST_CHECK(!b.has_lost_consistency()); + } + std::cout << "\nResult value second move assignment throws, on recover second move assignment throws:" << std::endl; + { + throwy a(2, 78), b(3, 65); // fails on second assignment, cannot restore + try + { + a.swap(b); + BOOST_REQUIRE(false); + } + catch(const std::bad_alloc & /*unused*/) + { + BOOST_CHECK(a.has_lost_consistency()); // both must be marked tainted + BOOST_CHECK(b.has_lost_consistency()); + } + } + } + std::cout << "\nResult error first swap succeeds, second swap first move assignment throws:" << std::endl; + { // Does swap implement the strong guarantee? + using throwy_t = Throwy; + using throwy = resulty2; + static_assert(!std::is_nothrow_move_constructible::value, "throwy not correct!"); + static_assert(!std::is_nothrow_move_assignable::value, "throwy not correct!"); + static_assert(!std::is_nothrow_move_constructible::value, "type not correct!"); + static_assert(!std::is_nothrow_move_assignable::value, "type not correct!"); + + static_assert(!detail::is_nothrow_swappable::value, "is_nothrow_swappable is not correct!"); + + { + throwy a(3, 78), b(4, 65); + a.swap(b); + static_assert(!noexcept(a.swap(b)), "type has a non-throwing swap!"); + BOOST_CHECK(a.error().id == 65); + BOOST_CHECK(b.error().id == 78); + try + { + a.swap(b); // fails on first assignment + BOOST_REQUIRE(false); + } + catch(const std::bad_alloc & /*unused*/) + { + BOOST_CHECK(a.error().id == 65); // ensure it is perfectly restored + BOOST_CHECK(b.error().id == 78); + } + BOOST_CHECK(!a.has_lost_consistency()); + BOOST_CHECK(!b.has_lost_consistency()); + } + std::cout << "\nResult error second move assignment throws, on recover second move assignment throws:" << std::endl; + { + throwy a(2, 78), b(3, 65); // fails on second assignment, cannot restore + try + { + a.swap(b); + BOOST_REQUIRE(false); + } + catch(const std::bad_alloc & /*unused*/) + { + BOOST_CHECK(a.has_lost_consistency()); // both must be marked tainted + BOOST_CHECK(b.has_lost_consistency()); + } + } + } + + { // Is noexcept propagated? + using nothrow_t = Throwy; + using nothrow = outcomey1; + static_assert(std::is_nothrow_move_constructible::value, "throwy not correct!"); + static_assert(std::is_nothrow_move_assignable::value, "throwy not correct!"); + static_assert(std::is_nothrow_move_constructible::value, "type not correct!"); + static_assert(std::is_nothrow_move_assignable::value, "type not correct!"); + + static_assert(detail::is_nothrow_swappable::value, "is_nothrow_swappable is not correct!"); + + static_assert(noexcept(nothrow(0, 0)), "type has a throwing value constructor!"); + nothrow a(1, 78), b(1, 65); + a.swap(b); + static_assert(noexcept(a.swap(b)), "type has a throwing swap!"); + } + { // Is noexcept propagated? + using nothrow_t = Throwy; + using nothrow = outcomey1; + static_assert(std::is_nothrow_move_constructible::value, "throwy not correct!"); + static_assert(std::is_nothrow_move_assignable::value, "throwy not correct!"); + static_assert(std::is_nothrow_move_constructible::value, "type not correct!"); + static_assert(std::is_nothrow_move_assignable::value, "type not correct!"); + + static_assert(detail::is_nothrow_swappable::value, "is_nothrow_swappable is not correct!"); + + static_assert(noexcept(nothrow(0, 0)), "type has a throwing value constructor!"); + nothrow a(1, 78), b(1, 65); + a.swap(b); + static_assert(noexcept(a.swap(b)), "type has a throwing swap!"); + } + + std::cout << "\n\nOutcome value first swap succeeds, second swap first move assignment throws:" << std::endl; + { // Does swap implement the strong guarantee? + using throwy_t = Throwy; + using throwy = outcomey1; + static_assert(!std::is_nothrow_move_constructible::value, "throwy not correct!"); + static_assert(!std::is_nothrow_move_assignable::value, "throwy not correct!"); + static_assert(!std::is_nothrow_move_constructible::value, "type not correct!"); + static_assert(!std::is_nothrow_move_assignable::value, "type not correct!"); + + static_assert(!detail::is_nothrow_swappable::value, "is_nothrow_swappable is not correct!"); + + { + throwy a(3, 78), b(4, 65); + a.swap(b); + static_assert(!noexcept(a.swap(b)), "type has a non-throwing swap!"); + BOOST_CHECK(a.error().id == 65); + BOOST_CHECK(b.error().id == 78); + try + { + a.swap(b); // fails on first assignment + BOOST_REQUIRE(false); + } + catch(const std::bad_alloc & /*unused*/) + { + BOOST_CHECK(a.error().id == 65); // ensure it is perfectly restored + BOOST_CHECK(b.error().id == 78); + } + BOOST_CHECK(!a.has_lost_consistency()); + BOOST_CHECK(!b.has_lost_consistency()); + } + std::cout << "\nOutcome value second move assignment throws, on recover second move assignment throws:" << std::endl; + { + throwy a(2, 78), b(3, 65); // fails on second assignment, cannot restore + try + { + a.swap(b); + BOOST_REQUIRE(false); + } + catch(const std::bad_alloc & /*unused*/) + { + BOOST_CHECK(a.has_lost_consistency()); // both must be marked tainted + BOOST_CHECK(b.has_lost_consistency()); + } + } + } + std::cout << "\nOutcome error first swap succeeds, second swap first move assignment throws:" << std::endl; + { // Does swap implement the strong guarantee? + using throwy_t = Throwy; + using throwy = outcomey2; + static_assert(!std::is_nothrow_move_constructible::value, "throwy not correct!"); + static_assert(!std::is_nothrow_move_assignable::value, "throwy not correct!"); + static_assert(!std::is_nothrow_move_constructible::value, "type not correct!"); + static_assert(!std::is_nothrow_move_assignable::value, "type not correct!"); + + static_assert(!detail::is_nothrow_swappable::value, "is_nothrow_swappable is not correct!"); + + { + throwy a(3, 78), b(4, 65); + a.swap(b); + static_assert(!noexcept(a.swap(b)), "type has a non-throwing swap!"); + BOOST_CHECK(a.exception().id == 65); + BOOST_CHECK(b.exception().id == 78); + try + { + a.swap(b); // fails on first assignment + BOOST_REQUIRE(false); + } + catch(const std::bad_alloc & /*unused*/) + { + BOOST_CHECK(a.exception().id == 65); // ensure it is perfectly restored + BOOST_CHECK(b.exception().id == 78); + } + BOOST_CHECK(!a.has_lost_consistency()); + BOOST_CHECK(!b.has_lost_consistency()); + } + std::cout << "\nOutcome error second move assignment throws, on recover second move assignment throws:" << std::endl; + { + throwy a(2, 78), b(3, 65); // fails on second assignment, cannot restore + try + { + a.swap(b); + BOOST_REQUIRE(false); + } + catch(const std::bad_alloc & /*unused*/) + { + BOOST_CHECK(a.has_lost_consistency()); // both must be marked tainted + BOOST_CHECK(b.has_lost_consistency()); + } + } + std::cout << std::endl; + } +#endif +} diff --git a/src/boost/libs/outcome/test/tests/udts.cpp b/src/boost/libs/outcome/test/tests/udts.cpp new file mode 100644 index 000000000..740ca51d2 --- /dev/null +++ b/src/boost/libs/outcome/test/tests/udts.cpp @@ -0,0 +1,154 @@ +/* Unit testing for outcomes +(C) 2013-2020 Niall Douglas (7 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 _MSC_VER +#pragma warning(disable : 4702) // unreachable code +#endif + +#include +#include +#include + +BOOST_OUTCOME_AUTO_TEST_CASE(works_outcome_udts, "Tests that the outcome works as intended with user-defined types") +{ + using namespace BOOST_OUTCOME_V2_NAMESPACE; + // No default constructor, no copy/move, no assignment + { + struct udt + { + int a; + explicit udt(int _a) + : a(_a) + { + } + udt() = delete; + udt(const udt &) = delete; + udt(udt &&) = delete; + udt &operator=(const udt &) = delete; + udt &operator=(udt &&) = delete; + ~udt() = default; + }; + outcome foo(in_place_type, 5); + BOOST_CHECK(5 == foo.value().a); + } +#ifndef BOOST_NO_EXCEPTIONS + // Emplace construct, throws during move and copy + { + struct udt + { + std::string a; + explicit udt(std::string _a) + : a(std::move(_a)) + { + } + udt() = delete; + udt(const udt & /*unused*/) { throw std::logic_error("copy"); } + udt(udt && /*unused*/) noexcept(false) { throw std::logic_error("move"); } // NOLINT + udt &operator=(const udt & /*unused*/) { throw std::logic_error("copy"); } + udt &operator=(udt && /*unused*/) noexcept(false) { throw std::logic_error("move"); } // NOLINT + ~udt() { a.clear(); } + }; + static_assert(!std::is_default_constructible::value, "udt is default constructible"); + static_assert(std::is_copy_constructible::value, "udt is not copy constructible"); + static_assert(std::is_move_constructible::value, "udt is not move constructible"); + static_assert(!std::is_default_constructible>::value, "outcome is default constructible"); + static_assert(std::is_copy_constructible>::value, "outcome is not copy constructible"); + static_assert(std::is_move_constructible>::value, "outcome is not move constructible"); + // Emplace constructs + outcome foo(in_place_type, "niall"); + BOOST_CHECK("niall" == foo.value().a); + try + { + auto foo2(foo); // NOLINT + BOOST_CHECK(false); + } + catch(const std::logic_error &e) + { + BOOST_CHECK(!strcmp(e.what(), "copy")); + } + catch(...) + { + BOOST_CHECK(false); + } + BOOST_CHECK("niall" == foo.value().a); + try + { + auto foo2(std::move(foo)); + BOOST_CHECK(false); + } + catch(const std::logic_error &e) + { + BOOST_CHECK(!strcmp(e.what(), "move")); + } + catch(...) + { + BOOST_CHECK(false); + } + BOOST_CHECK("niall" == foo.value().a); // NOLINT + // Does throwing during copy assignment work? + { + outcome foo2(in_place_type, "douglas"); + try + { + foo2 = foo; + BOOST_CHECK(false); + } + catch(const std::logic_error &e) + { + BOOST_CHECK(!strcmp(e.what(), "copy")); + BOOST_CHECK(foo2.value().a == "douglas"); + } + catch(...) + { + BOOST_CHECK(false); + } + BOOST_CHECK("niall" == foo.value().a); + } + // Does throwing during move assignment work? + { + outcome foo2(in_place_type, "douglas"); + try + { + foo2 = std::move(foo); + BOOST_CHECK(false); + } + catch(const std::logic_error &e) + { + BOOST_CHECK(!strcmp(e.what(), "move")); + BOOST_CHECK(foo2.value().a == "douglas"); + } + catch(...) + { + BOOST_CHECK(false); + } + BOOST_CHECK("niall" == foo.value().a); // NOLINT + } + } +#endif +} diff --git a/src/boost/libs/outcome/test/tests/value-or-error.cpp b/src/boost/libs/outcome/test/tests/value-or-error.cpp new file mode 100644 index 000000000..558e229a7 --- /dev/null +++ b/src/boost/libs/outcome/test/tests/value-or-error.cpp @@ -0,0 +1,54 @@ +/* Unit testing for outcomes +(C) 2013-2020 Niall Douglas (3 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. +*/ + +#include +#include +#include + +BOOST_OUTCOME_AUTO_TEST_CASE(works_outcome_valueorerror, "Tests that outcome constructs from ValueOrError and ValueOrNone concept inputs") +{ + using namespace BOOST_OUTCOME_V2_NAMESPACE; + { + struct value_or_error + { + using value_type = int; + using error_type = void; + bool has_value() const { return true; } + int value() const { return 78; } + void error() const {} + } a; + static_assert(convert::ValueOrNone, ""); + static_assert(convert::ValueOrError, ""); + BOOST_CHECK((convert::value_or_error, value_or_error>{}(a).value() == 78)); + + result b(a); + BOOST_CHECK(b.has_value()); + BOOST_CHECK(b.value() == 78); + } +} -- cgit v1.2.3