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