brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.2 KiB · 4a2ae96 Raw
87 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, 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>, int>);29static_assert(HasOperatorSpaceship<std::reference_wrapper<WeakOrder>, int>);30static_assert(HasOperatorSpaceship<std::reference_wrapper<PartialOrder>, int>);31 32static_assert(!HasOperatorSpaceship<std::reference_wrapper<NonComparable>, int>);33 34// Test comparisons.35 36template <typename T, typename Order>37constexpr void test() {38  T t{47};39 40  T bigger{94};41  T smaller{82};42 43  T unordered{std::numeric_limits<int>::min()};44 45  // Identical contents46  {47    std::reference_wrapper<T> rw1{t};48    assert(testOrder(rw1, t, Order::equivalent));49  }50  // Less51  {52    std::reference_wrapper<T> rw1{smaller};53    assert(testOrder(rw1, bigger, Order::less));54  }55  // Greater56  {57    std::reference_wrapper<T> rw1{bigger};58    assert(testOrder(rw1, smaller, Order::greater));59  }60  // Unordered61  if constexpr (std::same_as<T, PartialOrder>) {62    std::reference_wrapper<T> rw1{bigger};63    assert(testOrder(rw1, unordered, Order::unordered));64  }65}66 67constexpr bool test() {68  test<int, std::strong_ordering>();69  test<StrongOrder, std::strong_ordering>();70  test<int, std::weak_ordering>();71  test<WeakOrder, std::weak_ordering>();72  test<int, std::partial_ordering>();73  test<PartialOrder, std::partial_ordering>();74 75  // `LessAndEqComp` does not have `operator<=>`. Ordering is synthesized based on `operator<`76  test<LessAndEqComp, std::weak_ordering>();77 78  return true;79}80 81int main(int, char**) {82  test();83  static_assert(test());84 85  return 0;86}87