93 lines · cpp
1//===----------------------------------------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8 9// REQUIRES: std-at-least-c++2610 11// <functional>12 13// class reference_wrapper14 15// [refwrap.comparisons], comparisons16 17// friend constexpr auto operator<=>(reference_wrapper, reference_wrapper<const T>); // Since C++2618 19#include <cassert>20#include <concepts>21#include <functional>22 23#include "test_comparisons.h"24#include "test_macros.h"25 26// Test SFINAE.27 28static_assert(HasOperatorSpaceship<std::reference_wrapper<StrongOrder>, std::reference_wrapper<const StrongOrder>>);29static_assert(HasOperatorSpaceship<std::reference_wrapper<WeakOrder>, std::reference_wrapper<const WeakOrder>>);30static_assert(HasOperatorSpaceship<std::reference_wrapper<PartialOrder>, std::reference_wrapper<const PartialOrder>>);31 32static_assert(!HasOperatorSpaceship<std::reference_wrapper<StrongOrder>, std::reference_wrapper<const NonComparable>>);33static_assert(!HasOperatorSpaceship<std::reference_wrapper<WeakOrder>, std::reference_wrapper<const NonComparable>>);34static_assert(!HasOperatorSpaceship<std::reference_wrapper<PartialOrder>, std::reference_wrapper<const NonComparable>>);35 36// Test comparisons.37 38template <typename T, typename Order>39constexpr void test() {40 T t{47};41 42 T bigger{94};43 T smaller{82};44 45 T unordered{std::numeric_limits<int>::min()};46 47 // Identical contents48 {49 std::reference_wrapper<T> rw1{t};50 std::reference_wrapper<const T> rw2{t};51 assert(testOrder(rw1, rw2, Order::equivalent));52 }53 // Less54 {55 std::reference_wrapper<T> rw1{smaller};56 std::reference_wrapper<const T> rw2{bigger};57 assert(testOrder(rw1, rw2, Order::less));58 }59 // Greater60 {61 std::reference_wrapper<T> rw1{bigger};62 std::reference_wrapper<const T> rw2{smaller};63 assert(testOrder(rw1, rw2, Order::greater));64 }65 // Unordered66 if constexpr (std::same_as<T, PartialOrder>) {67 std::reference_wrapper<T> rw1{bigger};68 std::reference_wrapper<const T> rw2{unordered};69 assert(testOrder(rw1, rw2, Order::unordered));70 }71}72 73constexpr bool test() {74 test<int, std::strong_ordering>();75 test<StrongOrder, std::strong_ordering>();76 test<int, std::weak_ordering>();77 test<WeakOrder, std::weak_ordering>();78 test<int, std::partial_ordering>();79 test<PartialOrder, std::partial_ordering>();80 81 // `LessAndEqComp` does not have `operator<=>`. Ordering is synthesized based on `operator<`82 test<LessAndEqComp, std::weak_ordering>();83 84 return true;85}86 87int main(int, char**) {88 test();89 static_assert(test());90 91 return 0;92}93