| 1 |
// Copyright (C) 2020-2025 Free Software Foundation, Inc. |
| 2 |
// |
| 3 |
// This file is part of the GNU ISO C++ Library. This library is free |
| 4 |
// software; you can redistribute it and/or modify it under the |
| 5 |
// terms of the GNU General Public License as published by the |
| 6 |
// Free Software Foundation; either version 3, or (at your option) |
| 7 |
// any later version. |
| 8 |
|
| 9 |
// This library is distributed in the hope that it will be useful, |
| 10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
// GNU General Public License for more details. |
| 13 |
|
| 14 |
// You should have received a copy of the GNU General Public License along |
| 15 |
// with this library; see the file COPYING3. If not see |
| 16 |
// <http://www.gnu.org/licenses/>. |
| 17 |
|
| 18 |
// { dg-do run { target c++20 } } |
| 19 |
|
| 20 |
#include <algorithm> |
| 21 |
#include <functional> |
| 22 |
#include <testsuite_hooks.h> |
| 23 |
#include <testsuite_iterators.h> |
| 24 |
|
| 25 |
using __gnu_test::test_container; |
| 26 |
using __gnu_test::test_range; |
| 27 |
using __gnu_test::forward_iterator_wrapper; |
| 28 |
|
| 29 |
namespace ranges = std::ranges; |
| 30 |
|
| 31 |
struct X |
| 32 |
{ |
| 33 |
int i, j; |
| 34 |
}; |
| 35 |
|
| 36 |
void |
| 37 |
test01() |
| 38 |
{ |
| 39 |
int x[] = {1,2,3,4}; |
| 40 |
do |
| 41 |
{ |
| 42 |
test_range<int, forward_iterator_wrapper> cx(x); |
| 43 |
VERIFY( *ranges::minmax_element(cx).min == 1 ); |
| 44 |
VERIFY( *ranges::minmax_element(cx).max == 4 ); |
| 45 |
VERIFY( *ranges::minmax_element(cx, ranges::greater{}).min == 4 ); |
| 46 |
VERIFY( *ranges::minmax_element(cx, ranges::greater{}).max == 1 ); |
| 47 |
VERIFY( *ranges::minmax_element(cx, {}, std::negate<>{}).min == 4); |
| 48 |
VERIFY( *ranges::minmax_element(cx, {}, std::negate<>{}).max == 1); |
| 49 |
VERIFY( *ranges::minmax_element(cx, ranges::greater{}, std::negate<>{}).min |
| 50 |
== 1 ); |
| 51 |
VERIFY( *ranges::minmax_element(cx, ranges::greater{}, std::negate<>{}).max |
| 52 |
== 4 ); |
| 53 |
} while (ranges::next_permutation(x).found); |
| 54 |
|
| 55 |
test_container<int, forward_iterator_wrapper> cx(x); |
| 56 |
VERIFY( ranges::minmax_element(cx.begin(), cx.begin()).min == cx.begin() ); |
| 57 |
VERIFY( ranges::minmax_element(cx.begin(), cx.begin()).max == cx.begin() ); |
| 58 |
|
| 59 |
constexpr X y[] = {{1,5},{1,2},{1,3}}; |
| 60 |
static_assert(ranges::minmax_element(y, y+3, {}, &X::i).min->j == 5); |
| 61 |
static_assert(ranges::minmax_element(y, y+3, {}, &X::i).max->j == 3); |
| 62 |
} |
| 63 |
|
| 64 |
void |
| 65 |
test02() |
| 66 |
{ |
| 67 |
// Verify we perform at most 3*N/2 applications of the comparison predicate. |
| 68 |
static int counter; |
| 69 |
struct counted_less |
| 70 |
{ bool operator()(int a, int b) { ++counter; return a < b; } }; |
| 71 |
|
| 72 |
int x[] = {1,2,3,4,5,6,7,8,9,10}; |
| 73 |
ranges::minmax_element(x, x+2, counted_less{}); |
| 74 |
VERIFY( counter == 1 ); |
| 75 |
|
| 76 |
counter = 0; |
| 77 |
ranges::minmax_element(x, x+3, counted_less{}); |
| 78 |
VERIFY( counter == 3 ); |
| 79 |
|
| 80 |
counter = 0; |
| 81 |
ranges::minmax_element(x, counted_less{}); |
| 82 |
VERIFY( counter <= 15 ); |
| 83 |
|
| 84 |
ranges::reverse(x); |
| 85 |
counter = 0; |
| 86 |
ranges::minmax_element(x, counted_less{}); |
| 87 |
VERIFY( counter <= 15 ); |
| 88 |
} |
| 89 |
|
| 90 |
int |
| 91 |
main() |
| 92 |
{ |
| 93 |
test01(); |
| 94 |
test02(); |
| 95 |
} |