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 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
|
//===----------------------------------------------------------------------===//
//
// 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 ranges::minmax_result<const T&>
// ranges::minmax(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 ranges::minmax_result<T>
// ranges::minmax(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 ranges::minmax_result<range_value_t<R>>
// ranges::minmax(R&& r, Comp comp = {}, Proj proj = {});
#include <algorithm>
#include <array>
#include <cassert>
#include <functional>
#include <ranges>
#include "test_iterators.h"
template <class T>
concept HasMinMax = requires { std::ranges::minmax(std::declval<T>()); };
struct NoLessThanOp {};
struct NotTotallyOrdered {
int i;
bool operator<(const NotTotallyOrdered& o) const { return i < o.i; }
};
struct MoveOnly {
MoveOnly(MoveOnly&&) = default;
MoveOnly& operator=(MoveOnly&&) = default;
MoveOnly(const MoveOnly&) = delete;
};
static_assert(!HasMinMax<int>);
static_assert(HasMinMax<int (&)[10]>); // make sure HasMinMax works with an array
static_assert(!HasMinMax<NoLessThanOp (&)[10]>);
static_assert(!HasMinMax<NotTotallyOrdered (&)[10]>);
static_assert(!HasMinMax<MoveOnly (&)[10]>);
static_assert(HasMinMax<std::initializer_list<int>>); // make sure HasMinMax works with an initializer_list
static_assert(!HasMinMax<std::initializer_list<NoLessThanOp>>);
static_assert(!HasMinMax<std::initializer_list<NotTotallyOrdered>>);
static_assert(!HasMinMax<std::initializer_list<MoveOnly>>);
static_assert(std::is_same_v<std::ranges::minmax_result<int>, std::ranges::min_max_result<int>>);
static_assert(std::is_same_v<decltype(std::ranges::minmax(1, 2)), std::ranges::minmax_result<const int&>>);
constexpr void test_2_arguments() {
const int one = 1;
const int two = 2;
{
auto result = std::ranges::minmax(one, two);
assert(result.min == 1);
assert(result.max == 2);
}
{
auto result = std::ranges::minmax(two, one);
assert(result.min == 1);
assert(result.max == 2);
}
{
// test comparator
auto result = std::ranges::minmax(one, two, std::ranges::greater{});
assert(result.min == 2);
assert(result.max == 1);
}
{
// test projection
auto result = std::ranges::minmax(one, two, std::ranges::less{}, [](int i) { return i == 1 ? 10 : i; });
assert(result.min == 2);
assert(result.max == 1);
}
{
// test if std::invoke is used for the predicate
struct S {
int i;
};
S a[3] = {S{2}, S{1}, S{3}};
std::same_as<std::ranges::minmax_result<const S&>> auto ret = std::ranges::minmax(a[0], a[1], {}, &S::i);
assert(&ret.min == &a[1]);
assert(&ret.max == &a[0]);
assert(ret.min.i == 1);
assert(ret.max.i == 2);
}
{
// check that std::invoke is used for the comparator
struct S {
int i;
constexpr bool comp(S rhs) const { return i < rhs.i; }
};
S a[] = {{2}, {5}};
auto ret = std::ranges::minmax(a[0], a[1], &S::comp);
assert(ret.min.i == 2);
assert(ret.max.i == 5);
}
{
// make sure that {a, b} is returned if b is not less than a
auto r = std::ranges::minmax(one, two);
assert(&r.min == &one);
assert(&r.max == &two);
}
}
constexpr void test_initializer_list() {
{
// test projection
auto proj = [](int i) { return i == 5 ? -100 : i; };
auto ret = std::ranges::minmax({7, 6, 9, 3, 5, 1, 2, 4}, std::ranges::less{}, proj);
assert(ret.min == 5);
assert(ret.max == 9);
}
{
// test comparator
auto ret = std::ranges::minmax({7, 6, 9, 3, 5, 1, 2, 4}, std::ranges::greater{});
assert(ret.min == 9);
assert(ret.max == 1);
}
{
// check predicate and projection call counts
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<std::ranges::minmax_result<int>> auto ret = std::ranges::minmax({1, 2, 3}, comparator, projection);
assert(ret.min == 1);
assert(ret.max == 3);
assert(compares == 3);
assert(projections == 6);
}
{
// check that std::invoke is used for the predicate
struct S {
int i;
};
std::same_as<std::ranges::minmax_result<S>> auto ret = std::ranges::minmax({S{2}, S{1}, S{3}}, {}, &S::i);
assert(ret.min.i == 1);
assert(ret.max.i == 3);
}
{
// check that std::invoke is used for the comparator
struct S {
int i;
constexpr bool comp(S rhs) const { return i < rhs.i; }
};
auto ret = std::ranges::minmax({S {1}, S {2}, S {3}, S {4}}, &S::comp);
assert(ret.min.i == 1);
assert(ret.max.i == 4);
}
{
// check that a single element works
auto ret = std::ranges::minmax({ 1 });
assert(ret.min == 1);
assert(ret.max == 1);
}
{
// check in ascending order
auto ret = std::ranges::minmax({1, 2, 3});
assert(ret.min == 1);
assert(ret.max == 3);
}
{
// check in descending order
auto ret = std::ranges::minmax({3, 2, 1});
assert(ret.min == 1);
assert(ret.max == 3);
}
}
template <class Iter, class Sent = Iter>
constexpr void test_range_types() {
{
// test projection
int a[] = {7, 6, 9, 3, 5, 1, 2, 4};
auto proj = [](int& i) { return i == 5 ? -100 : i; };
auto range = std::ranges::subrange(Iter(a), Sent(Iter(a + 8)));
auto ret = std::ranges::minmax(range, std::ranges::less{}, proj);
assert(ret.min == 5);
assert(ret.max == 9);
}
{
// test comparator
int a[] = {7, 6, 9, 3, 5, 1, 2, 4};
auto range = std::ranges::subrange(Iter(a), Sent(Iter(a + 8)));
auto ret = std::ranges::minmax(range, std::ranges::greater{});
assert(ret.min == 9);
assert(ret.max == 1);
}
{
// check that complexity requirements are met
int compares = 0;
int projections = 0;
auto comparator = [&](int x, int y) {
++compares;
return x < y;
};
auto projection = [&](int x) {
++projections;
return x;
};
int a[] = {1, 2, 3};
auto range = std::ranges::subrange(Iter(a), Sent(Iter(a + 3)));
std::same_as<std::ranges::minmax_result<int>> auto ret = std::ranges::minmax(range, comparator, projection);
assert(ret.min == 1);
assert(ret.max == 3);
assert(compares == 3);
assert(projections == 6);
}
{
// check that a single element works
int a[] = { 1 };
auto range = std::ranges::subrange(Iter(a), Sent(Iter(a + 1)));
auto ret = std::ranges::minmax(range);
assert(ret.min == 1);
assert(ret.max == 1);
}
{
// check in ascending order
int a[] = {1, 2, 3};
auto range = std::ranges::subrange(Iter(a), Sent(Iter(a + 3)));
auto ret = std::ranges::minmax(range);
assert(ret.min == 1);
assert(ret.max == 3);
}
{
// check in descending order
int a[] = {3, 2, 1};
auto range = std::ranges::subrange(Iter(a), Sent(Iter(a + 3)));
auto ret = std::ranges::minmax(range);
assert(ret.min == 1);
assert(ret.max == 3);
}
}
constexpr void test_range() {
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*>>();
{
// check that std::invoke is used for the predicate
struct S {
int i;
};
S b[3] = {S{2}, S{1}, S{3}};
std::same_as<std::ranges::minmax_result<S>> auto ret = std::ranges::minmax(b, {}, &S::i);
assert(ret.min.i == 1);
assert(ret.max.i == 3);
}
{
// check that std::invoke is used for the comparator
struct S {
int i;
constexpr bool comp(S rhs) const { return i < rhs.i; }
};
S a[] = {{1}, {2}, {3}, {4}};
auto ret = std::ranges::minmax(a, &S::comp);
assert(ret.min.i == 1);
assert(ret.max.i == 4);
}
{
// check that the leftmost value for min an rightmost for max are returned
struct S {
int comp;
int other;
};
S a[] = { {0, 0}, {0, 2}, {0, 1} };
auto ret = std::ranges::minmax(a, {}, &S::comp);
assert(ret.min.comp == 0);
assert(ret.max.comp == 0);
assert(ret.min.other == 0);
assert(ret.max.other == 1);
}
{
// check that an rvalue array works
int a[] = {1, 2, 3, 4};
auto ret = std::ranges::minmax(std::move(a));
assert(ret.min == 1);
assert(ret.max == 4);
}
}
constexpr bool test() {
test_2_arguments();
test_initializer_list();
test_range();
return true;
}
int main(int, char**) {
test();
static_assert(test());
{
// check that the iterator isn't moved from multiple times
std::shared_ptr<int> a[] = { std::make_shared<int>(42) };
auto range = std::ranges::subrange(std::move_iterator(a), std::move_iterator(a + 1));
auto [min, max] = std::ranges::minmax(range);
assert(a[0] == nullptr);
assert(min != nullptr);
assert(max == min);
}
return 0;
}
|