File: ranges.max.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 (262 lines) | stat: -rw-r--r-- 8,049 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
//===----------------------------------------------------------------------===//
//
// 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

// template<class T, class Proj = identity,
//          indirect_strict_weak_order<projected<const T*, Proj>> Comp = ranges::less>
//   constexpr const T& ranges::max(const T& a, const T& b, Comp comp = {}, Proj proj = {});
//
// template<copyable T, class Proj = identity,
//          indirect_strict_weak_order<projected<const T*, Proj>> Comp = ranges::less>
//   constexpr T ranges::max(initializer_list<T> r, Comp comp = {}, Proj proj = {});
//
// template<input_range R, class Proj = identity,
//          indirect_strict_weak_order<projected<iterator_t<R>, Proj>> Comp = ranges::less>
//   requires indirectly_copyable_storable<iterator_t<R>, range_value_t<R>*>
//   constexpr range_value_t<R>
//     ranges::max(R&& r, Comp comp = {}, Proj proj = {});

#include <algorithm>
#include <array>
#include <cassert>
#include <functional>
#include <ranges>

#include "almost_satisfies_types.h"
#include "test_iterators.h"
#include "test_macros.h"

template <class T>
concept HasMaxR = requires { std::ranges::max(std::declval<T>()); };

struct NoLessThanOp {};
struct NotTotallyOrdered {
  int i;
  bool operator<(const NotTotallyOrdered& o) const { return i < o.i; }
};

struct Movable {
  Movable& operator=(Movable&&) = default;
  Movable(Movable&&) = default;
  Movable(const Movable&) = delete;
};

static_assert(!HasMaxR<int>);

static_assert(HasMaxR<int(&)[10]>);
static_assert(HasMaxR<int(&&)[10]>);
static_assert(!HasMaxR<NoLessThanOp(&)[10]>);
static_assert(!HasMaxR<NotTotallyOrdered(&)[10]>);
static_assert(!HasMaxR<Movable(&)[10]>);

static_assert(HasMaxR<std::initializer_list<int>>);
static_assert(!HasMaxR<std::initializer_list<NoLessThanOp>>);
static_assert(!HasMaxR<std::initializer_list<NotTotallyOrdered>>);
static_assert(!HasMaxR<std::initializer_list<Movable>>);
static_assert(!HasMaxR<InputRangeNotDerivedFrom>);
static_assert(!HasMaxR<InputRangeNotIndirectlyReadable>);
static_assert(!HasMaxR<InputRangeNotInputOrOutputIterator>);
static_assert(!HasMaxR<InputRangeNotSentinelSemiregular>);
static_assert(!HasMaxR<InputRangeNotSentinelEqualityComparableWith>);

template <class T, class U = T>
concept HasMax2 = requires { std::ranges::max(std::declval<T>(), std::declval<U>()); };

static_assert(HasMax2<int>);
static_assert(!HasMax2<int, long>);

static_assert(std::is_same_v<decltype(std::ranges::max(1, 2)), const int&>);

constexpr void test_2_arguments() {
  assert(std::ranges::max(1, 2) == 2);
  assert(std::ranges::max(2, 1) == 2);
  // test comparator
  assert(std::ranges::max(1, 2, std::ranges::greater{}) == 1);
  // test projection
  assert(std::ranges::max(1, 2, std::ranges::less{}, [](int i){ return i == 1 ? 10 : i; }) == 1);

  { // check that std::invoke is used
    struct S { int i; };
    S a[3] = { S{2}, S{1}, S{3} };
    decltype(auto) ret = std::ranges::max(a[0], a[1], {}, &S::i);
    ASSERT_SAME_TYPE(decltype(ret), const S&);
    assert(&ret == &a[0]);
    assert(ret.i == 2);
  }

  { // check that pointers are compared and not a range
    int i[1];
    int* a[] = {i, i + 1};
    auto ret = std::ranges::max(a[0], a[1]);
    assert(ret == i + 1);
  }

  { // test predicate and projection count
    int compares = 0;
    int projections = 0;
    auto comparator = [&](int x, int y) {
      ++compares;
      return x < y;
    };
    auto projection = [&](int x) {
      ++projections;
      return x;
    };
    auto ret = std::ranges::max(1, 2, comparator, projection);
    assert(ret == 2);
    assert(compares == 1);
    assert(projections == 2);
  }

  { // check that the first argument is returned
    struct S { int check; int other; };
    auto ret = std::ranges::max(S {0, 1}, S {0, 2}, {}, &S::check);
    assert(ret.other == 1);
  }
}

