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