| 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 | // Iter |
| 14 | // max_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 | Iter i = std::max_element(first, last); |
| 30 | if (first != last) |
| 31 | { |
| 32 | for (Iter j = first; j != last; ++j) |
| 33 | assert(!(*i < *j)); |
| 34 | } |
| 35 | else |
| 36 | assert(i == last); |
| 37 | } |
| 38 | |
| 39 | template <class Iter> |
| 40 | void |
| 41 | test(int N) |
| 42 | { |
| 43 | int* a = new int[N]; |
| 44 | for (int i = 0; i < N; ++i) |
| 45 | a[i] = i; |
| 46 | std::shuffle(first: a, last: a+N, g&: randomness); |
| 47 | test(Iter(a), Iter(a+N)); |
| 48 | delete [] a; |
| 49 | } |
| 50 | |
| 51 | template <class Iter> |
| 52 | void |
| 53 | test() |
| 54 | { |
| 55 | test<Iter>(0); |
| 56 | test<Iter>(1); |
| 57 | test<Iter>(2); |
| 58 | test<Iter>(3); |
| 59 | test<Iter>(10); |
| 60 | test<Iter>(1000); |
| 61 | } |
| 62 | |
| 63 | #if TEST_STD_VER >= 14 |
| 64 | constexpr int il[] = { 2, 4, 6, 8, 7, 5, 3, 1 }; |
| 65 | #endif |
| 66 | |
| 67 | void constexpr_test() |
| 68 | { |
| 69 | #if TEST_STD_VER >= 14 |
| 70 | constexpr auto p = std::max_element(il,il+8); |
| 71 | static_assert ( *p == 8, "" ); |
| 72 | #endif |
| 73 | } |
| 74 | |
| 75 | int main(int, char**) |
| 76 | { |
| 77 | test<forward_iterator<const int*> >(); |
| 78 | test<bidirectional_iterator<const int*> >(); |
| 79 | test<random_access_iterator<const int*> >(); |
| 80 | test<const int*>(); |
| 81 | |
| 82 | constexpr_test (); |
| 83 | |
| 84 | return 0; |
| 85 | } |
| 86 | |