constexpr void test_initializer_list() {
  { // test projection
    auto proj = [](int i) { return i == 5 ? 100 : i; };
    int ret = std::ranges::max({7, 6, 9, 3, 5, 1, 2, 4}, {}, proj);
    assert(ret == 5);
  }

  { // test comparator
    int ret = std::ranges::max({7, 6, 9, 3, 5, 1, 2, 4}, std::ranges::greater{});
    assert(ret == 1);
  }

  { // check that complexity requirements are met
    int compares = 0;
    int projections = 0;
    auto comparator = [&](int a, int b) {
      ++compares;
      return a < b;
    };
    auto projection = [&](int a) {
      ++projections;
      return a;
    };
    std::same_as<int> decltype(auto) ret = std::ranges::max({1, 2, 3}, comparator, projection);
    assert(ret == 3);
    assert(compares == 2);
    assert(projections == 4);
  }

  { // check that std::invoke is used
    struct S { int i; };
    std::same_as<S> decltype(auto) ret = std::ranges::max({ S{2}, S{1}, S{3} }, {}, &S::i);
    assert(ret.i == 3);
  }

  { // check that the first largest element is returned
    { // where the first element is the largest
      struct S { int check; int other; };
      auto ret = std::ranges::max({ S{1, 1}, S{0, 2}, S{1, 3} }, {}, &S::check);
      assert(ret.check == 1);
      assert(ret.other == 1);
    }
    { // where the first element isn't the largest
      struct S { int check; int other; };
      auto ret = std::ranges::max({ S{0, 1}, S{1, 2}, S{1, 3} }, {}, &S::check);
      assert(ret.check == 1);
      assert(ret.other == 2);
    }
  }
}

template <class It, class Sent = It>
constexpr void test_range_types() {
  int a[] = {7, 6, 9, 3, 5, 1, 2, 4};
  auto range = std::ranges::subrange(It(a), Sent(It(a + 8)));
  int ret = std::ranges::max(range);
  assert(ret == 9);
}

constexpr void test_range() {
  { // check that all range types work
    test_range_types<cpp20_input_iterator<int*>, sentinel_wrapper<cpp20_input_iterator<int*>>>();
    test_range_types<forward_iterator<int*>>();
    test_range_types<bidirectional_iterator<int*>>();
    test_range_types<random_access_iterator<int*>>();
    test_range_types<contiguous_iterator<int*>>();
  }

  int a[] = {7, 6, 9, 3, 5, 1, 2, 4};
  { // test projection
    auto proj = [](int& i) { return i == 5 ? 100 : i; };
    int ret = std::ranges::max(a, std::ranges::less{}, proj);
    assert(ret == 5);
  }

  { // test comparator
    int ret = std::ranges::max(a, std::ranges::greater{});
    assert(ret == 1);
  }

  { // check that predicate and projection call counts are correct
    int compares = 0;
    int projections = 0;
    auto comparator = [&](int x, int y) {
      ++compares;
      return x < y;
    };
    auto projection = [&](int x) {
      ++projections;
      return x;
    };
    std::same_as<int> decltype(auto) ret = std::ranges::max(std::array{1, 2, 3}, comparator, projection);
    assert(ret == 3);
    assert(compares == 2);
    assert(projections == 4);
  }

  { // check that std::invoke is used
    struct S { int i; };
    S b[3] = { S{2}, S{1}, S{3} };
    std::same_as<S> decltype(auto) ret = std::ranges::max(b, {}, &S::i);
    assert(ret.i == 3);
  }

  { // check that the first largest element is returned
    { // where the first element is the largest
      struct S { int check; int other; };
      S b[] = { S{1, 1}, S{0, 2}, S{1, 3} };
      auto ret = std::ranges::max(b, {}, &S::check);
      assert(ret.check == 1);
      assert(ret.other == 1);
    }
    { // where the first element isn't the largest
      struct S { int check; int other; };
      S b[] = { S{0, 1}, S{1, 2}, S{1, 3} };
      auto ret = std::ranges::max(b, {}, &S::check);
      assert(ret.check == 1);
      assert(ret.other == 2);
    }
  }
}

constexpr bool test() {
  test_2_arguments();
  test_initializer_list();
  test_range();

  return true;
}

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

  return 0;
}