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//   pair<Iter, Iter>
15//   minmax_element(Iter first, Iter last, Compare comp);
16
17#include <algorithm>
18#include <functional>
19#include <cassert>
20
21#include "test_iterators.h"
22
23template <class Iter>
24void
25test(Iter first, Iter last)
26{
27    typedef std::greater<int> Compare;
28    Compare comp;
29    std::pair<Iter, Iter> p = std::minmax_element(first, last, comp);
30    if (first != last)
31    {
32        for (Iter j = first; j != last; ++j)
33        {
34            assert(!comp(*j, *p.first));
35            assert(!comp(*p.second, *j));
36        }
37    }
38    else
39    {
40        assert(p.first == last);
41        assert(p.second == last);
42    }
43}
44
45template <class Iter>
46void
47test(unsigned N)
48{
49    int* a = new int[N];
50    for (int i = 0; i < N; ++i)
51        a[i] = i;
52    std::random_shuffle(a, a+N);
53    test(Iter(a), Iter(a+N));
54    delete [] a;
55}
56
57template <class Iter>
58void
59test()
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 unsigned N = 100;
69    int* a = new int[N];
70    for (int i = 0; i < N; ++i)
71        a[i] = 5;
72    std::random_shuffle(a, a+N);
73    typedef std::greater<int> Compare;
74    Compare comp;
75    std::pair<Iter, Iter> p = std::minmax_element(Iter(a), Iter(a+N), comp);
76    assert(base(p.first) == a);
77    assert(base(p.second) == a+N-1);
78    delete [] a;
79    }
80}
81
82int main()
83{
84    test<forward_iterator<const int*> >();
85    test<bidirectional_iterator<const int*> >();
86    test<random_access_iterator<const int*> >();
87    test<const int*>();
88}
89