1 // Copyright (C) 2020-2025 Free Software Foundation, Inc.
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)
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.
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/>.
18 // { dg-do run { target c++20 } }
22 #include <testsuite_hooks.h>
23 #include <testsuite_iterators.h>
25 using __gnu_test::test_container
;
26 using __gnu_test::test_range
;
27 using __gnu_test::forward_iterator_wrapper
;
29 namespace ranges
= std::ranges
;
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
51 VERIFY( *ranges::minmax_element(cx
, ranges::greater
{}, std::negate
<>{}).max
53 } while (ranges::next_permutation(x
).found
);
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() );
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);
67 // Verify we perform at most 3*N/2 applications of the comparison predicate.
70 { bool operator()(int a
, int b
) { ++counter
; return a
< b
; } };
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 );
77 ranges::minmax_element(x
, x
+3, counted_less
{});
78 VERIFY( counter
== 3 );
81 ranges::minmax_element(x
, counted_less
{});
82 VERIFY( counter
<= 15 );
86 ranges::minmax_element(x
, counted_less
{});
87 VERIFY( counter
<= 15 );