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, StrictWeakOrder<auto, Iter::value_type> Compare>
13 // requires CopyConstructible<Compare>
14 // Iter
15 // max_element(Iter first, Iter last, Compare comp);
16
17 #include <algorithm>
18 #include <functional>
19 #include <cassert>
20
21 #include "test_iterators.h"
22
23 template <class Iter>
24 void
test(Iter first,Iter last)25 test(Iter first, Iter last)
26 {
27 Iter i = std::max_element(first, last, std::greater<int>());
28 if (first != last)
29 {
30 for (Iter j = first; j != last; ++j)
31 assert(!std::greater<int>()(*i, *j));
32 }
33 else
34 assert(i == last);
35 }
36
37 template <class Iter>
38 void
test(unsigned N)39 test(unsigned N)
40 {
41 int* a = new int[N];
42 for (int i = 0; i < N; ++i)
43 a[i] = i;
44 std::random_shuffle(a, a+N);
45 test(Iter(a), Iter(a+N));
46 delete [] a;
47 }
48
49 template <class Iter>
50 void
test()51 test()
52 {
53 test<Iter>(0);
54 test<Iter>(1);
55 test<Iter>(2);
56 test<Iter>(3);
57 test<Iter>(10);
58 test<Iter>(1000);
59 }
60
61 template <class Iter, class Pred>
test_eq0(Iter first,Iter last,Pred p)62 void test_eq0(Iter first, Iter last, Pred p)
63 {
64 assert(first == std::max_element(first, last, p));
65 }
66
test_eq()67 void test_eq()
68 {
69 const size_t N = 10;
70 int* a = new int[N];
71 for (int i = 0; i < N; ++i)
72 a[i] = 10; // all the same
73 test_eq0(a, a+N, std::less<int>());
74 test_eq0(a, a+N, std::greater<int>());
75 delete [] a;
76 }
77
main()78 int main()
79 {
80 test<forward_iterator<const int*> >();
81 test<bidirectional_iterator<const int*> >();
82 test<random_access_iterator<const int*> >();
83 test<const int*>();
84 test_eq();
85 }
86