minmax_element.pass.cpp (2175B)
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> 13 // requires LessThanComparable<Iter::value_type> 14 // pair<Iter, Iter> 15 // minmax_element(Iter first, Iter last); 16 17 #include <algorithm> 18 #include <random> 19 #include <cassert> 20 21 #include "test_iterators.h" 22 23 std::mt19937 randomness; 24 25 template <class Iter> 26 void 27 test(Iter first, Iter last) 28 { 29 std::pair<Iter, Iter> p = std::minmax_element(first, last); 30 if (first != last) 31 { 32 for (Iter j = first; j != last; ++j) 33 { 34 assert(!(*j < *p.first)); 35 assert(!(*p.second < *j)); 36 } 37 } 38 else 39 { 40 assert(p.first == last); 41 assert(p.second == last); 42 } 43 } 44 45 template <class Iter> 46 void 47 test(int N) 48 { 49 int* a = new int[N]; 50 for (int i = 0; i < N; ++i) 51 a[i] = i; 52 std::shuffle(a, a+N, randomness); 53 test(Iter(a), Iter(a+N)); 54 delete [] a; 55 } 56 57 template <class Iter> 58 void 59 test() 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 int N = 100; 69 int* a = new int[N]; 70 for (int i = 0; i < N; ++i) 71 a[i] = 5; 72 std::shuffle(a, a+N, randomness); 73 std::pair<Iter, Iter> p = std::minmax_element(Iter(a), Iter(a+N)); 74 assert(base(p.first) == a); 75 assert(base(p.second) == a+N-1); 76 delete [] a; 77 } 78 } 79 80 #if TEST_STD_VER >= 14 81 constexpr int il[] = { 2, 4, 6, 8, 7, 5, 3, 1 }; 82 #endif 83 84 void constexpr_test() 85 { 86 #if TEST_STD_VER >= 14 87 constexpr auto p = std::minmax_element(il, il+8); 88 static_assert ( *(p.first) == 1, "" ); 89 static_assert ( *(p.second) == 8, "" ); 90 #endif 91 } 92 93 int main() 94 { 95 test<forward_iterator<const int*> >(); 96 test<bidirectional_iterator<const int*> >(); 97 test<random_access_iterator<const int*> >(); 98 test<const int*>(); 99 100 constexpr_test(); 101 }