]>
Commit | Line | Data |
---|---|---|
02d92e3b SW |
1 | // Copyright (C) 2001 Free Software Foundation, Inc. |
2 | // | |
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 2, or (at your option) | |
7 | // any later version. | |
8 | ||
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. | |
13 | ||
14 | // You should have received a copy of the GNU General Public License along | |
15 | // with this library; see the file COPYING. If not, write to the Free | |
83f51799 | 16 | // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, |
02d92e3b SW |
17 | // USA. |
18 | ||
19 | // 25.3.1 algorithms, sort() | |
20 | ||
21 | #include <algorithm> | |
fe413112 | 22 | #include <testsuite_hooks.h> |
02d92e3b | 23 | |
11f10e6b | 24 | bool test __attribute__((unused)) = true; |
02d92e3b SW |
25 | |
26 | const int A[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}; | |
27 | const int B[] = {10, 20, 1, 11, 2, 12, 3, 13, 4, 14, 5, 15, 6, 16, 7, 17, 8, 18, 9, 19}; | |
28 | const int C[] = {20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1}; | |
29 | const int N = sizeof(A) / sizeof(int); | |
30 | const int logN = 3; // ln(N) rounded up | |
31 | const int P = 7; | |
32 | ||
33 | // comparison predicate for stable_sort: order by rightmost digit | |
34 | struct CompLast | |
35 | { | |
0098d806 BK |
36 | bool |
37 | operator()(const int x, const int y) | |
38 | { return x % 10 < y % 10; } | |
02d92e3b SW |
39 | }; |
40 | ||
41 | // This functor has the equivalent functionality of std::geater<>, | |
42 | // but there is no dependency on <functional> and it also tracks the | |
43 | // number of invocations since creation. | |
44 | class Gt | |
45 | { | |
46 | public: | |
0098d806 BK |
47 | static int count() { return itsCount; } |
48 | static void reset() { itsCount = 0; } | |
49 | ||
50 | bool | |
51 | operator()(const int& x, const int& y) | |
52 | { | |
53 | ++itsCount; | |
54 | return x > y; | |
55 | } | |
02d92e3b SW |
56 | |
57 | private: | |
58 | static int itsCount; | |
59 | }; | |
60 | ||
61 | int Gt::itsCount = 0; | |
62 | ||
63 | ||
02d92e3b SW |
64 | // 25.3.1.4 partial_sort_copy() |
65 | void | |
66 | test04() | |
67 | { | |
68 | using std::partial_sort_copy; | |
69 | ||
70 | int s1[N]; | |
71 | std::copy(B, B + N, s1); | |
72 | VERIFY(std::equal(s1, s1 + N, B)); | |
73 | ||
74 | int s2[2*N]; | |
75 | ||
76 | partial_sort_copy(s1, s1 + N, s2, s2 + P); | |
77 | VERIFY(std::equal(s2, s2 + P, A)); | |
78 | ||
79 | Gt gt; | |
80 | gt.reset(); | |
81 | partial_sort_copy(s1, s1 + N, s2, s2 + P, gt); | |
82 | VERIFY(std::equal(s2, s2 + P, C)); | |
83 | ||
84 | VERIFY(std::equal(s2, partial_sort_copy(s1, s1 + N, s2, s2 + 2*N), A)); | |
85 | } | |
86 | ||
02d92e3b | 87 | int |
11f10e6b | 88 | main() |
02d92e3b | 89 | { |
11f10e6b | 90 | test04(); |
11f10e6b | 91 | return 0; |
02d92e3b | 92 | } |