| 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-exception |
| 6 |
// |
| 7 |
//===----------------------------------------------------------------------===// |
| 8 |
|
| 9 |
// <algorithm> |
| 10 |
|
| 11 |
// template<ForwardIterator Iter> |
| 12 |
// requires LessThanComparable<Iter::value_type> |
| 13 |
// pair<Iter, Iter> |
| 14 |
// minmax_element(Iter first, Iter last); |
| 15 |
|
| 16 |
#include <algorithm> |
| 17 |
#include <random> |
| 18 |
#include <cassert> |
| 19 |
|
| 20 |
#include "test_macros.h" |
| 21 |
#include "test_iterators.h" |
| 22 |
|
| 23 |
std::mt19937 randomness; |
| 24 |
|
| 25 |
template <class Iter> |
| 26 |
void |
| 27 |
test(Iter first, Iter last) |
| 28 |
{ |
| 29 |
std::pair<Iter, Iter> p = std::minmax_element(first, last); |
| 30 |
if (first != last) |
| 31 |
{ |
| 32 |
for (Iter j = first; j != last; ++j) |
| 33 |
{ |
| 34 |
assert(!(*j < *p.first)); |
| 35 |
assert(!(*p.second < *j)); |
| 36 |
} |
| 37 |
} |
| 38 |
else |
| 39 |
{ |
| 40 |
assert(p.first == last); |
| 41 |
assert(p.second == last); |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
template <class Iter> |
| 46 |
void |
| 47 |
test(int N) |
| 48 |
{ |
| 49 |
int* a = new int[N]; |
| 50 |
for (int i = 0; i < N; ++i) |
| 51 |
a[i] = i; |
| 52 |
std::shuffle(a, a+N, randomness); |
| 53 |
test(Iter(a), Iter(a+N)); |
| 54 |
delete [] a; |
| 55 |
} |
| 56 |
|
| 57 |
template <class Iter> |
| 58 |
void |
| 59 |
test() |
| 60 |
{ |
| 61 |
test<Iter>(0); |
| 62 |
test<Iter>(1); |
| 63 |
test<Iter>(2); |
| 64 |
test<Iter>(3); |
| 65 |
test<Iter>(10); |
| 66 |
test<Iter>(1000); |
| 67 |
{ |
| 68 |
const int N = 100; |
| 69 |
int* a = new int[N]; |
| 70 |
for (int i = 0; i < N; ++i) |
| 71 |
a[i] = 5; |
| 72 |
std::shuffle(a, a+N, randomness); |
| 73 |
std::pair<Iter, Iter> p = std::minmax_element(Iter(a), Iter(a+N)); |
| 74 |
assert(base(p.first) == a); |
| 75 |
assert(base(p.second) == a+N-1); |
| 76 |
delete [] a; |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
#if TEST_STD_VER >= 14 |
| 81 |
constexpr int il[] = { 2, 4, 6, 8, 7, 5, 3, 1 }; |
| 82 |
#endif |
| 83 |
|
| 84 |
void constexpr_test() |
| 85 |
{ |
| 86 |
#if TEST_STD_VER >= 14 |
| 87 |
constexpr auto p = std::minmax_element(il, il+8); |
| 88 |
static_assert ( *(p.first) == 1, "" ); |
| 89 |
static_assert ( *(p.second) == 8, "" ); |
| 90 |
#endif |
| 91 |
} |
| 92 |
|
| 93 |
int main(int, char**) |
| 94 |
{ |
| 95 |
test<forward_iterator<const int*> >(); |
| 96 |
test<bidirectional_iterator<const int*> >(); |
| 97 |
test<random_access_iterator<const int*> >(); |
| 98 |
test<const int*>(); |
| 99 |
|
| 100 |
constexpr_test(); |
| 101 |
|
| 102 |
return 0; |
| 103 |
} |