diff options
author | Dimitry Andric <dim@FreeBSD.org> | 2017-04-16 16:03:23 +0000 |
---|---|---|
committer | Dimitry Andric <dim@FreeBSD.org> | 2017-04-16 16:03:23 +0000 |
commit | 0dc0969cd0a732760f0aa79942a04e0eaef297c4 (patch) | |
tree | 051bdb57b1ac6ee143f61ddbb47bd0da619f6f0c /test/std/utilities/optional | |
parent | 868847c6900e575417c03bced6e562b3af891318 (diff) |
Notes
Diffstat (limited to 'test/std/utilities/optional')
21 files changed, 757 insertions, 426 deletions
diff --git a/test/std/utilities/optional/optional.comp_with_t/equal.pass.cpp b/test/std/utilities/optional/optional.comp_with_t/equal.pass.cpp index b54a08f5575ba..29fb7a8431f1a 100644 --- a/test/std/utilities/optional/optional.comp_with_t/equal.pass.cpp +++ b/test/std/utilities/optional/optional.comp_with_t/equal.pass.cpp @@ -17,37 +17,48 @@ using std::optional; -struct X -{ - int i_; +struct X { + int i_; - constexpr X(int i) : i_(i) {} + constexpr X(int i) : i_(i) {} }; -constexpr bool operator == ( const X &lhs, const X &rhs ) - { return lhs.i_ == rhs.i_ ; } +constexpr bool operator==(const X& lhs, const X& rhs) { + return lhs.i_ == rhs.i_; +} -int main() -{ - { +int main() { + { typedef X T; typedef optional<T> O; constexpr T val(2); - constexpr O o1; // disengaged - constexpr O o2{1}; // engaged - constexpr O o3{val}; // engaged - - static_assert ( !(o1 == T(1)), "" ); - static_assert ( (o2 == T(1)), "" ); - static_assert ( !(o3 == T(1)), "" ); - static_assert ( (o3 == T(2)), "" ); - static_assert ( (o3 == val), "" ); - - static_assert ( !(T(1) == o1), "" ); - static_assert ( (T(1) == o2), "" ); - static_assert ( !(T(1) == o3), "" ); - static_assert ( (T(2) == o3), "" ); - static_assert ( (val == o3), "" ); - } + constexpr O o1; // disengaged + constexpr O o2{1}; // engaged + constexpr O o3{val}; // engaged + + static_assert(!(o1 == T(1)), ""); + static_assert((o2 == T(1)), ""); + static_assert(!(o3 == T(1)), ""); + static_assert((o3 == T(2)), ""); + static_assert((o3 == val), ""); + + static_assert(!(T(1) == o1), ""); + static_assert((T(1) == o2), ""); + static_assert(!(T(1) == o3), ""); + static_assert((T(2) == o3), ""); + static_assert((val == o3), ""); + } + { + using O = optional<int>; + constexpr O o1(42); + static_assert(o1 == 42l, ""); + static_assert(!(101l == o1), ""); + } + { + using O = optional<const int>; + constexpr O o1(42); + static_assert(o1 == 42, ""); + static_assert(!(101 == o1), ""); + } } diff --git a/test/std/utilities/optional/optional.comp_with_t/greater.pass.cpp b/test/std/utilities/optional/optional.comp_with_t/greater.pass.cpp index 064114fb9db2b..ae34eb20517e7 100644 --- a/test/std/utilities/optional/optional.comp_with_t/greater.pass.cpp +++ b/test/std/utilities/optional/optional.comp_with_t/greater.pass.cpp @@ -17,39 +17,48 @@ using std::optional; -struct X -{ - int i_; +struct X { + int i_; - constexpr X(int i) : i_(i) {} + constexpr X(int i) : i_(i) {} }; -constexpr bool operator > ( const X &lhs, const X &rhs ) - { return lhs.i_ > rhs.i_ ; } +constexpr bool operator>(const X& lhs, const X& rhs) { return lhs.i_ > rhs.i_; } -int main() -{ - { +int main() { + { typedef X T; typedef optional<T> O; constexpr T val(2); - constexpr O o1; // disengaged - constexpr O o2{1}; // engaged - constexpr O o3{val}; // engaged - - static_assert ( !(o1 > T(1)), "" ); - static_assert ( !(o2 > T(1)), "" ); // equal - static_assert ( (o3 > T(1)), "" ); - static_assert ( !(o2 > val), "" ); - static_assert ( !(o3 > val), "" ); // equal - static_assert ( !(o3 > T(3)), "" ); - - static_assert ( (T(1) > o1), "" ); - static_assert ( !(T(1) > o2), "" ); // equal - static_assert ( !(T(1) > o3), "" ); - static_assert ( (val > o2), "" ); - static_assert ( !(val > o3), "" ); // equal - static_assert ( (T(3) > o3), "" ); - } + constexpr O o1; // disengaged + constexpr O o2{1}; // engaged + constexpr O o3{val}; // engaged + + static_assert(!(o1 > T(1)), ""); + static_assert(!(o2 > T(1)), ""); // equal + static_assert((o3 > T(1)), ""); + static_assert(!(o2 > val), ""); + static_assert(!(o3 > val), ""); // equal + static_assert(!(o3 > T(3)), ""); + + static_assert((T(1) > o1), ""); + static_assert(!(T(1) > o2), ""); // equal + static_assert(!(T(1) > o3), ""); + static_assert((val > o2), ""); + static_assert(!(val > o3), ""); // equal + static_assert((T(3) > o3), ""); + } + { + using O = optional<int>; + constexpr O o1(42); + static_assert(o1 > 11l, ""); + static_assert(!(42l > o1), ""); + } + { + using O = optional<const int>; + constexpr O o1(42); + static_assert(o1 > 11, ""); + static_assert(!(42 > o1), ""); + } } diff --git a/test/std/utilities/optional/optional.comp_with_t/greater_equal.pass.cpp b/test/std/utilities/optional/optional.comp_with_t/greater_equal.pass.cpp index 663686cdf3478..dac94002661f8 100644 --- a/test/std/utilities/optional/optional.comp_with_t/greater_equal.pass.cpp +++ b/test/std/utilities/optional/optional.comp_with_t/greater_equal.pass.cpp @@ -17,39 +17,50 @@ using std::optional; -struct X -{ - int i_; +struct X { + int i_; - constexpr X(int i) : i_(i) {} + constexpr X(int i) : i_(i) {} }; -constexpr bool operator >= ( const X &lhs, const X &rhs ) - { return lhs.i_ >= rhs.i_ ; } +constexpr bool operator>=(const X& lhs, const X& rhs) { + return lhs.i_ >= rhs.i_; +} -int main() -{ - { +int main() { + { typedef X T; typedef optional<T> O; constexpr T val(2); - constexpr O o1; // disengaged - constexpr O o2{1}; // engaged - constexpr O o3{val}; // engaged - - static_assert ( !(o1 >= T(1)), "" ); - static_assert ( (o2 >= T(1)), "" ); // equal - static_assert ( (o3 >= T(1)), "" ); - static_assert ( !(o2 >= val), "" ); - static_assert ( (o3 >= val), "" ); // equal - static_assert ( !(o3 >= T(3)), "" ); - - static_assert ( (T(1) >= o1), "" ); - static_assert ( (T(1) >= o2), "" ); // equal - static_assert ( !(T(1) >= o3), "" ); - static_assert ( (val >= o2), "" ); - static_assert ( (val >= o3), "" ); // equal - static_assert ( (T(3) >= o3), "" ); - } + constexpr O o1; // disengaged + constexpr O o2{1}; // engaged + constexpr O o3{val}; // engaged + + static_assert(!(o1 >= T(1)), ""); + static_assert((o2 >= T(1)), ""); // equal + static_assert((o3 >= T(1)), ""); + static_assert(!(o2 >= val), ""); + static_assert((o3 >= val), ""); // equal + static_assert(!(o3 >= T(3)), ""); + + static_assert((T(1) >= o1), ""); + static_assert((T(1) >= o2), ""); // equal + static_assert(!(T(1) >= o3), ""); + static_assert((val >= o2), ""); + static_assert((val >= o3), ""); // equal + static_assert((T(3) >= o3), ""); + } + { + using O = optional<int>; + constexpr O o1(42); + static_assert(o1 >= 42l, ""); + static_assert(!(11l >= o1), ""); + } + { + using O = optional<const int>; + constexpr O o1(42); + static_assert(o1 >= 42, ""); + static_assert(!(11 >= o1), ""); + } } diff --git a/test/std/utilities/optional/optional.comp_with_t/less_equal.pass.cpp b/test/std/utilities/optional/optional.comp_with_t/less_equal.pass.cpp index 05ac5eb12b48a..b71f8363b0a62 100644 --- a/test/std/utilities/optional/optional.comp_with_t/less_equal.pass.cpp +++ b/test/std/utilities/optional/optional.comp_with_t/less_equal.pass.cpp @@ -17,39 +17,50 @@ using std::optional; -struct X -{ - int i_; +struct X { + int i_; - constexpr X(int i) : i_(i) {} + constexpr X(int i) : i_(i) {} }; -constexpr bool operator <= ( const X &lhs, const X &rhs ) - { return lhs.i_ <= rhs.i_ ; } +constexpr bool operator<=(const X& lhs, const X& rhs) { + return lhs.i_ <= rhs.i_; +} -int main() -{ - { +int main() { + { typedef X T; typedef optional<T> O; constexpr T val(2); - constexpr O o1; // disengaged - constexpr O o2{1}; // engaged - constexpr O o3{val}; // engaged - - static_assert ( (o1 <= T(1)), "" ); - static_assert ( (o2 <= T(1)), "" ); // equal - static_assert ( !(o3 <= T(1)), "" ); - static_assert ( (o2 <= val), "" ); - static_assert ( (o3 <= val), "" ); // equal - static_assert ( (o3 <= T(3)), "" ); - - static_assert ( !(T(1) <= o1), "" ); - static_assert ( (T(1) <= o2), "" ); // equal - static_assert ( (T(1) <= o3), "" ); - static_assert ( !(val <= o2), "" ); - static_assert ( (val <= o3), "" ); // equal - static_assert ( !(T(3) <= o3), "" ); - } + constexpr O o1; // disengaged + constexpr O o2{1}; // engaged + constexpr O o3{val}; // engaged + + static_assert((o1 <= T(1)), ""); + static_assert((o2 <= T(1)), ""); // equal + static_assert(!(o3 <= T(1)), ""); + static_assert((o2 <= val), ""); + static_assert((o3 <= val), ""); // equal + static_assert((o3 <= T(3)), ""); + + static_assert(!(T(1) <= o1), ""); + static_assert((T(1) <= o2), ""); // equal + static_assert((T(1) <= o3), ""); + static_assert(!(val <= o2), ""); + static_assert((val <= o3), ""); // equal + static_assert(!(T(3) <= o3), ""); + } + { + using O = optional<int>; + constexpr O o1(42); + static_assert(o1 <= 42l, ""); + static_assert(!(101l <= o1), ""); + } + { + using O = optional<const int>; + constexpr O o1(42); + static_assert(o1 <= 42, ""); + static_assert(!(101 <= o1), ""); + } } diff --git a/test/std/utilities/optional/optional.comp_with_t/less_than.pass.cpp b/test/std/utilities/optional/optional.comp_with_t/less_than.pass.cpp index d1891a286d7b8..84456b3baa6ef 100644 --- a/test/std/utilities/optional/optional.comp_with_t/less_than.pass.cpp +++ b/test/std/utilities/optional/optional.comp_with_t/less_than.pass.cpp @@ -17,39 +17,48 @@ using std::optional; -struct X -{ - int i_; +struct X { + int i_; - constexpr X(int i) : i_(i) {} + constexpr X(int i) : i_(i) {} }; -constexpr bool operator < ( const X &lhs, const X &rhs ) - { return lhs.i_ < rhs.i_ ; } +constexpr bool operator<(const X& lhs, const X& rhs) { return lhs.i_ < rhs.i_; } -int main() -{ - { +int main() { + { typedef X T; typedef optional<T> O; constexpr T val(2); - constexpr O o1; // disengaged - constexpr O o2{1}; // engaged - constexpr O o3{val}; // engaged - - static_assert ( (o1 < T(1)), "" ); - static_assert ( !(o2 < T(1)), "" ); // equal - static_assert ( !(o3 < T(1)), "" ); - static_assert ( (o2 < val), "" ); - static_assert ( !(o3 < val), "" ); // equal - static_assert ( (o3 < T(3)), "" ); - - static_assert ( !(T(1) < o1), "" ); - static_assert ( !(T(1) < o2), "" ); // equal - static_assert ( (T(1) < o3), "" ); - static_assert ( !(val < o2), "" ); - static_assert ( !(val < o3), "" ); // equal - static_assert ( !(T(3) < o3), "" ); - } + constexpr O o1; // disengaged + constexpr O o2{1}; // engaged + constexpr O o3{val}; // engaged + + static_assert((o1 < T(1)), ""); + static_assert(!(o2 < T(1)), ""); // equal + static_assert(!(o3 < T(1)), ""); + static_assert((o2 < val), ""); + static_assert(!(o3 < val), ""); // equal + static_assert((o3 < T(3)), ""); + + static_assert(!(T(1) < o1), ""); + static_assert(!(T(1) < o2), ""); // equal + static_assert((T(1) < o3), ""); + static_assert(!(val < o2), ""); + static_assert(!(val < o3), ""); // equal + static_assert(!(T(3) < o3), ""); + } + { + using O = optional<int>; + constexpr O o1(42); + static_assert(o1 < 101l, ""); + static_assert(!(42l < o1), ""); + } + { + using O = optional<const int>; + constexpr O o1(42); + static_assert(o1 < 101, ""); + static_assert(!(42 < o1), ""); + } } diff --git a/test/std/utilities/optional/optional.comp_with_t/not_equal.pass.cpp b/test/std/utilities/optional/optional.comp_with_t/not_equal.pass.cpp index ae2ff808fb25a..a4ffdc25e7213 100644 --- a/test/std/utilities/optional/optional.comp_with_t/not_equal.pass.cpp +++ b/test/std/utilities/optional/optional.comp_with_t/not_equal.pass.cpp @@ -17,37 +17,48 @@ using std::optional; -struct X -{ - int i_; +struct X { + int i_; - constexpr X(int i) : i_(i) {} + constexpr X(int i) : i_(i) {} }; -constexpr bool operator != ( const X &lhs, const X &rhs ) - { return lhs.i_ != rhs.i_ ; } +constexpr bool operator!=(const X& lhs, const X& rhs) { + return lhs.i_ != rhs.i_; +} -int main() -{ - { +int main() { + { typedef X T; typedef optional<T> O; constexpr T val(2); - constexpr O o1; // disengaged - constexpr O o2{1}; // engaged - constexpr O o3{val}; // engaged - - static_assert ( (o1 != T(1)), "" ); - static_assert ( !(o2 != T(1)), "" ); - static_assert ( (o3 != T(1)), "" ); - static_assert ( !(o3 != T(2)), "" ); - static_assert ( !(o3 != val), "" ); - - static_assert ( (T(1) != o1), "" ); - static_assert ( !(T(1) != o2), "" ); - static_assert ( (T(1) != o3), "" ); - static_assert ( !(T(2) != o3), "" ); - static_assert ( !(val != o3), "" ); - } + constexpr O o1; // disengaged + constexpr O o2{1}; // engaged + constexpr O o3{val}; // engaged + + static_assert((o1 != T(1)), ""); + static_assert(!(o2 != T(1)), ""); + static_assert((o3 != T(1)), ""); + static_assert(!(o3 != T(2)), ""); + static_assert(!(o3 != val), ""); + + static_assert((T(1) != o1), ""); + static_assert(!(T(1) != o2), ""); + static_assert((T(1) != o3), ""); + static_assert(!(T(2) != o3), ""); + static_assert(!(val != o3), ""); + } + { + using O = optional<int>; + constexpr O o1(42); + static_assert(o1 != 101l, ""); + static_assert(!(42l != o1), ""); + } + { + using O = optional<const int>; + constexpr O o1(42); + static_assert(o1 != 101, ""); + static_assert(!(42 != o1), ""); + } } diff --git a/test/std/utilities/optional/optional.hash/enabled_hash.pass.cpp b/test/std/utilities/optional/optional.hash/enabled_hash.pass.cpp new file mode 100644 index 0000000000000..e54a4ced84118 --- /dev/null +++ b/test/std/utilities/optional/optional.hash/enabled_hash.pass.cpp @@ -0,0 +1,26 @@ +//===----------------------------------------------------------------------===// +// +// 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> + +// Test that <optional> provides all of the arithmetic, enum, and pointer +// hash specializations. + +#include <optional> + +#include "poisoned_hash_helper.hpp" + +int main() { + test_library_hash_specializations_available(); + { + + } +} diff --git a/test/std/utilities/optional/optional.hash/hash.pass.cpp b/test/std/utilities/optional/optional.hash/hash.pass.cpp index dfdd07ddf452e..8c91d6da761e2 100644 --- a/test/std/utilities/optional/optional.hash/hash.pass.cpp +++ b/test/std/utilities/optional/optional.hash/hash.pass.cpp @@ -17,6 +17,19 @@ #include <memory> #include <cassert> +#include "poisoned_hash_helper.hpp" + +struct A {}; +struct B {}; + +namespace std { + +template <> +struct hash<B> { + size_t operator()(B const&) noexcept(false) { return 0; } +}; + +} int main() { @@ -27,6 +40,7 @@ int main() { typedef int T; optional<T> opt; + ASSERT_NOT_NOEXCEPT(std::hash<optional<T>>()(opt)); assert(std::hash<optional<T>>{}(opt) == nullopt_hash); opt = 2; assert(std::hash<optional<T>>{}(opt) == std::hash<T>{}(*opt)); @@ -34,6 +48,7 @@ int main() { typedef std::string T; optional<T> opt; + ASSERT_NOT_NOEXCEPT(std::hash<optional<T>>()(opt)); assert(std::hash<optional<T>>{}(opt) == nullopt_hash); opt = std::string("123"); assert(std::hash<optional<T>>{}(opt) == std::hash<T>{}(*opt)); @@ -41,8 +56,21 @@ int main() { typedef std::unique_ptr<int> T; optional<T> opt; + ASSERT_NOT_NOEXCEPT(std::hash<optional<T>>()(opt)); assert(std::hash<optional<T>>{}(opt) == nullopt_hash); opt = std::unique_ptr<int>(new int(3)); assert(std::hash<optional<T>>{}(opt) == std::hash<T>{}(*opt)); } + { + test_hash_enabled_for_type<std::optional<int> >(); + test_hash_enabled_for_type<std::optional<int*> >(); + test_hash_enabled_for_type<std::optional<const int> >(); + test_hash_enabled_for_type<std::optional<int* const> >(); + + test_hash_disabled_for_type<std::optional<A>>(); + test_hash_disabled_for_type<std::optional<const A>>(); + + test_hash_enabled_for_type<std::optional<B>>(); + test_hash_enabled_for_type<std::optional<const B>>(); + } } diff --git a/test/std/utilities/optional/optional.nullopt/nullopt_t.fail.cpp b/test/std/utilities/optional/optional.nullopt/nullopt_t.fail.cpp new file mode 100644 index 0000000000000..f9ea7b47911fa --- /dev/null +++ b/test/std/utilities/optional/optional.nullopt/nullopt_t.fail.cpp @@ -0,0 +1,27 @@ +//===----------------------------------------------------------------------===// +// +// 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> + +// struct nullopt_t{see below}; +// constexpr nullopt_t nullopt(unspecified); + +// [optional.nullopt]/2: +// Type nullopt_t shall not have a default constructor or an initializer-list constructor. +// It shall not be an aggregate and shall be a literal type. +// Constant nullopt shall be initialized with an argument of literal type. + +#include <optional> +#include "test_macros.h" + +int main() +{ + std::nullopt_t n = {}; +} diff --git a/test/std/utilities/optional/optional.nullopt/nullopt_t.pass.cpp b/test/std/utilities/optional/optional.nullopt/nullopt_t.pass.cpp index 84bb29fabac8d..9b752a665a837 100644 --- a/test/std/utilities/optional/optional.nullopt/nullopt_t.pass.cpp +++ b/test/std/utilities/optional/optional.nullopt/nullopt_t.pass.cpp @@ -13,6 +13,11 @@ // struct nullopt_t{see below}; // constexpr nullopt_t nullopt(unspecified); +// [optional.nullopt]/2: +// Type nullopt_t shall not have a default constructor or an initializer-list constructor. +// It shall not be an aggregate and shall be a literal type. +// Constant nullopt shall be initialized with an argument of literal type. + #include <optional> #include <type_traits> @@ -29,9 +34,9 @@ test(const nullopt_t&) int main() { - static_assert((std::is_class<nullopt_t>::value), ""); - static_assert((std::is_empty<nullopt_t>::value), ""); - static_assert((std::is_literal_type<nullopt_t>::value), ""); + static_assert(( std::is_class<nullopt_t>::value), ""); + static_assert(( std::is_empty<nullopt_t>::value), ""); + static_assert(( std::is_literal_type<nullopt_t>::value), ""); static_assert((!std::is_default_constructible<nullopt_t>::value), ""); static_assert(test(nullopt) == 3, ""); diff --git a/test/std/utilities/optional/optional.object/optional.object.assign/assign_value.pass.cpp b/test/std/utilities/optional/optional.object/optional.object.assign/assign_value.pass.cpp index 1207e24150ff7..0fcc52be13546 100644 --- a/test/std/utilities/optional/optional.object/optional.object.assign/assign_value.pass.cpp +++ b/test/std/utilities/optional/optional.object/optional.object.assign/assign_value.pass.cpp @@ -216,7 +216,6 @@ void test_throws() #ifndef TEST_HAS_NO_EXCEPTIONS using T = ThrowAssign; { - using T = ThrowAssign; optional<T> opt; try { opt = 42; diff --git a/test/std/utilities/optional/optional.object/optional.object.assign/emplace.pass.cpp b/test/std/utilities/optional/optional.object/optional.object.assign/emplace.pass.cpp index b5362589963c2..0696c11526d59 100644 --- a/test/std/utilities/optional/optional.object/optional.object.assign/emplace.pass.cpp +++ b/test/std/utilities/optional/optional.object/optional.object.assign/emplace.pass.cpp @@ -10,7 +10,7 @@ // UNSUPPORTED: c++98, c++03, c++11, c++14 // <optional> -// template <class... Args> void optional<T>::emplace(Args&&... args); +// template <class... Args> T& optional<T>::emplace(Args&&... args); #include <optional> #include <type_traits> @@ -51,27 +51,35 @@ void test_one_arg() { using Opt = std::optional<T>; { Opt opt; - opt.emplace(); + auto & v = opt.emplace(); + static_assert( std::is_same_v<T&, decltype(v)>, "" ); assert(static_cast<bool>(opt) == true); assert(*opt == T(0)); + assert(&v == &*opt); } { Opt opt; - opt.emplace(1); + auto & v = opt.emplace(1); + static_assert( std::is_same_v<T&, decltype(v)>, "" ); assert(static_cast<bool>(opt) == true); assert(*opt == T(1)); + assert(&v == &*opt); } { Opt opt(2); - opt.emplace(); + auto & v = opt.emplace(); + static_assert( std::is_same_v<T&, decltype(v)>, "" ); assert(static_cast<bool>(opt) == true); assert(*opt == T(0)); + assert(&v == &*opt); } { Opt opt(2); - opt.emplace(1); + auto & v = opt.emplace(1); + static_assert( std::is_same_v<T&, decltype(v)>, "" ); assert(static_cast<bool>(opt) == true); assert(*opt == T(1)); + assert(&v == &*opt); } } @@ -83,20 +91,26 @@ void test_multi_arg() using Opt = std::optional<T>; { Opt opt; - opt.emplace(101, 41); + auto &v = opt.emplace(101, 41); + static_assert( std::is_same_v<T&, decltype(v)>, "" ); assert(static_cast<bool>(opt) == true); + assert( v == T(101, 41)); assert(*opt == T(101, 41)); } { Opt opt; - opt.emplace({1, 2, 3, 4}); + auto &v = opt.emplace({1, 2, 3, 4}); + static_assert( std::is_same_v<T&, decltype(v)>, "" ); assert(static_cast<bool>(opt) == true); - assert(*opt == T(4)); // T sets its value to the size of the init list + assert( v == T(4)); // T sets its value to the size of the init list + assert(*opt == T(4)); } { Opt opt; - opt.emplace({1, 2, 3, 4, 5}, 6); + auto &v = opt.emplace({1, 2, 3, 4, 5}, 6); + static_assert( std::is_same_v<T&, decltype(v)>, "" ); assert(static_cast<bool>(opt) == true); + assert( v == T(5)); // T sets its value to the size of the init list assert(*opt == T(5)); // T sets its value to the size of the init list } } @@ -109,73 +123,87 @@ void test_on_test_type() { assert(T::alive == 0); { T::reset_constructors(); - opt.emplace(); + auto &v = opt.emplace(); + static_assert( std::is_same_v<T&, decltype(v)>, "" ); assert(T::alive == 1); assert(T::constructed == 1); assert(T::default_constructed == 1); assert(T::destroyed == 0); assert(static_cast<bool>(opt) == true); assert(*opt == T()); + assert(&v == &*opt); } { T::reset_constructors(); - opt.emplace(); + auto &v = opt.emplace(); + static_assert( std::is_same_v<T&, decltype(v)>, "" ); assert(T::alive == 1); assert(T::constructed == 1); assert(T::default_constructed == 1); assert(T::destroyed == 1); assert(static_cast<bool>(opt) == true); assert(*opt == T()); + assert(&v == &*opt); } { T::reset_constructors(); - opt.emplace(101); + auto &v = opt.emplace(101); + static_assert( std::is_same_v<T&, decltype(v)>, "" ); assert(T::alive == 1); assert(T::constructed == 1); assert(T::value_constructed == 1); assert(T::destroyed == 1); assert(static_cast<bool>(opt) == true); assert(*opt == T(101)); + assert(&v == &*opt); } { T::reset_constructors(); - opt.emplace(-10, 99); + auto &v = opt.emplace(-10, 99); + static_assert( std::is_same_v<T&, decltype(v)>, "" ); assert(T::alive == 1); assert(T::constructed == 1); assert(T::value_constructed == 1); assert(T::destroyed == 1); assert(static_cast<bool>(opt) == true); assert(*opt == T(-10, 99)); + assert(&v == &*opt); } { T::reset_constructors(); - opt.emplace(-10, 99); + auto &v = opt.emplace(-10, 99); + static_assert( std::is_same_v<T&, decltype(v)>, "" ); assert(T::alive == 1); assert(T::constructed == 1); assert(T::value_constructed == 1); assert(T::destroyed == 1); assert(static_cast<bool>(opt) == true); assert(*opt == T(-10, 99)); + assert(&v == &*opt); } { T::reset_constructors(); - opt.emplace({-10, 99, 42, 1}); + auto &v = opt.emplace({-10, 99, 42, 1}); + static_assert( std::is_same_v<T&, decltype(v)>, "" ); assert(T::alive == 1); assert(T::constructed == 1); assert(T::value_constructed == 1); assert(T::destroyed == 1); assert(static_cast<bool>(opt) == true); assert(*opt == T(4)); // size of the initializer list + assert(&v == &*opt); } { T::reset_constructors(); - opt.emplace({-10, 99, 42, 1}, 42); + auto &v = opt.emplace({-10, 99, 42, 1}, 42); + static_assert( std::is_same_v<T&, decltype(v)>, "" ); assert(T::alive == 1); assert(T::constructed == 1); assert(T::value_constructed == 1); assert(T::destroyed == 1); assert(static_cast<bool>(opt) == true); assert(*opt == T(4)); // size of the initializer list + assert(&v == &*opt); } } @@ -210,8 +238,10 @@ int main() } { optional<const int> opt; - opt.emplace(42); + auto &v = opt.emplace(42); + static_assert( std::is_same_v<const int&, decltype(v)>, "" ); assert(*opt == 42); + assert( v == 42); opt.emplace(); assert(*opt == 0); } diff --git a/test/std/utilities/optional/optional.object/optional.object.ctor/U.pass.cpp b/test/std/utilities/optional/optional.object/optional.object.ctor/U.pass.cpp index c4d4763a6f0ae..d068fbc2db7e7 100644 --- a/test/std/utilities/optional/optional.object/optional.object.ctor/U.pass.cpp +++ b/test/std/utilities/optional/optional.object/optional.object.ctor/U.pass.cpp @@ -35,6 +35,11 @@ struct ExplicitThrow constexpr explicit ExplicitThrow(int x) { if (x != -1) TEST_THROW(6);} }; +struct ImplicitAny { + template <class U> + constexpr ImplicitAny(U&&) {} +}; + template <class To, class From> constexpr bool implicit_conversion(optional<To>&& opt, const From& v) @@ -61,11 +66,9 @@ constexpr bool explicit_conversion(Input&& in, const Expect& v) void test_implicit() { { - using T = long long; static_assert(implicit_conversion<long long>(42, 42), ""); } { - using T = long double; static_assert(implicit_conversion<long double>(3.14, 3.14), ""); } { @@ -81,6 +84,15 @@ void test_implicit() using T = TestTypes::TestType; assert(implicit_conversion<T>(3, T(3))); } + { + using O = optional<ImplicitAny>; + static_assert(!test_convertible<O, std::in_place_t>(), ""); + static_assert(!test_convertible<O, std::in_place_t&>(), ""); + static_assert(!test_convertible<O, const std::in_place_t&>(), ""); + static_assert(!test_convertible<O, std::in_place_t&&>(), ""); + static_assert(!test_convertible<O, const std::in_place_t&&>(), ""); + + } #ifndef TEST_HAS_NO_EXCEPTIONS { try { @@ -97,18 +109,15 @@ void test_implicit() void test_explicit() { { using T = ExplicitTrivialTestTypes::TestType; - using O = optional<T>; static_assert(explicit_conversion<T>(42, 42), ""); } { using T = ExplicitConstexprTestTypes::TestType; - using O = optional<T>; static_assert(explicit_conversion<T>(42, 42), ""); static_assert(!std::is_convertible<int, T>::value, ""); } { using T = ExplicitTestTypes::TestType; - using O = optional<T>; T::reset(); { assert(explicit_conversion<T>(42, 42)); diff --git a/test/std/utilities/optional/optional.object/optional.object.ctor/copy.fail.cpp b/test/std/utilities/optional/optional.object/optional.object.ctor/copy.fail.cpp new file mode 100644 index 0000000000000..d9e5ca821e85f --- /dev/null +++ b/test/std/utilities/optional/optional.object/optional.object.ctor/copy.fail.cpp @@ -0,0 +1,36 @@ +//===----------------------------------------------------------------------===// +// +// 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 optional(const optional<T>& rhs); +// If is_trivially_copy_constructible_v<T> is true, +// this constructor shall be a constexpr constructor. + +#include <optional> +#include <type_traits> +#include <cassert> + +#include "test_macros.h" + +struct S { + constexpr S() : v_(0) {} + S(int v) : v_(v) {} + S(const S &rhs) : v_(rhs.v_) {} // make it not trivially copyable + int v_; + }; + + +int main() +{ + static_assert (!std::is_trivially_copy_constructible_v<S>, "" ); + constexpr std::optional<S> o1; + constexpr std::optional<S> o2 = o1; // not constexpr +} diff --git a/test/std/utilities/optional/optional.object/optional.object.ctor/move.fail.cpp b/test/std/utilities/optional/optional.object/optional.object.ctor/move.fail.cpp new file mode 100644 index 0000000000000..c7458d95abfd3 --- /dev/null +++ b/test/std/utilities/optional/optional.object/optional.object.ctor/move.fail.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 optional(const optional<T>&& rhs); +// If is_trivially_move_constructible_v<T> is true, +// this constructor shall be a constexpr constructor. + +#include <optional> +#include <type_traits> +#include <cassert> + +#include "test_macros.h" + +struct S { + constexpr S() : v_(0) {} + S(int v) : v_(v) {} + constexpr S(const S &rhs) : v_(rhs.v_) {} // not trivially moveable + constexpr S(const S &&rhs) : v_(rhs.v_) {} // not trivially moveable + int v_; + }; + + +int main() +{ + static_assert (!std::is_trivially_move_constructible_v<S>, "" ); + constexpr std::optional<S> o1; + constexpr std::optional<S> o2 = std::move(o1); // not constexpr +} diff --git a/test/std/utilities/optional/optional.relops/equal.pass.cpp b/test/std/utilities/optional/optional.relops/equal.pass.cpp index 6650b6720a8e9..7667540f97601 100644 --- a/test/std/utilities/optional/optional.relops/equal.pass.cpp +++ b/test/std/utilities/optional/optional.relops/equal.pass.cpp @@ -18,57 +18,69 @@ using std::optional; -struct X -{ - int i_; +struct X { + int i_; - constexpr X(int i) : i_(i) {} + constexpr X(int i) : i_(i) {} }; -constexpr bool operator == ( const X &lhs, const X &rhs ) - { return lhs.i_ == rhs.i_ ; } +constexpr bool operator==(const X& lhs, const X& rhs) { + return lhs.i_ == rhs.i_; +} -int main() -{ - { +int main() { + { typedef X T; typedef optional<T> O; - constexpr O o1; // disengaged - constexpr O o2; // disengaged - constexpr O o3{1}; // engaged - constexpr O o4{2}; // engaged - constexpr O o5{1}; // engaged - - static_assert ( o1 == o1 , "" ); - static_assert ( o1 == o2 , "" ); - static_assert ( !(o1 == o3), "" ); - static_assert ( !(o1 == o4), "" ); - static_assert ( !(o1 == o5), "" ); + constexpr O o1; // disengaged + constexpr O o2; // disengaged + constexpr O o3{1}; // engaged + constexpr O o4{2}; // engaged + constexpr O o5{1}; // engaged - static_assert ( o2 == o1 , "" ); - static_assert ( o2 == o2 , "" ); - static_assert ( !(o2 == o3), "" ); - static_assert ( !(o2 == o4), "" ); - static_assert ( !(o2 == o5), "" ); + static_assert(o1 == o1, ""); + static_assert(o1 == o2, ""); + static_assert(!(o1 == o3), ""); + static_assert(!(o1 == o4), ""); + static_assert(!(o1 == o5), ""); - static_assert ( !(o3 == o1), "" ); - static_assert ( !(o3 == o2), "" ); - static_assert ( o3 == o3 , "" ); - static_assert ( !(o3 == o4), "" ); - static_assert ( o3 == o5 , "" ); + static_assert(o2 == o1, ""); + static_assert(o2 == o2, ""); + static_assert(!(o2 == o3), ""); + static_assert(!(o2 == o4), ""); + static_assert(!(o2 == o5), ""); - static_assert ( !(o4 == o1), "" ); - static_assert ( !(o4 == o2), "" ); - static_assert ( !(o4 == o3), "" ); - static_assert ( o4 == o4 , "" ); - static_assert ( !(o4 == o5), "" ); + static_assert(!(o3 == o1), ""); + static_assert(!(o3 == o2), ""); + static_assert(o3 == o3, ""); + static_assert(!(o3 == o4), ""); + static_assert(o3 == o5, ""); - static_assert ( !(o5 == o1), "" ); - static_assert ( !(o5 == o2), "" ); - static_assert ( o5 == o3 , "" ); - static_assert ( !(o5 == o4), "" ); - static_assert ( o5 == o5 , "" ); + static_assert(!(o4 == o1), ""); + static_assert(!(o4 == o2), ""); + static_assert(!(o4 == o3), ""); + static_assert(o4 == o4, ""); + static_assert(!(o4 == o5), ""); - } + static_assert(!(o5 == o1), ""); + static_assert(!(o5 == o2), ""); + static_assert(o5 == o3, ""); + static_assert(!(o5 == o4), ""); + static_assert(o5 == o5, ""); + } + { + using O1 = optional<int>; + using O2 = optional<long>; + constexpr O1 o1(42); + static_assert(o1 == O2(42), ""); + static_assert(!(O2(101) == o1), ""); + } + { + using O1 = optional<int>; + using O2 = optional<const int>; + constexpr O1 o1(42); + static_assert(o1 == O2(42), ""); + static_assert(!(O2(101) == o1), ""); + } } diff --git a/test/std/utilities/optional/optional.relops/greater_equal.pass.cpp b/test/std/utilities/optional/optional.relops/greater_equal.pass.cpp index f9b30449638aa..0e05834c147a5 100644 --- a/test/std/utilities/optional/optional.relops/greater_equal.pass.cpp +++ b/test/std/utilities/optional/optional.relops/greater_equal.pass.cpp @@ -16,55 +16,68 @@ using std::optional; -struct X -{ - int i_; +struct X { + int i_; - constexpr X(int i) : i_(i) {} + constexpr X(int i) : i_(i) {} }; -constexpr bool operator >= ( const X &lhs, const X &rhs ) - { return lhs.i_ >= rhs.i_ ; } +constexpr bool operator>=(const X& lhs, const X& rhs) { + return lhs.i_ >= rhs.i_; +} -int main() -{ - { +int main() { + { typedef optional<X> O; - constexpr O o1; // disengaged - constexpr O o2; // disengaged - constexpr O o3{1}; // engaged - constexpr O o4{2}; // engaged - constexpr O o5{1}; // engaged + constexpr O o1; // disengaged + constexpr O o2; // disengaged + constexpr O o3{1}; // engaged + constexpr O o4{2}; // engaged + constexpr O o5{1}; // engaged - static_assert ( (o1 >= o1), "" ); - static_assert ( (o1 >= o2), "" ); - static_assert ( !(o1 >= o3), "" ); - static_assert ( !(o1 >= o4), "" ); - static_assert ( !(o1 >= o5), "" ); + static_assert((o1 >= o1), ""); + static_assert((o1 >= o2), ""); + static_assert(!(o1 >= o3), ""); + static_assert(!(o1 >= o4), ""); + static_assert(!(o1 >= o5), ""); - static_assert ( (o2 >= o1), "" ); - static_assert ( (o2 >= o2), "" ); - static_assert ( !(o2 >= o3), "" ); - static_assert ( !(o2 >= o4), "" ); - static_assert ( !(o2 >= o5), "" ); + static_assert((o2 >= o1), ""); + static_assert((o2 >= o2), ""); + static_assert(!(o2 >= o3), ""); + static_assert(!(o2 >= o4), ""); + static_assert(!(o2 >= o5), ""); - static_assert ( (o3 >= o1), "" ); - static_assert ( (o3 >= o2), "" ); - static_assert ( (o3 >= o3), "" ); - static_assert ( !(o3 >= o4), "" ); - static_assert ( (o3 >= o5), "" ); + static_assert((o3 >= o1), ""); + static_assert((o3 >= o2), ""); + static_assert((o3 >= o3), ""); + static_assert(!(o3 >= o4), ""); + static_assert((o3 >= o5), ""); - static_assert ( (o4 >= o1), "" ); - static_assert ( (o4 >= o2), "" ); - static_assert ( (o4 >= o3), "" ); - static_assert ( (o4 >= o4), "" ); - static_assert ( (o4 >= o5), "" ); + static_assert((o4 >= o1), ""); + static_assert((o4 >= o2), ""); + static_assert((o4 >= o3), ""); + static_assert((o4 >= o4), ""); + static_assert((o4 >= o5), ""); - static_assert ( (o5 >= o1), "" ); - static_assert ( (o5 >= o2), "" ); - static_assert ( (o5 >= o3), "" ); - static_assert ( !(o5 >= o4), "" ); - static_assert ( (o5 >= o5), "" ); - } + static_assert((o5 >= o1), ""); + static_assert((o5 >= o2), ""); + static_assert((o5 >= o3), ""); + static_assert(!(o5 >= o4), ""); + static_assert((o5 >= o5), ""); + } + { + using O1 = optional<int>; + using O2 = optional<long>; + constexpr O1 o1(42); + static_assert(o1 >= O2(42), ""); + static_assert(!(O2(11) >= o1), ""); + } + { + using O1 = optional<int>; + using O2 = optional<const int>; + constexpr O1 o1(42); + static_assert(o1 >= O2(42), ""); + static_assert(!(O2(1) >= o1), ""); + } } diff --git a/test/std/utilities/optional/optional.relops/greater_than.pass.cpp b/test/std/utilities/optional/optional.relops/greater_than.pass.cpp index 8a27eb471f283..3946a6102ba9d 100644 --- a/test/std/utilities/optional/optional.relops/greater_than.pass.cpp +++ b/test/std/utilities/optional/optional.relops/greater_than.pass.cpp @@ -16,55 +16,66 @@ using std::optional; -struct X -{ - int i_; +struct X { + int i_; - constexpr X(int i) : i_(i) {} + constexpr X(int i) : i_(i) {} }; -constexpr bool operator > ( const X &lhs, const X &rhs ) - { return lhs.i_ > rhs.i_ ; } +constexpr bool operator>(const X& lhs, const X& rhs) { return lhs.i_ > rhs.i_; } -int main() -{ - { +int main() { + { typedef optional<X> O; - constexpr O o1; // disengaged - constexpr O o2; // disengaged - constexpr O o3{1}; // engaged - constexpr O o4{2}; // engaged - constexpr O o5{1}; // engaged + constexpr O o1; // disengaged + constexpr O o2; // disengaged + constexpr O o3{1}; // engaged + constexpr O o4{2}; // engaged + constexpr O o5{1}; // engaged - static_assert ( !(o1 > o1), "" ); - static_assert ( !(o1 > o2), "" ); - static_assert ( !(o1 > o3), "" ); - static_assert ( !(o1 > o4), "" ); - static_assert ( !(o1 > o5), "" ); + static_assert(!(o1 > o1), ""); + static_assert(!(o1 > o2), ""); + static_assert(!(o1 > o3), ""); + static_assert(!(o1 > o4), ""); + static_assert(!(o1 > o5), ""); - static_assert ( !(o2 > o1), "" ); - static_assert ( !(o2 > o2), "" ); - static_assert ( !(o2 > o3), "" ); - static_assert ( !(o2 > o4), "" ); - static_assert ( !(o2 > o5), "" ); + static_assert(!(o2 > o1), ""); + static_assert(!(o2 > o2), ""); + static_assert(!(o2 > o3), ""); + static_assert(!(o2 > o4), ""); + static_assert(!(o2 > o5), ""); - static_assert ( (o3 > o1), "" ); - static_assert ( (o3 > o2), "" ); - static_assert ( !(o3 > o3), "" ); - static_assert ( !(o3 > o4), "" ); - static_assert ( !(o3 > o5), "" ); + static_assert((o3 > o1), ""); + static_assert((o3 > o2), ""); + static_assert(!(o3 > o3), ""); + static_assert(!(o3 > o4), ""); + static_assert(!(o3 > o5), ""); - static_assert ( (o4 > o1), "" ); - static_assert ( (o4 > o2), "" ); - static_assert ( (o4 > o3), "" ); - static_assert ( !(o4 > o4), "" ); - static_assert ( (o4 > o5), "" ); + static_assert((o4 > o1), ""); + static_assert((o4 > o2), ""); + static_assert((o4 > o3), ""); + static_assert(!(o4 > o4), ""); + static_assert((o4 > o5), ""); - static_assert ( (o5 > o1), "" ); - static_assert ( (o5 > o2), "" ); - static_assert ( !(o5 > o3), "" ); - static_assert ( !(o5 > o4), "" ); - static_assert ( !(o5 > o5), "" ); - } + static_assert((o5 > o1), ""); + static_assert((o5 > o2), ""); + static_assert(!(o5 > o3), ""); + static_assert(!(o5 > o4), ""); + static_assert(!(o5 > o5), ""); + } + { + using O1 = optional<int>; + using O2 = optional<long>; + constexpr O1 o1(42); + static_assert(o1 > O2(1), ""); + static_assert(!(O2(42) > o1), ""); + } + { + using O1 = optional<int>; + using O2 = optional<const int>; + constexpr O1 o1(42); + static_assert(o1 > O2(1), ""); + static_assert(!(O2(42) > o1), ""); + } } diff --git a/test/std/utilities/optional/optional.relops/less_equal.pass.cpp b/test/std/utilities/optional/optional.relops/less_equal.pass.cpp index a7d594dd34a2d..5a1954154f442 100644 --- a/test/std/utilities/optional/optional.relops/less_equal.pass.cpp +++ b/test/std/utilities/optional/optional.relops/less_equal.pass.cpp @@ -16,55 +16,68 @@ using std::optional; -struct X -{ - int i_; +struct X { + int i_; - constexpr X(int i) : i_(i) {} + constexpr X(int i) : i_(i) {} }; -constexpr bool operator <= ( const X &lhs, const X &rhs ) - { return lhs.i_ <= rhs.i_ ; } +constexpr bool operator<=(const X& lhs, const X& rhs) { + return lhs.i_ <= rhs.i_; +} -int main() -{ - { +int main() { + { typedef optional<X> O; - constexpr O o1; // disengaged - constexpr O o2; // disengaged - constexpr O o3{1}; // engaged - constexpr O o4{2}; // engaged - constexpr O o5{1}; // engaged + constexpr O o1; // disengaged + constexpr O o2; // disengaged + constexpr O o3{1}; // engaged + constexpr O o4{2}; // engaged + constexpr O o5{1}; // engaged - static_assert ( (o1 <= o1), "" ); - static_assert ( (o1 <= o2), "" ); - static_assert ( (o1 <= o3), "" ); - static_assert ( (o1 <= o4), "" ); - static_assert ( (o1 <= o5), "" ); + static_assert((o1 <= o1), ""); + static_assert((o1 <= o2), ""); + static_assert((o1 <= o3), ""); + static_assert((o1 <= o4), ""); + static_assert((o1 <= o5), ""); - static_assert ( (o2 <= o1), "" ); - static_assert ( (o2 <= o2), "" ); - static_assert ( (o2 <= o3), "" ); - static_assert ( (o2 <= o4), "" ); - static_assert ( (o2 <= o5), "" ); + static_assert((o2 <= o1), ""); + static_assert((o2 <= o2), ""); + static_assert((o2 <= o3), ""); + static_assert((o2 <= o4), ""); + static_assert((o2 <= o5), ""); - static_assert ( !(o3 <= o1), "" ); - static_assert ( !(o3 <= o2), "" ); - static_assert ( (o3 <= o3), "" ); - static_assert ( (o3 <= o4), "" ); - static_assert ( (o3 <= o5), "" ); + static_assert(!(o3 <= o1), ""); + static_assert(!(o3 <= o2), ""); + static_assert((o3 <= o3), ""); + static_assert((o3 <= o4), ""); + static_assert((o3 <= o5), ""); - static_assert ( !(o4 <= o1), "" ); - static_assert ( !(o4 <= o2), "" ); - static_assert ( !(o4 <= o3), "" ); - static_assert ( (o4 <= o4), "" ); - static_assert ( !(o4 <= o5), "" ); + static_assert(!(o4 <= o1), ""); + static_assert(!(o4 <= o2), ""); + static_assert(!(o4 <= o3), ""); + static_assert((o4 <= o4), ""); + static_assert(!(o4 <= o5), ""); - static_assert ( !(o5 <= o1), "" ); - static_assert ( !(o5 <= o2), "" ); - static_assert ( (o5 <= o3), "" ); - static_assert ( (o5 <= o4), "" ); - static_assert ( (o5 <= o5), "" ); - } + static_assert(!(o5 <= o1), ""); + static_assert(!(o5 <= o2), ""); + static_assert((o5 <= o3), ""); + static_assert((o5 <= o4), ""); + static_assert((o5 <= o5), ""); + } + { + using O1 = optional<int>; + using O2 = optional<long>; + constexpr O1 o1(42); + static_assert(o1 <= O2(42), ""); + static_assert(!(O2(101) <= o1), ""); + } + { + using O1 = optional<int>; + using O2 = optional<const int>; + constexpr O1 o1(42); + static_assert(o1 <= O2(42), ""); + static_assert(!(O2(101) <= o1), ""); + } } diff --git a/test/std/utilities/optional/optional.relops/less_than.pass.cpp b/test/std/utilities/optional/optional.relops/less_than.pass.cpp index deffa5e849f9d..35956e6f4a70e 100644 --- a/test/std/utilities/optional/optional.relops/less_than.pass.cpp +++ b/test/std/utilities/optional/optional.relops/less_than.pass.cpp @@ -16,55 +16,66 @@ using std::optional; -struct X -{ - int i_; +struct X { + int i_; - constexpr X(int i) : i_(i) {} + constexpr X(int i) : i_(i) {} }; -constexpr bool operator < ( const X &lhs, const X &rhs ) - { return lhs.i_ < rhs.i_ ; } +constexpr bool operator<(const X& lhs, const X& rhs) { return lhs.i_ < rhs.i_; } -int main() -{ - { +int main() { + { typedef optional<X> O; - constexpr O o1; // disengaged - constexpr O o2; // disengaged - constexpr O o3{1}; // engaged - constexpr O o4{2}; // engaged - constexpr O o5{1}; // engaged + constexpr O o1; // disengaged + constexpr O o2; // disengaged + constexpr O o3{1}; // engaged + constexpr O o4{2}; // engaged + constexpr O o5{1}; // engaged - static_assert ( !(o1 < o1), "" ); - static_assert ( !(o1 < o2), "" ); - static_assert ( (o1 < o3), "" ); - static_assert ( (o1 < o4), "" ); - static_assert ( (o1 < o5), "" ); + static_assert(!(o1 < o1), ""); + static_assert(!(o1 < o2), ""); + static_assert((o1 < o3), ""); + static_assert((o1 < o4), ""); + static_assert((o1 < o5), ""); - static_assert ( !(o2 < o1), "" ); - static_assert ( !(o2 < o2), "" ); - static_assert ( (o2 < o3), "" ); - static_assert ( (o2 < o4), "" ); - static_assert ( (o2 < o5), "" ); + static_assert(!(o2 < o1), ""); + static_assert(!(o2 < o2), ""); + static_assert((o2 < o3), ""); + static_assert((o2 < o4), ""); + static_assert((o2 < o5), ""); - static_assert ( !(o3 < o1), "" ); - static_assert ( !(o3 < o2), "" ); - static_assert ( !(o3 < o3), "" ); - static_assert ( (o3 < o4), "" ); - static_assert ( !(o3 < o5), "" ); + static_assert(!(o3 < o1), ""); + static_assert(!(o3 < o2), ""); + static_assert(!(o3 < o3), ""); + static_assert((o3 < o4), ""); + static_assert(!(o3 < o5), ""); - static_assert ( !(o4 < o1), "" ); - static_assert ( !(o4 < o2), "" ); - static_assert ( !(o4 < o3), "" ); - static_assert ( !(o4 < o4), "" ); - static_assert ( !(o4 < o5), "" ); + static_assert(!(o4 < o1), ""); + static_assert(!(o4 < o2), ""); + static_assert(!(o4 < o3), ""); + static_assert(!(o4 < o4), ""); + static_assert(!(o4 < o5), ""); - static_assert ( !(o5 < o1), "" ); - static_assert ( !(o5 < o2), "" ); - static_assert ( !(o5 < o3), "" ); - static_assert ( (o5 < o4), "" ); - static_assert ( !(o5 < o5), "" ); - } + static_assert(!(o5 < o1), ""); + static_assert(!(o5 < o2), ""); + static_assert(!(o5 < o3), ""); + static_assert((o5 < o4), ""); + static_assert(!(o5 < o5), ""); + } + { + using O1 = optional<int>; + using O2 = optional<long>; + constexpr O1 o1(42); + static_assert(o1 < O2(101), ""); + static_assert(!(O2(101) < o1), ""); + } + { + using O1 = optional<int>; + using O2 = optional<const int>; + constexpr O1 o1(42); + static_assert(o1 < O2(101), ""); + static_assert(!(O2(101) < o1), ""); + } } diff --git a/test/std/utilities/optional/optional.relops/not_equal.pass.cpp b/test/std/utilities/optional/optional.relops/not_equal.pass.cpp index fd11b2a207ca8..1256537d6a867 100644 --- a/test/std/utilities/optional/optional.relops/not_equal.pass.cpp +++ b/test/std/utilities/optional/optional.relops/not_equal.pass.cpp @@ -18,57 +18,69 @@ using std::optional; -struct X -{ - int i_; +struct X { + int i_; - constexpr X(int i) : i_(i) {} + constexpr X(int i) : i_(i) {} }; -constexpr bool operator != ( const X &lhs, const X &rhs ) - { return lhs.i_ != rhs.i_ ; } +constexpr bool operator!=(const X& lhs, const X& rhs) { + return lhs.i_ != rhs.i_; +} -int main() -{ - { +int main() { + { typedef X T; typedef optional<T> O; - constexpr O o1; // disengaged - constexpr O o2; // disengaged - constexpr O o3{1}; // engaged - constexpr O o4{2}; // engaged - constexpr O o5{1}; // engaged - - static_assert ( !(o1 != o1), "" ); - static_assert ( !(o1 != o2), "" ); - static_assert ( (o1 != o3), "" ); - static_assert ( (o1 != o4), "" ); - static_assert ( (o1 != o5), "" ); + constexpr O o1; // disengaged + constexpr O o2; // disengaged + constexpr O o3{1}; // engaged + constexpr O o4{2}; // engaged + constexpr O o5{1}; // engaged - static_assert ( !(o2 != o1), "" ); - static_assert ( !(o2 != o2), "" ); - static_assert ( (o2 != o3), "" ); - static_assert ( (o2 != o4), "" ); - static_assert ( (o2 != o5), "" ); + static_assert(!(o1 != o1), ""); + static_assert(!(o1 != o2), ""); + static_assert((o1 != o3), ""); + static_assert((o1 != o4), ""); + static_assert((o1 != o5), ""); - static_assert ( (o3 != o1), "" ); - static_assert ( (o3 != o2), "" ); - static_assert ( !(o3 != o3), "" ); - static_assert ( (o3 != o4), "" ); - static_assert ( !(o3 != o5), "" ); + static_assert(!(o2 != o1), ""); + static_assert(!(o2 != o2), ""); + static_assert((o2 != o3), ""); + static_assert((o2 != o4), ""); + static_assert((o2 != o5), ""); - static_assert ( (o4 != o1), "" ); - static_assert ( (o4 != o2), "" ); - static_assert ( (o4 != o3), "" ); - static_assert ( !(o4 != o4), "" ); - static_assert ( (o4 != o5), "" ); + static_assert((o3 != o1), ""); + static_assert((o3 != o2), ""); + static_assert(!(o3 != o3), ""); + static_assert((o3 != o4), ""); + static_assert(!(o3 != o5), ""); - static_assert ( (o5 != o1), "" ); - static_assert ( (o5 != o2), "" ); - static_assert ( !(o5 != o3), "" ); - static_assert ( (o5 != o4), "" ); - static_assert ( !(o5 != o5), "" ); + static_assert((o4 != o1), ""); + static_assert((o4 != o2), ""); + static_assert((o4 != o3), ""); + static_assert(!(o4 != o4), ""); + static_assert((o4 != o5), ""); - } + static_assert((o5 != o1), ""); + static_assert((o5 != o2), ""); + static_assert(!(o5 != o3), ""); + static_assert((o5 != o4), ""); + static_assert(!(o5 != o5), ""); + } + { + using O1 = optional<int>; + using O2 = optional<long>; + constexpr O1 o1(42); + static_assert(o1 != O2(101), ""); + static_assert(!(O2(42) != o1), ""); + } + { + using O1 = optional<int>; + using O2 = optional<const int>; + constexpr O1 o1(42); + static_assert(o1 != O2(101), ""); + static_assert(!(O2(42) != o1), ""); + } } |