| 1 |
// { dg-do run { target c++20 } } |
| 2 |
|
| 3 |
#include <compare> |
| 4 |
#include <cstring> |
| 5 |
#include <functional> |
| 6 |
#include <testsuite_hooks.h> |
| 7 |
|
| 8 |
constexpr const char arr[] = "efgh\0abcd\0ijkl"; |
| 9 |
constexpr const char* s1 = arr; |
| 10 |
constexpr const char* s2 = arr+5; |
| 11 |
constexpr const char* s3 = arr+6; |
| 12 |
|
| 13 |
struct CStrNone |
| 14 |
{ |
| 15 |
const char* str; |
| 16 |
|
| 17 |
constexpr |
| 18 |
operator const char*() const |
| 19 |
{ return str; } |
| 20 |
}; |
| 21 |
|
| 22 |
template<typename ResultCreator> |
| 23 |
struct CStrMem |
| 24 |
{ |
| 25 |
const char* str; |
| 26 |
|
| 27 |
constexpr |
| 28 |
operator const char*() const |
| 29 |
{ return str; } |
| 30 |
|
| 31 |
auto operator<=>(CStrMem const& rhs) const |
| 32 |
{ return ResultCreator::create(std::strcmp(this->str, rhs.str)); } |
| 33 |
|
| 34 |
auto operator<=>(const char* rhs) const |
| 35 |
{ return ResultCreator::create(std::strcmp(this->str, rhs)); } |
| 36 |
}; |
| 37 |
|
| 38 |
template<typename ResultCreator> |
| 39 |
struct CStrFriend |
| 40 |
{ |
| 41 |
const char* str; |
| 42 |
|
| 43 |
constexpr |
| 44 |
operator const char*() const |
| 45 |
{ return str; } |
| 46 |
|
| 47 |
friend auto operator<=>(CStrFriend lhs, CStrFriend rhs) |
| 48 |
{ return ResultCreator::create(std::strcmp(lhs.str, rhs.str)); } |
| 49 |
|
| 50 |
friend auto operator<=>(CStrFriend lhs, const char* rhs) |
| 51 |
{ return ResultCreator::create(std::strcmp(lhs.str, rhs)); } |
| 52 |
}; |
| 53 |
|
| 54 |
template<typename ResultCreator> |
| 55 |
struct CStrFree |
| 56 |
{ |
| 57 |
const char* str; |
| 58 |
|
| 59 |
constexpr |
| 60 |
operator const char*() const |
| 61 |
{ return str; } |
| 62 |
}; |
| 63 |
|
| 64 |
template<typename RC> |
| 65 |
auto operator<=>(CStrFree<RC> lhs, CStrFree<RC> rhs) |
| 66 |
{ return RC::create(std::strcmp(lhs.str, rhs.str)); } |
| 67 |
|
| 68 |
template<typename RC> |
| 69 |
auto operator<=>(CStrFree<RC> lhs, const char* rhs) |
| 70 |
{ return RC::create(std::strcmp(lhs.str, rhs)); } |
| 71 |
|
| 72 |
template<typename ResultCreator> |
| 73 |
struct CStrMixed |
| 74 |
{ |
| 75 |
const char* str; |
| 76 |
|
| 77 |
constexpr |
| 78 |
operator const char*() const |
| 79 |
{ return str; } |
| 80 |
}; |
| 81 |
|
| 82 |
template<typename RC> |
| 83 |
auto operator<=>(CStrMixed<RC> lhs, CStrMixed<RC> rhs) |
| 84 |
{ return RC::create(std::strcmp(lhs.str, rhs.str)); } |
| 85 |
|
| 86 |
template<typename RC> |
| 87 |
auto operator<=>(CStrMixed<RC> lhs, CStrFree<RC> rhs) |
| 88 |
{ return RC::create(std::strcmp(lhs.str, rhs.str)); } |
| 89 |
|
| 90 |
// If the type returned from shapeship does not support relational |
| 91 |
// operators, then synthesized operators are ill-formed, SFINAEable. |
| 92 |
struct ReturnVoid |
| 93 |
{ |
| 94 |
constexpr static void |
| 95 |
create(int) { } |
| 96 |
}; |
| 97 |
|
| 98 |
struct NoOperators |
| 99 |
{ |
| 100 |
constexpr static NoOperators |
| 101 |
create(int) |
| 102 |
{ return NoOperators(); } |
| 103 |
}; |
| 104 |
|
| 105 |
// std defined ordering types are expected |
| 106 |
template<typename Ord = std::strong_ordering> |
| 107 |
struct ReturnOrd |
| 108 |
{ |
| 109 |
constexpr static Ord |
| 110 |
create(int cmp) |
| 111 |
{ return cmp <=> 0; } |
| 112 |
}; |
| 113 |
|
| 114 |
// However, other types that provide required |
| 115 |
// operators are supported. |
| 116 |
struct ReturnInt |
| 117 |
{ |
| 118 |
constexpr static int |
| 119 |
create(int cmp) |
| 120 |
{ return cmp; } |
| 121 |
}; |
| 122 |
|
| 123 |
struct CustomOrd |
| 124 |
{ |
| 125 |
constexpr static CustomOrd |
| 126 |
create(int cmp) |
| 127 |
{ return CustomOrd(cmp); } |
| 128 |
|
| 129 |
CustomOrd() = default; |
| 130 |
|
| 131 |
explicit constexpr |
| 132 |
CustomOrd(int cmp) |
| 133 |
: v(cmp) {} |
| 134 |
|
| 135 |
friend constexpr bool |
| 136 |
operator<(CustomOrd c, std::nullptr_t) |
| 137 |
{ return c.v < 0; } |
| 138 |
|
| 139 |
friend constexpr bool |
| 140 |
operator<(std::nullptr_t, CustomOrd c) |
| 141 |
{ return 0 < c.v; } |
| 142 |
|
| 143 |
friend constexpr bool |
| 144 |
operator>(CustomOrd c, std::nullptr_t) |
| 145 |
{ return c.v > 0; } |
| 146 |
|
| 147 |
friend constexpr bool |
| 148 |
operator>(std::nullptr_t, CustomOrd c) |
| 149 |
{ return 0 > c.v; } |
| 150 |
|
| 151 |
friend constexpr bool |
| 152 |
operator<=(CustomOrd c, std::nullptr_t) |
| 153 |
{ return c.v <= 0; } |
| 154 |
|
| 155 |
friend constexpr bool |
| 156 |
operator<=(std::nullptr_t, CustomOrd c) |
| 157 |
{ return 0 <= c.v; } |
| 158 |
|
| 159 |
friend constexpr bool |
| 160 |
operator>=(CustomOrd c, std::nullptr_t) |
| 161 |
{ return c.v >= 0; } |
| 162 |
|
| 163 |
friend constexpr bool |
| 164 |
operator>=(std::nullptr_t, CustomOrd c) |
| 165 |
{ return 0 >= c.v; } |
| 166 |
|
| 167 |
friend constexpr CustomOrd |
| 168 |
operator<=>(CustomOrd c, std::nullptr_t) |
| 169 |
{ return c; } |
| 170 |
|
| 171 |
friend constexpr CustomOrd |
| 172 |
operator<=>(std::nullptr_t, CustomOrd c) |
| 173 |
{ return CustomOrd(-c.v); } |
| 174 |
|
| 175 |
private: |
| 176 |
int v = 0; |
| 177 |
}; |
| 178 |
|
| 179 |
template<typename CStr1, typename CStr2> |
| 180 |
void |
| 181 |
test_relational(bool use_overloaded) |
| 182 |
{ |
| 183 |
CStr1 cs1{s1}; CStr2 cs2{s2}; |
| 184 |
|
| 185 |
if (use_overloaded) |
| 186 |
{ |
| 187 |
// Overloaded operaetors compare content of the string, |
| 188 |
// and cs1 > cs2; |
| 189 |
|
| 190 |
VERIFY( !(cs1 < cs2) ); |
| 191 |
VERIFY( !std::less<>{}(cs1, cs2) ); |
| 192 |
VERIFY( !std::ranges::less{}(cs1, cs2) ); |
| 193 |
|
| 194 |
VERIFY( (cs1 > cs2) ); |
| 195 |
VERIFY( std::greater<>{}(cs1, cs2) ); |
| 196 |
VERIFY( std::ranges::greater{}(cs1, cs2) ); |
| 197 |
|
| 198 |
VERIFY( !(cs1 < cs2) ); |
| 199 |
VERIFY( !std::less_equal<>{}(cs1, cs2) ); |
| 200 |
VERIFY( !std::ranges::less_equal{}(cs1, cs2) ); |
| 201 |
|
| 202 |
VERIFY( (cs1 > cs2) ); |
| 203 |
VERIFY( std::greater_equal<>{}(cs1, cs2) ); |
| 204 |
VERIFY( std::ranges::greater_equal{}(cs1, cs2) ); |
| 205 |
} |
| 206 |
else |
| 207 |
{ |
| 208 |
// Without overloaded operators, we comapre pointers, |
| 209 |
// and cs1 < cs2; |
| 210 |
|
| 211 |
VERIFY( (cs1 < cs2) ); |
| 212 |
VERIFY( std::less<>{}(cs1, cs2) ); |
| 213 |
VERIFY( std::ranges::less{}(cs1, cs2) ); |
| 214 |
|
| 215 |
VERIFY( !(cs1 > cs2) ); |
| 216 |
VERIFY( !std::greater<>{}(cs1, cs2) ); |
| 217 |
VERIFY( !std::ranges::greater{}(cs1, cs2) ); |
| 218 |
|
| 219 |
VERIFY( (cs1 < cs2) ); |
| 220 |
VERIFY( std::less_equal<>{}(cs1, cs2) ); |
| 221 |
VERIFY( std::ranges::less_equal{}(cs1, cs2) ); |
| 222 |
|
| 223 |
VERIFY( !(cs1 > cs2) ); |
| 224 |
VERIFY( !std::greater_equal<>{}(cs1, cs2) ); |
| 225 |
VERIFY( !std::ranges::greater_equal{}(cs1, cs2) ); |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
template<typename CStr> |
| 230 |
void |
| 231 |
test_relational_type(bool use_overloaded) |
| 232 |
{ |
| 233 |
test_relational<CStr, CStr>(use_overloaded); |
| 234 |
test_relational<CStr, const char*>(use_overloaded); |
| 235 |
test_relational<const char*, CStr>(use_overloaded); |
| 236 |
} |
| 237 |
|
| 238 |
template<typename RC> |
| 239 |
void |
| 240 |
test_relational_return() |
| 241 |
{ |
| 242 |
test_relational_type<CStrMem<RC>>(true); |
| 243 |
test_relational_type<CStrFriend<RC>>(true); |
| 244 |
test_relational_type<CStrFree<RC>>(true); |
| 245 |
test_relational<CStrMixed<RC>, CStrFree<RC>>(true); |
| 246 |
test_relational<CStrFree<RC>, CStrMixed<RC>>(true); |
| 247 |
} |
| 248 |
|
| 249 |
template<typename CStr1, typename CStr2> |
| 250 |
void |
| 251 |
test_spaceship(bool use_overloaded) |
| 252 |
{ |
| 253 |
CStr1 cs1{s1}; CStr2 cs2{s2}; |
| 254 |
|
| 255 |
if (use_overloaded) |
| 256 |
{ |
| 257 |
// Overloaded operaetors compare content of the string, |
| 258 |
// and cs1 > cs2; |
| 259 |
VERIFY( (cs1 <=> cs2) > 0 ); |
| 260 |
VERIFY( std::compare_three_way{}(cs1, cs2) > 0 ); |
| 261 |
} |
| 262 |
else |
| 263 |
{ |
| 264 |
// Without overloaded operators, we comapre pointers, |
| 265 |
// and cs1 < cs2; |
| 266 |
VERIFY( (cs1 <=> cs2) < 0 ); |
| 267 |
VERIFY( std::compare_three_way{}(cs1, cs2) < 0 ); |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
template<typename CStr> |
| 272 |
void |
| 273 |
test_spaceship_type(bool use_overloaded) |
| 274 |
{ |
| 275 |
test_spaceship<CStr, CStr>(use_overloaded); |
| 276 |
test_spaceship<CStr, const char*>(use_overloaded); |
| 277 |
test_spaceship<const char*, CStr>(use_overloaded); |
| 278 |
} |
| 279 |
|
| 280 |
template<typename Ordering> |
| 281 |
void |
| 282 |
test_std_ordering() |
| 283 |
{ |
| 284 |
using RC = ReturnOrd<Ordering>; |
| 285 |
test_relational_return<RC>(); |
| 286 |
test_spaceship_type<CStrMem<RC>>(true); |
| 287 |
test_spaceship_type<CStrFriend<RC>>(true); |
| 288 |
test_spaceship_type<CStrFree<RC>>(true); |
| 289 |
test_spaceship<CStrMixed<RC>, CStrFree<RC>>(true); |
| 290 |
test_spaceship<CStrFree<RC>, CStrMixed<RC>>(true); |
| 291 |
} |
| 292 |
|
| 293 |
template<typename CStr1, typename CStr2> |
| 294 |
void |
| 295 |
test_no_relational() |
| 296 |
{ |
| 297 |
CStr1 c1{}; CStr2 c2{}; |
| 298 |
static_assert(!requires { c1 < c2; }); |
| 299 |
static_assert(!requires { c1 > c2; }); |
| 300 |
static_assert(!requires { c1 <= c2; }); |
| 301 |
static_assert(!requires { c1 >= c2; }); |
| 302 |
} |
| 303 |
|
| 304 |
template<typename CStr> |
| 305 |
void |
| 306 |
test_no_relational_type() |
| 307 |
{ |
| 308 |
test_no_relational<CStr, CStr>(); |
| 309 |
test_no_relational<CStr, const char*>(); |
| 310 |
test_no_relational<const char*, CStr>(); |
| 311 |
} |
| 312 |
|
| 313 |
template<typename RC> |
| 314 |
void |
| 315 |
test_no_relational_return() |
| 316 |
{ |
| 317 |
test_no_relational_type<CStrMem<RC>>(); |
| 318 |
test_no_relational_type<CStrFriend<RC>>(); |
| 319 |
test_no_relational_type<CStrFree<RC>>(); |
| 320 |
test_no_relational<CStrMixed<RC>, CStrFree<RC>>(); |
| 321 |
test_no_relational<CStrFree<RC>, CStrMixed<RC>>(); |
| 322 |
} |
| 323 |
|
| 324 |
int main() |
| 325 |
{ |
| 326 |
test_std_ordering<std::strong_ordering>(); |
| 327 |
test_std_ordering<std::weak_ordering>(); |
| 328 |
test_std_ordering<std::partial_ordering>(); |
| 329 |
|
| 330 |
test_relational_type<CStrNone>(false); |
| 331 |
test_relational_return<ReturnInt>(); |
| 332 |
test_relational_return<CustomOrd>(); |
| 333 |
|
| 334 |
test_no_relational_return<ReturnVoid>(); |
| 335 |
test_no_relational_return<NoOperators>(); |
| 336 |
} |