1 // Copyright (C) 2020-2022 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-options "-std=gnu++2a" }
19 // { dg-do run { target c++2a } }
20 // { dg-require-cstdint "" }
24 #include <testsuite_hooks.h>
25 #include <testsuite_iterators.h>
27 using __gnu_test::test_container
;
28 using __gnu_test::test_range
;
29 using __gnu_test::random_access_iterator_wrapper
;
31 namespace ranges
= std::ranges
;
37 std::iota(x
, x
+50, 0);
39 auto pred
= std::greater
{};
40 auto proj
= [] (int a
) { return -a
; };
41 for (int i
= 0; i
< 50; i
++)
43 test_range
<int, random_access_iterator_wrapper
> rx(x
);
44 std::ranlux48_base
g(i
);
45 ranges::shuffle(rx
, g
);
47 auto result
= ranges::nth_element(rx
, rx
.begin()+i
, pred
, proj
);
48 VERIFY( result
== rx
.end() );
50 for (int j
= 0; j
< i
; j
++)
51 for (int k
= i
; k
< 50; k
++)
52 VERIFY( !pred(proj(x
[k
]), proj(x
[j
])) );
54 result
= ranges::nth_element(rx
, rx
.begin()+i
, pred
);
55 VERIFY( result
== rx
.end() );
56 VERIFY( x
[i
] == 49-i
);
57 for (int j
= 0; j
< i
; j
++)
58 for (int k
= i
; k
< 50; k
++)
59 VERIFY( !pred(x
[k
], x
[j
]) );
66 int x
[] = {5,2,1,3,4};
67 ranges::nth_element(x
, x
+3);
75 static_assert(test02());