1 | // $Id: utility.cc 2926 2012-12-20 11:06:07Z jari $ |
---|
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 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 | |
---|
61 | template<typename InputIterator, typename Key> |
---|
62 | void test_inverse_validate(InputIterator first, InputIterator last, |
---|
63 | const std::map<Key, std::vector<size_t> >& m, |
---|
64 | test::Suite& suite); |
---|
65 | template<typename InputIterator, typename Key> |
---|
66 | void test_inverse_validate(InputIterator first, |
---|
67 | const std::multimap<Key, size_t>& m, |
---|
68 | test::Suite& suite); |
---|
69 | |
---|
70 | void test_sort_index(test::Suite& suite); |
---|
71 | void test_less_nan(test::Suite& suite); |
---|
72 | void test_ptr_compare(test::Suite& suite); |
---|
73 | void test_compose_functors(test::Suite& suite); |
---|
74 | void test_dereferencer(test::Suite& suite); |
---|
75 | |
---|
76 | int main(int argc, char* argv[]) |
---|
77 | { |
---|
78 | using namespace theplu::yat; |
---|
79 | test::Suite suite(argc, argv); |
---|
80 | suite.err() << "testing utility ... " << std::endl; |
---|
81 | |
---|
82 | // test float/double |
---|
83 | std::string s("1.2"); |
---|
84 | if (!utility::is<double>(s)){ |
---|
85 | suite.add(false); |
---|
86 | } |
---|
87 | else if (!utility::is<float>(s)) { |
---|
88 | suite.add(false); |
---|
89 | } |
---|
90 | else if (utility::is<int>(s)) { |
---|
91 | suite.add(false); |
---|
92 | } |
---|
93 | else if (utility::is_nan(s)) { |
---|
94 | suite.add(false); |
---|
95 | } |
---|
96 | |
---|
97 | // test int |
---|
98 | s="23"; |
---|
99 | if (!utility::is<double>(s)){ |
---|
100 | suite.add(false); |
---|
101 | } |
---|
102 | else if (!utility::is<float>(s)) { |
---|
103 | suite.add(false); |
---|
104 | } |
---|
105 | else if (!utility::is<int>(s)) { |
---|
106 | suite.add(false); |
---|
107 | } |
---|
108 | else if (utility::is_nan(s)) { |
---|
109 | suite.add(false); |
---|
110 | } |
---|
111 | |
---|
112 | // test nan |
---|
113 | s=" nAn "; |
---|
114 | if (!utility::is<double>(s)){ |
---|
115 | suite.add(false); |
---|
116 | suite.err() << "error: " << s << " is a double\n"; |
---|
117 | } |
---|
118 | /* we don't require NaN for float and int |
---|
119 | else if (!utility::is_float(s)) { |
---|
120 | suite.add(false); |
---|
121 | suite.err() << "error: " << s << " is a float\n"; |
---|
122 | } |
---|
123 | else if (!utility::is_int(s)) { |
---|
124 | suite.add(false); |
---|
125 | suite.err() << "error: " << s << " is a int\n"; |
---|
126 | } |
---|
127 | */ |
---|
128 | else if (!utility::is_nan(s)) { |
---|
129 | suite.add(false); |
---|
130 | suite.err() << "error: " << s << " is nan\n"; |
---|
131 | |
---|
132 | } |
---|
133 | |
---|
134 | // testing trailing values |
---|
135 | s=" 23 23 "; |
---|
136 | if (utility::is<double>(s)){ |
---|
137 | suite.add(false); |
---|
138 | } |
---|
139 | else if (utility::is<float>(s)) { |
---|
140 | suite.add(false); |
---|
141 | } |
---|
142 | else if (utility::is<int>(s)) { |
---|
143 | suite.add(false); |
---|
144 | } |
---|
145 | else if (utility::is_nan(s)) { |
---|
146 | suite.add(false); |
---|
147 | } |
---|
148 | |
---|
149 | if (utility::convert<double>("1.23")!=1.23) |
---|
150 | suite.add(false); |
---|
151 | utility::convert<double>("-inf"); |
---|
152 | utility::convert<double>("inf"); |
---|
153 | utility::convert<double>("NaN"); |
---|
154 | |
---|
155 | suite.add(utility::convert(1.23)=="1.23"); |
---|
156 | suite.add(utility::convert(1)=="1"); |
---|
157 | |
---|
158 | suite.add(utility::is<unsigned int>("1")); |
---|
159 | suite.add(!utility::is<unsigned int>(" ")); |
---|
160 | suite.add(!utility::is<unsigned int>(" -1")); |
---|
161 | // test -1 cannot be assigned to unsigned int |
---|
162 | try { |
---|
163 | unsigned int x = utility::convert<unsigned int>("-1"); |
---|
164 | suite.err() << "convert<unsigned>(\"-1\") did not throw\n"; |
---|
165 | suite.err() << "x: " << x << "\n"; |
---|
166 | suite.add(false); |
---|
167 | exit(1); |
---|
168 | } |
---|
169 | catch (std::runtime_error& e) { |
---|
170 | suite.out() << "expected error: " << e.what() << "\n"; |
---|
171 | } |
---|
172 | |
---|
173 | if (!suite.add(utility::is<double>("-inf"))) |
---|
174 | suite.err() << "is<double>(\"-inf\") should return true\n"; |
---|
175 | if (!suite.add(utility::is<double>("inf"))) |
---|
176 | suite.err() << "is<double>(\"inf\") should return true\n"; |
---|
177 | if (!suite.add(utility::is<double>("NaN"))) |
---|
178 | suite.err() << "is<double>(\"NaN\") should return true\n"; |
---|
179 | if (!suite.add(utility::is<double>("1.23"))) |
---|
180 | suite.err() << "is<double>(\"1.23\") should return true\n"; |
---|
181 | if (!suite.add(!utility::is<double>("1.23.2"))) |
---|
182 | suite.err() << "is<double>(\"1.23.2\") should return false\n"; |
---|
183 | if (!suite.add(!utility::is<int>("1.23"))) |
---|
184 | suite.err() << "is<int>(\"1.23\") should return false\n"; |
---|
185 | if (!suite.add(!utility::is<int>(""))) |
---|
186 | suite.err() << "is<int>(\"\") should return false\n"; |
---|
187 | |
---|
188 | { |
---|
189 | utility::Log<long double> f; |
---|
190 | utility::Log<double> f2(1.3); |
---|
191 | f(2.0); |
---|
192 | utility::Exp<double> e; |
---|
193 | e(3.2); |
---|
194 | } |
---|
195 | |
---|
196 | test_get_map(suite); |
---|
197 | test_inverse(suite); |
---|
198 | test_sort_index(suite); |
---|
199 | test_less_nan(suite); |
---|
200 | test_errno_error(suite); |
---|
201 | test_ptr_compare(suite); |
---|
202 | test_load(suite); |
---|
203 | test_load2(suite); |
---|
204 | |
---|
205 | test_compose_functors(suite); |
---|
206 | |
---|
207 | double x = 8.0; |
---|
208 | suite.add(suite.equal(utility::log2(x), 3.0)); |
---|
209 | float xf = x; |
---|
210 | suite.add(suite.equal(utility::log2(xf), 3.0)); |
---|
211 | long double xld = 8.0; |
---|
212 | suite.add(suite.equal(utility::log2(xld), 3.0)); |
---|
213 | |
---|
214 | test_basename(suite); |
---|
215 | test_dirname(suite); |
---|
216 | test_fnmatch(suite); |
---|
217 | |
---|
218 | return suite.return_value(); |
---|
219 | } |
---|
220 | |
---|
221 | |
---|
222 | void test_errno_error_func(void) |
---|
223 | { |
---|
224 | errno=1; |
---|
225 | throw utility::errno_error(""); |
---|
226 | } |
---|
227 | |
---|
228 | |
---|
229 | void test_errno_error(test::Suite& suite) |
---|
230 | { |
---|
231 | suite.out() << "testing errno_error\n"; |
---|
232 | try { |
---|
233 | test_errno_error_func(); |
---|
234 | } |
---|
235 | catch ( utility::errno_error& e) { |
---|
236 | suite.out() << "catching expected exception with what():\n"; |
---|
237 | suite.out() << e.what() << "\n"; |
---|
238 | return; |
---|
239 | } |
---|
240 | suite.err() << "error: expected thrown exception\n"; |
---|
241 | suite.add(false); |
---|
242 | } |
---|
243 | |
---|
244 | |
---|
245 | void test_compose_functors(test::Suite& suite) |
---|
246 | { |
---|
247 | typedef utility::abs<double> U1; |
---|
248 | // we use float here to check conversion from float to double in functors |
---|
249 | typedef utility::Exp<float> U2; |
---|
250 | typedef std::plus<double> B; |
---|
251 | U1 u1; |
---|
252 | U2 u2; |
---|
253 | B b; |
---|
254 | |
---|
255 | utility::compose_f_gx_hy<B, U1, U2> binary = |
---|
256 | utility::make_compose_f_gx_hy(b, u1, u2); |
---|
257 | |
---|
258 | utility::compose_f_gxy<U2, B> binary2 = |
---|
259 | utility::make_compose_f_gxy(u2, b); |
---|
260 | |
---|
261 | utility::compose_f_gx<U1, U2> unary = |
---|
262 | utility::make_compose_f_gx(u1, u2); |
---|
263 | } |
---|
264 | |
---|
265 | |
---|
266 | void test_dereferencer(test::Suite& suite) |
---|
267 | { |
---|
268 | using utility::Dereferencer; |
---|
269 | Dereferencer<double*> deref; |
---|
270 | double x = 0; |
---|
271 | double* px = &x; |
---|
272 | deref(px) = 1.662; |
---|
273 | if (!suite.add(x==1.662)) { |
---|
274 | suite.out() << "test_dereferencer failed: x: " << x << "\n"; |
---|
275 | } |
---|
276 | } |
---|
277 | |
---|
278 | |
---|
279 | void test_get_map(test::Suite& suite) |
---|
280 | { |
---|
281 | std::map<std::string, int> m; |
---|
282 | m["one"] = 1; |
---|
283 | m["two"] = 2; |
---|
284 | const std::map<std::string, int> m2(m); |
---|
285 | int i = utility::get(m2, static_cast<std::string>("one")); |
---|
286 | i = 0; // avoid compiler warning |
---|
287 | try { |
---|
288 | utility::get(m2, static_cast<std::string>("three")); |
---|
289 | suite.add(false); |
---|
290 | } |
---|
291 | catch (std::runtime_error& e) { |
---|
292 | suite.out() << "expected exception thrown with what() = " |
---|
293 | << e.what() << "\n"; |
---|
294 | } |
---|
295 | } |
---|
296 | |
---|
297 | void test_less_nan(test::Suite& suite) |
---|
298 | { |
---|
299 | suite.out() << "test less_nan\n"; |
---|
300 | bool prior_ok=suite.ok(); |
---|
301 | utility::less_nan<double> f; |
---|
302 | suite.add(f(2.7,3.14)); |
---|
303 | suite.add(!f(2.7,-3.14)); |
---|
304 | suite.add(!f(2.7,2.7)); |
---|
305 | suite.add(f(3.14, std::numeric_limits<double>::quiet_NaN())); |
---|
306 | suite.add(!f(std::numeric_limits<double>::quiet_NaN(), 2.7)); |
---|
307 | suite.add(!f(std::numeric_limits<double>::quiet_NaN(), |
---|
308 | std::numeric_limits<double>::quiet_NaN())); |
---|
309 | utility::less_nan<utility::DataWeight> fw; |
---|
310 | suite.add(fw(utility::DataWeight(2), utility::DataWeight(3))); |
---|
311 | |
---|
312 | bool post_ok=suite.ok(); |
---|
313 | if (!post_ok && prior_ok) |
---|
314 | suite.err() << "test_less_nan failed\n"; |
---|
315 | } |
---|
316 | |
---|
317 | |
---|
318 | void test_load(test::Suite& suite) |
---|
319 | { |
---|
320 | std::istringstream iss(std::string("1.69 3.14")); |
---|
321 | std::vector<double> double_vec; |
---|
322 | utility::load(iss, double_vec); |
---|
323 | suite.add(double_vec.size()==2); |
---|
324 | |
---|
325 | std::istringstream iss2(std::string("1 2")); |
---|
326 | std::vector<double> uint_vec; |
---|
327 | utility::load(iss2, uint_vec); |
---|
328 | suite.add(uint_vec.size()==2); |
---|
329 | |
---|
330 | std::istringstream iss3(std::string("1.69 3.14")); |
---|
331 | std::vector<std::string> str_vec; |
---|
332 | utility::load(iss3, str_vec); |
---|
333 | if (str_vec.size()==2) { |
---|
334 | if (str_vec[1]!="3.14") { |
---|
335 | suite.out() << "error: load<string>:" << "\n" |
---|
336 | << " str_vec[1] = " << str_vec[1] << "\n" |
---|
337 | << " expected: 3.14n"; |
---|
338 | suite.add(false); |
---|
339 | } |
---|
340 | } |
---|
341 | else { |
---|
342 | suite.out() << "str_vec.size() is not 2\n"; |
---|
343 | suite.add(false); |
---|
344 | } |
---|
345 | } |
---|
346 | |
---|
347 | |
---|
348 | void test_load2(test::Suite& suite) |
---|
349 | { |
---|
350 | suite.out() << "test load<double>(matrix)\n"; |
---|
351 | std::string str("1.69 3.14\n1.23 1.22\n"); |
---|
352 | std::istringstream iss(str); |
---|
353 | std::vector<std::vector<double> > double_m; |
---|
354 | utility::load(iss, double_m, '\0', '\n'); |
---|
355 | suite.add(double_m.size()==2); |
---|
356 | |
---|
357 | suite.out() << "test load<std::string>(matrix)\n"; |
---|
358 | suite.out() << str; |
---|
359 | std::istringstream iss2(str); |
---|
360 | std::vector<std::vector<std::string> > str_m; |
---|
361 | utility::load(iss2, str_m, '\0', '\n'); |
---|
362 | std::cout << str_m.size() << std::endl; |
---|
363 | for (size_t i=0; i<str_m.size(); ++i) |
---|
364 | for (size_t j=0; j<str_m[i].size(); ++j) |
---|
365 | suite.out() << str_m[i][j] << "\n"; |
---|
366 | suite.add(str_m.size()==2); |
---|
367 | } |
---|
368 | |
---|
369 | |
---|
370 | void test_inverse(test::Suite& suite) |
---|
371 | { |
---|
372 | suite.err() << "Testing inverse\n"; |
---|
373 | std::vector<std::string> vec; |
---|
374 | vec.push_back("No"); |
---|
375 | vec.push_back("one"); |
---|
376 | vec.push_back("shall"); |
---|
377 | vec.push_back("be"); |
---|
378 | vec.push_back("subjected"); |
---|
379 | std::map<std::string, std::vector<size_t> > map; |
---|
380 | utility::inverse(vec.begin(), vec.end(), map); |
---|
381 | test_inverse_validate(vec.begin(), vec.end(), map, suite); |
---|
382 | utility::inverse(vec.begin()+1, vec.end(), map); |
---|
383 | test_inverse_validate(vec.begin()+1, vec.end(), map, suite); |
---|
384 | const std::vector<std::string> vec2(vec); |
---|
385 | utility::inverse(vec2.begin(), vec2.end(), map); |
---|
386 | test_inverse_validate(vec2.begin(), vec2.end(), map, suite); |
---|
387 | std::multimap<std::string, size_t> mmap; |
---|
388 | utility::inverse(vec.begin(), vec.end(), mmap); |
---|
389 | test_inverse_validate(vec.begin(), mmap, suite); |
---|
390 | utility::inverse(vec.begin()+1, vec.end(), mmap); |
---|
391 | test_inverse_validate(vec.begin()+1, mmap, suite); |
---|
392 | // do not run compile tests |
---|
393 | if (false) { |
---|
394 | std::map<std::string, std::vector<size_t> > m; |
---|
395 | boost::input_iterator_archetype<std::string> start; |
---|
396 | boost::input_iterator_archetype<std::string> end; |
---|
397 | utility::inverse(start, end, m); |
---|
398 | std::map<std::string, std::vector<size_t>, std::greater<std::string> > m2; |
---|
399 | utility::inverse(start, end, m2); |
---|
400 | std::multimap<std::string, size_t> m3; |
---|
401 | utility::inverse(start, end, m3); |
---|
402 | std::multimap<std::string, size_t, std::greater<std::string> > m4; |
---|
403 | utility::inverse(start, end, m4); |
---|
404 | |
---|
405 | std::map<std::string, size_t> unique_map; |
---|
406 | utility::inverse(start, end, unique_map); |
---|
407 | } |
---|
408 | } |
---|
409 | |
---|
410 | |
---|
411 | template<typename InputIterator, typename Key> |
---|
412 | void test_inverse_validate(InputIterator first, InputIterator last, |
---|
413 | const std::map<Key, std::vector<size_t> >& m, |
---|
414 | test::Suite& suite) |
---|
415 | { |
---|
416 | typedef typename std::map<Key, std::vector<size_t> >::const_iterator Iter; |
---|
417 | for ( InputIterator iter=first; iter != last; ++iter) { |
---|
418 | Iter map_iter = m.find(*iter); |
---|
419 | if (!suite.add(map_iter!=m.end())) { |
---|
420 | suite.err() << "test_inverse_validate() failed\n"; |
---|
421 | suite.err() << " could not find: " << *first << " in map m\n"; |
---|
422 | } |
---|
423 | for (size_t i=0; i<map_iter->second.size(); ++i) |
---|
424 | if (!suite.add(map_iter->second[i] == |
---|
425 | static_cast<size_t>(distance(first, iter))) ) { |
---|
426 | suite.err() << "test_inverse_validate() failed\n"; |
---|
427 | suite.err() << " comparing: " << map_iter->second[i] |
---|
428 | << " and " << distance(first, iter) |
---|
429 | << " expected them to be equal\n"; |
---|
430 | } |
---|
431 | } |
---|
432 | } |
---|
433 | |
---|
434 | |
---|
435 | template<typename InputIterator, typename Key> |
---|
436 | void test_inverse_validate(InputIterator first, |
---|
437 | const std::multimap<Key, size_t>& m, |
---|
438 | test::Suite& suite) |
---|
439 | { |
---|
440 | for (typename std::multimap<Key, size_t>::const_iterator iter=m.begin(); |
---|
441 | iter!=m.end(); ++iter) { |
---|
442 | suite.add(*(first + iter->second) == iter->first); |
---|
443 | } |
---|
444 | |
---|
445 | } |
---|
446 | |
---|
447 | void test_sort_index(test::Suite& suite) |
---|
448 | { |
---|
449 | suite.err() << "testing sort_index" << std::endl; |
---|
450 | utility::Vector a(10); |
---|
451 | for (size_t i=0; i<a.size(); ++i) |
---|
452 | a(i) = std::pow(i-4.2,2); |
---|
453 | std::vector<size_t> vec; |
---|
454 | utility::sort_index(vec, a); |
---|
455 | |
---|
456 | std::vector<size_t> vec2; |
---|
457 | utility::sort_index(a.begin(), a.end(), vec2); |
---|
458 | if (vec.size()==vec2.size()) { |
---|
459 | if (!suite.equal_range(vec.begin(), vec.end(), vec2.begin())) { |
---|
460 | suite.add(false); |
---|
461 | } |
---|
462 | } |
---|
463 | else { |
---|
464 | suite.add(false); |
---|
465 | suite.err() << "size mismatch: vec.size()=" << vec.size() |
---|
466 | << "vec2.size()=" << vec2.size() << "\n"; |
---|
467 | } |
---|
468 | const utility::VectorConstView b(a, 0, 5, 2); |
---|
469 | |
---|
470 | std::vector<size_t> vec3; |
---|
471 | utility::sort_index(vec3, b); |
---|
472 | std::vector<size_t> vec4; |
---|
473 | utility::sort_index(b.begin(), b.end(), vec4); |
---|
474 | if (vec3.size()!=vec4.size()) { |
---|
475 | suite.add(false); |
---|
476 | suite.err() << "size mismatch: vec3.size()=" << vec3.size() |
---|
477 | << " vec4.size()=" << vec4.size() << "\n"; |
---|
478 | } |
---|
479 | else { |
---|
480 | if (!suite.equal_range(vec3.begin(), vec3.end(), vec4.begin())){ |
---|
481 | suite.add(false); |
---|
482 | } |
---|
483 | } |
---|
484 | |
---|
485 | std::list<double> list; |
---|
486 | for (size_t i=0; i<a.size(); ++i) |
---|
487 | list.push_back(a(i)); |
---|
488 | std::vector<size_t> vec5; |
---|
489 | utility::sort_index(list.begin(), list.end(), vec5); |
---|
490 | if (vec.size()!=vec5.size()) { |
---|
491 | suite.add(false); |
---|
492 | suite.err() << "size mismatch: vec.size()=" << vec.size() |
---|
493 | << " vec5.size()=" << vec5.size() << "\n"; |
---|
494 | } |
---|
495 | else { |
---|
496 | if (!suite.equal_range(vec.begin(), vec.end(), vec5.begin())){ |
---|
497 | suite.add(false); |
---|
498 | } |
---|
499 | } |
---|
500 | |
---|
501 | // do not run compiler tests |
---|
502 | if (false) { |
---|
503 | std::vector<size_t> vec; |
---|
504 | utility::sort_index(boost::forward_iterator_archetype<double>(), |
---|
505 | boost::forward_iterator_archetype<double>(), |
---|
506 | vec); |
---|
507 | } |
---|
508 | } |
---|
509 | |
---|
510 | void test_ptr_compare(test::Suite& suite) |
---|
511 | { |
---|
512 | using utility::make_ptr_compare; |
---|
513 | std::vector<double*> vec(10); |
---|
514 | std::greater<double> d_greater; |
---|
515 | for (size_t i=0; i<vec.size(); ++i) |
---|
516 | vec[i] = new double(i); |
---|
517 | std::sort(vec.begin(), vec.end(), make_ptr_compare(*vec.begin(), d_greater)); |
---|
518 | for (size_t i=1; i<vec.size(); ++i) { |
---|
519 | suite.add( *vec[i-1] > *vec[i]); |
---|
520 | } |
---|
521 | std::sort(vec.begin(), vec.end(), make_ptr_compare(*vec.begin())); |
---|
522 | for (size_t i=1; i<vec.size(); ++i) { |
---|
523 | suite.add( *vec[i-1] < *vec[i]); |
---|
524 | } |
---|
525 | for (size_t i=0; i<vec.size(); ++i) |
---|
526 | delete vec[i]; |
---|
527 | |
---|
528 | } |
---|
529 | |
---|
530 | |
---|
531 | void test_fnmatch(test::Suite& suite) |
---|
532 | { |
---|
533 | bool ok=true; |
---|
534 | ok &= test_fnmatch(true,"peter", "peter"); |
---|
535 | ok &= test_fnmatch(false,"peter", "peterj"); |
---|
536 | |
---|
537 | ok &= test_fnmatch(true,"*", "peterj"); |
---|
538 | ok &= test_fnmatch(true,"p*", "peterj"); |
---|
539 | ok &= test_fnmatch(true, "p*", "peter"); |
---|
540 | ok &= test_fnmatch(false, "p*j", "peter"); |
---|
541 | ok &= test_fnmatch(true, "p*j", "peterj"); |
---|
542 | ok &= test_fnmatch(true, "*peter", "peter"); |
---|
543 | |
---|
544 | ok &= test_fnmatch(true, "p?ter", "peter"); |
---|
545 | ok &= test_fnmatch(false, "p?er", "peter"); |
---|
546 | ok &= test_fnmatch(false, "p?eter", "peter"); |
---|
547 | |
---|
548 | ok &= test_fnmatch(true, "filename", "filename"); |
---|
549 | ok &= test_fnmatch(true, "*name", "filename"); |
---|
550 | ok &= test_fnmatch(true, "[fa]il?name", "filename"); |
---|
551 | ok &= test_fnmatch(true, "[fa]*il?name", "ffilename"); |
---|
552 | |
---|
553 | ok &= test_fnmatch(true, "[fa]*il?name", "fafafailename"); |
---|
554 | ok &= test_fnmatch(false, "[fa]?il?name", "ilename"); |
---|
555 | ok &= test_fnmatch(false, "?[fa]il?name", "ilename"); |
---|
556 | ok &= test_fnmatch(true, "[fa]il?name", "filename"); |
---|
557 | ok &= test_fnmatch(false, "[fa]?il?name", "fafafailename"); |
---|
558 | |
---|
559 | ok &= test_fnmatch(true, "*name", "/path/to/filename"); |
---|
560 | ok &= test_fnmatch(true, "*name", "file.name"); |
---|
561 | ok &= test_fnmatch(true, "*.txt", ".file.txt"); |
---|
562 | suite.add(ok); |
---|
563 | } |
---|
564 | |
---|
565 | |
---|
566 | void test_dirname(test::Suite& suite) |
---|
567 | { |
---|
568 | std::vector<std::string> path; |
---|
569 | path.push_back("/usr/lib"); |
---|
570 | path.push_back("/usr/"); |
---|
571 | path.push_back("usr"); |
---|
572 | path.push_back("/"); |
---|
573 | path.push_back("."); |
---|
574 | path.push_back(".."); |
---|
575 | std::vector<std::string> dir; |
---|
576 | dir.push_back("/usr"); |
---|
577 | dir.push_back("/"); |
---|
578 | dir.push_back("."); |
---|
579 | dir.push_back("/"); |
---|
580 | dir.push_back("."); |
---|
581 | dir.push_back("."); |
---|
582 | assert(dir.size()==path.size()); |
---|
583 | using utility::dirname; |
---|
584 | for (size_t i=0; i<dir.size(); ++i) { |
---|
585 | if (dir[i] != dirname(path[i])) { |
---|
586 | suite.add(false); |
---|
587 | suite.out() << "error:\n"; |
---|
588 | suite.out() << "path: " << path[i] << "\n"; |
---|
589 | suite.out() << "directory_name: " << dirname(path[i]) << "\n"; |
---|
590 | suite.out() << "expected: " << dir[i] << "\n"; |
---|
591 | } |
---|
592 | } |
---|
593 | } |
---|
594 | |
---|
595 | |
---|
596 | void test_basename(test::Suite& suite) |
---|
597 | { |
---|
598 | std::vector<std::string> path; |
---|
599 | path.push_back("/usr/lib"); |
---|
600 | path.push_back("/usr/"); |
---|
601 | path.push_back("usr"); |
---|
602 | path.push_back("/"); |
---|
603 | path.push_back("."); |
---|
604 | path.push_back(".."); |
---|
605 | std::vector<std::string> file; |
---|
606 | file.push_back("lib"); |
---|
607 | file.push_back("usr"); |
---|
608 | file.push_back("usr"); |
---|
609 | file.push_back("/"); |
---|
610 | file.push_back("."); |
---|
611 | file.push_back(".."); |
---|
612 | assert(file.size()==path.size()); |
---|
613 | using utility::basename; |
---|
614 | for (size_t i=0; i<file.size(); ++i) { |
---|
615 | if (file[i] != basename(path[i])) { |
---|
616 | suite.add(false); |
---|
617 | suite.out() << "error:\n"; |
---|
618 | suite.out() << "path: " << path[i] << "\n"; |
---|
619 | suite.out() << "basename: " << basename(path[i]) << "\n"; |
---|
620 | suite.out() << "expected: " << file[i] << "\n"; |
---|
621 | } |
---|
622 | } |
---|
623 | } |
---|
624 | |
---|
625 | |
---|
626 | bool test_fnmatch(bool answ, std::string a, std::string b) |
---|
627 | { |
---|
628 | using namespace utility; |
---|
629 | bool res = fnmatch(a, b); |
---|
630 | // check that fnmatch and regexp agree |
---|
631 | if (res == answ) |
---|
632 | return true; |
---|
633 | if (res!=answ) |
---|
634 | std::cerr << "fnmatch(" << a << ", " << b << ") results " |
---|
635 | << res |
---|
636 | << ". Expects " << answ << std::endl; |
---|
637 | return false; |
---|
638 | } |
---|