1 //===----------------------------------------------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 // <algorithm>
11
12 // template<ForwardIterator Iter>
13 // requires LessThanComparable<Iter::value_type>
14 // Iter
15 // max_element(Iter first, Iter last);
16
17 #include <algorithm>
18 #include <cassert>
19
20 #include "test_iterators.h"
21
22 template <class Iter>
23 void
test(Iter first,Iter last)24 test(Iter first, Iter last)
25 {
26 Iter i = std::max_element(first, last);
27 if (first != last)
28 {
29 for (Iter j = first; j != last; ++j)
30 assert(!(*i < *j));
31 }
32 else
33 assert(i == last);
34 }
35
36 template <class Iter>
37 void
test(unsigned N)38 test(unsigned N)
39 {
40 int* a = new int[N];
41 for (int i = 0; i < N; ++i)
42 a[i] = i;
43 std::random_shuffle(a, a+N);
44 test(Iter(a), Iter(a+N));
45 delete [] a;
46 }
47
48 template <class Iter>
49 void
test()50 test()
51 {
52 test<Iter>(0);
53 test<Iter>(1);
54 test<Iter>(2);
55 test<Iter>(3);
56 test<Iter>(10);
57 test<Iter>(1000);
58 }
59
60 #if __cplusplus >= 201402L
61 constexpr int il[] = { 2, 4, 6, 8, 7, 5, 3, 1 };
62 #endif
63
constexpr_test()64 void constexpr_test()
65 {
66 #if __cplusplus >= 201402L
67 constexpr auto p = std::max_element(il,il+8);
68 static_assert ( *p == 8, "" );
69 #endif
70 }
71
main()72 int main()
73 {
74 test<forward_iterator<const int*> >();
75 test<bidirectional_iterator<const int*> >();
76 test<random_access_iterator<const int*> >();
77 test<const int*>();
78
79 constexpr_test ();
80 }
81