diff options
Diffstat (limited to 'test/std/experimental/optional/optional.object/optional.object.assign/copy.pass.cpp')
-rw-r--r-- | test/std/experimental/optional/optional.object/optional.object.assign/copy.pass.cpp | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/test/std/experimental/optional/optional.object/optional.object.assign/copy.pass.cpp b/test/std/experimental/optional/optional.object/optional.object.assign/copy.pass.cpp new file mode 100644 index 000000000000..81c3b4e6dfa2 --- /dev/null +++ b/test/std/experimental/optional/optional.object/optional.object.assign/copy.pass.cpp @@ -0,0 +1,90 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// <optional> + +// optional<T>& operator=(const optional<T>& rhs); + +#include <experimental/optional> +#include <type_traits> +#include <cassert> + +#if _LIBCPP_STD_VER > 11 + +using std::experimental::optional; + +struct X +{ + static bool throw_now; + + X() = default; + X(const X&) + { + if (throw_now) + throw 6; + } +}; + +bool X::throw_now = false; + +#endif // _LIBCPP_STD_VER > 11 + +int main() +{ +#if _LIBCPP_STD_VER > 11 + { + optional<int> opt; + constexpr optional<int> opt2; + opt = opt2; + static_assert(static_cast<bool>(opt2) == false, ""); + assert(static_cast<bool>(opt) == static_cast<bool>(opt2)); + } + { + optional<int> opt; + constexpr optional<int> opt2(2); + opt = opt2; + static_assert(static_cast<bool>(opt2) == true, ""); + static_assert(*opt2 == 2, ""); + assert(static_cast<bool>(opt) == static_cast<bool>(opt2)); + assert(*opt == *opt2); + } + { + optional<int> opt(3); + constexpr optional<int> opt2; + opt = opt2; + static_assert(static_cast<bool>(opt2) == false, ""); + assert(static_cast<bool>(opt) == static_cast<bool>(opt2)); + } + { + optional<int> opt(3); + constexpr optional<int> opt2(2); + opt = opt2; + static_assert(static_cast<bool>(opt2) == true, ""); + static_assert(*opt2 == 2, ""); + assert(static_cast<bool>(opt) == static_cast<bool>(opt2)); + assert(*opt == *opt2); + } + { + optional<X> opt; + optional<X> opt2(X{}); + assert(static_cast<bool>(opt2) == true); + try + { + X::throw_now = true; + opt = opt2; + assert(false); + } + catch (int i) + { + assert(i == 6); + assert(static_cast<bool>(opt) == false); + } + } +#endif // _LIBCPP_STD_VER > 11 +} |