#include <cassert>
#include <limits>
#include <type_traits>
#include <utility>
#include <variant>
#include "test_macros.h"
#include "test_comparisons.h"
#ifndef TEST_HAS_NO_EXCEPTIONS
struct MakeEmptyT {
MakeEmptyT() = default;
MakeEmptyT(MakeEmptyT&&) { throw 42; }
MakeEmptyT& operator=(MakeEmptyT&&) { throw 42; }
};
inline bool operator==(const MakeEmptyT&, const MakeEmptyT&) {
assert(false);
return false;
}
inline std::weak_ordering operator<=>(const MakeEmptyT&, const MakeEmptyT&) {
assert(false);
return std::weak_ordering::equivalent;
}
template <class Variant>
void makeEmpty(Variant& v) {
Variant v2(std::in_place_type<MakeEmptyT>);
try {
v = std::move(v2);
assert(false);
} catch (...) {
assert(v.valueless_by_exception());
}
}
void test_empty() {
{
using V = std::variant<int, MakeEmptyT>;
V v1;
V v2;
makeEmpty(v2);
assert(testOrder(v1, v2, std::weak_ordering::greater));
}
{
using V = std::variant<int, MakeEmptyT>;
V v1;
makeEmpty(v1);
V v2;
assert(testOrder(v1, v2, std::weak_ordering::less));
}
{
using V = std::variant<int, MakeEmptyT>;
V v1;
makeEmpty(v1);
V v2;
makeEmpty(v2);
assert(testOrder(v1, v2, std::weak_ordering::equivalent));
}
}
#endif
template <class T1, class T2, class Order>
constexpr bool test_with_types() {
using V = std::variant<T1, T2>;
AssertOrderReturn<Order, V>();
{
constexpr V v1(std::in_place_index<0>, T1{1});
constexpr V v2(std::in_place_index<0>, T1{1});
assert(testOrder(v1, v2, Order::equivalent));
}
{
constexpr V v1(std::in_place_index<0>, T1{0});
constexpr V v2(std::in_place_index<0>, T1{1});
assert(testOrder(v1, v2, Order::less));
}
{
constexpr V v1(std::in_place_index<0>, T1{1});
constexpr V v2(std::in_place_index<0>, T1{0});
assert(testOrder(v1, v2, Order::greater));
}
{
constexpr V v1(std::in_place_index<0>, T1{0});
constexpr V v2(std::in_place_index<1>, T2{0});
assert(testOrder(v1, v2, Order::less));
}
{
constexpr V v1(std::in_place_index<1>, T2{0});
constexpr V v2(std::in_place_index<0>, T1{0});
assert(testOrder(v1, v2, Order::greater));
}
return true;
}
constexpr bool test_three_way() {
assert((test_with_types<int, double, std::partial_ordering>()));
assert((test_with_types<int, long, std::strong_ordering>()));
{
using V = std::variant<int, double>;
constexpr double nan = std::numeric_limits<double>::quiet_NaN();
{
constexpr V v1(std::in_place_type<int>, 1);
constexpr V v2(std::in_place_type<double>, nan);
assert(testOrder(v1, v2, std::partial_ordering::less));
}
{
constexpr V v1(std::in_place_type<double>, nan);
constexpr V v2(std::in_place_type<int>, 2);
assert(testOrder(v1, v2, std::partial_ordering::greater));
}
{
constexpr V v1(std::in_place_type<double>, nan);
constexpr V v2(std::in_place_type<double>, nan);
assert(testOrder(v1, v2, std::partial_ordering::unordered));
}
}
return true;
}
template <class T, class U = T>
concept has_three_way_op = requires (T& t, U& u) { t <=> u; };
using std::three_way_comparable;
struct HasSimpleOrdering {
constexpr bool operator==(const HasSimpleOrdering&) const;
constexpr bool operator<(const HasSimpleOrdering&) const;
};
struct HasOnlySpaceship {
constexpr bool operator==(const HasOnlySpaceship&) const = delete;
constexpr std::weak_ordering operator<=>(const HasOnlySpaceship&) const;
};
struct HasFullOrdering {
constexpr bool operator==(const HasFullOrdering&) const;
constexpr std::weak_ordering operator<=>(const HasFullOrdering&) const;
};
static_assert(!has_three_way_op<HasSimpleOrdering>);
static_assert(!has_three_way_op<std::variant<int, HasSimpleOrdering>>);
static_assert(!three_way_comparable<HasSimpleOrdering>);
static_assert(!three_way_comparable<std::variant<int, HasSimpleOrdering>>);
static_assert(has_three_way_op<HasOnlySpaceship>);
static_assert(!has_three_way_op<std::variant<int, HasOnlySpaceship>>);
static_assert(!three_way_comparable<HasOnlySpaceship>);
static_assert(!three_way_comparable<std::variant<int, HasOnlySpaceship>>);
static_assert( has_three_way_op<HasFullOrdering>);
static_assert( has_three_way_op<std::variant<int, HasFullOrdering>>);
static_assert( three_way_comparable<HasFullOrdering>);
static_assert( three_way_comparable<std::variant<int, HasFullOrdering>>);
int main(int, char**) {
test_three_way();
static_assert(test_three_way());
#ifndef TEST_HAS_NO_EXCEPTIONS
test_empty();
#endif
return 0;
}