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