1 //===----------------------------------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
11 // UNSUPPORTED: c++03, c++11, c++14, c++17
13 // template<class T, class Proj = identity,
14 // indirect_strict_weak_order<projected<const T*, Proj>> Comp = ranges::less>
15 // constexpr const T& ranges::min(const T& a, const T& b, Comp comp = {}, Proj proj = {});
16 // template<copyable T, class Proj = identity,
17 // indirect_strict_weak_order<projected<const T*, Proj>> Comp = ranges::less>
18 // constexpr T ranges::min(initializer_list<T> r, Comp comp = {}, Proj proj = {});
19 // template<input_range R, class Proj = identity,
20 // indirect_strict_weak_order<projected<iterator_t<R>, Proj>> Comp = ranges::less>
21 // requires indirectly_copyable_storable<iterator_t<R>, range_value_t<R>*>
22 // constexpr range_value_t<R>
23 // ranges::min(R&& r, Comp comp = {}, Proj proj = {});
31 #include "almost_satisfies_types.h"
32 #include "test_iterators.h"
33 #include "test_macros.h"
36 concept HasMinR
= requires
{ std::ranges::min(std::declval
<T
>()); };
38 struct NoLessThanOp
{};
39 struct NotTotallyOrdered
{
41 bool operator<(const NotTotallyOrdered
& o
) const { return i
< o
.i
; }
45 Movable
& operator=(Movable
&&) = default;
46 Movable(Movable
&&) = default;
47 Movable(const Movable
&) = delete;
50 static_assert(!HasMinR
<int>);
52 static_assert(HasMinR
<int(&)[10]>);
53 static_assert(HasMinR
<int(&&)[10]>);
54 static_assert(!HasMinR
<NoLessThanOp(&)[10]>);
55 static_assert(!HasMinR
<NotTotallyOrdered(&)[10]>);
56 static_assert(!HasMinR
<Movable(&)[10]>);
58 static_assert(HasMinR
<std::initializer_list
<int>>);
59 static_assert(!HasMinR
<std::initializer_list
<NoLessThanOp
>>);
60 static_assert(!HasMinR
<std::initializer_list
<NotTotallyOrdered
>>);
61 static_assert(!HasMinR
<std::initializer_list
<Movable
>>);
62 static_assert(!HasMinR
<InputRangeNotDerivedFrom
>);
63 static_assert(!HasMinR
<InputRangeNotIndirectlyReadable
>);
64 static_assert(!HasMinR
<InputRangeNotInputOrOutputIterator
>);
65 static_assert(!HasMinR
<InputRangeNotSentinelSemiregular
>);
66 static_assert(!HasMinR
<InputRangeNotSentinelEqualityComparableWith
>);
68 template <class T
, class U
= T
>
69 concept HasMin2
= requires
{ std::ranges::min(std::declval
<T
>(), std::declval
<U
>()); };
71 static_assert(HasMin2
<int>);
72 static_assert(!HasMin2
<int, long>);
74 static_assert(std::is_same_v
<decltype(std::ranges::min(1, 2)), const int&>);
76 constexpr void test_2_arguments() {
77 assert(std::ranges::min(1, 2) == 1);
78 assert(std::ranges::min(2, 1) == 1);
80 assert(std::ranges::min(1, 2, std::ranges::greater
{}) == 2);
82 assert(std::ranges::min(1, 2, std::ranges::less
{}, [](int i
){ return i
== 1 ? 10 : i
; }) == 2);
84 { // check that std::invoke is used
86 S a
[3] = { S
{2}, S
{1}, S
{3} };
87 decltype(auto) ret
= std::ranges::min(a
[0], a
[1], {}, &S::i
);
88 ASSERT_SAME_TYPE(decltype(ret
), const S
&);
89 assert(&ret
== &a
[1]);
93 { // check that pointers are compared and not a range
95 int* a
[] = {&i
, &i
+ 1};
96 auto ret
= std::ranges::min(a
[0], a
[1]);
100 { // test predicate and projection count
103 auto comparator
= [&](int x
, int y
) {
107 auto projection
= [&](int x
) {
111 auto ret
= std::ranges::min(1, 2, comparator
, projection
);
113 assert(compares
== 1);
114 assert(projections
== 2);
117 { // check that the first argument is returned
118 struct S
{ int check
; int other
; };
119 auto ret
= std::ranges::min(S
{0, 1}, S
{0, 2}, {}, &S::check
);
120 assert(ret
.other
== 1);
124 constexpr void test_initializer_list() {
127 auto proj
= [](int i
) { return i
== 5 ? -100 : i
; };
128 int ret
= std::ranges::min({7, 6, 9, 3, 5, 1, 2, 4}, std::ranges::less
{}, proj
);
134 int ret
= std::ranges::min({7, 6, 9, 3, 5, 1, 2, 4}, std::ranges::greater
{});
141 auto comparator
= [&](int a
, int b
) {
145 auto projection
= [&](int a
) {
149 std::same_as
<int> decltype(auto) ret
= std::ranges::min({1, 2, 3}, comparator
, projection
);
151 assert(compares
== 2);
152 assert(projections
== 4);
157 decltype(auto) ret
= std::ranges::min({ S
{2}, S
{1}, S
{3} }, {}, &S::i
);
158 ASSERT_SAME_TYPE(decltype(ret
), S
);
163 int a
[] = {7, 6, 9, 3, 5, 1, 2, 4};
164 using It
= cpp20_input_iterator
<int*>;
165 using Sent
= sentinel_wrapper
<It
>;
166 auto range
= std::ranges::subrange(It(a
), Sent(It(a
+ 8)));
167 auto ret
= std::ranges::min(range
);
172 template <class It
, class Sent
= It
>
173 constexpr void test_range_types() {
174 std::iter_value_t
<It
> a
[] = {7, 6, 9, 3, 5, 1, 2, 4};
175 auto range
= std::ranges::subrange(It(a
), Sent(It(a
+ 8)));
176 auto ret
= std::ranges::min(range
);
180 constexpr void test_range() {
181 // check that all range types work
183 struct NonTrivialInt
{
185 constexpr NonTrivialInt(int val
) : val_(val
) {}
186 constexpr NonTrivialInt(const NonTrivialInt
& other
) : val_(other
.val_
) {}
187 constexpr NonTrivialInt
& operator=(const NonTrivialInt
& other
) {
192 constexpr ~NonTrivialInt() {}
194 auto operator<=>(const NonTrivialInt
&) const = default;
197 auto call_with_sentinels
= []<class Iter
> {
198 if constexpr (std::forward_iterator
<Iter
>)
199 test_range_types
<Iter
, Iter
>();
200 test_range_types
<Iter
, sentinel_wrapper
<Iter
>>();
201 test_range_types
<Iter
, sized_sentinel
<Iter
>>();
204 types::for_each(types::cpp20_input_iterator_list
<int*>{}, call_with_sentinels
);
205 types::for_each(types::cpp20_input_iterator_list
<NonTrivialInt
*>{}, call_with_sentinels
);
208 int a
[] = {7, 6, 9, 3, 5, 1, 2, 4};
211 auto proj
= [](int& i
) { return i
== 5 ? -100 : i
; };
212 int ret
= std::ranges::min(a
, std::ranges::less
{}, proj
);
218 int ret
= std::ranges::min(a
, std::ranges::greater
{});
222 { // check that predicate and projection call counts are correct
225 auto comparator
= [&](int x
, int y
) {
229 auto projection
= [&](int x
) {
233 std::same_as
<int> decltype(auto) ret
= std::ranges::min(std::array
{1, 2, 3}, comparator
, projection
);
235 assert(compares
== 2);
236 assert(projections
== 4);
239 { // check that std::invoke is used
241 S b
[3] = { S
{2}, S
{1}, S
{3} };
242 std::same_as
<S
> decltype(auto) ret
= std::ranges::min(b
, {}, &S::i
);
246 { // check that the first smallest element is returned
247 { // where the first element is the smallest
248 struct S
{ int check
; int other
; };
249 S b
[] = { S
{0, 1}, S
{1, 2}, S
{0, 3} };
250 auto ret
= std::ranges::min(b
, {}, &S::check
);
251 assert(ret
.check
== 0);
252 assert(ret
.other
== 1);
254 { // where the first element isn't the smallest
255 struct S
{ int check
; int other
; };
256 S b
[] = { S
{2, 1}, S
{0, 2}, S
{0, 3} };
257 auto ret
= std::ranges::min(b
, {}, &S::check
);
258 assert(ret
.check
== 0);
259 assert(ret
.other
== 2);
264 constexpr bool test() {
266 test_initializer_list();
272 int main(int, char**) {
274 static_assert(test());