diff options
Diffstat (limited to 'libcxx/test/std/utilities/optional/optional.object/optional.object.observe')
15 files changed, 957 insertions, 0 deletions
diff --git a/libcxx/test/std/utilities/optional/optional.object/optional.object.observe/bool.pass.cpp b/libcxx/test/std/utilities/optional/optional.object/optional.object.observe/bool.pass.cpp new file mode 100644 index 00000000000..9820d50f632 --- /dev/null +++ b/libcxx/test/std/utilities/optional/optional.object/optional.object.observe/bool.pass.cpp @@ -0,0 +1,37 @@ +//===----------------------------------------------------------------------===// +// +// 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. +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++98, c++03, c++11, c++14 +// <optional> + +// constexpr explicit optional<T>::operator bool() const noexcept; + +#include <optional> +#include <type_traits> +#include <cassert> + +#include "test_macros.h" + +int main() +{ + using std::optional; + { + const optional<int> opt; ((void)opt); + ASSERT_NOEXCEPT(bool(opt)); + static_assert(!std::is_convertible<optional<int>, bool>::value, ""); + } + { + constexpr optional<int> opt; + static_assert(!opt, ""); + } + { + constexpr optional<int> opt(0); + static_assert(opt, ""); + } +} diff --git a/libcxx/test/std/utilities/optional/optional.object/optional.object.observe/dereference.pass.cpp b/libcxx/test/std/utilities/optional/optional.object/optional.object.observe/dereference.pass.cpp new file mode 100644 index 00000000000..4087cfdf104 --- /dev/null +++ b/libcxx/test/std/utilities/optional/optional.object/optional.object.observe/dereference.pass.cpp @@ -0,0 +1,73 @@ +//===----------------------------------------------------------------------===// +// +// 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. +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++98, c++03, c++11, c++14 +// <optional> + +// constexpr T& optional<T>::operator*() &; + +#ifdef _LIBCPP_DEBUG +#define _LIBCPP_ASSERT(x, m) ((x) ? (void)0 : std::exit(0)) +#endif + +#include <optional> +#include <type_traits> +#include <cassert> + +#include "test_macros.h" + +using std::optional; + +struct X +{ + constexpr int test() const& {return 3;} + int test() & {return 4;} + constexpr int test() const&& {return 5;} + int test() && {return 6;} +}; + +struct Y +{ + constexpr int test() {return 7;} +}; + +constexpr int +test() +{ + optional<Y> opt{Y{}}; + return (*opt).test(); +} + +int main() +{ + { + optional<X> opt; ((void)opt); + ASSERT_SAME_TYPE(decltype(*opt), X&); + // ASSERT_NOT_NOEXCEPT(*opt); + // FIXME: This assertion fails with GCC because it can see that + // (A) operator*() is constexpr, and + // (B) there is no path through the function that throws. + // It's arguable if this is the correct behavior for the noexcept + // operator. + // Regardless this function should still be noexcept(false) because + // it has a narrow contract. + } + { + optional<X> opt(X{}); + assert((*opt).test() == 4); + } + static_assert(test() == 7, ""); +#ifdef _LIBCPP_DEBUG + { + optional<X> opt; + assert((*opt).test() == 3); + assert(false); + } +#endif // _LIBCPP_DEBUG +} diff --git a/libcxx/test/std/utilities/optional/optional.object/optional.object.observe/dereference_const.pass.cpp b/libcxx/test/std/utilities/optional/optional.object/optional.object.observe/dereference_const.pass.cpp new file mode 100644 index 00000000000..0779c9047c9 --- /dev/null +++ b/libcxx/test/std/utilities/optional/optional.object/optional.object.observe/dereference_const.pass.cpp @@ -0,0 +1,69 @@ +//===----------------------------------------------------------------------===// +// +// 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. +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++98, c++03, c++11, c++14 +// <optional> + +// constexpr const T& optional<T>::operator*() const &; + +#ifdef _LIBCPP_DEBUG +#define _LIBCPP_ASSERT(x, m) ((x) ? (void)0 : std::exit(0)) +#endif + +#include <optional> +#include <type_traits> +#include <cassert> + +#include "test_macros.h" + +using std::optional; + +struct X +{ + constexpr int test() const& {return 3;} + int test() & {return 4;} + constexpr int test() const&& {return 5;} + int test() && {return 6;} +}; + +struct Y +{ + int test() const {return 2;} +}; + +int main() +{ + { + const optional<X> opt; ((void)opt); + ASSERT_SAME_TYPE(decltype(*opt), X const&); + // ASSERT_NOT_NOEXCEPT(*opt); + // FIXME: This assertion fails with GCC because it can see that + // (A) operator*() is constexpr, and + // (B) there is no path through the function that throws. + // It's arguable if this is the correct behavior for the noexcept + // operator. + // Regardless this function should still be noexcept(false) because + // it has a narrow contract. + } + { + constexpr optional<X> opt(X{}); + static_assert((*opt).test() == 3, ""); + } + { + constexpr optional<Y> opt(Y{}); + assert((*opt).test() == 2); + } +#ifdef _LIBCPP_DEBUG + { + const optional<X> opt; + assert((*opt).test() == 3); + assert(false); + } +#endif // _LIBCPP_DEBUG +} diff --git a/libcxx/test/std/utilities/optional/optional.object/optional.object.observe/dereference_const_rvalue.pass.cpp b/libcxx/test/std/utilities/optional/optional.object/optional.object.observe/dereference_const_rvalue.pass.cpp new file mode 100644 index 00000000000..78fd992952c --- /dev/null +++ b/libcxx/test/std/utilities/optional/optional.object/optional.object.observe/dereference_const_rvalue.pass.cpp @@ -0,0 +1,69 @@ +//===----------------------------------------------------------------------===// +// +// 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. +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++98, c++03, c++11, c++14 +// <optional> + +// constexpr T&& optional<T>::operator*() const &&; + +#ifdef _LIBCPP_DEBUG +#define _LIBCPP_ASSERT(x, m) ((x) ? (void)0 : std::exit(0)) +#endif + +#include <optional> +#include <type_traits> +#include <cassert> + +#include "test_macros.h" + +using std::optional; + +struct X +{ + constexpr int test() const& {return 3;} + int test() & {return 4;} + constexpr int test() const&& {return 5;} + int test() && {return 6;} +}; + +struct Y +{ + int test() const && {return 2;} +}; + +int main() +{ + { + const optional<X> opt; ((void)opt); + ASSERT_SAME_TYPE(decltype(*std::move(opt)), X const &&); + // ASSERT_NOT_NOEXCEPT(*std::move(opt)); + // FIXME: This assertion fails with GCC because it can see that + // (A) operator*() is constexpr, and + // (B) there is no path through the function that throws. + // It's arguable if this is the correct behavior for the noexcept + // operator. + // Regardless this function should still be noexcept(false) because + // it has a narrow contract. + } + { + constexpr optional<X> opt(X{}); + static_assert((*std::move(opt)).test() == 5, ""); + } + { + constexpr optional<Y> opt(Y{}); + assert((*std::move(opt)).test() == 2); + } +#ifdef _LIBCPP_DEBUG + { + optional<X> opt; + assert((*std::move(opt)).test() == 5); + assert(false); + } +#endif // _LIBCPP_DEBUG +} diff --git a/libcxx/test/std/utilities/optional/optional.object/optional.object.observe/dereference_rvalue.pass.cpp b/libcxx/test/std/utilities/optional/optional.object/optional.object.observe/dereference_rvalue.pass.cpp new file mode 100644 index 00000000000..2924123234a --- /dev/null +++ b/libcxx/test/std/utilities/optional/optional.object/optional.object.observe/dereference_rvalue.pass.cpp @@ -0,0 +1,73 @@ +//===----------------------------------------------------------------------===// +// +// 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. +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++98, c++03, c++11, c++14 +// <optional> + +// constexpr T&& optional<T>::operator*() &&; + +#ifdef _LIBCPP_DEBUG +#define _LIBCPP_ASSERT(x, m) ((x) ? (void)0 : std::exit(0)) +#endif + +#include <optional> +#include <type_traits> +#include <cassert> + +#include "test_macros.h" + +using std::optional; + +struct X +{ + constexpr int test() const& {return 3;} + int test() & {return 4;} + constexpr int test() const&& {return 5;} + int test() && {return 6;} +}; + +struct Y +{ + constexpr int test() && {return 7;} +}; + +constexpr int +test() +{ + optional<Y> opt{Y{}}; + return (*std::move(opt)).test(); +} + +int main() +{ + { + optional<X> opt; ((void)opt); + ASSERT_SAME_TYPE(decltype(*std::move(opt)), X&&); + // ASSERT_NOT_NOEXCEPT(*std::move(opt)); + // FIXME: This assertion fails with GCC because it can see that + // (A) operator*() is constexpr, and + // (B) there is no path through the function that throws. + // It's arguable if this is the correct behavior for the noexcept + // operator. + // Regardless this function should still be noexcept(false) because + // it has a narrow contract. + } + { + optional<X> opt(X{}); + assert((*std::move(opt)).test() == 6); + } + static_assert(test() == 7, ""); +#ifdef _LIBCPP_DEBUG + { + optional<X> opt; + assert((*std::move(opt)).test() == 3); + assert(false); + } +#endif // _LIBCPP_DEBUG +} diff --git a/libcxx/test/std/utilities/optional/optional.object/optional.object.observe/has_value.pass.cpp b/libcxx/test/std/utilities/optional/optional.object/optional.object.observe/has_value.pass.cpp new file mode 100644 index 00000000000..5df295d01e2 --- /dev/null +++ b/libcxx/test/std/utilities/optional/optional.object/optional.object.observe/has_value.pass.cpp @@ -0,0 +1,37 @@ +//===----------------------------------------------------------------------===// +// +// 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. +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++98, c++03, c++11, c++14 +// <optional> + +// constexpr bool optional<T>::has_value() const noexcept; + +#include <optional> +#include <type_traits> +#include <cassert> + +#include "test_macros.h" + +int main() +{ + using std::optional; + { + const optional<int> opt; ((void)opt); + ASSERT_NOEXCEPT(opt.has_value()); + ASSERT_SAME_TYPE(decltype(opt.has_value()), bool); + } + { + constexpr optional<int> opt; + static_assert(!opt.has_value(), ""); + } + { + constexpr optional<int> opt(0); + static_assert(opt.has_value(), ""); + } +} diff --git a/libcxx/test/std/utilities/optional/optional.object/optional.object.observe/op_arrow.pass.cpp b/libcxx/test/std/utilities/optional/optional.object/optional.object.observe/op_arrow.pass.cpp new file mode 100644 index 00000000000..2f1648c48c8 --- /dev/null +++ b/libcxx/test/std/utilities/optional/optional.object/optional.object.observe/op_arrow.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. +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++98, c++03, c++11, c++14 +// <optional> + +// constexpr T* optional<T>::operator->(); + +#ifdef _LIBCPP_DEBUG +#define _LIBCPP_ASSERT(x, m) ((x) ? (void)0 : std::exit(0)) +#endif + +#include <optional> +#include <type_traits> +#include <cassert> + +#include "test_macros.h" + +using std::optional; + +struct X +{ + int test() noexcept {return 3;} +}; + +struct Y +{ + constexpr int test() {return 3;} +}; + +constexpr int +test() +{ + optional<Y> opt{Y{}}; + return opt->test(); +} + +int main() +{ + { + std::optional<X> opt; ((void)opt); + ASSERT_SAME_TYPE(decltype(opt.operator->()), X*); + // ASSERT_NOT_NOEXCEPT(opt.operator->()); + // FIXME: This assertion fails with GCC because it can see that + // (A) operator->() is constexpr, and + // (B) there is no path through the function that throws. + // It's arguable if this is the correct behavior for the noexcept + // operator. + // Regardless this function should still be noexcept(false) because + // it has a narrow contract. + } + { + optional<X> opt(X{}); + assert(opt->test() == 3); + } + { + static_assert(test() == 3, ""); + } +#ifdef _LIBCPP_DEBUG + { + optional<X> opt; + assert(opt->test() == 3); + assert(false); + } +#endif // _LIBCPP_DEBUG +} diff --git a/libcxx/test/std/utilities/optional/optional.object/optional.object.observe/op_arrow_const.pass.cpp b/libcxx/test/std/utilities/optional/optional.object/optional.object.observe/op_arrow_const.pass.cpp new file mode 100644 index 00000000000..887edc7114e --- /dev/null +++ b/libcxx/test/std/utilities/optional/optional.object/optional.object.observe/op_arrow_const.pass.cpp @@ -0,0 +1,76 @@ +//===----------------------------------------------------------------------===// +// +// 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. +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++98, c++03, c++11, c++14 +// <optional> + +// constexpr const T* optional<T>::operator->() const; + +#ifdef _LIBCPP_DEBUG +#define _LIBCPP_ASSERT(x, m) ((x) ? (void)0 : std::exit(0)) +#endif + +#include <optional> +#include <type_traits> +#include <cassert> + +#include "test_macros.h" + +using std::optional; + +struct X +{ + constexpr int test() const {return 3;} +}; + +struct Y +{ + int test() const noexcept {return 2;} +}; + +struct Z +{ + const Z* operator&() const; + constexpr int test() const {return 1;} +}; + +int main() +{ + { + const std::optional<X> opt; ((void)opt); + ASSERT_SAME_TYPE(decltype(opt.operator->()), X const*); + // ASSERT_NOT_NOEXCEPT(opt.operator->()); + // FIXME: This assertion fails with GCC because it can see that + // (A) operator->() is constexpr, and + // (B) there is no path through the function that throws. + // It's arguable if this is the correct behavior for the noexcept + // operator. + // Regardless this function should still be noexcept(false) because + // it has a narrow contract. + } + { + constexpr optional<X> opt(X{}); + static_assert(opt->test() == 3, ""); + } + { + constexpr optional<Y> opt(Y{}); + assert(opt->test() == 2); + } + { + constexpr optional<Z> opt(Z{}); + static_assert(opt->test() == 1, ""); + } +#ifdef _LIBCPP_DEBUG + { + const optional<X> opt; + assert(opt->test() == 3); + assert(false); + } +#endif // _LIBCPP_DEBUG +} diff --git a/libcxx/test/std/utilities/optional/optional.object/optional.object.observe/value.pass.cpp b/libcxx/test/std/utilities/optional/optional.object/optional.object.observe/value.pass.cpp new file mode 100644 index 00000000000..516a79db5f6 --- /dev/null +++ b/libcxx/test/std/utilities/optional/optional.object/optional.object.observe/value.pass.cpp @@ -0,0 +1,73 @@ +//===----------------------------------------------------------------------===// +// +// 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. +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++98, c++03, c++11, c++14 +// <optional> + +// constexpr T& optional<T>::value() &; + +#include <optional> +#include <type_traits> +#include <cassert> + +#include "test_macros.h" + +using std::optional; +using std::bad_optional_access; + +struct X +{ + X() = default; + X(const X&) = delete; + constexpr int test() const & {return 3;} + int test() & {return 4;} + constexpr int test() const && {return 5;} + int test() && {return 6;} +}; + +struct Y +{ + constexpr int test() & {return 7;} +}; + +constexpr int +test() +{ + optional<Y> opt{Y{}}; + return opt.value().test(); +} + + +int main() +{ + { + optional<X> opt; ((void)opt); + ASSERT_NOT_NOEXCEPT(opt.value()); + ASSERT_SAME_TYPE(decltype(opt.value()), X&); + } + { + optional<X> opt; + opt.emplace(); + assert(opt.value().test() == 4); + } +#ifndef TEST_HAS_NO_EXCEPTIONS + { + optional<X> opt; + try + { + opt.value(); + assert(false); + } + catch (const bad_optional_access&) + { + } + } +#endif + static_assert(test() == 7, ""); +} diff --git a/libcxx/test/std/utilities/optional/optional.object/optional.object.observe/value_const.fail.cpp b/libcxx/test/std/utilities/optional/optional.object/optional.object.observe/value_const.fail.cpp new file mode 100644 index 00000000000..6076c509fa4 --- /dev/null +++ b/libcxx/test/std/utilities/optional/optional.object/optional.object.observe/value_const.fail.cpp @@ -0,0 +1,33 @@ +//===----------------------------------------------------------------------===// +// +// 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. +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++98, c++03, c++11, c++14 +// <optional> + +// constexpr const T& optional<T>::value() const &; + +#include <optional> +#include <type_traits> +#include <cassert> + +using std::optional; + +struct X +{ + constexpr int test() const {return 3;} + int test() {return 4;} +}; + +int main() +{ + { + constexpr optional<X> opt; + static_assert(opt.value().test() == 3, ""); + } +} diff --git a/libcxx/test/std/utilities/optional/optional.object/optional.object.observe/value_const.pass.cpp b/libcxx/test/std/utilities/optional/optional.object/optional.object.observe/value_const.pass.cpp new file mode 100644 index 00000000000..d4038e4efa6 --- /dev/null +++ b/libcxx/test/std/utilities/optional/optional.object/optional.object.observe/value_const.pass.cpp @@ -0,0 +1,64 @@ +//===----------------------------------------------------------------------===// +// +// 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. +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++98, c++03, c++11, c++14 +// <optional> + +// constexpr const T& optional<T>::value() const &; + +#include <optional> +#include <type_traits> +#include <cassert> + +#include "test_macros.h" + +using std::optional; +using std::in_place_t; +using std::in_place; +using std::bad_optional_access; + +struct X +{ + X() = default; + X(const X&) = delete; + constexpr int test() const & {return 3;} + int test() & {return 4;} + constexpr int test() const && {return 5;} + int test() && {return 6;} +}; + +int main() +{ + { + const optional<X> opt; ((void)opt); + ASSERT_NOT_NOEXCEPT(opt.value()); + ASSERT_SAME_TYPE(decltype(opt.value()), X const&); + } + { + constexpr optional<X> opt(in_place); + static_assert(opt.value().test() == 3, ""); + } + { + const optional<X> opt(in_place); + assert(opt.value().test() == 3); + } +#ifndef TEST_HAS_NO_EXCEPTIONS + { + const optional<X> opt; + try + { + opt.value(); + assert(false); + } + catch (const bad_optional_access&) + { + } + } +#endif +} diff --git a/libcxx/test/std/utilities/optional/optional.object/optional.object.observe/value_const_rvalue.pass.cpp b/libcxx/test/std/utilities/optional/optional.object/optional.object.observe/value_const_rvalue.pass.cpp new file mode 100644 index 00000000000..e189d3af688 --- /dev/null +++ b/libcxx/test/std/utilities/optional/optional.object/optional.object.observe/value_const_rvalue.pass.cpp @@ -0,0 +1,64 @@ +//===----------------------------------------------------------------------===// +// +// 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. +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++98, c++03, c++11, c++14 +// <optional> + +// constexpr const T& optional<T>::value() const &&; + +#include <optional> +#include <type_traits> +#include <cassert> + +#include "test_macros.h" + +using std::optional; +using std::in_place_t; +using std::in_place; +using std::bad_optional_access; + +struct X +{ + X() = default; + X(const X&) = delete; + constexpr int test() const & {return 3;} + int test() & {return 4;} + constexpr int test() const && {return 5;} + int test() && {return 6;} +}; + +int main() +{ + { + const optional<X> opt; ((void)opt); + ASSERT_NOT_NOEXCEPT(std::move(opt).value()); + ASSERT_SAME_TYPE(decltype(std::move(opt).value()), X const&&); + } + { + constexpr optional<X> opt(in_place); + static_assert(std::move(opt).value().test() == 5, ""); + } + { + const optional<X> opt(in_place); + assert(std::move(opt).value().test() == 5); + } +#ifndef TEST_HAS_NO_EXCEPTIONS + { + const optional<X> opt; + try + { + std::move(opt).value(); + assert(false); + } + catch (const bad_optional_access&) + { + } + } +#endif +} diff --git a/libcxx/test/std/utilities/optional/optional.object/optional.object.observe/value_or.pass.cpp b/libcxx/test/std/utilities/optional/optional.object/optional.object.observe/value_or.pass.cpp new file mode 100644 index 00000000000..c219e970471 --- /dev/null +++ b/libcxx/test/std/utilities/optional/optional.object/optional.object.observe/value_or.pass.cpp @@ -0,0 +1,68 @@ +//===----------------------------------------------------------------------===// +// +// 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. +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++98, c++03, c++11, c++14 +// <optional> + +// template <class U> T optional<T>::value_or(U&& v) &&; + +#include <optional> +#include <type_traits> +#include <cassert> + +#include "test_macros.h" + +using std::optional; +using std::in_place_t; +using std::in_place; + +struct Y +{ + int i_; + + Y(int i) : i_(i) {} +}; + +struct X +{ + int i_; + + X(int i) : i_(i) {} + X(X&& x) : i_(x.i_) {x.i_ = 0;} + X(const Y& y) : i_(y.i_) {} + X(Y&& y) : i_(y.i_+1) {} + friend constexpr bool operator==(const X& x, const X& y) + {return x.i_ == y.i_;} +}; + +int main() +{ + { + optional<X> opt(in_place, 2); + Y y(3); + assert(std::move(opt).value_or(y) == 2); + assert(*opt == 0); + } + { + optional<X> opt(in_place, 2); + assert(std::move(opt).value_or(Y(3)) == 2); + assert(*opt == 0); + } + { + optional<X> opt; + Y y(3); + assert(std::move(opt).value_or(y) == 3); + assert(!opt); + } + { + optional<X> opt; + assert(std::move(opt).value_or(Y(3)) == 4); + assert(!opt); + } +} diff --git a/libcxx/test/std/utilities/optional/optional.object/optional.object.observe/value_or_const.pass.cpp b/libcxx/test/std/utilities/optional/optional.object/optional.object.observe/value_or_const.pass.cpp new file mode 100644 index 00000000000..36a85811ba4 --- /dev/null +++ b/libcxx/test/std/utilities/optional/optional.object/optional.object.observe/value_or_const.pass.cpp @@ -0,0 +1,77 @@ +//===----------------------------------------------------------------------===// +// +// 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. +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++98, c++03, c++11, c++14 +// <optional> + +// template <class U> constexpr T optional<T>::value_or(U&& v) const&; + +#include <optional> +#include <type_traits> +#include <cassert> + +using std::optional; + +struct Y +{ + int i_; + + constexpr Y(int i) : i_(i) {} +}; + +struct X +{ + int i_; + + constexpr X(int i) : i_(i) {} + constexpr X(const Y& y) : i_(y.i_) {} + constexpr X(Y&& y) : i_(y.i_+1) {} + friend constexpr bool operator==(const X& x, const X& y) + {return x.i_ == y.i_;} +}; + +int main() +{ + { + constexpr optional<X> opt(2); + constexpr Y y(3); + static_assert(opt.value_or(y) == 2, ""); + } + { + constexpr optional<X> opt(2); + static_assert(opt.value_or(Y(3)) == 2, ""); + } + { + constexpr optional<X> opt; + constexpr Y y(3); + static_assert(opt.value_or(y) == 3, ""); + } + { + constexpr optional<X> opt; + static_assert(opt.value_or(Y(3)) == 4, ""); + } + { + const optional<X> opt(2); + const Y y(3); + assert(opt.value_or(y) == 2); + } + { + const optional<X> opt(2); + assert(opt.value_or(Y(3)) == 2); + } + { + const optional<X> opt; + const Y y(3); + assert(opt.value_or(y) == 3); + } + { + const optional<X> opt; + assert(opt.value_or(Y(3)) == 4); + } +} diff --git a/libcxx/test/std/utilities/optional/optional.object/optional.object.observe/value_rvalue.pass.cpp b/libcxx/test/std/utilities/optional/optional.object/optional.object.observe/value_rvalue.pass.cpp new file mode 100644 index 00000000000..2ef485b7fe5 --- /dev/null +++ b/libcxx/test/std/utilities/optional/optional.object/optional.object.observe/value_rvalue.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. +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++98, c++03, c++11, c++14 +// <optional> + +// constexpr T& optional<T>::value() &&; + +#include <optional> +#include <type_traits> +#include <cassert> + +#include "test_macros.h" + +using std::optional; +using std::bad_optional_access; + +struct X +{ + X() = default; + X(const X&) = delete; + constexpr int test() const & {return 3;} + int test() & {return 4;} + constexpr int test() const && {return 5;} + int test() && {return 6;} +}; + +struct Y +{ + constexpr int test() && {return 7;} +}; + +constexpr int +test() +{ + optional<Y> opt{Y{}}; + return std::move(opt).value().test(); +} + +int main() +{ + { + optional<X> opt; ((void)opt); + ASSERT_NOT_NOEXCEPT(std::move(opt).value()); + ASSERT_SAME_TYPE(decltype(std::move(opt).value()), X&&); + } + { + optional<X> opt; + opt.emplace(); + assert(std::move(opt).value().test() == 6); + } +#ifndef TEST_HAS_NO_EXCEPTIONS + { + optional<X> opt; + try + { + std::move(opt).value(); + assert(false); + } + catch (const bad_optional_access&) + { + } + } +#endif + static_assert(test() == 7, ""); +} |