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 } }
19 // { dg-require-cstdint "" }
23 #include <testsuite_hooks.h>
24 #include <testsuite_iterators.h>
26 using __gnu_test::test_container
;
27 using __gnu_test::test_range
;
28 using __gnu_test::random_access_iterator_wrapper
;
30 namespace ranges
= std::ranges
;
36 std::iota(x
, x
+50, 0);
38 auto pred
= std::greater
{};
39 auto proj
= [] (int a
) { return -a
; };
40 for (int i
= 0; i
< 50; i
++)
42 test_range
<int, random_access_iterator_wrapper
> rx(x
);
43 std::ranlux48_base
g(i
);
44 ranges::shuffle(rx
, g
);
46 auto result
= ranges::nth_element(rx
, rx
.begin()+i
, pred
, proj
);
47 VERIFY( result
== rx
.end() );
49 for (int j
= 0; j
< i
; j
++)
50 for (int k
= i
; k
< 50; k
++)
51 VERIFY( !pred(proj(x
[k
]), proj(x
[j
])) );
53 result
= ranges::nth_element(rx
, rx
.begin()+i
, pred
);
54 VERIFY( result
== rx
.end() );
55 VERIFY( x
[i
] == 49-i
);
56 for (int j
= 0; j
< i
; j
++)
57 for (int k
= i
; k
< 50; k
++)
58 VERIFY( !pred(x
[k
], x
[j
]) );
65 int x
[] = {5,2,1,3,4};
66 ranges::nth_element(x
, x
+3);
74 static_assert(test02());