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