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