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// Iter 15// max_element(Iter first, Iter last); 16 17#include <algorithm> 18#include <random> 19#include <cassert> 20 21#include "test_iterators.h" 22 23std::mt19937 randomness; 24 25template <class Iter> 26void 27test(Iter first, Iter last) 28{ 29 Iter i = std::max_element(first, last); 30 if (first != last) 31 { 32 for (Iter j = first; j != last; ++j) 33 assert(!(*i < *j)); 34 } 35 else 36 assert(i == last); 37} 38 39template <class Iter> 40void 41test(int N) 42{ 43 int* a = new int[N]; 44 for (int i = 0; i < N; ++i) 45 a[i] = i; 46 std::shuffle(a, a+N, randomness); 47 test(Iter(a), Iter(a+N)); 48 delete [] a; 49} 50 51template <class Iter> 52void 53test() 54{ 55 test<Iter>(0); 56 test<Iter>(1); 57 test<Iter>(2); 58 test<Iter>(3); 59 test<Iter>(10); 60 test<Iter>(1000); 61} 62 63#if TEST_STD_VER >= 14 64constexpr int il[] = { 2, 4, 6, 8, 7, 5, 3, 1 }; 65#endif 66 67void constexpr_test() 68{ 69#if TEST_STD_VER >= 14 70 constexpr auto p = std::max_element(il,il+8); 71 static_assert ( *p == 8, "" ); 72#endif 73} 74 75int main() 76{ 77 test<forward_iterator<const int*> >(); 78 test<bidirectional_iterator<const int*> >(); 79 test<random_access_iterator<const int*> >(); 80 test<const int*>(); 81 82 constexpr_test (); 83} 84