File: ranges.clamp.pass.cpp

package info (click to toggle)
llvm-toolchain-15 1%3A15.0.6-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,554,644 kB
  • sloc: cpp: 5,922,452; ansic: 1,012,136; asm: 674,362; python: 191,568; objc: 73,855; f90: 42,327; lisp: 31,913; pascal: 11,973; javascript: 10,144; sh: 9,421; perl: 7,447; ml: 5,527; awk: 3,523; makefile: 2,520; xml: 885; cs: 573; fortran: 567
file content (123 lines) | stat: -rw-r--r-- 3,890 bytes parent folder | download
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
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

// <algorithm>

// UNSUPPORTED: c++03, c++11, c++14, c++17
// UNSUPPORTED: libcpp-has-no-incomplete-ranges

// Older Clangs don't properly deduce decltype(auto) with a concept constraint
// XFAIL: apple-clang-13.0

// template<class T, class Proj = identity,
//          indirect_strict_weak_order<projected<const T*, Proj>> Comp = ranges::less>
//   constexpr const T&
//     ranges::clamp(const T& v, const T& lo, const T& hi, Comp comp = {}, Proj proj = {});

#include <algorithm>
#include <cassert>
#include <functional>
#include <utility>

template <class T, class Comp = std::ranges::less, class Proj = std::identity>
concept HasClamp =
    requires(T&& val, T&& low, T&& high, Comp&& comp, Proj&& proj) {
      std::ranges::clamp(std::forward<T>(val), std::forward<T>(low), std::forward<T>(high),
          std::forward<Comp>(comp), std::forward<Proj>(proj));
    };

struct NoComp {};
struct CreateNoComp {
  auto operator()(int) const { return NoComp(); }
};

static_assert(HasClamp<int, std::ranges::less, std::identity>);
static_assert(!HasClamp<NoComp>);
static_assert(!HasClamp<int, NoComp>);
static_assert(!HasClamp<int, std::ranges::less, CreateNoComp>);

constexpr bool test() {
  { // low < val < high
    int val = 2;
    int low = 1;
    int high = 3;
    std::same_as<const int&> decltype(auto) ret = std::ranges::clamp(val, low, high);
    assert(ret == 2);
    assert(&ret == &val);
  }

  { // low > val < high
    assert(std::ranges::clamp(10, 20, 30) == 20);
  }

  { // low < val > high
    assert(std::ranges::clamp(15, 5, 10) == 10);
  }

  { // low == val == high
    int val = 10;
    assert(&std::ranges::clamp(val, 10, 10) == &val);
  }

  { // Check that a custom comparator works.
    assert(std::ranges::clamp(10, 30, 20, std::ranges::greater{}) == 20);
  }

  { // Check that a custom projection works.
    struct S {
      int i;

      constexpr const int& lvalue_proj() const { return i; }
      constexpr int prvalue_proj() const { return i; }
    };

    struct Comp {
      constexpr bool operator()(const int& lhs, const int& rhs) const { return lhs < rhs; }
      constexpr bool operator()(int&& lhs, int&& rhs) const { return lhs > rhs; }
    };

    auto val = S{10};
    auto low = S{20};
    auto high = S{30};
    // Check that the value category of the projection return type is preserved.
    assert(&std::ranges::clamp(val, low, high, Comp{}, &S::lvalue_proj) == &low);
    assert(&std::ranges::clamp(val, high, low, Comp{}, &S::prvalue_proj) == &low);
  }

  { // Check that the implementation doesn't cause double moves (which could result from calling the projection on
    // `value` once and then forwarding the result into the comparator).
    struct CheckDoubleMove {
      int i;
      bool moved = false;

      constexpr explicit CheckDoubleMove(int set_i) : i(set_i) {}
      constexpr CheckDoubleMove(const CheckDoubleMove&) = default;
      constexpr CheckDoubleMove(CheckDoubleMove&& rhs) noexcept : i(rhs.i) {
        assert(!rhs.moved);
        rhs.moved = true;
      }
    };

    auto val = CheckDoubleMove{20};
    auto low = CheckDoubleMove{10};
    auto high = CheckDoubleMove{30};

    auto moving_comp = [](CheckDoubleMove lhs, CheckDoubleMove rhs) { return lhs.i < rhs.i; };
    auto prvalue_proj = [](const CheckDoubleMove& x) -> CheckDoubleMove { return x; };
    assert(&std::ranges::clamp(val, low, high, moving_comp, prvalue_proj) == &val);
  }

  return true;
}

int main(int, char**) {
  test();
  static_assert(test());

  return 0;
}