File: ranges.clamp.pass.cpp

package info (click to toggle)
llvm-toolchain-19 1%3A19.1.7-3~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 1,998,492 kB
  • sloc: cpp: 6,951,680; ansic: 1,486,157; asm: 913,598; python: 232,024; f90: 80,126; objc: 75,281; lisp: 37,276; pascal: 16,990; sh: 10,009; ml: 5,058; perl: 4,724; awk: 3,523; makefile: 3,167; javascript: 2,504; xml: 892; fortran: 664; cs: 573
file content (126 lines) | stat: -rw-r--r-- 4,060 bytes parent folder | download | duplicates (7)
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
//===----------------------------------------------------------------------===//
//
// 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

// 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 <concepts>
#include <functional>
#include <iterator>
#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>);

struct EnsureValueCategoryComp {
  constexpr bool operator()(const int&& x, const int&& y) const { return x < y; }
  constexpr bool operator()(const int&& x, int& y) const { return x < y; }
  constexpr bool operator()(int& x, const int&& y) const { return x < y; }
  constexpr bool operator()(int& x, int& y) const { return x < y; }
  constexpr bool operator()(std::same_as<const int&> auto&& x, std::same_as<const int&> auto&& y) const {
    return x < y;
  }
};

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 bool operator==(S const& other) const { return i == other.i; }
    };

    auto val = S{10};
    auto low = S{20};
    auto high = S{30};
    auto proj = [](S const& s) -> int const& { return s.i; };

    assert(std::ranges::clamp(val, low, high, std::less{}, proj) == low);
  }

  { // Ensure that we respect the value category of the projection when calling the comparator.
    // This additional example was provided by Tim Song in https://github.com/microsoft/STL/issues/3970#issuecomment-1685120958.
    struct MoveProj {
      constexpr int const&& operator()(int const& x) const { return std::move(x); }
    };

    static_assert(std::indirect_strict_weak_order<EnsureValueCategoryComp, std::projected<const int*, MoveProj>>);

    assert(std::ranges::clamp(0, 1, 2, EnsureValueCategoryComp{}, MoveProj{}) == 1);
  }

  { // Make sure we don't call the projection more than three times per [alg.clamp], see #64717
    int counter              = 0;
    auto projection_function = [&counter](const int value) -> int {
      counter++;
      return value;
    };
    assert(std::ranges::clamp(3, 2, 4, std::ranges::less{}, projection_function) == 3);
#if defined(_LIBCPP_HARDENING_MODE) && \
      _LIBCPP_HARDENING_MODE != _LIBCPP_HARDENING_MODE_EXTENSIVE && \
      _LIBCPP_HARDENING_MODE != _LIBCPP_HARDENING_MODE_DEBUG
    assert(counter <= 3);
#endif
  }

  return true;
}

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

  return 0;
}