//===----------------------------------------------------------------------===// // // The LLVM Compiler Infrastructure // // This file is dual licensed under the MIT and the University of Illinois Open // Source Licenses. See LICENSE.TXT for details. // //===----------------------------------------------------------------------===// // // template Compare> // requires CopyConstructible // Iter // max_element(Iter first, Iter last, Compare comp); #include #include #include #include "test_iterators.h" template void test(Iter first, Iter last) { Iter i = std::max_element(first, last, std::greater()); if (first != last) { for (Iter j = first; j != last; ++j) assert(!std::greater()(*i, *j)); } else assert(i == last); } template void test(unsigned N) { int* a = new int[N]; for (int i = 0; i < N; ++i) a[i] = i; std::random_shuffle(a, a+N); test(Iter(a), Iter(a+N)); delete [] a; } template void test() { test(0); test(1); test(2); test(3); test(10); test(1000); } template void test_eq0(Iter first, Iter last, Pred p) { assert(first == std::max_element(first, last, p)); } void test_eq() { const size_t N = 10; int* a = new int[N]; for (int i = 0; i < N; ++i) a[i] = 10; // all the same test_eq0(a, a+N, std::less()); test_eq0(a, a+N, std::greater()); delete [] a; } #if TEST_STD_VER >= 14 constexpr int il[] = { 2, 4, 6, 8, 7, 5, 3, 1 }; struct less { constexpr bool operator ()( const int &x, const int &y) const { return x < y; }}; #endif void constexpr_test() { #if TEST_STD_VER >= 14 constexpr auto p = std::max_element(il, il+8, less()); static_assert ( *p == 8, "" ); #endif } int main() { test >(); test >(); test >(); test(); test_eq(); constexpr_test(); }