| 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 <string> |
| 22 |
#include <utility> |
| 23 |
#include <vector> |
| 24 |
#include <testsuite_hooks.h> |
| 25 |
#include <testsuite_iterators.h> |
| 26 |
|
| 27 |
using __gnu_test::test_container; |
| 28 |
using __gnu_test::test_range; |
| 29 |
using __gnu_test::input_iterator_wrapper; |
| 30 |
|
| 31 |
namespace ranges = std::ranges; |
| 32 |
|
| 33 |
template<typename T1, typename T2> |
| 34 |
constexpr bool |
| 35 |
operator==(const ranges::minmax_result<T1>& lhs, |
| 36 |
const ranges::minmax_result<T2>& rhs) |
| 37 |
{ |
| 38 |
return (lhs.min == rhs.min |
| 39 |
&& rhs.max == rhs.max); |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
struct X |
| 44 |
{ |
| 45 |
int i, j; |
| 46 |
}; |
| 47 |
|
| 48 |
using res_t = ranges::minmax_result<int>; |
| 49 |
|
| 50 |
void |
| 51 |
test01() |
| 52 |
{ |
| 53 |
VERIFY( ranges::minmax(1, 2) == res_t(1,2) ); |
| 54 |
VERIFY( ranges::minmax(2, 1) == res_t(1,2) ); |
| 55 |
VERIFY( ranges::minmax(1, 2, ranges::greater{}) == res_t(2,1) ); |
| 56 |
VERIFY( ranges::minmax(1, 2, ranges::greater{}, std::negate<>{}) == res_t(1,2) ); |
| 57 |
VERIFY( ranges::minmax(1, 2, {}, std::negate<>{}) == res_t(2,1) ); |
| 58 |
VERIFY( ranges::minmax(X{1,2}, X{1,3}, {}, &X::i).min.j == 2 ); |
| 59 |
VERIFY( ranges::minmax(X{1,2}, X{1,3}, {}, &X::i).max.j == 3 ); |
| 60 |
} |
| 61 |
|
| 62 |
void |
| 63 |
test02() |
| 64 |
{ |
| 65 |
int x[] = {1,2,3,4}; |
| 66 |
do |
| 67 |
{ |
| 68 |
test_range<int, input_iterator_wrapper> cx(x); |
| 69 |
VERIFY( ranges::minmax(cx) == res_t(1,4) ); |
| 70 |
cx.bounds.first = x; |
| 71 |
VERIFY( ranges::minmax(cx, ranges::greater{}) == res_t(4,1) ); |
| 72 |
cx.bounds.first = x; |
| 73 |
VERIFY( ranges::minmax(cx, {}, std::negate<>{}) == res_t(4,1)); |
| 74 |
cx.bounds.first = x; |
| 75 |
VERIFY( ranges::minmax(cx, ranges::greater{}, std::negate<>{}) |
| 76 |
== res_t(1,4) ); |
| 77 |
} while (ranges::next_permutation(x).found); |
| 78 |
|
| 79 |
constexpr X y[] = {{1,5},{1,2},{1,3}}; |
| 80 |
static_assert(ranges::minmax(y, {}, &X::i).min.j == 5); |
| 81 |
static_assert(ranges::minmax(y, {}, &X::i).max.j == 3); |
| 82 |
} |
| 83 |
|
| 84 |
void |
| 85 |
test03() |
| 86 |
{ |
| 87 |
VERIFY( ranges::minmax({2,3,1,4}) == res_t(1,4) ); |
| 88 |
VERIFY( ranges::minmax({2,3,1,4}, ranges::greater{}) == res_t(4,1) ); |
| 89 |
VERIFY( ranges::minmax({2,3,1,4}, {}, std::negate<>{}) == res_t(4,1) ); |
| 90 |
VERIFY( ranges::minmax({2,3,1,4}, ranges::greater{}, std::negate<>{}) |
| 91 |
== res_t(1,4) ); |
| 92 |
} |
| 93 |
|
| 94 |
void |
| 95 |
test04() |
| 96 |
{ |
| 97 |
// Verify we perform at most 3*N/2 applications of the comparison predicate. |
| 98 |
static int counter; |
| 99 |
struct counted_less |
| 100 |
{ bool operator()(int a, int b) { ++counter; return a < b; } }; |
| 101 |
|
| 102 |
ranges::minmax({1,2}, counted_less{}); |
| 103 |
VERIFY( counter == 1 ); |
| 104 |
|
| 105 |
counter = 0; |
| 106 |
ranges::minmax({1,2,3}, counted_less{}); |
| 107 |
VERIFY( counter == 3 ); |
| 108 |
|
| 109 |
counter = 0; |
| 110 |
ranges::minmax({1,2,3,4,5,6,7,8,9,10}, counted_less{}); |
| 111 |
VERIFY( counter <= 15 ); |
| 112 |
|
| 113 |
counter = 0; |
| 114 |
ranges::minmax({10,9,8,7,6,5,4,3,2,1}, counted_less{}); |
| 115 |
VERIFY( counter <= 15 ); |
| 116 |
} |
| 117 |
|
| 118 |
void |
| 119 |
test05() |
| 120 |
{ |
| 121 |
// PR libstdc++/100387 |
| 122 |
using namespace std::literals::string_literals; |
| 123 |
auto comp = [](const auto& a, const auto& b) { |
| 124 |
return a.size() == b.size() ? a.front() < b.front() : a.size() > b.size(); |
| 125 |
}; |
| 126 |
auto result = ranges::minmax({"b"s, "a"s}, comp); |
| 127 |
VERIFY( result.min == "a"s && result.max == "b"s ); |
| 128 |
result = ranges::minmax({"c"s, "b"s, "a"s}, comp); |
| 129 |
VERIFY( result.min == "a"s && result.max == "c"s ); |
| 130 |
} |
| 131 |
|
| 132 |
struct A { |
| 133 |
A() = delete; |
| 134 |
A(int i) : i(i) { } |
| 135 |
A(const A&) = default; |
| 136 |
A(A&& other) : A(std::as_const(other)) { ++move_count; } |
| 137 |
A& operator=(const A&) = default; |
| 138 |
A& operator=(A&& other) { |
| 139 |
++move_count; |
| 140 |
return *this = std::as_const(other); |
| 141 |
}; |
| 142 |
friend auto operator<=>(const A&, const A&) = default; |
| 143 |
static inline int move_count = 0; |
| 144 |
int i; |
| 145 |
}; |
| 146 |
|
| 147 |
void |
| 148 |
test06() |
| 149 |
{ |
| 150 |
// PR libstdc++/104858 |
| 151 |
// Verify ranges::minmax doesn't dereference the iterator for the first |
| 152 |
// element in the range twice. |
| 153 |
A a(42); |
| 154 |
ranges::subrange r = {std::move_iterator(&a), std::move_sentinel(&a + 1)}; |
| 155 |
auto result = ranges::minmax(r); |
| 156 |
VERIFY( A::move_count == 1 ); |
| 157 |
VERIFY( result.min.i == 42 && result.max.i == 42 ); |
| 158 |
} |
| 159 |
|
| 160 |
int |
| 161 |
main() |
| 162 |
{ |
| 163 |
test01(); |
| 164 |
test02(); |
| 165 |
test03(); |
| 166 |
test04(); |
| 167 |
test05(); |
| 168 |
test06(); |
| 169 |
} |