diff --git a/libcxx/test/libcxx/compare/synthesized.pass.cpp b/libcxx/test/libcxx/compare/synthesized.pass.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/libcxx/compare/synthesized.pass.cpp @@ -0,0 +1,62 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17 + +// template auto __synth_three_way(T __t, U __u); + +#include <__compare/__synthesized.h> +#include +#include +#include + +#include "test_macros.h" + +int main(int, char**) { +#if TEST_STD_VER >= 20 + static_assert(std::__synth_three_way(1, 2) == std::strong_ordering::less); + static_assert(std::is_same_v >, ""); + + struct NoSpaceship { + int value; + constexpr bool operator==(const NoSpaceship&) const = default; + constexpr bool operator<(const NoSpaceship& other) const { + return value < other.value; + }; + }; + static_assert(NoSpaceship{1} < NoSpaceship{2}, ""); + static_assert(!(NoSpaceship{1} < NoSpaceship{1}), ""); + static_assert(std::__synth_three_way(NoSpaceship{1}, NoSpaceship{2}) == std::weak_ordering::less); + static_assert(std::is_same_v >, ""); + + struct WithSpaceship { + int value; + constexpr bool operator==(const WithSpaceship&) const = default; + constexpr std::strong_ordering operator<=>(const WithSpaceship& other) const { + return value <=> other.value; + }; + }; + static_assert(WithSpaceship{1} < WithSpaceship{2}, ""); + static_assert(!(WithSpaceship{1} < WithSpaceship{1}), ""); + static_assert(std::__synth_three_way(WithSpaceship{1}, WithSpaceship{2}) == std::strong_ordering::less, ""); + static_assert(std::is_same_v >, ""); + + struct WithPartialSpaceship { + int value; + constexpr bool operator==(const WithPartialSpaceship&) const = default; + constexpr std::partial_ordering operator<=>(const WithPartialSpaceship& other) const { + return value <=> other.value; + } + }; + static_assert(WithPartialSpaceship{1} < WithPartialSpaceship{2}, ""); + static_assert(!(WithPartialSpaceship{1} < WithPartialSpaceship{1}), ""); + static_assert(std::__synth_three_way(WithPartialSpaceship{1}, WithPartialSpaceship{2}) == std::partial_ordering::less, ""); + static_assert(std::is_same_v >, ""); +#endif + return 0; +} diff --git a/libcxx/test/libcxx/utilities/utility/pairs/pairs.pair/comparison.pass.cpp b/libcxx/test/libcxx/utilities/utility/pairs/pairs.pair/comparison.pass.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/libcxx/utilities/utility/pairs/pairs.pair/comparison.pass.cpp @@ -0,0 +1,57 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17 + +#include +#include +#include + +#include "test_macros.h" + +int main(int, char**) { +#if TEST_STD_VER >= 20 + { + // Pairs of types that both have strong ordering should compare with strong ordering. + static_assert((std::make_pair(1, 1) <=> std::make_pair(1, 2)) < 0, ""); + static_assert((std::make_pair(2, 1) <=> std::make_pair(1, 2)) > 0, ""); + auto same = std::make_pair(0, 0) <=> std::make_pair(0, 0); + assert(same == 0); + static_assert(std::is_same_v); + } + { + // Pairs of int and a type with no spaceship operator should compare with weak ordering. + struct NoSpaceship { + int value; + constexpr bool operator==(const NoSpaceship&) const = default; + constexpr bool operator<(const NoSpaceship& other) const { return value < other.value; } + }; + static_assert((std::make_pair(1, NoSpaceship{1}) <=> std::make_pair(1, NoSpaceship{2})) < 0, ""); + static_assert((std::make_pair(2, NoSpaceship{1}) <=> std::make_pair(1, NoSpaceship{2})) > 0, ""); + auto same = std::make_pair(0, NoSpaceship{0}) <=> std::make_pair(0, NoSpaceship{0}); + assert(same == 0); + static_assert(std::is_same_v, ""); + } + { + // Pairs of int and a type with partial_ordering three way comparison should compare with partial ordering. + struct WithPartialSpaceship { + int value; + constexpr bool operator==(const WithPartialSpaceship&) const = default; + constexpr std::partial_ordering operator<=>(const WithPartialSpaceship& other) const { + return value <=> other.value; + } + }; + static_assert((std::make_pair(1, WithPartialSpaceship{1}) <=> std::make_pair(1, WithPartialSpaceship{2})) < 0, ""); + static_assert((std::make_pair(2, WithPartialSpaceship{1}) <=> std::make_pair(1, WithPartialSpaceship{2})) > 0, ""); + auto same = std::make_pair(0, WithPartialSpaceship{0}) <=> std::make_pair(0, WithPartialSpaceship{0}); + assert(same == 0); + static_assert(std::is_same_v, ""); + } +#endif + return 0; +}