1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
|
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// <tuple>
// template <class... Types> class tuple;
// template<class... TTypes, class... UTypes>
// auto
// operator<=>(const tuple<TTypes...>& t, const tuple<UTypes...>& u);
// UNSUPPORTED: c++03, c++11, c++14, c++17
#include "test_macros.h"
TEST_CLANG_DIAGNOSTIC_IGNORED("-Wsign-compare")
TEST_GCC_DIAGNOSTIC_IGNORED("-Wsign-compare")
TEST_MSVC_DIAGNOSTIC_IGNORED(4242 4244)
#include <cassert>
#include <compare>
#include <limits>
#include <tuple>
#include <type_traits> // std::is_constant_evaluated
// A custom three-way result type
struct CustomEquality {
friend constexpr bool operator==(const CustomEquality&, int) noexcept { return true; }
friend constexpr bool operator<(const CustomEquality&, int) noexcept { return false; }
friend constexpr bool operator<(int, const CustomEquality&) noexcept { return false; }
};
constexpr bool test() {
// Empty tuple
{
typedef std::tuple<> T0;
// No member types yields strong ordering (all are equal).
ASSERT_SAME_TYPE(decltype(T0() <=> T0()), std::strong_ordering);
assert((T0() <=> T0()) == std::strong_ordering::equal);
}
// Mixed types with integers, which compare strongly ordered
{
typedef std::tuple<long> T1;
typedef std::tuple<short> T2;
ASSERT_SAME_TYPE(decltype(T1() <=> T2()), std::strong_ordering);
assert((T1(1) <=> T2(1)) == std::strong_ordering::equal);
assert((T1(1) <=> T2(0)) == std::strong_ordering::greater);
assert((T1(1) <=> T2(2)) == std::strong_ordering::less);
}
{
typedef std::tuple<long, unsigned int> T1;
typedef std::tuple<short, unsigned long> T2;
ASSERT_SAME_TYPE(decltype(T1() <=> T2()), std::strong_ordering);
assert((T1(1, 2) <=> T2(1, 2)) == std::strong_ordering::equal);
assert((T1(1, 2) <=> T2(0, 2)) == std::strong_ordering::greater);
assert((T1(1, 2) <=> T2(2, 2)) == std::strong_ordering::less);
assert((T1(1, 2) <=> T2(1, 1)) == std::strong_ordering::greater);
assert((T1(1, 2) <=> T2(1, 3)) == std::strong_ordering::less);
}
{
typedef std::tuple<long, int, unsigned short> T1;
typedef std::tuple<short, long, unsigned int> T2;
ASSERT_SAME_TYPE(decltype(T1() <=> T2()), std::strong_ordering);
assert((T1(1, 2, 3) <=> T2(1, 2, 3)) == std::strong_ordering::equal);
assert((T1(1, 2, 3) <=> T2(0, 2, 3)) == std::strong_ordering::greater);
assert((T1(1, 2, 3) <=> T2(2, 2, 3)) == std::strong_ordering::less);
assert((T1(1, 2, 3) <=> T2(1, 1, 3)) == std::strong_ordering::greater);
assert((T1(1, 2, 3) <=> T2(1, 3, 3)) == std::strong_ordering::less);
assert((T1(1, 2, 3) <=> T2(1, 2, 2)) == std::strong_ordering::greater);
assert((T1(1, 2, 3) <=> T2(1, 2, 4)) == std::strong_ordering::less);
}
// Mixed types with floating point, which compare partially ordered
{
typedef std::tuple<long> T1;
typedef std::tuple<double> T2;
ASSERT_SAME_TYPE(decltype(T1() <=> T2()), std::partial_ordering);
assert((T1(1) <=> T2(1)) == std::partial_ordering::equivalent);
assert((T1(1) <=> T2(0.9)) == std::partial_ordering::greater);
assert((T1(1) <=> T2(1.1)) == std::partial_ordering::less);
}
{
typedef std::tuple<long, float> T1;
typedef std::tuple<double, unsigned int> T2;
ASSERT_SAME_TYPE(decltype(T1() <=> T2()), std::partial_ordering);
assert((T1(1, 2) <=> T2(1, 2)) == std::partial_ordering::equivalent);
assert((T1(1, 2) <=> T2(0.9, 2)) == std::partial_ordering::greater);
assert((T1(1, 2) <=> T2(1.1, 2)) == std::partial_ordering::less);
assert((T1(1, 2) <=> T2(1, 1)) == std::partial_ordering::greater);
assert((T1(1, 2) <=> T2(1, 3)) == std::partial_ordering::less);
}
{
typedef std::tuple<short, float, double> T1;
typedef std::tuple<double, long, unsigned int> T2;
ASSERT_SAME_TYPE(decltype(T1() <=> T2()), std::partial_ordering);
assert((T1(1, 2, 3) <=> T2(1, 2, 3)) == std::partial_ordering::equivalent);
assert((T1(1, 2, 3) <=> T2(0.9, 2, 3)) == std::partial_ordering::greater);
assert((T1(1, 2, 3) <=> T2(1.1, 2, 3)) == std::partial_ordering::less);
assert((T1(1, 2, 3) <=> T2(1, 1, 3)) == std::partial_ordering::greater);
assert((T1(1, 2, 3) <=> T2(1, 3, 3)) == std::partial_ordering::less);
assert((T1(1, 2, 3) <=> T2(1, 2, 2)) == std::partial_ordering::greater);
assert((T1(1, 2, 3) <=> T2(1, 2, 4)) == std::partial_ordering::less);
}
{
typedef std::tuple<float> T1;
typedef std::tuple<double> T2;
constexpr double nan = std::numeric_limits<double>::quiet_NaN();
// Comparisons with NaN and non-NaN are non-constexpr in GCC, so both sides must be NaN
assert((T1(nan) <=> T2(nan)) == std::partial_ordering::unordered);
}
{
typedef std::tuple<double, double> T1;
typedef std::tuple<float, float> T2;
constexpr double nan = std::numeric_limits<double>::quiet_NaN();
assert((T1(nan, 2) <=> T2(nan, 2)) == std::partial_ordering::unordered);
assert((T1(1, nan) <=> T2(1, nan)) == std::partial_ordering::unordered);
}
{
typedef std::tuple<double, float, float> T1;
typedef std::tuple<double, double, float> T2;
constexpr double nan = std::numeric_limits<double>::quiet_NaN();
assert((T1(nan, 2, 3) <=> T2(nan, 2, 3)) == std::partial_ordering::unordered);
assert((T1(1, nan, 3) <=> T2(1, nan, 3)) == std::partial_ordering::unordered);
assert((T1(1, 2, nan) <=> T2(1, 2, nan)) == std::partial_ordering::unordered);
}
// Ordering classes and synthesized three way comparison
{
typedef std::tuple<long, int, unsigned int> T1;
typedef std::tuple<int, long, unsigned short> T2;
// All strongly ordered members yields strong ordering.
ASSERT_SAME_TYPE(decltype(T1() <=> T2()), std::strong_ordering);
}
{
struct WeakSpaceship {
constexpr bool operator==(const WeakSpaceship&) const { return true; }
constexpr std::weak_ordering operator<=>(const WeakSpaceship&) const { return std::weak_ordering::equivalent; }
};
{
typedef std::tuple<int, unsigned int, WeakSpaceship> T1;
typedef std::tuple<int, unsigned long, WeakSpaceship> T2;
// Strongly ordered members and a weakly ordered member yields weak ordering.
ASSERT_SAME_TYPE(decltype(T1() <=> T2()), std::weak_ordering);
}
{
typedef std::tuple<unsigned int, int, WeakSpaceship> T1;
typedef std::tuple<double, long, WeakSpaceship> T2;
// Doubles are partially ordered, so one partial, one strong, and one weak ordering
// yields partial ordering.
ASSERT_SAME_TYPE(decltype(T1() <=> T2()), std::partial_ordering);
}
}
{
struct NoSpaceship {
constexpr bool operator==(const NoSpaceship&) const { return true; }
constexpr bool operator<(const NoSpaceship&) const { return false; }
};
typedef std::tuple<int, unsigned int, NoSpaceship> T1;
typedef std::tuple<int, unsigned long, NoSpaceship> T2;
// Strongly ordered members and a weakly ordered member (synthesized) yields weak ordering.
ASSERT_SAME_TYPE(decltype(T1() <=> T2()), std::weak_ordering);
}
{
struct SpaceshipNoEquals {
constexpr std::strong_ordering operator<=>(const SpaceshipNoEquals&) const { return std::strong_ordering::equal; }
constexpr bool operator<(const SpaceshipNoEquals&) const { return false; }
};
typedef std::tuple<int, unsigned int, SpaceshipNoEquals> T1;
typedef std::tuple<int, unsigned long, SpaceshipNoEquals> T2;
// Spaceship operator with no == operator falls back on the < operator and weak ordering.
ASSERT_SAME_TYPE(decltype(T1() <=> T2()), std::weak_ordering);
}
{
struct CustomSpaceship {
constexpr CustomEquality operator<=>(const CustomSpaceship&) const { return CustomEquality(); }
};
typedef std::tuple<int, unsigned int, CustomSpaceship> T1;
typedef std::tuple<short, unsigned long, CustomSpaceship> T2;
typedef std::tuple<CustomSpaceship> T3;
// Custom three way return types cannot be used in synthesized three way comparison,
// but they can be used for (rewritten) operator< when synthesizing a weak ordering.
ASSERT_SAME_TYPE(decltype(T1() <=> T2()), std::weak_ordering);
ASSERT_SAME_TYPE(decltype(T3() <=> T3()), std::weak_ordering);
}
{
typedef std::tuple<long, int> T1;
typedef std::tuple<long, unsigned int> T2;
// Even with the warning suppressed (-Wno-sign-compare) there should still be no <=> operator
// between signed and unsigned types, so we should end up with a synthesized weak ordering.
ASSERT_SAME_TYPE(decltype(T1() <=> T2()), std::weak_ordering);
}
#ifdef TEST_COMPILER_GCC
// GCC cannot evaluate NaN @ non-NaN constexpr, so test that runtime-only.
if (!std::is_constant_evaluated())
#endif
{
{
typedef std::tuple<double> T1;
typedef std::tuple<int> T2;
constexpr double nan = std::numeric_limits<double>::quiet_NaN();
ASSERT_SAME_TYPE(decltype(T1() <=> T2()), std::partial_ordering);
assert((T1(nan) <=> T2(1)) == std::partial_ordering::unordered);
}
{
typedef std::tuple<double, double> T1;
typedef std::tuple<int, int> T2;
constexpr double nan = std::numeric_limits<double>::quiet_NaN();
ASSERT_SAME_TYPE(decltype(T1() <=> T2()), std::partial_ordering);
assert((T1(nan, 2) <=> T2(1, 2)) == std::partial_ordering::unordered);
assert((T1(1, nan) <=> T2(1, 2)) == std::partial_ordering::unordered);
}
{
typedef std::tuple<double, double, double> T1;
typedef std::tuple<int, int, int> T2;
constexpr double nan = std::numeric_limits<double>::quiet_NaN();
ASSERT_SAME_TYPE(decltype(T1() <=> T2()), std::partial_ordering);
assert((T1(nan, 2, 3) <=> T2(1, 2, 3)) == std::partial_ordering::unordered);
assert((T1(1, nan, 3) <=> T2(1, 2, 3)) == std::partial_ordering::unordered);
assert((T1(1, 2, nan) <=> T2(1, 2, 3)) == std::partial_ordering::unordered);
}
}
return true;
}
int main(int, char**) {
test();
static_assert(test());
return 0;
}
|