diff --git a/libcxx/test/std/utilities/optional/optional.object/optional.object.ctor/U.pass.cpp b/libcxx/test/std/utilities/optional/optional.object/optional.object.ctor/U.pass.cpp index a90fecfd075fe..1e951ebdf1d74 100644 --- a/libcxx/test/std/utilities/optional/optional.object/optional.object.ctor/U.pass.cpp +++ b/libcxx/test/std/utilities/optional/optional.object/optional.object.ctor/U.pass.cpp @@ -13,25 +13,28 @@ // template // constexpr EXPLICIT optional(U&& u); +#include #include #include -#include #include "test_macros.h" #include "archetypes.h" #include "test_convertible.h" - using std::optional; -struct ImplicitThrow -{ - constexpr ImplicitThrow(int x) { if (x != -1) TEST_THROW(6);} +struct ImplicitThrow { + constexpr ImplicitThrow(int x) { + if (x != -1) + TEST_THROW(6); + } }; -struct ExplicitThrow -{ - constexpr explicit ExplicitThrow(int x) { if (x != -1) TEST_THROW(6);} +struct ExplicitThrow { + constexpr explicit ExplicitThrow(int x) { + if (x != -1) + TEST_THROW(6); + } }; struct ImplicitAny { @@ -39,56 +42,52 @@ struct ImplicitAny { constexpr ImplicitAny(U&&) {} }; - template -constexpr bool implicit_conversion(optional&& opt, const From& v) -{ - using O = optional; - static_assert(test_convertible(), ""); - static_assert(!test_convertible(), ""); - static_assert(!test_convertible(), ""); - return opt && *opt == static_cast(v); +constexpr bool implicit_conversion(optional&& opt, const From& v) { + using O = optional; + static_assert(test_convertible(), ""); + static_assert(!test_convertible(), ""); + static_assert(!test_convertible(), ""); + return opt && *opt == static_cast(v); } template -constexpr bool explicit_conversion(Input&& in, const Expect& v) -{ - using O = optional; - static_assert(std::is_constructible::value, ""); - static_assert(!std::is_convertible::value, ""); - static_assert(!std::is_constructible::value, ""); - static_assert(!std::is_constructible::value, ""); - optional opt(std::forward(in)); - optional opt2{std::forward(in)}; - return opt && *opt == static_cast(v) && (opt2 && *opt2 == static_cast(v)); +constexpr bool explicit_conversion(Input&& in, const Expect& v) { + using O = optional; + static_assert(std::is_constructible::value, ""); + static_assert(!std::is_convertible::value, ""); + static_assert(!std::is_constructible::value, ""); + static_assert(!std::is_constructible::value, ""); + optional opt(std::forward(in)); + optional opt2{std::forward(in)}; + return opt && *opt == static_cast(v) && (opt2 && *opt2 == static_cast(v)); } -void test_implicit() -{ - { - static_assert(implicit_conversion(42, 42), ""); - } - { - static_assert(implicit_conversion(3.14, 3.14), ""); - } - { - int x = 42; - optional o(&x); - assert(*o == &x); - } - { - using T = TrivialTestTypes::TestType; - static_assert(implicit_conversion(42, 42), ""); - } - { - using T = TestTypes::TestType; - assert(implicit_conversion(3, T(3))); - } - { - using T = TestTypes::TestType; - optional opt({3}); - assert(opt && *opt == static_cast(3)); - } +void test_implicit() { + { + static_assert(implicit_conversion(42, 42), ""); + } + { + static_assert(implicit_conversion(3.14, 3.14), ""); + } + { + int x = 42; + optional o(&x); + assert(*o == &x); + } + { + using T = TrivialTestTypes::TestType; + static_assert(implicit_conversion(42, 42), ""); + } + { + using T = TestTypes::TestType; + assert(implicit_conversion(3, T(3))); + } + { + using T = TestTypes::TestType; + optional opt({3}); + assert(opt && *opt == static_cast(3)); + } { using O = optional; static_assert(!test_convertible(), ""); @@ -96,64 +95,63 @@ void test_implicit() static_assert(!test_convertible(), ""); static_assert(!test_convertible(), ""); static_assert(!test_convertible(), ""); - } #ifndef TEST_HAS_NO_EXCEPTIONS - { - try { - using T = ImplicitThrow; - optional t = 42; - assert(false); - ((void)t); - } catch (int) { - } + { + try { + using T = ImplicitThrow; + optional t = 42; + assert(false); + ((void)t); + } catch (int) { } + } #endif } void test_explicit() { + { + using T = ExplicitTrivialTestTypes::TestType; + static_assert(explicit_conversion(42, 42), ""); + } + { + using T = ExplicitConstexprTestTypes::TestType; + static_assert(explicit_conversion(42, 42), ""); + static_assert(!std::is_convertible::value, ""); + } + { + using T = ExplicitTestTypes::TestType; + T::reset(); { - using T = ExplicitTrivialTestTypes::TestType; - static_assert(explicit_conversion(42, 42), ""); - } - { - using T = ExplicitConstexprTestTypes::TestType; - static_assert(explicit_conversion(42, 42), ""); - static_assert(!std::is_convertible::value, ""); + assert(explicit_conversion(42, 42)); + assert(T::alive == 0); } + T::reset(); { - using T = ExplicitTestTypes::TestType; - T::reset(); - { - assert(explicit_conversion(42, 42)); - assert(T::alive == 0); - } - T::reset(); - { - optional t(42); - assert(T::alive == 1); - assert(T::value_constructed == 1); - assert(T::move_constructed == 0); - assert(T::copy_constructed == 0); - assert(t.value().value == 42); - } - assert(T::alive == 0); + optional t(42); + assert(T::alive == 1); + assert(T::value_constructed == 1); + assert(T::move_constructed == 0); + assert(T::copy_constructed == 0); + assert(t.value().value == 42); } + assert(T::alive == 0); + } #ifndef TEST_HAS_NO_EXCEPTIONS - { - try { - using T = ExplicitThrow; - optional t(42); - assert(false); - } catch (int) { - } + { + try { + using T = ExplicitThrow; + optional t(42); + assert(false); + } catch (int) { } + } #endif } int main(int, char**) { - test_implicit(); - test_explicit(); + test_implicit(); + test_explicit(); return 0; } diff --git a/libcxx/test/std/utilities/optional/optional.object/optional.object.ctor/const_T.pass.cpp b/libcxx/test/std/utilities/optional/optional.object/optional.object.ctor/const_T.pass.cpp index 91a2323eebbf4..67d0fcfc18b86 100644 --- a/libcxx/test/std/utilities/optional/optional.object/optional.object.ctor/const_T.pass.cpp +++ b/libcxx/test/std/utilities/optional/optional.object/optional.object.ctor/const_T.pass.cpp @@ -12,117 +12,102 @@ // constexpr optional(const T& v); +#include #include #include -#include #include "test_macros.h" #include "archetypes.h" using std::optional; -int main(int, char**) -{ - { - typedef int T; - constexpr T t(5); - constexpr optional opt(t); - static_assert(static_cast(opt) == true, ""); - static_assert(*opt == 5, ""); - - struct test_constexpr_ctor - : public optional - { - constexpr test_constexpr_ctor(const T&) {} - }; - - } - { - typedef double T; - constexpr T t(3); - constexpr optional opt(t); - static_assert(static_cast(opt) == true, ""); - static_assert(*opt == 3, ""); - - struct test_constexpr_ctor - : public optional - { - constexpr test_constexpr_ctor(const T&) {} - }; +int main(int, char**) { + { + typedef int T; + constexpr T t(5); + constexpr optional opt(t); + static_assert(static_cast(opt) == true, ""); + static_assert(*opt == 5, ""); - } - { - const int x = 42; - optional o(x); - assert(*o == x); - } - { - typedef TestTypes::TestType T; - T::reset(); - const T t(3); - optional opt = t; - assert(T::alive == 2); - assert(T::copy_constructed == 1); - assert(static_cast(opt) == true); - assert(opt.value().value == 3); - } - { - typedef ExplicitTestTypes::TestType T; - static_assert(!std::is_convertible>::value, ""); - T::reset(); - const T t(3); - optional opt(t); - assert(T::alive == 2); - assert(T::copy_constructed == 1); - assert(static_cast(opt) == true); - assert(opt.value().value == 3); - } - { - typedef ConstexprTestTypes::TestType T; - constexpr T t(3); - constexpr optional opt = {t}; - static_assert(static_cast(opt) == true, ""); - static_assert(opt.value().value == 3, ""); + struct test_constexpr_ctor : public optional { + constexpr test_constexpr_ctor(const T&) {} + }; + } + { + typedef double T; + constexpr T t(3); + constexpr optional opt(t); + static_assert(static_cast(opt) == true, ""); + static_assert(*opt == 3, ""); - struct test_constexpr_ctor - : public optional - { - constexpr test_constexpr_ctor(const T&) {} - }; - } - { - typedef ExplicitConstexprTestTypes::TestType T; - static_assert(!std::is_convertible>::value, ""); - constexpr T t(3); - constexpr optional opt(t); - static_assert(static_cast(opt) == true, ""); - static_assert(opt.value().value == 3, ""); + struct test_constexpr_ctor : public optional { + constexpr test_constexpr_ctor(const T&) {} + }; + } + { + const int x = 42; + optional o(x); + assert(*o == x); + } + { + typedef TestTypes::TestType T; + T::reset(); + const T t(3); + optional opt = t; + assert(T::alive == 2); + assert(T::copy_constructed == 1); + assert(static_cast(opt) == true); + assert(opt.value().value == 3); + } + { + typedef ExplicitTestTypes::TestType T; + static_assert(!std::is_convertible>::value, ""); + T::reset(); + const T t(3); + optional opt(t); + assert(T::alive == 2); + assert(T::copy_constructed == 1); + assert(static_cast(opt) == true); + assert(opt.value().value == 3); + } + { + typedef ConstexprTestTypes::TestType T; + constexpr T t(3); + constexpr optional opt = {t}; + static_assert(static_cast(opt) == true, ""); + static_assert(opt.value().value == 3, ""); - struct test_constexpr_ctor - : public optional - { - constexpr test_constexpr_ctor(const T&) {} - }; + struct test_constexpr_ctor : public optional { + constexpr test_constexpr_ctor(const T&) {} + }; + } + { + typedef ExplicitConstexprTestTypes::TestType T; + static_assert(!std::is_convertible>::value, ""); + constexpr T t(3); + constexpr optional opt(t); + static_assert(static_cast(opt) == true, ""); + static_assert(opt.value().value == 3, ""); - } + struct test_constexpr_ctor : public optional { + constexpr test_constexpr_ctor(const T&) {} + }; + } #ifndef TEST_HAS_NO_EXCEPTIONS - { - struct Z { - Z(int) {} - Z(const Z&) {throw 6;} - }; - typedef Z T; - try - { - const T t(3); - optional opt(t); - assert(false); - } - catch (int i) - { - assert(i == 6); - } + { + struct Z { + Z(int) {} + Z(const Z&) { throw 6; } + }; + typedef Z T; + try { + const T t(3); + optional opt(t); + assert(false); + } catch (int i) { + assert(i == 6); } + } #endif return 0; diff --git a/libcxx/test/std/utilities/optional/optional.object/optional.object.ctor/const_optional_U.pass.cpp b/libcxx/test/std/utilities/optional/optional.object/optional.object.ctor/const_optional_U.pass.cpp index 9505238e6e5e2..70fd76ec6ed0b 100644 --- a/libcxx/test/std/utilities/optional/optional.object/optional.object.ctor/const_optional_U.pass.cpp +++ b/libcxx/test/std/utilities/optional/optional.object/optional.object.ctor/const_optional_U.pass.cpp @@ -12,74 +12,69 @@ // template // optional(const optional& rhs); +#include #include #include -#include #include "test_macros.h" using std::optional; template -TEST_CONSTEXPR_CXX20 void -test(const optional& rhs, bool is_going_to_throw = false) -{ - bool rhs_engaged = static_cast(rhs); +TEST_CONSTEXPR_CXX20 void test(const optional& rhs, bool is_going_to_throw = false) { + bool rhs_engaged = static_cast(rhs); #ifndef TEST_HAS_NO_EXCEPTIONS - try - { - optional lhs = rhs; - assert(is_going_to_throw == false); - assert(static_cast(lhs) == rhs_engaged); - if (rhs_engaged) - assert(*lhs == *rhs); - } - catch (int i) - { - assert(i == 6); - } -#else - if (is_going_to_throw) return; + try { optional lhs = rhs; + assert(is_going_to_throw == false); assert(static_cast(lhs) == rhs_engaged); if (rhs_engaged) - assert(*lhs == *rhs); + assert(*lhs == *rhs); + } catch (int i) { + assert(i == 6); + } +#else + if (is_going_to_throw) + return; + optional lhs = rhs; + assert(static_cast(lhs) == rhs_engaged); + if (rhs_engaged) + assert(*lhs == *rhs); #endif } -class X -{ - int i_; +class X { + int i_; + public: - constexpr X(int i) : i_(i) {} - constexpr X(const X& x) : i_(x.i_) {} - TEST_CONSTEXPR_CXX20 ~X() {i_ = 0;} - friend constexpr bool operator==(const X& x, const X& y) {return x.i_ == y.i_;} + constexpr X(int i) : i_(i) {} + constexpr X(const X& x) : i_(x.i_) {} + TEST_CONSTEXPR_CXX20 ~X() { i_ = 0; } + friend constexpr bool operator==(const X& x, const X& y) { return x.i_ == y.i_; } }; -class Y -{ - int i_; +class Y { + int i_; + public: - constexpr Y(int i) : i_(i) {} + constexpr Y(int i) : i_(i) {} - friend constexpr bool operator==(const Y& x, const Y& y) {return x.i_ == y.i_;} + friend constexpr bool operator==(const Y& x, const Y& y) { return x.i_ == y.i_; } }; int count = 0; -class Z -{ - int i_; +class Z { + int i_; + public: - Z(int i) : i_(i) {TEST_THROW(6);} + Z(int i) : i_(i) { TEST_THROW(6); } - friend bool operator==(const Z& x, const Z& y) {return x.i_ == y.i_;} + friend bool operator==(const Z& x, const Z& y) { return x.i_ == y.i_; } }; -template -constexpr bool test_all() -{ +template +constexpr bool test_all() { { optional rhs; test(rhs); @@ -91,30 +86,29 @@ constexpr bool test_all() return true; } -int main(int, char**) -{ - test_all(); - test_all(); - test_all(); +int main(int, char**) { + test_all(); + test_all(); + test_all(); #if TEST_STD_VER > 17 - static_assert(test_all()); - static_assert(test_all()); - static_assert(test_all()); + static_assert(test_all()); + static_assert(test_all()); + static_assert(test_all()); #endif - { - typedef Z T; - typedef int U; - optional rhs; - test(rhs); - } - { - typedef Z T; - typedef int U; - optional rhs(U{3}); - test(rhs, true); - } - - static_assert(!(std::is_constructible, const optional&>::value), ""); + { + typedef Z T; + typedef int U; + optional rhs; + test(rhs); + } + { + typedef Z T; + typedef int U; + optional rhs(U{3}); + test(rhs, true); + } + + static_assert(!(std::is_constructible, const optional&>::value), ""); return 0; } diff --git a/libcxx/test/std/utilities/optional/optional.object/optional.object.ctor/copy.pass.cpp b/libcxx/test/std/utilities/optional/optional.object/optional.object.ctor/copy.pass.cpp index 54a424c4c347d..f61a22c23a04d 100644 --- a/libcxx/test/std/utilities/optional/optional.object/optional.object.ctor/copy.pass.cpp +++ b/libcxx/test/std/utilities/optional/optional.object/optional.object.ctor/copy.pass.cpp @@ -11,173 +11,165 @@ // constexpr optional(const optional& rhs); +#include #include #include -#include #include "test_macros.h" #include "archetypes.h" using std::optional; -template -void test(InitArgs&&... args) -{ - const optional rhs(std::forward(args)...); - bool rhs_engaged = static_cast(rhs); - optional lhs = rhs; - assert(static_cast(lhs) == rhs_engaged); - if (rhs_engaged) - assert(*lhs == *rhs); +template +void test(InitArgs&&... args) { + const optional rhs(std::forward(args)...); + bool rhs_engaged = static_cast(rhs); + optional lhs = rhs; + assert(static_cast(lhs) == rhs_engaged); + if (rhs_engaged) + assert(*lhs == *rhs); } -template -constexpr bool constexpr_test(InitArgs&&... args) -{ - static_assert( std::is_trivially_copy_constructible_v, ""); // requirement - const optional rhs(std::forward(args)...); - optional lhs = rhs; - return (lhs.has_value() == rhs.has_value()) && - (lhs.has_value() ? *lhs == *rhs : true); +template +constexpr bool constexpr_test(InitArgs&&... args) { + static_assert(std::is_trivially_copy_constructible_v, ""); // requirement + const optional rhs(std::forward(args)...); + optional lhs = rhs; + return (lhs.has_value() == rhs.has_value()) && (lhs.has_value() ? *lhs == *rhs : true); } void test_throwing_ctor() { #ifndef TEST_HAS_NO_EXCEPTIONS - struct Z { - Z() : count(0) {} - Z(Z const& o) : count(o.count + 1) - { if (count == 2) throw 6; } - int count; - }; - const Z z; - const optional rhs(z); - try - { - optional lhs(rhs); - assert(false); - } - catch (int i) - { - assert(i == 6); + struct Z { + Z() : count(0) {} + Z(Z const& o) : count(o.count + 1) { + if (count == 2) + throw 6; } + int count; + }; + const Z z; + const optional rhs(z); + try { + optional lhs(rhs); + assert(false); + } catch (int i) { + assert(i == 6); + } #endif } -template -void test_ref(InitArgs&&... args) -{ - const optional rhs(std::forward(args)...); - bool rhs_engaged = static_cast(rhs); - optional lhs = rhs; - assert(static_cast(lhs) == rhs_engaged); - if (rhs_engaged) - assert(&(*lhs) == &(*rhs)); +template +void test_ref(InitArgs&&... args) { + const optional rhs(std::forward(args)...); + bool rhs_engaged = static_cast(rhs); + optional lhs = rhs; + assert(static_cast(lhs) == rhs_engaged); + if (rhs_engaged) + assert(&(*lhs) == &(*rhs)); } - -void test_reference_extension() -{ +void test_reference_extension() { #if defined(_LIBCPP_VERSION) && 0 // FIXME these extensions are currently disabled. - using T = TestTypes::TestType; - T::reset(); - { - T t; - T::reset_constructors(); - test_ref(); - test_ref(t); - assert(T::alive == 1); - assert(T::constructed == 0); - assert(T::assigned == 0); - assert(T::destroyed == 0); - } - assert(T::destroyed == 1); - assert(T::alive == 0); - { - T t; - const T& ct = t; - T::reset_constructors(); - test_ref(); - test_ref(t); - test_ref(ct); - assert(T::alive == 1); - assert(T::constructed == 0); - assert(T::assigned == 0); - assert(T::destroyed == 0); - } - assert(T::alive == 0); - assert(T::destroyed == 1); - { - static_assert(!std::is_copy_constructible>::value, ""); - static_assert(!std::is_copy_constructible>::value, ""); - } + using T = TestTypes::TestType; + T::reset(); + { + T t; + T::reset_constructors(); + test_ref(); + test_ref(t); + assert(T::alive == 1); + assert(T::constructed == 0); + assert(T::assigned == 0); + assert(T::destroyed == 0); + } + assert(T::destroyed == 1); + assert(T::alive == 0); + { + T t; + const T& ct = t; + T::reset_constructors(); + test_ref(); + test_ref(t); + test_ref(ct); + assert(T::alive == 1); + assert(T::constructed == 0); + assert(T::assigned == 0); + assert(T::destroyed == 0); + } + assert(T::alive == 0); + assert(T::destroyed == 1); + { + static_assert(!std::is_copy_constructible>::value, ""); + static_assert(!std::is_copy_constructible>::value, ""); + } #endif } -int main(int, char**) -{ - test(); - test(3); - static_assert(constexpr_test(), "" ); - static_assert(constexpr_test(3), "" ); +int main(int, char**) { + test(); + test(3); + static_assert(constexpr_test(), ""); + static_assert(constexpr_test(3), ""); - { - const optional o(42); - optional o2(o); - assert(*o2 == 42); - } - { - using T = TestTypes::TestType; - T::reset(); - const optional rhs; - assert(T::alive == 0); - const optional lhs(rhs); - assert(lhs.has_value() == false); - assert(T::alive == 0); - } - TestTypes::TestType::reset(); - { - using T = TestTypes::TestType; - T::reset(); - const optional rhs(42); - assert(T::alive == 1); - assert(T::value_constructed == 1); - assert(T::copy_constructed == 0); - const optional lhs(rhs); - assert(lhs.has_value()); - assert(T::copy_constructed == 1); - assert(T::alive == 2); - } - TestTypes::TestType::reset(); - { - using namespace ConstexprTestTypes; - test(); - test(42); - } - { - using namespace TrivialTestTypes; - test(); - test(42); - } - { - test_throwing_ctor(); - } - { - test_reference_extension(); - } - { - constexpr std::optional o1{4}; - constexpr std::optional o2 = o1; - static_assert( *o2 == 4, "" ); - } + { + const optional o(42); + optional o2(o); + assert(*o2 == 42); + } + { + using T = TestTypes::TestType; + T::reset(); + const optional rhs; + assert(T::alive == 0); + const optional lhs(rhs); + assert(lhs.has_value() == false); + assert(T::alive == 0); + } + TestTypes::TestType::reset(); + { + using T = TestTypes::TestType; + T::reset(); + const optional rhs(42); + assert(T::alive == 1); + assert(T::value_constructed == 1); + assert(T::copy_constructed == 0); + const optional lhs(rhs); + assert(lhs.has_value()); + assert(T::copy_constructed == 1); + assert(T::alive == 2); + } + TestTypes::TestType::reset(); + { + using namespace ConstexprTestTypes; + test(); + test(42); + } + { + using namespace TrivialTestTypes; + test(); + test(42); + } + { + test_throwing_ctor(); + } + { + test_reference_extension(); + } + { + constexpr std::optional o1{4}; + constexpr std::optional o2 = o1; + static_assert(*o2 == 4, ""); + } - // LWG3836 https://wg21.link/LWG3836 - // std::optional conversion constructor optional(const optional&) - // should take precedence over optional(U&&) with operator bool - { - std::optional o1(false); - std::optional o2(o1); - assert(!o2.value()); - } + // LWG3836 https://wg21.link/LWG3836 + // std::optional conversion constructor optional(const optional&) + // should take precedence over optional(U&&) with operator bool + { + std::optional o1(false); + std::optional o2(o1); + assert(!o2.value()); + } return 0; } diff --git a/libcxx/test/std/utilities/optional/optional.object/optional.object.ctor/ctor.verify.cpp b/libcxx/test/std/utilities/optional/optional.object/optional.object.ctor/ctor.verify.cpp index c5281783d4350..00ca941668eb2 100644 --- a/libcxx/test/std/utilities/optional/optional.object/optional.object.ctor/ctor.verify.cpp +++ b/libcxx/test/std/utilities/optional/optional.object/optional.object.ctor/ctor.verify.cpp @@ -13,28 +13,25 @@ // and shall satisfy the Cpp17Destructible requirements. // Note: array types do not satisfy the Cpp17Destructible requirements. -#include -#include #include +#include #include "test_macros.h" -struct NonDestructible { ~NonDestructible() = delete; }; +struct NonDestructible { + ~NonDestructible() = delete; +}; -int main(int, char**) -{ +int main(int, char**) { + // clang-format off { #if TEST_STD_VER >= 26 - std::optional - opt2; // expected-error-re@optional:* {{static assertion failed{{.*}}instantiation of optional with an rvalue reference type is ill-formed}} + std::optional opt2; // expected-error-re@optional:* {{static assertion failed{{.*}}instantiation of optional with an rvalue reference type is ill-formed}} #else - std::optional - o1; // expected-error-re@optional:* {{static assertion failed{{.*}}instantiation of optional with a reference type is ill-formed}} + std::optional o1; // expected-error-re@optional:* {{static assertion failed{{.*}}instantiation of optional with a reference type is ill-formed}} #endif - std::optional - o2; // expected-error-re@optional:* {{static assertion failed{{.*}}instantiation of optional with a non-destructible type is ill-formed}} - std::optional - o3; // expected-error-re@optional:* {{static assertion failed{{.*}}instantiation of optional with an array type is ill-formed}} + std::optional o2; // expected-error-re@optional:* {{static assertion failed{{.*}}instantiation of optional with a non-destructible type is ill-formed}} + std::optional o3; // expected-error-re@optional:* {{static assertion failed{{.*}}instantiation of optional with an array type is ill-formed}} } { @@ -44,12 +41,12 @@ int main(int, char**) std::optional o4; // expected-error-re@optional:* {{static assertion failed{{.*}}instantiation of optional with in_place_t is ill-formed}} } - { + { std::optional< std::nullopt_t> o1; // expected-error-re@optional:* {{static assertion failed{{.*}}instantiation of optional with nullopt_t is ill-formed}} std::optional o2; // expected-error-re@optional:* {{static assertion failed{{.*}}instantiation of optional with nullopt_t is ill-formed}} std::optional< volatile std::nullopt_t> o3; // expected-error-re@optional:* {{static assertion failed{{.*}}instantiation of optional with nullopt_t is ill-formed}} std::optional o4; // expected-error-re@optional:* {{static assertion failed{{.*}}instantiation of optional with nullopt_t is ill-formed}} - } - - return 0; + } + // clang-format on + return 0; } diff --git a/libcxx/test/std/utilities/optional/optional.object/optional.object.ctor/deduct.pass.cpp b/libcxx/test/std/utilities/optional/optional.object/optional.object.ctor/deduct.pass.cpp index 9bfde5abaa9ac..bc1d26aa8bd18 100644 --- a/libcxx/test/std/utilities/optional/optional.object/optional.object.ctor/deduct.pass.cpp +++ b/libcxx/test/std/utilities/optional/optional.object/optional.object.ctor/deduct.pass.cpp @@ -12,67 +12,66 @@ // template // optional(T) -> optional; -#include #include +#include #include "test_macros.h" struct A {}; -int main(int, char**) -{ -// Test the explicit deduction guides - { -// optional(T) +int main(int, char**) { + // Test the explicit deduction guides + { + // optional(T) std::optional opt(5); ASSERT_SAME_TYPE(decltype(opt), std::optional); assert(static_cast(opt)); assert(*opt == 5); - } + } - { -// optional(T) + { + // optional(T) std::optional opt(A{}); ASSERT_SAME_TYPE(decltype(opt), std::optional); assert(static_cast(opt)); - } + } - { -// optional(const T&); + { + // optional(const T&); const int& source = 5; std::optional opt(source); ASSERT_SAME_TYPE(decltype(opt), std::optional); assert(static_cast(opt)); assert(*opt == 5); - } + } - { -// optional(T*); + { + // optional(T*); const int* source = nullptr; std::optional opt(source); ASSERT_SAME_TYPE(decltype(opt), std::optional); assert(static_cast(opt)); assert(*opt == nullptr); - } + } - { -// optional(T[]); + { + // optional(T[]); int source[] = {1, 2, 3}; std::optional opt(source); ASSERT_SAME_TYPE(decltype(opt), std::optional); assert(static_cast(opt)); assert((*opt)[0] == 1); - } + } -// Test the implicit deduction guides - { -// optional(optional); + // Test the implicit deduction guides + { + // optional(optional); std::optional source('A'); std::optional opt(source); ASSERT_SAME_TYPE(decltype(opt), std::optional); assert(static_cast(opt) == static_cast(source)); assert(*opt == *source); - } + } return 0; } diff --git a/libcxx/test/std/utilities/optional/optional.object/optional.object.ctor/deduct.verify.cpp b/libcxx/test/std/utilities/optional/optional.object/optional.object.ctor/deduct.verify.cpp index 364f9b2e955f0..7ab842bf68c51 100644 --- a/libcxx/test/std/utilities/optional/optional.object/optional.object.ctor/deduct.verify.cpp +++ b/libcxx/test/std/utilities/optional/optional.object/optional.object.ctor/deduct.verify.cpp @@ -13,25 +13,26 @@ // template // optional(T) -> optional; -#include #include +#include struct A {}; -int main(int, char**) -{ -// Test the explicit deduction guides +int main(int, char**) { + // Test the explicit deduction guides -// Test the implicit deduction guides - { -// optional() - std::optional opt; // expected-error-re {{no viable constructor or deduction guide for deduction of template arguments of '{{(std::)?}}optional'}} - } + // Test the implicit deduction guides - { -// optional(nullopt_t) - std::optional opt(std::nullopt); // expected-error-re@optional:* {{static assertion failed{{.*}}instantiation of optional with nullopt_t is ill-formed}} - } + // clang-format off + { + // optional() + std::optional opt; // expected-error-re {{no viable constructor or deduction guide for deduction of template arguments of '{{(std::)?}}optional'}} + } + { + // optional(nullopt_t) + std::optional opt(std::nullopt); // expected-error-re@optional:* {{static assertion failed{{.*}}instantiation of optional with nullopt_t is ill-formed}} + } + // clang-format on return 0; } diff --git a/libcxx/test/std/utilities/optional/optional.object/optional.object.ctor/default.pass.cpp b/libcxx/test/std/utilities/optional/optional.object/optional.object.ctor/default.pass.cpp index 61a365edb64ea..71d4d052da3b7 100644 --- a/libcxx/test/std/utilities/optional/optional.object/optional.object.ctor/default.pass.cpp +++ b/libcxx/test/std/utilities/optional/optional.object/optional.object.ctor/default.pass.cpp @@ -11,9 +11,9 @@ // constexpr optional() noexcept; +#include #include #include -#include #include "test_macros.h" #include "archetypes.h" @@ -21,61 +21,52 @@ using std::optional; template -void -test_constexpr() -{ - static_assert(std::is_nothrow_default_constructible::value, ""); - static_assert(std::is_trivially_destructible::value, ""); - static_assert(std::is_trivially_destructible::value, ""); +void test_constexpr() { + static_assert(std::is_nothrow_default_constructible::value, ""); + static_assert(std::is_trivially_destructible::value, ""); + static_assert(std::is_trivially_destructible::value, ""); - constexpr Opt opt; - static_assert(static_cast(opt) == false, ""); + constexpr Opt opt; + static_assert(static_cast(opt) == false, ""); - struct test_constexpr_ctor - : public Opt - { - constexpr test_constexpr_ctor() {} - }; + struct test_constexpr_ctor : public Opt { + constexpr test_constexpr_ctor() {} + }; } template -void -test() -{ - static_assert(std::is_nothrow_default_constructible::value, ""); - static_assert(!std::is_trivially_destructible::value, ""); - static_assert(!std::is_trivially_destructible::value, ""); - { - Opt opt; - assert(static_cast(opt) == false); - } - { - const Opt opt; - assert(static_cast(opt) == false); - } +void test() { + static_assert(std::is_nothrow_default_constructible::value, ""); + static_assert(!std::is_trivially_destructible::value, ""); + static_assert(!std::is_trivially_destructible::value, ""); + { + Opt opt; + assert(static_cast(opt) == false); + } + { + const Opt opt; + assert(static_cast(opt) == false); + } - struct test_constexpr_ctor - : public Opt - { - constexpr test_constexpr_ctor() {} - }; + struct test_constexpr_ctor : public Opt { + constexpr test_constexpr_ctor() {} + }; } -int main(int, char**) -{ - test_constexpr>(); - test_constexpr>(); - test_constexpr>(); - test_constexpr>(); - test_constexpr>(); - test>(); - // EXTENSIONS +int main(int, char**) { + test_constexpr>(); + test_constexpr>(); + test_constexpr>(); + test_constexpr>(); + test_constexpr>(); + test>(); + // EXTENSIONS #if defined(_LIBCPP_VERSION) && 0 // FIXME these extensions are currently disabled. - test_constexpr>(); - test_constexpr>(); - test_constexpr>(); - test_constexpr>(); - test_constexpr>(); + test_constexpr>(); + test_constexpr>(); + test_constexpr>(); + test_constexpr>(); + test_constexpr>(); #endif return 0; diff --git a/libcxx/test/std/utilities/optional/optional.object/optional.object.ctor/empty_in_place_t_does_not_clobber.pass.cpp b/libcxx/test/std/utilities/optional/optional.object/optional.object.ctor/empty_in_place_t_does_not_clobber.pass.cpp index 594aac770bc82..f19174841813c 100644 --- a/libcxx/test/std/utilities/optional/optional.object/optional.object.ctor/empty_in_place_t_does_not_clobber.pass.cpp +++ b/libcxx/test/std/utilities/optional/optional.object/optional.object.ctor/empty_in_place_t_does_not_clobber.pass.cpp @@ -15,9 +15,9 @@ // in_place_t constructor with no arguments when the Clang is trying to check // copy constructor. +#include #include #include -#include #include "test_macros.h" #include "archetypes.h" diff --git a/libcxx/test/std/utilities/optional/optional.object/optional.object.ctor/explicit_const_optional_U.pass.cpp b/libcxx/test/std/utilities/optional/optional.object/optional.object.ctor/explicit_const_optional_U.pass.cpp index d8594bc03b132..1b9882fb25633 100644 --- a/libcxx/test/std/utilities/optional/optional.object/optional.object.ctor/explicit_const_optional_U.pass.cpp +++ b/libcxx/test/std/utilities/optional/optional.object/optional.object.ctor/explicit_const_optional_U.pass.cpp @@ -12,75 +12,70 @@ // template // explicit optional(const optional& rhs); +#include #include #include -#include #include "test_macros.h" using std::optional; template -TEST_CONSTEXPR_CXX20 void -test(const optional& rhs, bool is_going_to_throw = false) -{ - static_assert(!(std::is_convertible&, optional>::value), ""); - bool rhs_engaged = static_cast(rhs); +TEST_CONSTEXPR_CXX20 void test(const optional& rhs, bool is_going_to_throw = false) { + static_assert(!(std::is_convertible&, optional>::value), ""); + bool rhs_engaged = static_cast(rhs); #ifndef TEST_HAS_NO_EXCEPTIONS - try - { - optional lhs(rhs); - assert(is_going_to_throw == false); - assert(static_cast(lhs) == rhs_engaged); - if (rhs_engaged) - assert(*lhs == T(*rhs)); - } - catch (int i) - { - assert(i == 6); - } -#else - if (is_going_to_throw) return; + try { optional lhs(rhs); + assert(is_going_to_throw == false); assert(static_cast(lhs) == rhs_engaged); if (rhs_engaged) - assert(*lhs == T(*rhs)); + assert(*lhs == T(*rhs)); + } catch (int i) { + assert(i == 6); + } +#else + if (is_going_to_throw) + return; + optional lhs(rhs); + assert(static_cast(lhs) == rhs_engaged); + if (rhs_engaged) + assert(*lhs == T(*rhs)); #endif } -class X -{ - int i_; +class X { + int i_; + public: - constexpr explicit X(int i) : i_(i) {} - constexpr X(const X& x) : i_(x.i_) {} - TEST_CONSTEXPR_CXX20 ~X() {i_ = 0;} - friend constexpr bool operator==(const X& x, const X& y) {return x.i_ == y.i_;} + constexpr explicit X(int i) : i_(i) {} + constexpr X(const X& x) : i_(x.i_) {} + TEST_CONSTEXPR_CXX20 ~X() { i_ = 0; } + friend constexpr bool operator==(const X& x, const X& y) { return x.i_ == y.i_; } }; -class Y -{ - int i_; +class Y { + int i_; + public: - constexpr explicit Y(int i) : i_(i) {} + constexpr explicit Y(int i) : i_(i) {} - friend constexpr bool operator==(const Y& x, const Y& y) {return x.i_ == y.i_;} + friend constexpr bool operator==(const Y& x, const Y& y) { return x.i_ == y.i_; } }; int count = 0; -class Z -{ - int i_; +class Z { + int i_; + public: - explicit Z(int i) : i_(i) {TEST_THROW(6);} + explicit Z(int i) : i_(i) { TEST_THROW(6); } - friend bool operator==(const Z& x, const Z& y) {return x.i_ == y.i_;} + friend bool operator==(const Z& x, const Z& y) { return x.i_ == y.i_; } }; -template -constexpr bool test_all() -{ +template +constexpr bool test_all() { { optional rhs; test(rhs); @@ -92,27 +87,25 @@ constexpr bool test_all() return true; } - -int main(int, char**) -{ - test_all(); - test_all(); +int main(int, char**) { + test_all(); + test_all(); #if TEST_STD_VER > 17 - static_assert(test_all()); - static_assert(test_all()); + static_assert(test_all()); + static_assert(test_all()); #endif - { - typedef Z T; - typedef int U; - optional rhs; - test(rhs); - } - { - typedef Z T; - typedef int U; - optional rhs(3); - test(rhs, true); - } + { + typedef Z T; + typedef int U; + optional rhs; + test(rhs); + } + { + typedef Z T; + typedef int U; + optional rhs(3); + test(rhs, true); + } return 0; } diff --git a/libcxx/test/std/utilities/optional/optional.object/optional.object.ctor/explicit_optional_U.pass.cpp b/libcxx/test/std/utilities/optional/optional.object/optional.object.ctor/explicit_optional_U.pass.cpp index 708370a47b616..bddbd4ba93d5a 100644 --- a/libcxx/test/std/utilities/optional/optional.object/optional.object.ctor/explicit_optional_U.pass.cpp +++ b/libcxx/test/std/utilities/optional/optional.object/optional.object.ctor/explicit_optional_U.pass.cpp @@ -12,83 +12,77 @@ // template // explicit optional(optional&& rhs); +#include #include #include -#include #include "test_macros.h" using std::optional; template -TEST_CONSTEXPR_CXX20 void test(optional&& rhs, bool is_going_to_throw = false) -{ - static_assert(!(std::is_convertible&&, optional>::value), ""); - bool rhs_engaged = static_cast(rhs); +TEST_CONSTEXPR_CXX20 void test(optional&& rhs, bool is_going_to_throw = false) { + static_assert(!(std::is_convertible&&, optional>::value), ""); + bool rhs_engaged = static_cast(rhs); #ifndef TEST_HAS_NO_EXCEPTIONS - try - { - optional lhs(std::move(rhs)); - assert(is_going_to_throw == false); - assert(static_cast(lhs) == rhs_engaged); - } - catch (int i) - { - assert(i == 6); - } -#else - if (is_going_to_throw) return; + try { optional lhs(std::move(rhs)); + assert(is_going_to_throw == false); assert(static_cast(lhs) == rhs_engaged); + } catch (int i) { + assert(i == 6); + } +#else + if (is_going_to_throw) + return; + optional lhs(std::move(rhs)); + assert(static_cast(lhs) == rhs_engaged); #endif } -class X -{ - int i_; +class X { + int i_; + public: - constexpr explicit X(int i) : i_(i) {} - constexpr X(X&& x) : i_(x.i_) { x.i_ = 0; } - TEST_CONSTEXPR_CXX20 ~X() {i_ = 0;} - friend constexpr bool operator==(const X& x, const X& y) {return x.i_ == y.i_;} + constexpr explicit X(int i) : i_(i) {} + constexpr X(X&& x) : i_(x.i_) { x.i_ = 0; } + TEST_CONSTEXPR_CXX20 ~X() { i_ = 0; } + friend constexpr bool operator==(const X& x, const X& y) { return x.i_ == y.i_; } }; int count = 0; -class Z -{ +class Z { public: - explicit Z(int) { TEST_THROW(6); } + explicit Z(int) { TEST_THROW(6); } }; -TEST_CONSTEXPR_CXX20 bool test() -{ - { - optional rhs; - test(std::move(rhs)); - } - { - optional rhs(3); - test(std::move(rhs)); - } +TEST_CONSTEXPR_CXX20 bool test() { + { + optional rhs; + test(std::move(rhs)); + } + { + optional rhs(3); + test(std::move(rhs)); + } - return true; + return true; } -int main(int, char**) -{ +int main(int, char**) { #if TEST_STD_VER > 17 - static_assert(test()); + static_assert(test()); #endif - test(); - { - optional rhs; - test(std::move(rhs)); - } - { - optional rhs(3); - test(std::move(rhs), true); - } + test(); + { + optional rhs; + test(std::move(rhs)); + } + { + optional rhs(3); + test(std::move(rhs), true); + } return 0; } diff --git a/libcxx/test/std/utilities/optional/optional.object/optional.object.ctor/in_place_t.pass.cpp b/libcxx/test/std/utilities/optional/optional.object/optional.object.ctor/in_place_t.pass.cpp index 65276c5a01976..902754418fbde 100644 --- a/libcxx/test/std/utilities/optional/optional.object/optional.object.ctor/in_place_t.pass.cpp +++ b/libcxx/test/std/utilities/optional/optional.object/optional.object.ctor/in_place_t.pass.cpp @@ -13,136 +13,112 @@ // template // constexpr explicit optional(in_place_t, Args&&... args); +#include #include #include -#include #include "test_macros.h" -using std::optional; -using std::in_place_t; using std::in_place; +using std::in_place_t; +using std::optional; + +class X { + int i_; + int j_ = 0; -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) {} + X() : i_(0) {} + X(int i) : i_(i) {} + X(int i, int j) : i_(i), j_(j) {} - ~X() {} + ~X() {} - friend bool operator==(const X& x, const X& y) - {return x.i_ == y.i_ && x.j_ == y.j_;} + friend bool operator==(const X& x, const X& y) { return x.i_ == y.i_ && x.j_ == y.j_; } }; -class Y -{ - int i_; - int j_ = 0; +class Y { + int i_; + int j_ = 0; + public: - constexpr Y() : i_(0) {} - constexpr Y(int i) : i_(i) {} - constexpr Y(int i, int j) : i_(i), j_(j) {} + constexpr Y() : i_(0) {} + constexpr Y(int i) : i_(i) {} + constexpr Y(int i, int j) : i_(i), j_(j) {} - friend constexpr bool operator==(const Y& x, const Y& y) - {return x.i_ == y.i_ && x.j_ == y.j_;} + friend constexpr bool operator==(const Y& x, const Y& y) { return x.i_ == y.i_ && x.j_ == y.j_; } }; -class Z -{ +class Z { public: - Z(int) {TEST_THROW(6);} + Z(int) { TEST_THROW(6); } }; - -int main(int, char**) -{ - { - constexpr optional opt(in_place, 5); - static_assert(static_cast(opt) == true, ""); - static_assert(*opt == 5, ""); - - struct test_constexpr_ctor - : public optional - { - constexpr test_constexpr_ctor(in_place_t, int i) - : optional(in_place, i) {} - }; - - } - { - optional opt(in_place, 5); - assert(*opt == 5); - } - { - const optional opt(in_place); - assert(static_cast(opt) == true); - assert(*opt == X()); - } - { - const optional opt(in_place, 5); - assert(static_cast(opt) == true); - assert(*opt == X(5)); - } - { - const optional opt(in_place, 5, 4); - assert(static_cast(opt) == true); - assert(*opt == X(5, 4)); - } - { - constexpr optional opt(in_place); - static_assert(static_cast(opt) == true, ""); - static_assert(*opt == Y(), ""); - - struct test_constexpr_ctor - : public optional - { - constexpr test_constexpr_ctor(in_place_t) - : optional(in_place) {} - }; - - } - { - constexpr optional opt(in_place, 5); - static_assert(static_cast(opt) == true, ""); - static_assert(*opt == Y(5), ""); - - struct test_constexpr_ctor - : public optional - { - constexpr test_constexpr_ctor(in_place_t, int i) - : optional(in_place, i) {} - }; - - } - { - constexpr optional opt(in_place, 5, 4); - static_assert(static_cast(opt) == true, ""); - static_assert(*opt == Y(5, 4), ""); - - struct test_constexpr_ctor - : public optional - { - constexpr test_constexpr_ctor(in_place_t, int i, int j) - : optional(in_place, i, j) {} - }; - - } +int main(int, char**) { + { + constexpr optional opt(in_place, 5); + static_assert(static_cast(opt) == true, ""); + static_assert(*opt == 5, ""); + + struct test_constexpr_ctor : public optional { + constexpr test_constexpr_ctor(in_place_t, int i) : optional(in_place, i) {} + }; + } + { + optional opt(in_place, 5); + assert(*opt == 5); + } + { + const optional opt(in_place); + assert(static_cast(opt) == true); + assert(*opt == X()); + } + { + const optional opt(in_place, 5); + assert(static_cast(opt) == true); + assert(*opt == X(5)); + } + { + const optional opt(in_place, 5, 4); + assert(static_cast(opt) == true); + assert(*opt == X(5, 4)); + } + { + constexpr optional opt(in_place); + static_assert(static_cast(opt) == true, ""); + static_assert(*opt == Y(), ""); + + struct test_constexpr_ctor : public optional { + constexpr test_constexpr_ctor(in_place_t) : optional(in_place) {} + }; + } + { + constexpr optional opt(in_place, 5); + static_assert(static_cast(opt) == true, ""); + static_assert(*opt == Y(5), ""); + + struct test_constexpr_ctor : public optional { + constexpr test_constexpr_ctor(in_place_t, int i) : optional(in_place, i) {} + }; + } + { + constexpr optional opt(in_place, 5, 4); + static_assert(static_cast(opt) == true, ""); + static_assert(*opt == Y(5, 4), ""); + + struct test_constexpr_ctor : public optional { + constexpr test_constexpr_ctor(in_place_t, int i, int j) : optional(in_place, i, j) {} + }; + } #ifndef TEST_HAS_NO_EXCEPTIONS - { - try - { - const optional opt(in_place, 1); - assert(false); - } - catch (int i) - { - assert(i == 6); - } + { + try { + const optional opt(in_place, 1); + assert(false); + } catch (int i) { + assert(i == 6); } + } #endif return 0; diff --git a/libcxx/test/std/utilities/optional/optional.object/optional.object.ctor/initializer_list.pass.cpp b/libcxx/test/std/utilities/optional/optional.object/optional.object.ctor/initializer_list.pass.cpp index 6c42df9e1e097..1993476792878 100644 --- a/libcxx/test/std/utilities/optional/optional.object/optional.object.ctor/initializer_list.pass.cpp +++ b/libcxx/test/std/utilities/optional/optional.object/optional.object.ctor/initializer_list.pass.cpp @@ -13,105 +13,93 @@ // constexpr // explicit optional(in_place_t, initializer_list il, Args&&... args); +#include +#include #include #include -#include #include -#include #include "test_macros.h" -using std::optional; -using std::in_place_t; using std::in_place; +using std::in_place_t; +using std::optional; + +class X { + int i_; + int j_ = 0; -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) {} + X() : i_(0) {} + X(int i) : i_(i) {} + X(int i, int j) : i_(i), j_(j) {} - ~X() {} + ~X() {} - friend bool operator==(const X& x, const X& y) - {return x.i_ == y.i_ && x.j_ == y.j_;} + friend bool operator==(const X& x, const X& y) { return x.i_ == y.i_ && x.j_ == y.j_; } }; -class Y -{ - int i_; - int j_ = 0; +class Y { + int i_; + int j_ = 0; + public: - constexpr Y() : i_(0) {} - constexpr Y(int i) : i_(i) {} - constexpr Y(std::initializer_list il) : i_(il.begin()[0]), j_(il.begin()[1]) {} + constexpr Y() : i_(0) {} + constexpr Y(int i) : i_(i) {} + constexpr Y(std::initializer_list 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_;} + 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; +class Z { + int i_; + int j_ = 0; + public: - Z() : i_(0) {} - Z(int i) : i_(i) {} - Z(std::initializer_list il) : i_(il.begin()[0]), j_(il.begin()[1]) - {TEST_THROW(6);} + Z() : i_(0) {} + Z(int i) : i_(i) {} + Z(std::initializer_list il) : i_(il.begin()[0]), j_(il.begin()[1]) { TEST_THROW(6); } - friend bool operator==(const Z& x, const Z& y) - {return x.i_ == y.i_ && x.j_ == y.j_;} + friend bool operator==(const Z& x, const Z& y) { return x.i_ == y.i_ && x.j_ == y.j_; } }; -int main(int, char**) -{ - { - static_assert(!std::is_constructible&>::value, ""); - static_assert(!std::is_constructible, std::initializer_list&>::value, ""); - } - { - optional> opt(in_place, {3, 1}); - assert(static_cast(opt) == true); - assert((*opt == std::vector{3, 1})); - assert(opt->size() == 2); - } - { - optional> opt(in_place, {3, 1}, std::allocator()); - assert(static_cast(opt) == true); - assert((*opt == std::vector{3, 1})); - assert(opt->size() == 2); - } - { - static_assert(std::is_constructible, std::initializer_list&>::value, ""); - constexpr optional opt(in_place, {3, 1}); - static_assert(static_cast(opt) == true, ""); - static_assert(*opt == Y{3, 1}, ""); - - struct test_constexpr_ctor - : public optional - { - constexpr test_constexpr_ctor(in_place_t, std::initializer_list i) - : optional(in_place, i) {} - }; - - } +int main(int, char**) { + { + static_assert(!std::is_constructible&>::value, ""); + static_assert(!std::is_constructible, std::initializer_list&>::value, ""); + } + { + optional> opt(in_place, {3, 1}); + assert(static_cast(opt) == true); + assert((*opt == std::vector{3, 1})); + assert(opt->size() == 2); + } + { + optional> opt(in_place, {3, 1}, std::allocator()); + assert(static_cast(opt) == true); + assert((*opt == std::vector{3, 1})); + assert(opt->size() == 2); + } + { + static_assert(std::is_constructible, std::initializer_list&>::value, ""); + constexpr optional opt(in_place, {3, 1}); + static_assert(static_cast(opt) == true, ""); + static_assert(*opt == Y{3, 1}, ""); + + struct test_constexpr_ctor : public optional { + constexpr test_constexpr_ctor(in_place_t, std::initializer_list i) : optional(in_place, i) {} + }; + } #ifndef TEST_HAS_NO_EXCEPTIONS - { - static_assert(std::is_constructible, std::initializer_list&>::value, ""); - try - { - optional opt(in_place, {3, 1}); - assert(false); - } - catch (int i) - { - assert(i == 6); - } + { + static_assert(std::is_constructible, std::initializer_list&>::value, ""); + try { + optional opt(in_place, {3, 1}); + assert(false); + } catch (int i) { + assert(i == 6); } + } #endif return 0; diff --git a/libcxx/test/std/utilities/optional/optional.object/optional.object.ctor/move.pass.cpp b/libcxx/test/std/utilities/optional/optional.object/optional.object.ctor/move.pass.cpp index f59fc3b82ad7f..583debcaac650 100644 --- a/libcxx/test/std/utilities/optional/optional.object/optional.object.ctor/move.pass.cpp +++ b/libcxx/test/std/utilities/optional/optional.object/optional.object.ctor/move.pass.cpp @@ -12,70 +12,64 @@ // constexpr optional(optional&& rhs); +#include #include #include -#include #include "test_macros.h" #include "archetypes.h" using std::optional; -template -void test(InitArgs&&... args) -{ - const optional orig(std::forward(args)...); - optional rhs(orig); - bool rhs_engaged = static_cast(rhs); - optional lhs = std::move(rhs); - assert(static_cast(lhs) == rhs_engaged); - if (rhs_engaged) - assert(*lhs == *orig); +template +void test(InitArgs&&... args) { + const optional orig(std::forward(args)...); + optional rhs(orig); + bool rhs_engaged = static_cast(rhs); + optional lhs = std::move(rhs); + assert(static_cast(lhs) == rhs_engaged); + if (rhs_engaged) + assert(*lhs == *orig); } -template -constexpr bool constexpr_test(InitArgs&&... args) -{ - static_assert( std::is_trivially_copy_constructible_v, ""); // requirement - const optional orig(std::forward(args)...); - optional rhs(orig); - optional lhs = std::move(rhs); - return (lhs.has_value() == orig.has_value()) && - (lhs.has_value() ? *lhs == *orig : true); +template +constexpr bool constexpr_test(InitArgs&&... args) { + static_assert(std::is_trivially_copy_constructible_v, ""); // requirement + const optional orig(std::forward(args)...); + optional rhs(orig); + optional lhs = std::move(rhs); + return (lhs.has_value() == orig.has_value()) && (lhs.has_value() ? *lhs == *orig : true); } void test_throwing_ctor() { #ifndef TEST_HAS_NO_EXCEPTIONS - struct Z { - Z() : count(0) {} - Z(Z&& o) : count(o.count + 1) - { if (count == 2) throw 6; } - int count; - }; - Z z; - optional rhs(std::move(z)); - try - { - optional lhs(std::move(rhs)); - assert(false); - } - catch (int i) - { - assert(i == 6); + struct Z { + Z() : count(0) {} + Z(Z&& o) : count(o.count + 1) { + if (count == 2) + throw 6; } + int count; + }; + Z z; + optional rhs(std::move(z)); + try { + optional lhs(std::move(rhs)); + assert(false); + } catch (int i) { + assert(i == 6); + } #endif } - -template -void test_ref(InitArgs&&... args) -{ - optional rhs(std::forward(args)...); - bool rhs_engaged = static_cast(rhs); - optional lhs = std::move(rhs); - assert(static_cast(lhs) == rhs_engaged); - if (rhs_engaged) - assert(&(*lhs) == &(*rhs)); +template +void test_ref(InitArgs&&... args) { + optional rhs(std::forward(args)...); + bool rhs_engaged = static_cast(rhs); + optional lhs = std::move(rhs); + assert(static_cast(lhs) == rhs_engaged); + if (rhs_engaged) + assert(&(*lhs) == &(*rhs)); } void test_reference_extension() { @@ -143,80 +137,79 @@ void test_reference_extension() { #endif } -int main(int, char**) -{ - test(); - test(3); - static_assert(constexpr_test(), "" ); - static_assert(constexpr_test(3), "" ); +int main(int, char**) { + test(); + test(3); + static_assert(constexpr_test(), ""); + static_assert(constexpr_test(3), ""); - { - optional o(42); - optional o2(std::move(o)); - assert(*o2 == 42); - } - { - using T = TestTypes::TestType; - T::reset(); - optional rhs; - assert(T::alive == 0); - const optional lhs(std::move(rhs)); - assert(lhs.has_value() == false); - assert(rhs.has_value() == false); - assert(T::alive == 0); - } - TestTypes::TestType::reset(); - { - using T = TestTypes::TestType; - T::reset(); - optional rhs(42); - assert(T::alive == 1); - assert(T::value_constructed == 1); - assert(T::move_constructed == 0); - const optional lhs(std::move(rhs)); - assert(lhs.has_value()); - assert(rhs.has_value()); - assert(lhs.value().value == 42); - assert(rhs.value().value == -1); - assert(T::move_constructed == 1); - assert(T::alive == 2); - } - TestTypes::TestType::reset(); - { - using namespace ConstexprTestTypes; - test(); - test(42); - } - { - using namespace TrivialTestTypes; - test(); - test(42); - } - { - test_throwing_ctor(); - } - { - struct ThrowsMove { - ThrowsMove() noexcept(false) {} - ThrowsMove(ThrowsMove const&) noexcept(false) {} - ThrowsMove(ThrowsMove &&) noexcept(false) {} - }; - static_assert(!std::is_nothrow_move_constructible>::value, ""); - struct NoThrowMove { - NoThrowMove() noexcept(false) {} - NoThrowMove(NoThrowMove const&) noexcept(false) {} - NoThrowMove(NoThrowMove &&) noexcept(true) {} - }; - static_assert(std::is_nothrow_move_constructible>::value, ""); - } - { - test_reference_extension(); - } - { + { + optional o(42); + optional o2(std::move(o)); + assert(*o2 == 42); + } + { + using T = TestTypes::TestType; + T::reset(); + optional rhs; + assert(T::alive == 0); + const optional lhs(std::move(rhs)); + assert(lhs.has_value() == false); + assert(rhs.has_value() == false); + assert(T::alive == 0); + } + TestTypes::TestType::reset(); + { + using T = TestTypes::TestType; + T::reset(); + optional rhs(42); + assert(T::alive == 1); + assert(T::value_constructed == 1); + assert(T::move_constructed == 0); + const optional lhs(std::move(rhs)); + assert(lhs.has_value()); + assert(rhs.has_value()); + assert(lhs.value().value == 42); + assert(rhs.value().value == -1); + assert(T::move_constructed == 1); + assert(T::alive == 2); + } + TestTypes::TestType::reset(); + { + using namespace ConstexprTestTypes; + test(); + test(42); + } + { + using namespace TrivialTestTypes; + test(); + test(42); + } + { + test_throwing_ctor(); + } + { + struct ThrowsMove { + ThrowsMove() noexcept(false) {} + ThrowsMove(ThrowsMove const&) noexcept(false) {} + ThrowsMove(ThrowsMove&&) noexcept(false) {} + }; + static_assert(!std::is_nothrow_move_constructible>::value, ""); + struct NoThrowMove { + NoThrowMove() noexcept(false) {} + NoThrowMove(NoThrowMove const&) noexcept(false) {} + NoThrowMove(NoThrowMove&&) noexcept(true) {} + }; + static_assert(std::is_nothrow_move_constructible>::value, ""); + } + { + test_reference_extension(); + } + { constexpr std::optional o1{4}; constexpr std::optional o2 = std::move(o1); - static_assert( *o2 == 4, "" ); - } + static_assert(*o2 == 4, ""); + } return 0; } diff --git a/libcxx/test/std/utilities/optional/optional.object/optional.object.ctor/nullopt_t.pass.cpp b/libcxx/test/std/utilities/optional/optional.object/optional.object.ctor/nullopt_t.pass.cpp index 36a60f29da854..c1bdd81e5ed47 100644 --- a/libcxx/test/std/utilities/optional/optional.object/optional.object.ctor/nullopt_t.pass.cpp +++ b/libcxx/test/std/utilities/optional/optional.object/optional.object.ctor/nullopt_t.pass.cpp @@ -11,66 +11,57 @@ // constexpr optional(nullopt_t) noexcept; +#include #include #include -#include #include "archetypes.h" #include "test_macros.h" -using std::optional; -using std::nullopt_t; using std::nullopt; +using std::nullopt_t; +using std::optional; template -void -test_constexpr() -{ - static_assert(std::is_nothrow_constructible::value, ""); - static_assert(std::is_trivially_destructible::value, ""); - static_assert(std::is_trivially_destructible::value, ""); +void test_constexpr() { + static_assert(std::is_nothrow_constructible::value, ""); + static_assert(std::is_trivially_destructible::value, ""); + static_assert(std::is_trivially_destructible::value, ""); - constexpr Opt opt(nullopt); - static_assert(static_cast(opt) == false, ""); + constexpr Opt opt(nullopt); + static_assert(static_cast(opt) == false, ""); - struct test_constexpr_ctor - : public Opt - { - constexpr test_constexpr_ctor() {} - }; + struct test_constexpr_ctor : public Opt { + constexpr test_constexpr_ctor() {} + }; } template -void -test() -{ - static_assert(std::is_nothrow_constructible::value, ""); - static_assert(!std::is_trivially_destructible::value, ""); - static_assert(!std::is_trivially_destructible::value, ""); - { +void test() { + static_assert(std::is_nothrow_constructible::value, ""); + static_assert(!std::is_trivially_destructible::value, ""); + static_assert(!std::is_trivially_destructible::value, ""); + { Opt opt(nullopt); assert(static_cast(opt) == false); - } - { + } + { const Opt opt(nullopt); assert(static_cast(opt) == false); - } - struct test_constexpr_ctor - : public Opt - { - constexpr test_constexpr_ctor() {} - }; + } + struct test_constexpr_ctor : public Opt { + constexpr test_constexpr_ctor() {} + }; } -int main(int, char**) -{ - test_constexpr>(); - test_constexpr>(); - test_constexpr>(); - test_constexpr>(); - test_constexpr>(); - test>(); +int main(int, char**) { + test_constexpr>(); + test_constexpr>(); + test_constexpr>(); + test_constexpr>(); + test_constexpr>(); + test>(); return 0; } diff --git a/libcxx/test/std/utilities/optional/optional.object/optional.object.ctor/optional_U.pass.cpp b/libcxx/test/std/utilities/optional/optional.object/optional.object.ctor/optional_U.pass.cpp index 14c400cdd1526..709b106c800a6 100644 --- a/libcxx/test/std/utilities/optional/optional.object/optional.object.ctor/optional_U.pass.cpp +++ b/libcxx/test/std/utilities/optional/optional.object/optional.object.ctor/optional_U.pass.cpp @@ -23,75 +23,68 @@ using std::optional; template -TEST_CONSTEXPR_CXX20 void -test(optional&& rhs, bool is_going_to_throw = false) -{ - bool rhs_engaged = static_cast(rhs); +TEST_CONSTEXPR_CXX20 void test(optional&& rhs, bool is_going_to_throw = false) { + bool rhs_engaged = static_cast(rhs); #ifndef TEST_HAS_NO_EXCEPTIONS - try - { - optional lhs = std::move(rhs); - assert(is_going_to_throw == false); - assert(static_cast(lhs) == rhs_engaged); - } - catch (int i) - { - assert(i == 6); - } -#else - if (is_going_to_throw) return; + try { optional lhs = std::move(rhs); + assert(is_going_to_throw == false); assert(static_cast(lhs) == rhs_engaged); + } catch (int i) { + assert(i == 6); + } +#else + if (is_going_to_throw) + return; + optional lhs = std::move(rhs); + assert(static_cast(lhs) == rhs_engaged); #endif } -class X -{ - int i_; +class X { + int i_; + public: - TEST_CONSTEXPR_CXX20 X(int i) : i_(i) {} - TEST_CONSTEXPR_CXX20 X(X&& x) : i_(std::exchange(x.i_, 0)) {} - TEST_CONSTEXPR_CXX20 ~X() {i_ = 0;} - friend constexpr bool operator==(const X& x, const X& y) {return x.i_ == y.i_;} + TEST_CONSTEXPR_CXX20 X(int i) : i_(i) {} + TEST_CONSTEXPR_CXX20 X(X&& x) : i_(std::exchange(x.i_, 0)) {} + TEST_CONSTEXPR_CXX20 ~X() { i_ = 0; } + friend constexpr bool operator==(const X& x, const X& y) { return x.i_ == y.i_; } }; -struct Z -{ - Z(int) { TEST_THROW(6); } +struct Z { + Z(int) { TEST_THROW(6); } }; -template -TEST_CONSTEXPR_CXX20 bool test_all() -{ - { - optional rhs; - test(std::move(rhs)); - } - { - optional rhs(short{3}); - test(std::move(rhs)); - } - return true; +template +TEST_CONSTEXPR_CXX20 bool test_all() { + { + optional rhs; + test(std::move(rhs)); + } + { + optional rhs(short{3}); + test(std::move(rhs)); + } + return true; } -int main(int, char**) -{ - test_all(); - test_all(); +int main(int, char**) { + test_all(); + test_all(); #if TEST_STD_VER > 17 - static_assert(test_all()); - static_assert(test_all()); + static_assert(test_all()); + static_assert(test_all()); #endif - { - optional rhs; - test(std::move(rhs)); - } - { - optional rhs(3); - test(std::move(rhs), true); - } + { + optional rhs; + test(std::move(rhs)); + } + { + optional rhs(3); + test(std::move(rhs), true); + } - static_assert(!(std::is_constructible, optional>::value), ""); + static_assert(!(std::is_constructible, optional>::value), ""); return 0; } diff --git a/libcxx/test/std/utilities/optional/optional.object/optional.object.ctor/rvalue_T.pass.cpp b/libcxx/test/std/utilities/optional/optional.object/optional.object.ctor/rvalue_T.pass.cpp index 12425955f5a86..e73eef4592256 100644 --- a/libcxx/test/std/utilities/optional/optional.object/optional.object.ctor/rvalue_T.pass.cpp +++ b/libcxx/test/std/utilities/optional/optional.object/optional.object.ctor/rvalue_T.pass.cpp @@ -12,137 +12,118 @@ // constexpr optional(T&& v); +#include #include #include -#include #include "test_macros.h" #include "archetypes.h" - using std::optional; - -class Z -{ +class Z { public: - Z(int) {} - Z(Z&&) {TEST_THROW(6);} + Z(int) {} + Z(Z&&) { TEST_THROW(6); } }; - -int main(int, char**) -{ - { - typedef int T; - constexpr optional opt(T(5)); - static_assert(static_cast(opt) == true, ""); - static_assert(*opt == 5, ""); - - struct test_constexpr_ctor - : public optional - { - constexpr test_constexpr_ctor(T&&) {} - }; - } - { - typedef double T; - constexpr optional opt(T(3)); - static_assert(static_cast(opt) == true, ""); - static_assert(*opt == 3, ""); - - struct test_constexpr_ctor - : public optional - { - constexpr test_constexpr_ctor(T&&) {} - }; - } - { - const int x = 42; - optional o(std::move(x)); - assert(*o == 42); - } - { - typedef TestTypes::TestType T; - T::reset(); - optional opt = T{3}; - assert(T::alive == 1); - assert(T::move_constructed == 1); - assert(static_cast(opt) == true); - assert(opt.value().value == 3); - } - { - typedef ExplicitTestTypes::TestType T; - static_assert(!std::is_convertible>::value, ""); - T::reset(); - optional opt(T{3}); - assert(T::alive == 1); - assert(T::move_constructed == 1); - assert(static_cast(opt) == true); - assert(opt.value().value == 3); - } - { - typedef TestTypes::TestType T; - T::reset(); - optional opt = {3}; - assert(T::alive == 1); - assert(T::value_constructed == 1); - assert(T::copy_constructed == 0); - assert(T::move_constructed == 0); - assert(static_cast(opt) == true); - assert(opt.value().value == 3); - } - { - typedef ConstexprTestTypes::TestType T; - constexpr optional opt = {T(3)}; - static_assert(static_cast(opt) == true, ""); - static_assert(opt.value().value == 3, ""); - - struct test_constexpr_ctor - : public optional - { - constexpr test_constexpr_ctor(const T&) {} - }; - } - { - typedef ConstexprTestTypes::TestType T; - constexpr optional opt = {3}; - static_assert(static_cast(opt) == true, ""); - static_assert(opt.value().value == 3, ""); - - struct test_constexpr_ctor - : public optional - { - constexpr test_constexpr_ctor(const T&) {} - }; - } - { - typedef ExplicitConstexprTestTypes::TestType T; - static_assert(!std::is_convertible>::value, ""); - constexpr optional opt(T{3}); - static_assert(static_cast(opt) == true, ""); - static_assert(opt.value().value == 3, ""); - - struct test_constexpr_ctor - : public optional - { - constexpr test_constexpr_ctor(T&&) {} - }; - - } +int main(int, char**) { + { + typedef int T; + constexpr optional opt(T(5)); + static_assert(static_cast(opt) == true, ""); + static_assert(*opt == 5, ""); + + struct test_constexpr_ctor : public optional { + constexpr test_constexpr_ctor(T&&) {} + }; + } + { + typedef double T; + constexpr optional opt(T(3)); + static_assert(static_cast(opt) == true, ""); + static_assert(*opt == 3, ""); + + struct test_constexpr_ctor : public optional { + constexpr test_constexpr_ctor(T&&) {} + }; + } + { + const int x = 42; + optional o(std::move(x)); + assert(*o == 42); + } + { + typedef TestTypes::TestType T; + T::reset(); + optional opt = T{3}; + assert(T::alive == 1); + assert(T::move_constructed == 1); + assert(static_cast(opt) == true); + assert(opt.value().value == 3); + } + { + typedef ExplicitTestTypes::TestType T; + static_assert(!std::is_convertible>::value, ""); + T::reset(); + optional opt(T{3}); + assert(T::alive == 1); + assert(T::move_constructed == 1); + assert(static_cast(opt) == true); + assert(opt.value().value == 3); + } + { + typedef TestTypes::TestType T; + T::reset(); + optional opt = {3}; + assert(T::alive == 1); + assert(T::value_constructed == 1); + assert(T::copy_constructed == 0); + assert(T::move_constructed == 0); + assert(static_cast(opt) == true); + assert(opt.value().value == 3); + } + { + typedef ConstexprTestTypes::TestType T; + constexpr optional opt = {T(3)}; + static_assert(static_cast(opt) == true, ""); + static_assert(opt.value().value == 3, ""); + + struct test_constexpr_ctor : public optional { + constexpr test_constexpr_ctor(const T&) {} + }; + } + { + typedef ConstexprTestTypes::TestType T; + constexpr optional opt = {3}; + static_assert(static_cast(opt) == true, ""); + static_assert(opt.value().value == 3, ""); + + struct test_constexpr_ctor : public optional { + constexpr test_constexpr_ctor(const T&) {} + }; + } + { + typedef ExplicitConstexprTestTypes::TestType T; + static_assert(!std::is_convertible>::value, ""); + constexpr optional opt(T{3}); + static_assert(static_cast(opt) == true, ""); + static_assert(opt.value().value == 3, ""); + + struct test_constexpr_ctor : public optional { + constexpr test_constexpr_ctor(T&&) {} + }; + } #ifndef TEST_HAS_NO_EXCEPTIONS - { - try - { - Z z(3); - optional opt(std::move(z)); - assert(false); - } - catch (int i) - { - assert(i == 6); - } + { + try { + Z z(3); + optional opt(std::move(z)); + assert(false); + } catch (int i) { + assert(i == 6); } + } #endif return 0;