1 /*  _________________________________________________________________________
2  *
3  *  UTILIB: A utility library for developing portable C++ codes.
4  *  Copyright (c) 2008 Sandia Corporation.
5  *  This software is distributed under the BSD License.
6  *  Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
7  *  the U.S. Government retains certain rights in this software.
8  *  For more information, see the README file in the top UTILIB directory.
9  *  _________________________________________________________________________
10  */
11 
12 //
13 // sort_test.cpp
14 //
15 // Test sort/order/rank operations
16 //
17 #include <utilib/std_headers.h>
18 #include <utilib/sort.h>
19 #include <utilib/stl_auxiliary.h>
20 
21 using namespace utilib;
22 using namespace std;
23 
24 class A
25 {
26 public:
27   int a;
28 };
29 
30 
31 template <class VTYPE>
reorder(VTYPE & vec)32 void reorder(VTYPE& vec)
33 {
34 for (unsigned int i=1; i<=vec.size(); i++) vec[i-1] = 5*i*i - i*i*i;
35 }
36 
test_sort(int,char **)37 int test_sort(int,char**)
38 {
39 try {
40 
41 CommonIO::begin();
42 
43 {
44 vector<int> tmp(10);
45 reorder(tmp);
46 ucout << "a " << tmp << endl;
47 }
48 
49 //
50 // SORT
51 //
52 {
53 vector<int> tmp(10);
54 reorder(tmp);
55 sort(tmp);
56 ucout << "a " << tmp << endl;
57 }
58 {
59 BasicArray<int> tmp(10);
60 reorder(tmp);
61 sort(tmp);
62 ucout << "a " << tmp << endl;
63 }
64 
65 {
66 vector<int> tmp(10);
67 reorder(tmp);
68 sort(tmp.begin(),tmp.end());
69 ucout << "a " << tmp << endl;
70 }
71 {
72 BasicArray<int> tmp(10);
73 reorder(tmp);
74 sort(tmp.begin(),tmp.end());
75 ucout << "a " << tmp << endl;
76 }
77 
78 {
79 vector<int> tmp(10);
80 reorder(tmp);
81 sort(tmp,less<int>());
82 ucout << "a " << tmp << endl;
83 }
84 {
85 BasicArray<int> tmp(10);
86 reorder(tmp);
87 sort(tmp,less<int>());
88 ucout << "a " << tmp << endl;
89 }
90 
91 {
92 vector<int> tmp(10);
93 reorder(tmp);
94 sort(tmp.begin(),tmp.end(),less<int>());
95 ucout << "a " << tmp << endl;
96 }
97 {
98 BasicArray<int> tmp(10);
99 reorder(tmp);
100 sort(tmp.begin(),tmp.end(),less<int>());
101 ucout << "a " << tmp << endl;
102 }
103 
104 //
105 // STABLE_SORT
106 //
107 {
108 vector<int> tmp(10);
109 reorder(tmp);
110 stable_sort(tmp);
111 ucout << "a " << tmp << endl;
112 }
113 {
114 BasicArray<int> tmp(10);
115 reorder(tmp);
116 stable_sort(tmp);
117 ucout << "a " << tmp << endl;
118 }
119 
120 {
121 vector<int> tmp(10);
122 reorder(tmp);
123 stable_sort(tmp.begin(),tmp.end());
124 ucout << "a " << tmp << endl;
125 }
126 {
127 BasicArray<int> tmp(10);
128 reorder(tmp);
129 stable_sort(tmp.begin(),tmp.end());
130 ucout << "a " << tmp << endl;
131 }
132 
133 {
134 vector<int> tmp(10);
135 reorder(tmp);
136 stable_sort(tmp,less<int>());
137 ucout << "a " << tmp << endl;
138 }
139 {
140 BasicArray<int> tmp(10);
141 reorder(tmp);
142 stable_sort(tmp,less<int>());
143 ucout << "a " << tmp << endl;
144 }
145 
146 {
147 vector<int> tmp(10);
148 reorder(tmp);
149 stable_sort(tmp.begin(),tmp.end(),less<int>());
150 ucout << "a " << tmp << endl;
151 }
152 {
153 BasicArray<int> tmp(10);
154 reorder(tmp);
155 stable_sort(tmp.begin(),tmp.end(),less<int>());
156 ucout << "a " << tmp << endl;
157 }
158 
159 //
160 // ORDER
161 //
162 {
163 vector<int> tmp(10);
164 vector<int> ord;
165 reorder(tmp);
166 order(ord,tmp);
167 ucout << "a " << ord << endl;
168 }
169 {
170 BasicArray<int> tmp(10);
171 BasicArray<int> ord;
172 reorder(tmp);
173 order(ord,tmp);
174 ucout << "a " << ord << endl;
175 }
176 
177 {
178 vector<int> tmp(10);
179 vector<int> ord;
180 reorder(tmp);
181 order(ord,tmp,less<int>());
182 ucout << "a " << ord << endl;
183 }
184 {
185 BasicArray<int> tmp(10);
186 BasicArray<int> ord;
187 reorder(tmp);
188 order(ord,tmp,less<int>());
189 ucout << "a " << ord << endl;
190 }
191 
192 //
193 // STABLE_ORDER
194 //
195 {
196 vector<int> tmp(10);
197 vector<int> ord;
198 reorder(tmp);
199 stable_order(ord,tmp);
200 ucout << "a " << ord << endl;
201 }
202 {
203 BasicArray<int> tmp(10);
204 BasicArray<int> ord;
205 reorder(tmp);
206 stable_order(ord,tmp);
207 ucout << "a " << ord << endl;
208 }
209 
210 {
211 vector<int> tmp(10);
212 vector<int> ord;
213 reorder(tmp);
214 stable_order(ord,tmp,less<int>());
215 ucout << "a " << ord << endl;
216 }
217 {
218 BasicArray<int> tmp(10);
219 BasicArray<int> ord;
220 reorder(tmp);
221 stable_order(ord,tmp,less<int>());
222 ucout << "a " << ord << endl;
223 }
224 
225 //
226 // RANK
227 //
228 {
229 vector<int> tmp(10);
230 vector<int> ord;
231 reorder(tmp);
232 rank(ord,tmp);
233 ucout << "a " << ord << endl;
234 }
235 {
236 BasicArray<int> tmp(10);
237 BasicArray<int> ord;
238 reorder(tmp);
239 rank(ord,tmp);
240 ucout << "a " << ord << endl;
241 }
242 
243 {
244 vector<int> tmp(10);
245 vector<int> ord;
246 reorder(tmp);
247 rank(ord,tmp,less<int>());
248 ucout << "a " << ord << endl;
249 }
250 {
251 BasicArray<int> tmp(10);
252 BasicArray<int> ord;
253 reorder(tmp);
254 rank(ord,tmp,less<int>());
255 ucout << "a " << ord << endl;
256 }
257 
258 //
259 // STABLE_RANK
260 //
261 {
262 vector<int> tmp(10);
263 vector<int> ord;
264 reorder(tmp);
265 stable_rank(ord,tmp);
266 ucout << "a " << ord << endl;
267 }
268 {
269 BasicArray<int> tmp(10);
270 BasicArray<int> ord;
271 reorder(tmp);
272 stable_rank(ord,tmp);
273 ucout << "a " << ord << endl;
274 }
275 
276 {
277 vector<int> tmp(10);
278 vector<int> ord;
279 reorder(tmp);
280 stable_rank(ord,tmp,less<int>());
281 ucout << "a " << ord << endl;
282 }
283 {
284 BasicArray<int> tmp(10);
285 BasicArray<int> ord;
286 reorder(tmp);
287 stable_rank(ord,tmp,less<int>());
288 ucout << "a " << ord << endl;
289 }
290 
291 CommonIO::end();
292 
293 }
294 STD_CATCH(;)
295 
296 return 0;
297 }
298