1 // Copyright (C) 2006-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 // 25.3.2 [lib.alg.nth.element]
20 // { dg-options "-DMAX_SIZE=256" { target simulator } }
23 #define MAX_SIZE (1 << 10)
28 #include <testsuite_hooks.h>
31 test_set(std::vector
<unsigned>& v
, unsigned size
)
35 for (unsigned i
= 0; i
< size
; i
+= 4)
38 v
.push_back((size
- 2) - (i
/ 2));
40 for (unsigned i
= 1; i
< size
; i
+= 2)
45 do_test01(unsigned size
)
47 std::vector
<unsigned> v
, s
;
49 for (unsigned j
= 0; j
< size
; ++j
)
53 std::sort(s
.begin(), s
.end());
55 std::nth_element(v
.begin(), v
.begin() + j
, v
.end());
57 VERIFY( v
[j
] == s
[j
] );
59 for (unsigned i
= 0; i
< j
; ++i
)
60 VERIFY( !(v
[j
] < v
[i
]) );
62 for (unsigned i
= j
; i
< v
.size(); ++i
)
63 VERIFY( !(v
[i
] < v
[j
]) );
70 for (unsigned size
= 4; size
<= MAX_SIZE
; size
<<= 1)