diff options
Diffstat (limited to 'libcxx/test/std/experimental/optional/optional.object/optional.object.assign')
6 files changed, 596 insertions, 0 deletions
diff --git a/libcxx/test/std/experimental/optional/optional.object/optional.object.assign/assign_value.pass.cpp b/libcxx/test/std/experimental/optional/optional.object/optional.object.assign/assign_value.pass.cpp new file mode 100644 index 00000000000..e256a098f1a --- /dev/null +++ b/libcxx/test/std/experimental/optional/optional.object/optional.object.assign/assign_value.pass.cpp @@ -0,0 +1,72 @@ +//===----------------------------------------------------------------------===// +// +// 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> + +// template <class U> optional<T>& operator=(U&& v); + +#include <experimental/optional> +#include <type_traits> +#include <cassert> +#include <memory> + +#if _LIBCPP_STD_VER > 11 + +using std::experimental::optional; + +struct X +{ +}; + +#endif // _LIBCPP_STD_VER > 11 + +int main() +{ +#if _LIBCPP_STD_VER > 11 + static_assert(std::is_assignable<optional<int>, int>::value, ""); + static_assert(std::is_assignable<optional<int>, int&>::value, ""); + static_assert(std::is_assignable<optional<int>&, int>::value, ""); + static_assert(std::is_assignable<optional<int>&, int&>::value, ""); + static_assert(std::is_assignable<optional<int>&, const int&>::value, ""); + static_assert(!std::is_assignable<const optional<int>&, const int&>::value, ""); + static_assert(!std::is_assignable<optional<int>, X>::value, ""); + { + optional<int> opt; + opt = 1; + assert(static_cast<bool>(opt) == true); + assert(*opt == 1); + } + { + optional<int> opt; + const int i = 2; + opt = i; + assert(static_cast<bool>(opt) == true); + assert(*opt == i); + } + { + optional<int> opt(3); + const int i = 2; + opt = i; + assert(static_cast<bool>(opt) == true); + assert(*opt == i); + } + { + optional<std::unique_ptr<int>> opt; + opt = std::unique_ptr<int>(new int(3)); + assert(static_cast<bool>(opt) == true); + assert(**opt == 3); + } + { + optional<std::unique_ptr<int>> opt(std::unique_ptr<int>(new int(2))); + opt = std::unique_ptr<int>(new int(3)); + assert(static_cast<bool>(opt) == true); + assert(**opt == 3); + } +#endif // _LIBCPP_STD_VER > 11 +} diff --git a/libcxx/test/std/experimental/optional/optional.object/optional.object.assign/copy.pass.cpp b/libcxx/test/std/experimental/optional/optional.object/optional.object.assign/copy.pass.cpp new file mode 100644 index 00000000000..81c3b4e6dfa --- /dev/null +++ b/libcxx/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 +} diff --git a/libcxx/test/std/experimental/optional/optional.object/optional.object.assign/emplace.pass.cpp b/libcxx/test/std/experimental/optional/optional.object/optional.object.assign/emplace.pass.cpp new file mode 100644 index 00000000000..c4361ea5178 --- /dev/null +++ b/libcxx/test/std/experimental/optional/optional.object/optional.object.assign/emplace.pass.cpp @@ -0,0 +1,148 @@ +//===----------------------------------------------------------------------===// +// +// 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> + +// template <class... Args> void optional<T>::emplace(Args&&... args); + +#include <experimental/optional> +#include <type_traits> +#include <cassert> +#include <memory> + +#if _LIBCPP_STD_VER > 11 + +using std::experimental::optional; + +class X +{ + int i_; + int j_ = 0; +public: + X() : i_(0) {} + X(int i) : i_(i) {} + X(int i, int j) : i_(i), j_(j) {} + + friend bool operator==(const X& x, const X& y) + {return x.i_ == y.i_ && x.j_ == y.j_;} +}; + +class Y +{ +public: + static bool dtor_called; + Y() = default; + ~Y() {dtor_called = true;} +}; + +bool Y::dtor_called = false; + +class Z +{ +public: + static bool dtor_called; + Z() = default; + Z(int) {throw 6;} + ~Z() {dtor_called = true;} +}; + +bool Z::dtor_called = false; + +#endif // _LIBCPP_STD_VER > 11 + +int main() +{ +#if _LIBCPP_STD_VER > 11 + { + optional<int> opt; + opt.emplace(); + assert(static_cast<bool>(opt) == true); + assert(*opt == 0); + } + { + optional<int> opt; + opt.emplace(1); + assert(static_cast<bool>(opt) == true); + assert(*opt == 1); + } + { + optional<int> opt(2); + opt.emplace(); + assert(static_cast<bool>(opt) == true); + assert(*opt == 0); + } + { + optional<int> opt(2); + opt.emplace(1); + assert(static_cast<bool>(opt) == true); + assert(*opt == 1); + } + { + optional<X> opt; + opt.emplace(); + assert(static_cast<bool>(opt) == true); + assert(*opt == X()); + } + { + optional<X> opt; + opt.emplace(1); + assert(static_cast<bool>(opt) == true); + assert(*opt == X(1)); + } + { + optional<X> opt; + opt.emplace(1, 2); + assert(static_cast<bool>(opt) == true); + assert(*opt == X(1, 2)); + } + { + optional<X> opt(X{3}); + opt.emplace(); + assert(static_cast<bool>(opt) == true); + assert(*opt == X()); + } + { + optional<X> opt(X{3}); + opt.emplace(1); + assert(static_cast<bool>(opt) == true); + assert(*opt == X(1)); + } + { + optional<X> opt(X{3}); + opt.emplace(1, 2); + assert(static_cast<bool>(opt) == true); + assert(*opt == X(1, 2)); + } + { + Y y; + { + optional<Y> opt(y); + assert(Y::dtor_called == false); + opt.emplace(); + assert(Y::dtor_called == true); + } + } + { + Z z; + optional<Z> opt(z); + try + { + assert(static_cast<bool>(opt) == true); + assert(Z::dtor_called == false); + opt.emplace(1); + } + catch (int i) + { + assert(i == 6); + assert(static_cast<bool>(opt) == false); + assert(Z::dtor_called == true); + } + } +#endif // _LIBCPP_STD_VER > 11 +} diff --git a/libcxx/test/std/experimental/optional/optional.object/optional.object.assign/emplace_initializer_list.pass.cpp b/libcxx/test/std/experimental/optional/optional.object/optional.object.assign/emplace_initializer_list.pass.cpp new file mode 100644 index 00000000000..8d20be3de19 --- /dev/null +++ b/libcxx/test/std/experimental/optional/optional.object/optional.object.assign/emplace_initializer_list.pass.cpp @@ -0,0 +1,116 @@ +//===----------------------------------------------------------------------===// +// +// 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> + +// template <class U, class... Args> +// void optional<T>::emplace(initializer_list<U> il, Args&&... args); + +#include <experimental/optional> +#include <type_traits> +#include <cassert> +#include <vector> + +#if _LIBCPP_STD_VER > 11 + +using std::experimental::optional; + +class X +{ + int i_; + int j_ = 0; +public: + static bool dtor_called; + constexpr X() : i_(0) {} + constexpr X(int i) : i_(i) {} + constexpr X(std::initializer_list<int> il) : i_(il.begin()[0]), j_(il.begin()[1]) {} + ~X() {dtor_called = true;} + + friend constexpr bool operator==(const X& x, const X& y) + {return x.i_ == y.i_ && x.j_ == y.j_;} +}; + +bool X::dtor_called = false; + +class Y +{ + int i_; + int j_ = 0; +public: + constexpr Y() : i_(0) {} + constexpr Y(int i) : i_(i) {} + constexpr Y(std::initializer_list<int> il) : i_(il.begin()[0]), j_(il.begin()[1]) {} + + friend constexpr bool operator==(const Y& x, const Y& y) + {return x.i_ == y.i_ && x.j_ == y.j_;} +}; + +class Z +{ + int i_; + int j_ = 0; +public: + static bool dtor_called; + constexpr Z() : i_(0) {} + constexpr Z(int i) : i_(i) {} + constexpr Z(std::initializer_list<int> il) : i_(il.begin()[0]), j_(il.begin()[1]) + {throw 6;} + ~Z() {dtor_called = true;} + + friend constexpr bool operator==(const Z& x, const Z& y) + {return x.i_ == y.i_ && x.j_ == y.j_;} +}; + +bool Z::dtor_called = false; + +#endif // _LIBCPP_STD_VER > 11 + +int main() +{ +#if _LIBCPP_STD_VER > 11 + { + X x; + { + optional<X> opt(x); + assert(X::dtor_called == false); + opt.emplace({1, 2}); + assert(X::dtor_called == true); + assert(*opt == X({1, 2})); + } + } + { + optional<std::vector<int>> opt; + opt.emplace({1, 2, 3}, std::allocator<int>()); + assert(static_cast<bool>(opt) == true); + assert(*opt == std::vector<int>({1, 2, 3})); + } + { + optional<Y> opt; + opt.emplace({1, 2}); + assert(static_cast<bool>(opt) == true); + assert(*opt == Y({1, 2})); + } + { + Z z; + optional<Z> opt(z); + try + { + assert(static_cast<bool>(opt) == true); + assert(Z::dtor_called == false); + opt.emplace({1, 2}); + } + catch (int i) + { + assert(i == 6); + assert(static_cast<bool>(opt) == false); + assert(Z::dtor_called == true); + } + } +#endif // _LIBCPP_STD_VER > 11 +} diff --git a/libcxx/test/std/experimental/optional/optional.object/optional.object.assign/move.pass.cpp b/libcxx/test/std/experimental/optional/optional.object/optional.object.assign/move.pass.cpp new file mode 100644 index 00000000000..d27313dad42 --- /dev/null +++ b/libcxx/test/std/experimental/optional/optional.object/optional.object.assign/move.pass.cpp @@ -0,0 +1,103 @@ +//===----------------------------------------------------------------------===// +// +// 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=(optional<T>&& rhs) +// noexcept(is_nothrow_move_assignable<T>::value && +// is_nothrow_move_constructible<T>::value); + +#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(X&&) + { + if (throw_now) + throw 6; + } + X& operator=(X&&) noexcept + { + return *this; + } +}; + +struct Y {}; + +bool X::throw_now = false; + +#endif // _LIBCPP_STD_VER > 11 + +int main() +{ +#if _LIBCPP_STD_VER > 11 + { + static_assert(std::is_nothrow_move_assignable<optional<int>>::value, ""); + optional<int> opt; + constexpr optional<int> opt2; + opt = std::move(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 = std::move(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 = std::move(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 = std::move(opt2); + static_assert(static_cast<bool>(opt2) == true, ""); + static_assert(*opt2 == 2, ""); + assert(static_cast<bool>(opt) == static_cast<bool>(opt2)); + assert(*opt == *opt2); + } + { + static_assert(!std::is_nothrow_move_assignable<optional<X>>::value, ""); + optional<X> opt; + optional<X> opt2(X{}); + assert(static_cast<bool>(opt2) == true); + try + { + X::throw_now = true; + opt = std::move(opt2); + assert(false); + } + catch (int i) + { + assert(i == 6); + assert(static_cast<bool>(opt) == false); + } + } + { + static_assert(std::is_nothrow_move_assignable<optional<Y>>::value, ""); + } +#endif // _LIBCPP_STD_VER > 11 +} diff --git a/libcxx/test/std/experimental/optional/optional.object/optional.object.assign/nullopt_t.pass.cpp b/libcxx/test/std/experimental/optional/optional.object/optional.object.assign/nullopt_t.pass.cpp new file mode 100644 index 00000000000..7f39744f055 --- /dev/null +++ b/libcxx/test/std/experimental/optional/optional.object/optional.object.assign/nullopt_t.pass.cpp @@ -0,0 +1,67 @@ +//===----------------------------------------------------------------------===// +// +// 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=(nullopt_t) noexcept; + +#include <experimental/optional> +#include <type_traits> +#include <cassert> + +#if _LIBCPP_STD_VER > 11 + +using std::experimental::optional; +using std::experimental::nullopt_t; +using std::experimental::nullopt; + +struct X +{ + static bool dtor_called; + ~X() {dtor_called = true;} +}; + +bool X::dtor_called = false; + +#endif // _LIBCPP_STD_VER > 11 + +int main() +{ +#if _LIBCPP_STD_VER > 11 + { + optional<int> opt; + static_assert(noexcept(opt = nullopt) == true, ""); + opt = nullopt; + assert(static_cast<bool>(opt) == false); + } + { + optional<int> opt(3); + opt = nullopt; + assert(static_cast<bool>(opt) == false); + } + { + optional<X> opt; + static_assert(noexcept(opt = nullopt) == true, ""); + assert(X::dtor_called == false); + opt = nullopt; + assert(X::dtor_called == false); + assert(static_cast<bool>(opt) == false); + } + { + X x; + { + optional<X> opt(x); + assert(X::dtor_called == false); + opt = nullopt; + assert(X::dtor_called == true); + assert(static_cast<bool>(opt) == false); + } + } +#endif // _LIBCPP_STD_VER > 11 +} |