#include <compare>
#include <cassert>
#include <cmath>
#include <iterator>
#include <limits>
#include <type_traits>
#include <utility>
#include "test_macros.h"
template<class T, class U>
constexpr auto has_partial_order(T&& t, U&& u)
-> decltype(std::partial_order(static_cast<T&&>(t), static_cast<U&&>(u)), true)
{
return true;
}
constexpr bool has_partial_order(...) {
return false;
}
namespace N11 {
struct A {};
struct B {};
std::strong_ordering partial_order(const A&, const A&) { return std::strong_ordering::less; }
std::strong_ordering partial_order(const A&, const B&);
}
void test_1_1()
{
static_assert( has_partial_order(1, 2));
static_assert(!has_partial_order(1, (short)2));
static_assert(!has_partial_order(1, 2.0));
static_assert(!has_partial_order(1.0f, 2.0));
static_assert( has_partial_order((int*)nullptr, (int*)nullptr));
static_assert(!has_partial_order((int*)nullptr, (const int*)nullptr));
static_assert(!has_partial_order((const int*)nullptr, (int*)nullptr));
static_assert( has_partial_order((const int*)nullptr, (const int*)nullptr));
N11::A a;
N11::B b;
static_assert( has_partial_order(a, a));
static_assert(!has_partial_order(a, b));
}
namespace N12 {
struct A {};
std::strong_ordering partial_order(A&, A&&) { return std::strong_ordering::less; }
std::weak_ordering partial_order(A&&, A&&) { return std::weak_ordering::equivalent; }
std::strong_ordering partial_order(const A&, const A&);
struct B {
friend int partial_order(B, B);
};
struct PartialOrder {
explicit operator std::partial_ordering() const { return std::partial_ordering::less; }
};
struct C {
bool touched = false;
friend PartialOrder partial_order(C& lhs, C&) { lhs.touched = true; return PartialOrder(); }
};
}
void test_1_2()
{
N12::A a;
assert(std::partial_order(a, std::move(a)) == std::partial_ordering::less);
assert(std::partial_order(std::move(a), std::move(a)) == std::partial_ordering::equivalent);
N12::B b;
static_assert(!has_partial_order(b, b));
N12::C c1, c2;
ASSERT_SAME_TYPE(decltype(std::partial_order(c1, c2)), std::partial_ordering);
assert(std::partial_order(c1, c2) == std::partial_ordering::less);
assert(c1.touched);
assert(!c2.touched);
}
namespace N13 {
struct A {};
bool operator==(const A&, const A&);
constexpr std::partial_ordering operator<=>(A&, A&&) { return std::partial_ordering::less; }
constexpr std::partial_ordering operator<=>(A&&, A&&) { return std::partial_ordering::equivalent; }
std::partial_ordering operator<=>(const A&, const A&);
static_assert(std::three_way_comparable<A>);
struct B {
std::partial_ordering operator<=>(const B&) const;
};
static_assert(!std::three_way_comparable<B>);
struct C {
bool *touched;
bool operator==(const C&) const;
constexpr std::partial_ordering operator<=>(const C& rhs) const {
*rhs.touched = true;
return std::partial_ordering::equivalent;
}
};
static_assert(std::three_way_comparable<C>);
}
constexpr bool test_1_3()
{
N13::A a;
assert(std::partial_order(a, std::move(a)) == std::partial_ordering::less);
assert(std::partial_order(std::move(a), std::move(a)) == std::partial_ordering::equivalent);
N13::B b;
static_assert(!has_partial_order(b, b));
bool c1_touched = false;
bool c2_touched = false;
N13::C c1 = {&c1_touched};
N13::C c2 = {&c2_touched};
assert(std::partial_order(c1, c2) == std::partial_ordering::equivalent);
assert(!c1_touched);
assert(c2_touched);
{
using F = float;
F nan = std::numeric_limits<F>::quiet_NaN();
assert(std::partial_order(F(1), F(2)) == std::partial_ordering::less);
assert(std::partial_order(F(0), -F(0)) == std::partial_ordering::equivalent);
#ifndef TEST_COMPILER_GCC
assert(std::partial_order(nan, F(1)) == std::partial_ordering::unordered);
#endif
assert(std::partial_order(nan, nan) == std::partial_ordering::unordered);
}
{
using F = double;
F nan = std::numeric_limits<F>::quiet_NaN();
assert(std::partial_order(F(1), F(2)) == std::partial_ordering::less);
assert(std::partial_order(F(0), -F(0)) == std::partial_ordering::equivalent);
#ifndef TEST_COMPILER_GCC
assert(std::partial_order(nan, F(1)) == std::partial_ordering::unordered);
#endif
assert(std::partial_order(nan, nan) == std::partial_ordering::unordered);
}
{
using F = long double;
F nan = std::numeric_limits<F>::quiet_NaN();
assert(std::partial_order(F(1), F(2)) == std::partial_ordering::less);
assert(std::partial_order(F(0), -F(0)) == std::partial_ordering::equivalent);
#ifndef TEST_COMPILER_GCC
assert(std::partial_order(nan, F(1)) == std::partial_ordering::unordered);
#endif
assert(std::partial_order(nan, nan) == std::partial_ordering::unordered);
}
return true;
}
namespace N14 {
struct A {};
constexpr std::strong_ordering weak_order(A&, A&&) { return std::strong_ordering::less; }
constexpr std::strong_ordering weak_order(A&&, A&&) { return std::strong_ordering::equal; }
std::strong_ordering weak_order(const A&, const A&);
struct B {
friend std::partial_ordering weak_order(B, B);
};
struct StrongOrder {
operator std::strong_ordering() const { return std::strong_ordering::less; }
};
struct C {
friend StrongOrder weak_order(C& lhs, C&);
};
struct WeakOrder {
constexpr explicit operator std::weak_ordering() const { return std::weak_ordering::less; }
operator std::partial_ordering() const = delete;
};
struct D {
bool touched = false;
friend constexpr WeakOrder weak_order(D& lhs, D&) { lhs.touched = true; return WeakOrder(); }
};
}
constexpr bool test_1_4()
{
N14::A a;
assert(std::partial_order(a, std::move(a)) == std::partial_ordering::less);
assert(std::partial_order(std::move(a), std::move(a)) == std::partial_ordering::equivalent);
N14::B b;
static_assert(!has_partial_order(b, b));
N14::C c;
static_assert(!has_partial_order(c, c));
N14::D d1, d2;
ASSERT_SAME_TYPE(decltype(std::partial_order(d1, d2)), std::partial_ordering);
assert(std::partial_order(d1, d2) == std::partial_ordering::less);
assert(d1.touched);
assert(!d2.touched);
return true;
}
int main(int, char**)
{
test_1_1();
test_1_2();
test_1_3();
test_1_4();
static_assert(test_1_3());
static_assert(test_1_4());
return 0;
}