]>
Commit | Line | Data |
---|---|---|
52066eae | 1 | // { dg-do run { target c++11 } } |
f5783e34 | 2 | |
99dee823 | 3 | // Copyright (C) 2005-2021 Free Software Foundation, Inc. |
f5783e34 CJ |
4 | // |
5 | // This file is part of the GNU ISO C++ Library. This library is free | |
6 | // software; you can redistribute it and/or modify it under the | |
7 | // terms of the GNU General Public License as published by the | |
748086b7 | 8 | // Free Software Foundation; either version 3, or (at your option) |
f5783e34 CJ |
9 | // any later version. |
10 | ||
11 | // This library is distributed in the hope that it will be useful, | |
12 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | // GNU General Public License for more details. | |
15 | ||
16 | // You should have received a copy of the GNU General Public License along | |
748086b7 JJ |
17 | // with this library; see the file COPYING3. If not see |
18 | // <http://www.gnu.org/licenses/>. | |
f5783e34 CJ |
19 | |
20 | // 25.3.2 [lib.alg.nth.element] | |
21 | ||
22 | #undef _GLIBCXX_CONCEPT_CHECKS | |
23 | ||
932f6f4a PC |
24 | // XXX FIXME: parallel-mode should deal correctly with moveable-only types |
25 | // per C++0x, at minimum smoothly fall back to serial. | |
26 | #undef _GLIBCXX_PARALLEL | |
27 | ||
f5783e34 CJ |
28 | #include <algorithm> |
29 | #include <testsuite_hooks.h> | |
30 | #include <testsuite_iterators.h> | |
31 | #include <testsuite_rvalref.h> | |
32 | ||
33 | using __gnu_test::test_container; | |
34 | using __gnu_test::random_access_iterator_wrapper; | |
35 | using std::nth_element; | |
36 | using __gnu_test::rvalstruct; | |
37 | ||
38 | typedef test_container<rvalstruct, random_access_iterator_wrapper> Container; | |
39 | ||
40 | void | |
41 | test1() | |
42 | { | |
43 | int intarray[] = {6, 5, 4, 3, 2, 1, 0}; | |
44 | rvalstruct array[7]; | |
45 | std::copy(intarray, intarray + 7, array); | |
46 | Container con(array, array + 7); | |
47 | nth_element(con.begin(), con.it(3), con.end()); | |
48 | for(int i = 0; i < 3; ++i) | |
01bbe151 | 49 | VERIFY( array[i].val < 3 ); |
f5783e34 | 50 | for(int i = 4; i < 7; ++i) |
01bbe151 | 51 | VERIFY( array[i].val > 3 ); |
f5783e34 | 52 | for(int i = 0; i < 7; ++i) |
01bbe151 | 53 | VERIFY( array[i].valid ); |
f5783e34 CJ |
54 | } |
55 | ||
56 | void | |
57 | test2() | |
58 | { | |
59 | int intarray[] = {0, 6, 1, 5, 2, 4, 3}; | |
60 | rvalstruct array[7]; | |
61 | std::copy(intarray, intarray + 7, array); | |
62 | Container con(array,array + 7); | |
63 | nth_element(con.begin(), con.it(3), con.end()); | |
64 | for(int i = 0; i < 3; ++i) | |
01bbe151 CJ |
65 | VERIFY( array[i].val < 3 ); |
66 | for(int i = 4; i < 7; ++i) | |
67 | VERIFY( array[i].val > 3 ); | |
68 | for(int i = 0; i < 7; ++i) | |
69 | VERIFY( array[i].valid ); | |
70 | } | |
71 | ||
72 | bool | |
73 | are_less(const rvalstruct& lhs, const rvalstruct& rhs) | |
74 | { return lhs < rhs; } | |
75 | ||
76 | void | |
77 | test3() | |
78 | { | |
01bbe151 CJ |
79 | int intarray[] = {0, 6, 1, 5, 2, 4, 3}; |
80 | rvalstruct array[7]; | |
81 | std::copy(intarray, intarray + 7, array); | |
82 | Container con(array,array + 7); | |
83 | nth_element(con.begin(), con.it(3), con.end(), are_less); | |
84 | for(int i = 0; i < 3; ++i) | |
85 | VERIFY( array[i].val < 3 ); | |
f5783e34 | 86 | for(int i = 4; i < 7; ++i) |
01bbe151 | 87 | VERIFY( array[i].val > 3 ); |
f5783e34 | 88 | for(int i = 0; i < 7; ++i) |
01bbe151 | 89 | VERIFY( array[i].valid ); |
f5783e34 CJ |
90 | } |
91 | ||
92 | int | |
93 | main() | |
94 | { | |
95 | test1(); | |
96 | test2(); | |
01bbe151 | 97 | test3(); |
f5783e34 CJ |
98 | return 0; |
99 | } |