1 | // $Id: utility.cc 3238 2014-05-24 10:27:39Z peter $ |
---|
2 | |
---|
3 | /* |
---|
4 | Copyright (C) 2006 Jari Häkkinen, Markus Ringnér |
---|
5 | Copyright (C) 2007, 2008 Jari Häkkinen, Peter Johansson |
---|
6 | Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014 Peter Johansson |
---|
7 | |
---|
8 | This file is part of the yat library, http://dev.thep.lu.se/yat |
---|
9 | |
---|
10 | The yat library is free software; you can redistribute it and/or |
---|
11 | modify it under the terms of the GNU General Public License as |
---|
12 | published by the Free Software Foundation; either version 3 of the |
---|
13 | License, or (at your option) any later version. |
---|
14 | |
---|
15 | The yat library is distributed in the hope that it will be useful, |
---|
16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
18 | General Public License for more details. |
---|
19 | |
---|
20 | You should have received a copy of the GNU General Public License |
---|
21 | along with yat. If not, see <http://www.gnu.org/licenses/>. |
---|
22 | */ |
---|
23 | |
---|
24 | #include <config.h> |
---|
25 | |
---|
26 | #include "Suite.h" |
---|
27 | |
---|
28 | #include "yat/utility/Exception.h" |
---|
29 | #include "yat/utility/Matrix.h" |
---|
30 | #include "yat/utility/utility.h" |
---|
31 | #include "yat/utility/Vector.h" |
---|
32 | #include "yat/utility/VectorConstView.h" |
---|
33 | #include "yat/utility/sort_index.h" |
---|
34 | #include "yat/utility/stl_utility.h" |
---|
35 | |
---|
36 | #include <boost/concept_archetype.hpp> |
---|
37 | |
---|
38 | #include <cassert> |
---|
39 | #include <cerrno> |
---|
40 | #include <fstream> |
---|
41 | #include <iostream> |
---|
42 | #include <limits> |
---|
43 | #include <list> |
---|
44 | #include <map> |
---|
45 | #include <string> |
---|
46 | #include <vector> |
---|
47 | |
---|
48 | using namespace theplu::yat; |
---|
49 | void test_errno_error(test::Suite& suite); |
---|
50 | void test_errno_error_func(void); |
---|
51 | void test_get_map(test::Suite&); |
---|
52 | void test_load(test::Suite&); |
---|
53 | void test_load2(test::Suite&); |
---|
54 | void test_inverse(test::Suite&); |
---|
55 | |
---|
56 | void test_dirname(test::Suite& suite); |
---|
57 | void test_basename(test::Suite& suite); |
---|
58 | void test_fnmatch(test::Suite& suite); |
---|
59 | bool test_fnmatch(bool, std::string, std::string); |
---|
60 | void test_replace(test::Suite& suite); |
---|
61 | |
---|
62 | template<typename InputIterator, typename Key> |
---|
63 | void test_inverse_validate(InputIterator first, InputIterator last, |
---|
64 | const std::map<Key, std::vector<size_t> >& m, |
---|
65 | test::Suite& suite); |
---|
66 | template<typename InputIterator, typename Key> |
---|
67 | void test_inverse_validate(InputIterator first, |
---|
68 | const std::multimap<Key, size_t>& m, |
---|
69 | test::Suite& suite); |
---|
70 | |
---|
71 | void test_sort_index(test::Suite& suite); |
---|
72 | void test_less_nan(test::Suite& suite); |
---|
73 | void test_ptr_compare(test::Suite& suite); |
---|
74 | void test_compose_functors(test::Suite& suite); |
---|
75 | void test_dereferencer(test::Suite& suite); |
---|
76 | |
---|
77 | int main(int argc, char* argv[]) |
---|
78 | { |
---|
79 | using namespace theplu::yat; |
---|
80 | test::Suite suite(argc, argv); |
---|
81 | suite.err() << "testing utility ... " << std::endl; |
---|
82 | |
---|
83 | // test float/double |
---|
84 | std::string s("1.2"); |
---|
85 | if (!utility::is<double>(s)){ |
---|
86 | suite.add(false); |
---|
87 | } |
---|
88 | else if (!utility::is<float>(s)) { |
---|
89 | suite.add(false); |
---|
90 | } |
---|
91 | else if (utility::is<int>(s)) { |
---|
92 | suite.add(false); |
---|
93 | } |
---|
94 | else if (utility::is_nan(s)) { |
---|
95 | suite.add(false); |
---|
96 | } |
---|
97 | |
---|
98 | // test int |
---|
99 | s="23"; |
---|
100 | if (!utility::is<double>(s)){ |
---|
101 | suite.add(false); |
---|
102 | } |
---|
103 | else if (!utility::is<float>(s)) { |
---|
104 | suite.add(false); |
---|
105 | } |
---|
106 | else if (!utility::is<int>(s)) { |
---|
107 | suite.add(false); |
---|
108 | } |
---|
109 | else if (utility::is_nan(s)) { |
---|
110 | suite.add(false); |
---|
111 | } |
---|
112 | |
---|
113 | // test nan |
---|
114 | s=" nAn "; |
---|
115 | if (!utility::is<double>(s)){ |
---|
116 | suite.add(false); |
---|
117 | suite.err() << "error: " << s << " is a double\n"; |
---|
118 | } |
---|
119 | /* we don't require NaN for float and int |
---|
120 | else if (!utility::is_float(s)) { |
---|
121 | suite.add(false); |
---|
122 | suite.err() << "error: " << s << " is a float\n"; |
---|
123 | } |
---|
124 | else if (!utility::is_int(s)) { |
---|
125 | suite.add(false); |
---|
126 | suite.err() << "error: " << s << " is a int\n"; |
---|
127 | } |
---|
128 | */ |
---|
129 | else if (!utility::is_nan(s)) { |
---|
130 | suite.add(false); |
---|
131 | suite.err() << "error: " << s << " is nan\n"; |
---|
132 | |
---|
133 | } |
---|
134 | |
---|
135 | // testing trailing values |
---|
136 | s=" 23 23 "; |
---|
137 | if (utility::is<double>(s)){ |
---|
138 | suite.add(false); |
---|
139 | } |
---|
140 | else if (utility::is<float>(s)) { |
---|
141 | suite.add(false); |
---|
142 | } |
---|
143 | else if (utility::is<int>(s)) { |
---|
144 | suite.add(false); |
---|
145 | } |
---|
146 | else if (utility::is_nan(s)) { |
---|
147 | suite.add(false); |
---|
148 | } |
---|
149 | |
---|
150 | if (utility::convert<double>("1.23")!=1.23) |
---|
151 | suite.add(false); |
---|
152 | utility::convert<double>("-inf"); |
---|
153 | utility::convert<double>("inf"); |
---|
154 | utility::convert<double>("NaN"); |
---|
155 | utility::convert<double>("-NaN"); |
---|
156 | utility::convert<double>(" -NaN"); |
---|
157 | |
---|
158 | suite.add(utility::convert(1.23)=="1.23"); |
---|
159 | suite.add(utility::convert(1)=="1"); |
---|
160 | |
---|
161 | suite.add(utility::is<unsigned int>("1")); |
---|
162 | suite.add(!utility::is<unsigned int>(" ")); |
---|
163 | suite.add(!utility::is<unsigned int>(" -1")); |
---|
164 | // test -1 cannot be assigned to unsigned int |
---|
165 | try { |
---|
166 | unsigned int x = utility::convert<unsigned int>("-1"); |
---|
167 | suite.err() << "convert<unsigned>(\"-1\") did not throw\n"; |
---|
168 | suite.err() << "x: " << x << "\n"; |
---|
169 | suite.add(false); |
---|
170 | exit(1); |
---|
171 | } |
---|
172 | catch (std::runtime_error& e) { |
---|
173 | suite.out() << "expected error: " << e.what() << "\n"; |
---|
174 | } |
---|
175 | |
---|
176 | if (!suite.add(utility::is<double>("-inf"))) |
---|
177 | suite.err() << "is<double>(\"-inf\") should return true\n"; |
---|
178 | if (!suite.add(utility::is<double>("inf"))) |
---|
179 | suite.err() << "is<double>(\"inf\") should return true\n"; |
---|
180 | if (!suite.add(utility::is<double>("NaN"))) |
---|
181 | suite.err() << "is<double>(\"NaN\") should return true\n"; |
---|
182 | if (!suite.add(utility::is<double>("1.23"))) |
---|
183 | suite.err() << "is<double>(\"1.23\") should return true\n"; |
---|
184 | if (!suite.add(!utility::is<double>("1.23.2"))) |
---|
185 | suite.err() << "is<double>(\"1.23.2\") should return false\n"; |
---|
186 | if (!suite.add(!utility::is<int>("1.23"))) |
---|
187 | suite.err() << "is<int>(\"1.23\") should return false\n"; |
---|
188 | if (!suite.add(!utility::is<int>(""))) |
---|
189 | suite.err() << "is<int>(\"\") should return false\n"; |
---|
190 | |
---|
191 | { |
---|
192 | utility::Log<long double> f; |
---|
193 | utility::Log<double> f2(1.3); |
---|
194 | f(2.0); |
---|
195 | utility::Exp<double> e; |
---|
196 | e(3.2); |
---|
197 | } |
---|
198 | |
---|
199 | test_get_map(suite); |
---|
200 | test_inverse(suite); |
---|
201 | test_sort_index(suite); |
---|
202 | test_less_nan(suite); |
---|
203 | test_errno_error(suite); |
---|
204 | test_ptr_compare(suite); |
---|
205 | test_load(suite); |
---|
206 | test_load2(suite); |
---|
207 | |
---|
208 | test_compose_functors(suite); |
---|
209 | |
---|
210 | double x = 8.0; |
---|
211 | suite.add(suite.equal(utility::log2(x), 3.0)); |
---|
212 | float xf = x; |
---|
213 | suite.add(suite.equal(utility::log2(xf), 3.0)); |
---|
214 | long double xld = 8.0; |
---|
215 | suite.add(suite.equal(utility::log2(xld), 3.0)); |
---|
216 | |
---|
217 | test_basename(suite); |
---|
218 | test_dirname(suite); |
---|
219 | test_fnmatch(suite); |
---|
220 | test_replace(suite); |
---|
221 | |
---|
222 | return suite.return_value(); |
---|
223 | } |
---|
224 | |
---|
225 | |
---|
226 | void test_errno_error_func(void) |
---|
227 | { |
---|
228 | errno=1; |
---|
229 | throw utility::errno_error(""); |
---|
230 | } |
---|
231 | |
---|
232 | |
---|
233 | void test_errno_error(test::Suite& suite) |
---|
234 | { |
---|
235 | suite.out() << "testing errno_error\n"; |
---|
236 | try { |
---|
237 | test_errno_error_func(); |
---|
238 | } |
---|
239 | catch ( utility::errno_error& e) { |
---|
240 | suite.out() << "catching expected exception with what():\n"; |
---|
241 | suite.out() << e.what() << "\n"; |
---|
242 | return; |
---|
243 | } |
---|
244 | suite.err() << "error: expected thrown exception\n"; |
---|
245 | suite.add(false); |
---|
246 | } |
---|
247 | |
---|
248 | |
---|
249 | void test_compose_functors(test::Suite& suite) |
---|
250 | { |
---|
251 | typedef utility::abs<double> U1; |
---|
252 | // we use float here to check conversion from float to double in functors |
---|
253 | typedef utility::Exp<float> U2; |
---|
254 | typedef std::plus<double> B; |
---|
255 | U1 u1; |
---|
256 | U2 u2; |
---|
257 | B b; |
---|
258 | |
---|
259 | utility::compose_f_gx_hy<B, U1, U2> binary; |
---|
260 | binary = utility::make_compose_f_gx_hy(b, u1, u2); |
---|
261 | |
---|
262 | utility::compose_f_gxy<U2, B> binary2; |
---|
263 | binary2 = utility::make_compose_f_gxy(u2, b); |
---|
264 | |
---|
265 | utility::compose_f_gx<U1, U2> unary; |
---|
266 | unary = utility::make_compose_f_gx(u1, u2); |
---|
267 | |
---|
268 | utility::compose_f_gx_hx<B, U1, U2> unary2; |
---|
269 | unary2 = utility::make_compose_f_gx_hx(b, u1, u2); |
---|
270 | if (!suite.add(suite.equal_fix(unary2(-2.0), 2.0 + std::exp(-2.0),1e-8))) |
---|
271 | suite.err() << "compose_f_gx_hx failed\n"; |
---|
272 | } |
---|
273 | |
---|
274 | |
---|
275 | void test_dereferencer(test::Suite& suite) |
---|
276 | { |
---|
277 | using utility::Dereferencer; |
---|
278 | Dereferencer<double*> deref; |
---|
279 | double x = 0; |
---|
280 | double* px = &x; |
---|
281 | deref(px) = 1.662; |
---|
282 | if (!suite.add(x==1.662)) { |
---|
283 | suite.out() << "test_dereferencer failed: x: " << x << "\n"; |
---|
284 | } |
---|
285 | } |
---|
286 | |
---|
287 | |
---|
288 | void test_get_map(test::Suite& suite) |
---|
289 | { |
---|
290 | std::map<std::string, int> m; |
---|
291 | m["one"] = 1; |
---|
292 | m["two"] = 2; |
---|
293 | const std::map<std::string, int> m2(m); |
---|
294 | int i = utility::get(m2, static_cast<std::string>("one")); |
---|
295 | test::dummie_function(i); // avoid compiler warning |
---|
296 | try { |
---|
297 | utility::get(m2, static_cast<std::string>("three")); |
---|
298 | suite.add(false); |
---|
299 | } |
---|
300 | catch (std::runtime_error& e) { |
---|
301 | suite.out() << "expected exception thrown with what() = " |
---|
302 | << e.what() << "\n"; |
---|
303 | } |
---|
304 | } |
---|
305 | |
---|
306 | void test_less_nan(test::Suite& suite) |
---|
307 | { |
---|
308 | suite.out() << "test less_nan\n"; |
---|
309 | bool prior_ok=suite.ok(); |
---|
310 | utility::less_nan<double> f; |
---|
311 | suite.add(f(2.7,3.14)); |
---|
312 | suite.add(!f(2.7,-3.14)); |
---|
313 | suite.add(!f(2.7,2.7)); |
---|
314 | suite.add(f(3.14, std::numeric_limits<double>::quiet_NaN())); |
---|
315 | suite.add(!f(std::numeric_limits<double>::quiet_NaN(), 2.7)); |
---|
316 | suite.add(!f(std::numeric_limits<double>::quiet_NaN(), |
---|
317 | std::numeric_limits<double>::quiet_NaN())); |
---|
318 | utility::less_nan<utility::DataWeight> fw; |
---|
319 | suite.add(fw(utility::DataWeight(2), utility::DataWeight(3))); |
---|
320 | |
---|
321 | bool post_ok=suite.ok(); |
---|
322 | if (!post_ok && prior_ok) |
---|
323 | suite.err() << "test_less_nan failed\n"; |
---|
324 | } |
---|
325 | |
---|
326 | |
---|
327 | void test_load(test::Suite& suite) |
---|
328 | { |
---|
329 | std::istringstream iss(std::string("1.69 3.14")); |
---|
330 | std::vector<double> double_vec; |
---|
331 | utility::load(iss, double_vec); |
---|
332 | suite.add(double_vec.size()==2); |
---|
333 | |
---|
334 | std::istringstream iss2(std::string("1 2")); |
---|
335 | std::vector<double> uint_vec; |
---|
336 | utility::load(iss2, uint_vec); |
---|
337 | suite.add(uint_vec.size()==2); |
---|
338 | |
---|
339 | std::istringstream iss3(std::string("1.69 3.14")); |
---|
340 | std::vector<std::string> str_vec; |
---|
341 | utility::load(iss3, str_vec); |
---|
342 | if (str_vec.size()==2) { |
---|
343 | if (str_vec[1]!="3.14") { |
---|
344 | suite.out() << "error: load<string>:" << "\n" |
---|
345 | << " str_vec[1] = " << str_vec[1] << "\n" |
---|
346 | << " expected: 3.14n"; |
---|
347 | suite.add(false); |
---|
348 | } |
---|
349 | } |
---|
350 | else { |
---|
351 | suite.out() << "str_vec.size() is not 2\n"; |
---|
352 | suite.add(false); |
---|
353 | } |
---|
354 | } |
---|
355 | |
---|
356 | |
---|
357 | void test_load2(test::Suite& suite) |
---|
358 | { |
---|
359 | suite.out() << "test load<double>(matrix)\n"; |
---|
360 | std::string str("1.69 3.14\n1.23 1.22\n"); |
---|
361 | std::istringstream iss(str); |
---|
362 | std::vector<std::vector<double> > double_m; |
---|
363 | utility::load(iss, double_m, '\0', '\n'); |
---|
364 | suite.add(double_m.size()==2); |
---|
365 | |
---|
366 | suite.out() << "test load<std::string>(matrix)\n"; |
---|
367 | suite.out() << str; |
---|
368 | std::istringstream iss2(str); |
---|
369 | std::vector<std::vector<std::string> > str_m; |
---|
370 | utility::load(iss2, str_m, '\0', '\n'); |
---|
371 | std::cout << str_m.size() << std::endl; |
---|
372 | for (size_t i=0; i<str_m.size(); ++i) |
---|
373 | for (size_t j=0; j<str_m[i].size(); ++j) |
---|
374 | suite.out() << str_m[i][j] << "\n"; |
---|
375 | suite.add(str_m.size()==2); |
---|
376 | } |
---|
377 | |
---|
378 | |
---|
379 | void test_inverse(test::Suite& suite) |
---|
380 | { |
---|
381 | suite.err() << "Testing inverse\n"; |
---|
382 | std::vector<std::string> vec; |
---|
383 | vec.push_back("No"); |
---|
384 | vec.push_back("one"); |
---|
385 | vec.push_back("shall"); |
---|
386 | vec.push_back("be"); |
---|
387 | vec.push_back("subjected"); |
---|
388 | std::map<std::string, std::vector<size_t> > map; |
---|
389 | utility::inverse(vec.begin(), vec.end(), map); |
---|
390 | test_inverse_validate(vec.begin(), vec.end(), map, suite); |
---|
391 | utility::inverse(vec.begin()+1, vec.end(), map); |
---|
392 | test_inverse_validate(vec.begin()+1, vec.end(), map, suite); |
---|
393 | const std::vector<std::string> vec2(vec); |
---|
394 | utility::inverse(vec2.begin(), vec2.end(), map); |
---|
395 | test_inverse_validate(vec2.begin(), vec2.end(), map, suite); |
---|
396 | std::multimap<std::string, size_t> mmap; |
---|
397 | utility::inverse(vec.begin(), vec.end(), mmap); |
---|
398 | test_inverse_validate(vec.begin(), mmap, suite); |
---|
399 | utility::inverse(vec.begin()+1, vec.end(), mmap); |
---|
400 | test_inverse_validate(vec.begin()+1, mmap, suite); |
---|
401 | // do not run compile tests |
---|
402 | if (false) { |
---|
403 | std::map<std::string, std::vector<size_t> > m; |
---|
404 | boost::input_iterator_archetype<std::string> start; |
---|
405 | boost::input_iterator_archetype<std::string> end; |
---|
406 | utility::inverse(start, end, m); |
---|
407 | std::map<std::string, std::vector<size_t>, std::greater<std::string> > m2; |
---|
408 | utility::inverse(start, end, m2); |
---|
409 | std::multimap<std::string, size_t> m3; |
---|
410 | utility::inverse(start, end, m3); |
---|
411 | std::multimap<std::string, size_t, std::greater<std::string> > m4; |
---|
412 | utility::inverse(start, end, m4); |
---|
413 | |
---|
414 | std::map<std::string, size_t> unique_map; |
---|
415 | utility::inverse(start, end, unique_map); |
---|
416 | } |
---|
417 | } |
---|
418 | |
---|
419 | |
---|
420 | template<typename InputIterator, typename Key> |
---|
421 | void test_inverse_validate(InputIterator first, InputIterator last, |
---|
422 | const std::map<Key, std::vector<size_t> >& m, |
---|
423 | test::Suite& suite) |
---|
424 | { |
---|
425 | typedef typename std::map<Key, std::vector<size_t> >::const_iterator Iter; |
---|
426 | for ( InputIterator iter=first; iter != last; ++iter) { |
---|
427 | Iter map_iter = m.find(*iter); |
---|
428 | if (!suite.add(map_iter!=m.end())) { |
---|
429 | suite.err() << "test_inverse_validate() failed\n"; |
---|
430 | suite.err() << " could not find: " << *first << " in map m\n"; |
---|
431 | } |
---|
432 | for (size_t i=0; i<map_iter->second.size(); ++i) |
---|
433 | if (!suite.add(map_iter->second[i] == |
---|
434 | static_cast<size_t>(distance(first, iter))) ) { |
---|
435 | suite.err() << "test_inverse_validate() failed\n"; |
---|
436 | suite.err() << " comparing: " << map_iter->second[i] |
---|
437 | << " and " << distance(first, iter) |
---|
438 | << " expected them to be equal\n"; |
---|
439 | } |
---|
440 | } |
---|
441 | } |
---|
442 | |
---|
443 | |
---|
444 | template<typename InputIterator, typename Key> |
---|
445 | void test_inverse_validate(InputIterator first, |
---|
446 | const std::multimap<Key, size_t>& m, |
---|
447 | test::Suite& suite) |
---|
448 | { |
---|
449 | for (typename std::multimap<Key, size_t>::const_iterator iter=m.begin(); |
---|
450 | iter!=m.end(); ++iter) { |
---|
451 | suite.add(*(first + iter->second) == iter->first); |
---|
452 | } |
---|
453 | |
---|
454 | } |
---|
455 | |
---|
456 | void test_sort_index(test::Suite& suite) |
---|
457 | { |
---|
458 | suite.err() << "testing sort_index" << std::endl; |
---|
459 | utility::Vector a(10); |
---|
460 | for (size_t i=0; i<a.size(); ++i) |
---|
461 | a(i) = std::pow(i-4.2,2); |
---|
462 | std::vector<size_t> vec; |
---|
463 | utility::sort_index(vec, a); |
---|
464 | |
---|
465 | std::vector<size_t> vec2; |
---|
466 | utility::sort_index(a.begin(), a.end(), vec2); |
---|
467 | if (vec.size()==vec2.size()) { |
---|
468 | if (!suite.equal_range(vec.begin(), vec.end(), vec2.begin())) { |
---|
469 | suite.add(false); |
---|
470 | } |
---|
471 | } |
---|
472 | else { |
---|
473 | suite.add(false); |
---|
474 | suite.err() << "size mismatch: vec.size()=" << vec.size() |
---|
475 | << "vec2.size()=" << vec2.size() << "\n"; |
---|
476 | } |
---|
477 | const utility::VectorConstView b(a, 0, 5, 2); |
---|
478 | |
---|
479 | std::vector<size_t> vec3; |
---|
480 | utility::sort_index(vec3, b); |
---|
481 | std::vector<size_t> vec4; |
---|
482 | utility::sort_index(b.begin(), b.end(), vec4); |
---|
483 | if (vec3.size()!=vec4.size()) { |
---|
484 | suite.add(false); |
---|
485 | suite.err() << "size mismatch: vec3.size()=" << vec3.size() |
---|
486 | << " vec4.size()=" << vec4.size() << "\n"; |
---|
487 | } |
---|
488 | else { |
---|
489 | if (!suite.equal_range(vec3.begin(), vec3.end(), vec4.begin())){ |
---|
490 | suite.add(false); |
---|
491 | } |
---|
492 | } |
---|
493 | |
---|
494 | std::list<double> list; |
---|
495 | for (size_t i=0; i<a.size(); ++i) |
---|
496 | list.push_back(a(i)); |
---|
497 | std::vector<size_t> vec5; |
---|
498 | utility::sort_index(list.begin(), list.end(), vec5); |
---|
499 | if (vec.size()!=vec5.size()) { |
---|
500 | suite.add(false); |
---|
501 | suite.err() << "size mismatch: vec.size()=" << vec.size() |
---|
502 | << " vec5.size()=" << vec5.size() << "\n"; |
---|
503 | } |
---|
504 | else { |
---|
505 | if (!suite.equal_range(vec.begin(), vec.end(), vec5.begin())){ |
---|
506 | suite.add(false); |
---|
507 | } |
---|
508 | } |
---|
509 | |
---|
510 | // do not run compiler tests |
---|
511 | if (false) { |
---|
512 | std::vector<size_t> vec; |
---|
513 | // value type must be possible to store in a vector so has to be |
---|
514 | // default constructible and assignable |
---|
515 | typedef boost::default_constructible_archetype< |
---|
516 | boost::sgi_assignable_archetype< |
---|
517 | boost::less_than_comparable_archetype<> > > value_type; |
---|
518 | boost::input_iterator_archetype<value_type> iter; |
---|
519 | utility::sort_index(iter, iter, vec); |
---|
520 | // try with a random access container as well |
---|
521 | std::vector<value_type> vec2; |
---|
522 | utility::sort_index(vec2.begin(), vec2.end(), vec); |
---|
523 | } |
---|
524 | // same thing with four argument version |
---|
525 | if (false) { |
---|
526 | std::vector<size_t> vec; |
---|
527 | // value type must be possible to store in a vector so has to be |
---|
528 | // default constructible and assignable |
---|
529 | typedef boost::default_constructible_archetype< |
---|
530 | boost::sgi_assignable_archetype<> > value_type; |
---|
531 | |
---|
532 | boost::input_iterator_archetype<value_type> iter; |
---|
533 | |
---|
534 | // Create a binary predicate that is assignable and default constructible |
---|
535 | boost::default_constructible_archetype< |
---|
536 | boost::sgi_assignable_archetype< |
---|
537 | boost::binary_predicate_archetype< |
---|
538 | value_type, value_type> > > comp; |
---|
539 | |
---|
540 | utility::sort_index(iter, iter, vec, comp); |
---|
541 | // try with a random access container as well |
---|
542 | std::vector<value_type> vec2; |
---|
543 | utility::sort_index(vec2.begin(), vec2.end(), vec, comp); |
---|
544 | } |
---|
545 | } |
---|
546 | |
---|
547 | void test_ptr_compare(test::Suite& suite) |
---|
548 | { |
---|
549 | using utility::make_ptr_compare; |
---|
550 | std::vector<double*> vec(10); |
---|
551 | std::greater<double> d_greater; |
---|
552 | for (size_t i=0; i<vec.size(); ++i) |
---|
553 | vec[i] = new double(i); |
---|
554 | std::sort(vec.begin(), vec.end(), make_ptr_compare(*vec.begin(), d_greater)); |
---|
555 | for (size_t i=1; i<vec.size(); ++i) { |
---|
556 | suite.add( *vec[i-1] > *vec[i]); |
---|
557 | } |
---|
558 | std::sort(vec.begin(), vec.end(), make_ptr_compare(*vec.begin())); |
---|
559 | for (size_t i=1; i<vec.size(); ++i) { |
---|
560 | suite.add( *vec[i-1] < *vec[i]); |
---|
561 | } |
---|
562 | for (size_t i=0; i<vec.size(); ++i) |
---|
563 | delete vec[i]; |
---|
564 | |
---|
565 | } |
---|
566 | |
---|
567 | |
---|
568 | void test_fnmatch(test::Suite& suite) |
---|
569 | { |
---|
570 | bool ok=true; |
---|
571 | ok &= test_fnmatch(true,"peter", "peter"); |
---|
572 | ok &= test_fnmatch(false,"peter", "peterj"); |
---|
573 | |
---|
574 | ok &= test_fnmatch(true,"*", "peterj"); |
---|
575 | ok &= test_fnmatch(true,"p*", "peterj"); |
---|
576 | ok &= test_fnmatch(true, "p*", "peter"); |
---|
577 | ok &= test_fnmatch(false, "p*j", "peter"); |
---|
578 | ok &= test_fnmatch(true, "p*j", "peterj"); |
---|
579 | ok &= test_fnmatch(true, "*peter", "peter"); |
---|
580 | |
---|
581 | ok &= test_fnmatch(true, "p?ter", "peter"); |
---|
582 | ok &= test_fnmatch(false, "p?er", "peter"); |
---|
583 | ok &= test_fnmatch(false, "p?eter", "peter"); |
---|
584 | |
---|
585 | ok &= test_fnmatch(true, "filename", "filename"); |
---|
586 | ok &= test_fnmatch(true, "*name", "filename"); |
---|
587 | ok &= test_fnmatch(true, "[fa]il?name", "filename"); |
---|
588 | ok &= test_fnmatch(true, "[fa]*il?name", "ffilename"); |
---|
589 | |
---|
590 | ok &= test_fnmatch(true, "[fa]*il?name", "fafafailename"); |
---|
591 | ok &= test_fnmatch(false, "[fa]?il?name", "ilename"); |
---|
592 | ok &= test_fnmatch(false, "?[fa]il?name", "ilename"); |
---|
593 | ok &= test_fnmatch(true, "[fa]il?name", "filename"); |
---|
594 | ok &= test_fnmatch(false, "[fa]?il?name", "fafafailename"); |
---|
595 | |
---|
596 | ok &= test_fnmatch(true, "*name", "/path/to/filename"); |
---|
597 | ok &= test_fnmatch(true, "*name", "file.name"); |
---|
598 | ok &= test_fnmatch(true, "*.txt", ".file.txt"); |
---|
599 | suite.add(ok); |
---|
600 | } |
---|
601 | |
---|
602 | |
---|
603 | void test_dirname(test::Suite& suite) |
---|
604 | { |
---|
605 | std::vector<std::string> path; |
---|
606 | path.push_back("/usr/lib"); |
---|
607 | path.push_back("/usr/"); |
---|
608 | path.push_back("usr"); |
---|
609 | path.push_back("/"); |
---|
610 | path.push_back("."); |
---|
611 | path.push_back(".."); |
---|
612 | std::vector<std::string> dir; |
---|
613 | dir.push_back("/usr"); |
---|
614 | dir.push_back("/"); |
---|
615 | dir.push_back("."); |
---|
616 | dir.push_back("/"); |
---|
617 | dir.push_back("."); |
---|
618 | dir.push_back("."); |
---|
619 | assert(dir.size()==path.size()); |
---|
620 | using utility::dirname; |
---|
621 | for (size_t i=0; i<dir.size(); ++i) { |
---|
622 | if (dir[i] != dirname(path[i])) { |
---|
623 | suite.add(false); |
---|
624 | suite.out() << "error:\n"; |
---|
625 | suite.out() << "path: " << path[i] << "\n"; |
---|
626 | suite.out() << "directory_name: " << dirname(path[i]) << "\n"; |
---|
627 | suite.out() << "expected: " << dir[i] << "\n"; |
---|
628 | } |
---|
629 | } |
---|
630 | } |
---|
631 | |
---|
632 | |
---|
633 | void test_basename(test::Suite& suite) |
---|
634 | { |
---|
635 | std::vector<std::string> path; |
---|
636 | path.push_back("/usr/lib"); |
---|
637 | path.push_back("/usr/"); |
---|
638 | path.push_back("usr"); |
---|
639 | path.push_back("/"); |
---|
640 | path.push_back("."); |
---|
641 | path.push_back(".."); |
---|
642 | std::vector<std::string> file; |
---|
643 | file.push_back("lib"); |
---|
644 | file.push_back("usr"); |
---|
645 | file.push_back("usr"); |
---|
646 | file.push_back("/"); |
---|
647 | file.push_back("."); |
---|
648 | file.push_back(".."); |
---|
649 | assert(file.size()==path.size()); |
---|
650 | using utility::basename; |
---|
651 | for (size_t i=0; i<file.size(); ++i) { |
---|
652 | if (file[i] != basename(path[i])) { |
---|
653 | suite.add(false); |
---|
654 | suite.out() << "error:\n"; |
---|
655 | suite.out() << "path: " << path[i] << "\n"; |
---|
656 | suite.out() << "basename: " << basename(path[i]) << "\n"; |
---|
657 | suite.out() << "expected: " << file[i] << "\n"; |
---|
658 | } |
---|
659 | } |
---|
660 | } |
---|
661 | |
---|
662 | |
---|
663 | bool test_fnmatch(bool answ, std::string a, std::string b) |
---|
664 | { |
---|
665 | using namespace utility; |
---|
666 | bool res = fnmatch(a, b); |
---|
667 | // check that fnmatch and regexp agree |
---|
668 | if (res == answ) |
---|
669 | return true; |
---|
670 | if (res!=answ) |
---|
671 | std::cerr << "fnmatch(" << a << ", " << b << ") results " |
---|
672 | << res |
---|
673 | << ". Expects " << answ << std::endl; |
---|
674 | return false; |
---|
675 | } |
---|
676 | |
---|
677 | |
---|
678 | void test_replace(test::Suite& suite) |
---|
679 | { |
---|
680 | std::string s = "Some magic string!!!"; |
---|
681 | utility::replace(s, "magic", "arbitrary"); |
---|
682 | if (!suite.add(s=="Some arbitrary string!!!")) |
---|
683 | suite.err() << "error: " << s << "\n"; |
---|
684 | } |
---|