diff options
author | yronglin <yronglin777@gmail.com> | 2023-05-18 01:05:13 +0800 |
---|---|---|
committer | yronglin <yronglin777@gmail.com> | 2023-05-18 01:05:25 +0800 |
commit | acce2a315945e386a7be6f014ebe90c2a28f38d9 (patch) | |
tree | 4dc99806d34604023905d11f645690782afe0a66 /libcxx/test/std/utilities/expected/expected.expected/observers/error_or.pass.cpp | |
parent | 5ae9098e5c1564bdfe0ae0fdbc02e581138eeac7 (diff) | |
download | llvm-acce2a315945e386a7be6f014ebe90c2a28f38d9.tar.gz |
[libc++] Implement P2505R5(Monadic operations for std::expected).
Implement P2505R5(https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/p2505r5.html)
Reviewed By: #libc, philnik, ldionne
Differential Revision: https://reviews.llvm.org/D140911
Diffstat (limited to 'libcxx/test/std/utilities/expected/expected.expected/observers/error_or.pass.cpp')
-rw-r--r-- | libcxx/test/std/utilities/expected/expected.expected/observers/error_or.pass.cpp | 96 |
1 files changed, 96 insertions, 0 deletions
diff --git a/libcxx/test/std/utilities/expected/expected.expected/observers/error_or.pass.cpp b/libcxx/test/std/utilities/expected/expected.expected/observers/error_or.pass.cpp new file mode 100644 index 000000000000..f698f44ba8f3 --- /dev/null +++ b/libcxx/test/std/utilities/expected/expected.expected/observers/error_or.pass.cpp @@ -0,0 +1,96 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 + +// template<class G = E> constexpr E error_or(G&& e) const &; +// template<class G = E> constexpr E error_or(G&& e) &&; + +#include <cassert> +#include <concepts> +#include <expected> +#include <type_traits> +#include <utility> + +struct ConstructFromInt { + int value; + constexpr ConstructFromInt(int v) : value(v) {} +}; + +constexpr bool test_default_template_arg() { + // const &, has_value() + { + const std::expected<int, ConstructFromInt> e(5); + std::same_as<ConstructFromInt> decltype(auto) x = e.error_or(10); + assert(x.value == 10); + } + + // const &, !has_value() + { + const std::expected<int, ConstructFromInt> e(std::unexpect, 5); + std::same_as<ConstructFromInt> decltype(auto) x = e.error_or(10); + assert(x.value == 5); + } + + // &&, has_value() + { + const std::expected<int, ConstructFromInt> e(5); + std::same_as<ConstructFromInt> decltype(auto) x = std::move(e).error_or(10); + assert(x.value == 10); + } + + // &&, !has_value() + { + const std::expected<int, ConstructFromInt> e(std::unexpect, 5); + std::same_as<ConstructFromInt> decltype(auto) x = std::move(e).error_or(10); + assert(x.value == 5); + } + + return true; +} + +constexpr bool test() { + // const &, has_value() + { + const std::expected<int, int> e(5); + std::same_as<int> decltype(auto) x = e.error_or(10); + assert(x == 10); + } + + // const &, !has_value() + { + const std::expected<int, int> e(std::unexpect, 5); + std::same_as<int> decltype(auto) x = e.error_or(10); + assert(x == 5); + } + + // &&, has_value() + { + std::expected<int, int> e(5); + std::same_as<int> decltype(auto) x = std::move(e).error_or(10); + assert(x == 10); + } + + // &&, !has_value() + { + std::expected<int, int> e(std::unexpect, 5); + std::same_as<int> decltype(auto) x = std::move(e).error_or(10); + assert(x == 5); + } + + return true; +} + +int main(int, char**) { + test(); + static_assert(test()); + test_default_template_arg(); + static_assert(test_default_template_arg()); + + return 0; +} |