1 | // $Id: utility.cc 3855 2020-01-02 01:11:34Z 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, 2015, 2016, 2017, 2018, 2019 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 | #include <boost/iterator/iterator_archetypes.hpp> |
---|
38 | |
---|
39 | #include <cassert> |
---|
40 | #include <cerrno> |
---|
41 | #include <fstream> |
---|
42 | #include <iostream> |
---|
43 | #include <limits> |
---|
44 | #include <list> |
---|
45 | #include <map> |
---|
46 | #include <string> |
---|
47 | #include <vector> |
---|
48 | |
---|
49 | using namespace theplu::yat; |
---|
50 | void test_binary_weight(void); |
---|
51 | void test_errno_error(test::Suite& suite); |
---|
52 | void test_errno_error_func(void); |
---|
53 | void test_get_map(test::Suite&); |
---|
54 | void test_load(test::Suite&); |
---|
55 | void test_load2(test::Suite&); |
---|
56 | void test_inverse(test::Suite&); |
---|
57 | |
---|
58 | void test_dirname(test::Suite& suite); |
---|
59 | void test_basename(test::Suite& suite); |
---|
60 | void test_fnmatch(test::Suite& suite); |
---|
61 | bool test_fnmatch(bool, std::string, std::string); |
---|
62 | void test_getcwd(test::Suite& suite); |
---|
63 | void test_remove(test::Suite& suite); |
---|
64 | void test_rename(test::Suite& suite); |
---|
65 | void test_replace(test::Suite& suite); |
---|
66 | void test_symlink(test::Suite& suite); |
---|
67 | |
---|
68 | template<typename InputIterator, typename Key> |
---|
69 | void test_inverse_validate(InputIterator first, InputIterator last, |
---|
70 | const std::map<Key, std::vector<size_t> >& m, |
---|
71 | test::Suite& suite); |
---|
72 | template<typename InputIterator, typename Key> |
---|
73 | void test_inverse_validate(InputIterator first, |
---|
74 | const std::multimap<Key, size_t>& m, |
---|
75 | test::Suite& suite); |
---|
76 | |
---|
77 | void test_sort_index(test::Suite& suite); |
---|
78 | void test_sum_weight(test::Suite& suite); |
---|
79 | void test_less_nan(test::Suite& suite); |
---|
80 | void test_ptr_compare(test::Suite& suite); |
---|
81 | void test_compose_functors(test::Suite& suite); |
---|
82 | void test_dereferencer(test::Suite& suite); |
---|
83 | void test_ticket842(test::Suite& suite); |
---|
84 | |
---|
85 | int main(int argc, char* argv[]) |
---|
86 | { |
---|
87 | using namespace theplu::yat; |
---|
88 | test::Suite suite(argc, argv); |
---|
89 | suite.err() << "testing utility ... " << std::endl; |
---|
90 | |
---|
91 | // test float/double |
---|
92 | std::string s("1.2"); |
---|
93 | if (!utility::is<double>(s)){ |
---|
94 | suite.add(false); |
---|
95 | } |
---|
96 | else if (!utility::is<float>(s)) { |
---|
97 | suite.add(false); |
---|
98 | } |
---|
99 | else if (utility::is<int>(s)) { |
---|
100 | suite.add(false); |
---|
101 | } |
---|
102 | else if (utility::is_nan(s)) { |
---|
103 | suite.add(false); |
---|
104 | } |
---|
105 | |
---|
106 | // test int |
---|
107 | s="23"; |
---|
108 | if (!utility::is<double>(s)){ |
---|
109 | suite.add(false); |
---|
110 | } |
---|
111 | else if (!utility::is<float>(s)) { |
---|
112 | suite.add(false); |
---|
113 | } |
---|
114 | else if (!utility::is<int>(s)) { |
---|
115 | suite.add(false); |
---|
116 | } |
---|
117 | else if (utility::is_nan(s)) { |
---|
118 | suite.add(false); |
---|
119 | } |
---|
120 | |
---|
121 | // test nan |
---|
122 | s=" nAn "; |
---|
123 | if (!utility::is<double>(s)){ |
---|
124 | suite.add(false); |
---|
125 | suite.err() << "error: " << s << " is a double\n"; |
---|
126 | } |
---|
127 | /* we don't require NaN for float and int |
---|
128 | else if (!utility::is_float(s)) { |
---|
129 | suite.add(false); |
---|
130 | suite.err() << "error: " << s << " is a float\n"; |
---|
131 | } |
---|
132 | else if (!utility::is_int(s)) { |
---|
133 | suite.add(false); |
---|
134 | suite.err() << "error: " << s << " is a int\n"; |
---|
135 | } |
---|
136 | */ |
---|
137 | else if (!utility::is_nan(s)) { |
---|
138 | suite.add(false); |
---|
139 | suite.err() << "error: " << s << " is nan\n"; |
---|
140 | |
---|
141 | } |
---|
142 | |
---|
143 | // testing trailing values |
---|
144 | s=" 23 23 "; |
---|
145 | if (utility::is<double>(s)){ |
---|
146 | suite.add(false); |
---|
147 | } |
---|
148 | else if (utility::is<float>(s)) { |
---|
149 | suite.add(false); |
---|
150 | } |
---|
151 | else if (utility::is<int>(s)) { |
---|
152 | suite.add(false); |
---|
153 | } |
---|
154 | else if (utility::is_nan(s)) { |
---|
155 | suite.add(false); |
---|
156 | } |
---|
157 | |
---|
158 | if (utility::convert<double>("1.23")!=1.23) |
---|
159 | suite.add(false); |
---|
160 | utility::convert<double>("-inf"); |
---|
161 | utility::convert<double>("inf"); |
---|
162 | utility::convert<double>("NaN"); |
---|
163 | utility::convert<double>("-NaN"); |
---|
164 | utility::convert<double>(" -NaN"); |
---|
165 | |
---|
166 | suite.add(utility::convert(1.23)=="1.23"); |
---|
167 | suite.add(utility::convert(1)=="1"); |
---|
168 | |
---|
169 | suite.add(utility::is<unsigned int>("1")); |
---|
170 | suite.add(!utility::is<unsigned int>(" ")); |
---|
171 | suite.add(!utility::is<unsigned int>(" -1")); |
---|
172 | // test -1 cannot be assigned to unsigned int |
---|
173 | try { |
---|
174 | unsigned int x = utility::convert<unsigned int>("-1"); |
---|
175 | suite.err() << "convert<unsigned>(\"-1\") did not throw\n"; |
---|
176 | suite.err() << "x: " << x << "\n"; |
---|
177 | suite.add(false); |
---|
178 | exit(1); |
---|
179 | } |
---|
180 | catch (std::runtime_error& e) { |
---|
181 | suite.out() << "expected error: " << e.what() << "\n"; |
---|
182 | } |
---|
183 | double result; |
---|
184 | if (!suite.add(utility::convert_try<double>("3.14", result))) |
---|
185 | suite.err() << "convert_tr<double>(\"3.14\", ...) failed\n"; |
---|
186 | if (!suite.add(!utility::convert_try<double>("3.14xx", result))) |
---|
187 | suite.err() << "convert_tr<double>(\"3.14xx\", ...) succeeded\n"; |
---|
188 | |
---|
189 | if (!suite.add(utility::is<double>("-inf"))) |
---|
190 | suite.err() << "is<double>(\"-inf\") should return true\n"; |
---|
191 | if (!suite.add(utility::is<double>("inf"))) |
---|
192 | suite.err() << "is<double>(\"inf\") should return true\n"; |
---|
193 | if (!suite.add(utility::is<double>("NaN"))) |
---|
194 | suite.err() << "is<double>(\"NaN\") should return true\n"; |
---|
195 | if (!suite.add(utility::is<double>("1.23"))) |
---|
196 | suite.err() << "is<double>(\"1.23\") should return true\n"; |
---|
197 | if (!suite.add(!utility::is<double>("1.23.2"))) |
---|
198 | suite.err() << "is<double>(\"1.23.2\") should return false\n"; |
---|
199 | if (!suite.add(!utility::is<int>("1.23"))) |
---|
200 | suite.err() << "is<int>(\"1.23\") should return false\n"; |
---|
201 | if (!suite.add(!utility::is<int>(""))) |
---|
202 | suite.err() << "is<int>(\"\") should return false\n"; |
---|
203 | |
---|
204 | { |
---|
205 | utility::Log<long double> f; |
---|
206 | utility::Log<double> f2(1.3); |
---|
207 | f(2.0); |
---|
208 | utility::Exp<double> e; |
---|
209 | e(3.2); |
---|
210 | } |
---|
211 | |
---|
212 | test_get_map(suite); |
---|
213 | test_inverse(suite); |
---|
214 | test_sort_index(suite); |
---|
215 | test_sum_weight(suite); |
---|
216 | test_less_nan(suite); |
---|
217 | test_errno_error(suite); |
---|
218 | test_ptr_compare(suite); |
---|
219 | test_load(suite); |
---|
220 | test_load2(suite); |
---|
221 | |
---|
222 | test_compose_functors(suite); |
---|
223 | |
---|
224 | double x = 8.0; |
---|
225 | suite.add(suite.equal(utility::log2(x), 3.0)); |
---|
226 | float xf = x; |
---|
227 | suite.add(suite.equal(utility::log2(xf), 3.0)); |
---|
228 | long double xld = 8.0; |
---|
229 | suite.add(suite.equal(utility::log2(xld), 3.0)); |
---|
230 | |
---|
231 | std::vector<std::string> vec(1, "text"); |
---|
232 | utility::clear(vec); |
---|
233 | suite.add(vec.size()==0); |
---|
234 | |
---|
235 | test_basename(suite); |
---|
236 | test_binary_weight(); |
---|
237 | test_dirname(suite); |
---|
238 | test_fnmatch(suite); |
---|
239 | test_getcwd(suite); |
---|
240 | test_remove(suite); |
---|
241 | test_rename(suite); |
---|
242 | test_replace(suite); |
---|
243 | test_symlink(suite); |
---|
244 | test_ticket842(suite); |
---|
245 | |
---|
246 | return suite.return_value(); |
---|
247 | } |
---|
248 | |
---|
249 | |
---|
250 | void test_binary_weight(void) |
---|
251 | { |
---|
252 | // do not run compilation test |
---|
253 | return; |
---|
254 | // this does not work, see ticket #865 |
---|
255 | // boost::input_iterator_archetype<double> in; |
---|
256 | boost::input_iterator_archetype_no_proxy<double> in; |
---|
257 | boost::detail::dummy_constructor arg; |
---|
258 | boost::output_iterator_archetype<double> out(arg); |
---|
259 | utility::binary_weight(in, in, out); |
---|
260 | } |
---|
261 | |
---|
262 | |
---|
263 | void test_errno_error_func(void) |
---|
264 | { |
---|
265 | errno=1; |
---|
266 | throw utility::errno_error(""); |
---|
267 | } |
---|
268 | |
---|
269 | |
---|
270 | void test_errno_error(test::Suite& suite) |
---|
271 | { |
---|
272 | suite.out() << "testing errno_error\n"; |
---|
273 | try { |
---|
274 | test_errno_error_func(); |
---|
275 | } |
---|
276 | catch ( utility::errno_error& e) { |
---|
277 | suite.out() << "catching expected exception with what():\n"; |
---|
278 | suite.out() << e.what() << "\n"; |
---|
279 | return; |
---|
280 | } |
---|
281 | suite.err() << "error: expected thrown exception\n"; |
---|
282 | suite.add(false); |
---|
283 | } |
---|
284 | |
---|
285 | |
---|
286 | void test_compose_functors(test::Suite& suite) |
---|
287 | { |
---|
288 | typedef utility::abs<double> U1; |
---|
289 | // we use float here to check conversion from float to double in functors |
---|
290 | typedef utility::Exp<float> U2; |
---|
291 | typedef std::plus<double> B; |
---|
292 | U1 u1; |
---|
293 | U2 u2; |
---|
294 | B b; |
---|
295 | |
---|
296 | utility::compose_f_gx_hy<B, U1, U2> binary; |
---|
297 | binary = utility::make_compose_f_gx_hy(b, u1, u2); |
---|
298 | |
---|
299 | utility::compose_f_gxy<U2, B> binary2; |
---|
300 | binary2 = utility::make_compose_f_gxy(u2, b); |
---|
301 | |
---|
302 | utility::compose_f_gx<U1, U2> unary; |
---|
303 | unary = utility::make_compose_f_gx(u1, u2); |
---|
304 | |
---|
305 | utility::compose_f_gx_hx<B, U1, U2> unary2; |
---|
306 | unary2 = utility::make_compose_f_gx_hx(b, u1, u2); |
---|
307 | if (!suite.add(suite.equal_fix(unary2(-2.0), 2.0 + std::exp(-2.0),1e-8))) |
---|
308 | suite.err() << "compose_f_gx_hx failed\n"; |
---|
309 | } |
---|
310 | |
---|
311 | |
---|
312 | void test_dereferencer(test::Suite& suite) |
---|
313 | { |
---|
314 | using utility::Dereferencer; |
---|
315 | Dereferencer<double*> deref; |
---|
316 | double x = 0; |
---|
317 | double* px = &x; |
---|
318 | deref(px) = 1.662; |
---|
319 | if (!suite.add(x==1.662)) { |
---|
320 | suite.out() << "test_dereferencer failed: x: " << x << "\n"; |
---|
321 | } |
---|
322 | } |
---|
323 | |
---|
324 | |
---|
325 | void test_get_map(test::Suite& suite) |
---|
326 | { |
---|
327 | std::map<std::string, int> m; |
---|
328 | m["one"] = 1; |
---|
329 | m["two"] = 2; |
---|
330 | const std::map<std::string, int> m2(m); |
---|
331 | int i = utility::get(m2, static_cast<std::string>("one")); |
---|
332 | test::avoid_compiler_warning(i); // avoid compiler warning |
---|
333 | try { |
---|
334 | utility::get(m2, static_cast<std::string>("three")); |
---|
335 | suite.add(false); |
---|
336 | } |
---|
337 | catch (std::runtime_error& e) { |
---|
338 | suite.out() << "expected exception thrown with what() = " |
---|
339 | << e.what() << "\n"; |
---|
340 | } |
---|
341 | // test ticket #799 |
---|
342 | if (!suite.add(utility::get(m, "one")==1)) |
---|
343 | suite.err() << "get(m, \"one\")!=1\n"; |
---|
344 | |
---|
345 | // don't run compiler test code |
---|
346 | if (false) { |
---|
347 | typedef boost::default_constructible_archetype< |
---|
348 | boost::sgi_assignable_archetype<> > key_type; |
---|
349 | |
---|
350 | typedef boost::default_constructible_archetype< |
---|
351 | boost::sgi_assignable_archetype< |
---|
352 | boost::null_archetype<std::string> > > value_type; |
---|
353 | |
---|
354 | typedef boost::default_constructible_archetype< |
---|
355 | boost::binary_function_archetype<key_type, key_type, bool> > Compare; |
---|
356 | |
---|
357 | std::map<key_type, value_type, Compare> m3; |
---|
358 | |
---|
359 | // create an object that is convertible to key_type |
---|
360 | boost::detail::dummy_constructor x; |
---|
361 | boost::convertible_to_archetype<key_type> my_key(x); |
---|
362 | utility::get(m3, my_key); |
---|
363 | } |
---|
364 | } |
---|
365 | |
---|
366 | void test_less_nan(test::Suite& suite) |
---|
367 | { |
---|
368 | suite.out() << "test less_nan\n"; |
---|
369 | bool prior_ok=suite.ok(); |
---|
370 | utility::less_nan<double> f; |
---|
371 | suite.add(f(2.7,3.14)); |
---|
372 | suite.add(!f(2.7,-3.14)); |
---|
373 | suite.add(!f(2.7,2.7)); |
---|
374 | suite.add(f(3.14, std::numeric_limits<double>::quiet_NaN())); |
---|
375 | suite.add(!f(std::numeric_limits<double>::quiet_NaN(), 2.7)); |
---|
376 | suite.add(!f(std::numeric_limits<double>::quiet_NaN(), |
---|
377 | std::numeric_limits<double>::quiet_NaN())); |
---|
378 | utility::less_nan<utility::DataWeight> fw; |
---|
379 | suite.add(fw(utility::DataWeight(2), utility::DataWeight(3))); |
---|
380 | |
---|
381 | bool post_ok=suite.ok(); |
---|
382 | if (!post_ok && prior_ok) |
---|
383 | suite.err() << "test_less_nan failed\n"; |
---|
384 | } |
---|
385 | |
---|
386 | |
---|
387 | void test_load(test::Suite& suite) |
---|
388 | { |
---|
389 | std::istringstream iss(std::string("1.69 3.14")); |
---|
390 | std::vector<double> double_vec; |
---|
391 | utility::load(iss, double_vec); |
---|
392 | suite.add(double_vec.size()==2); |
---|
393 | |
---|
394 | std::istringstream iss2(std::string("1 2")); |
---|
395 | std::vector<double> uint_vec; |
---|
396 | utility::load(iss2, uint_vec); |
---|
397 | suite.add(uint_vec.size()==2); |
---|
398 | |
---|
399 | std::istringstream iss3(std::string("1.69 3.14")); |
---|
400 | std::vector<std::string> str_vec; |
---|
401 | utility::load(iss3, str_vec); |
---|
402 | if (str_vec.size()==2) { |
---|
403 | if (str_vec[1]!="3.14") { |
---|
404 | suite.out() << "error: load<string>:" << "\n" |
---|
405 | << " str_vec[1] = " << str_vec[1] << "\n" |
---|
406 | << " expected: 3.14n"; |
---|
407 | suite.add(false); |
---|
408 | } |
---|
409 | } |
---|
410 | else { |
---|
411 | suite.out() << "str_vec.size() is not 2\n"; |
---|
412 | suite.add(false); |
---|
413 | } |
---|
414 | } |
---|
415 | |
---|
416 | |
---|
417 | void test_load2(test::Suite& suite) |
---|
418 | { |
---|
419 | suite.out() << "test load<double>(matrix)\n"; |
---|
420 | std::string str("1.69 3.14\n1.23 1.22\n"); |
---|
421 | std::istringstream iss(str); |
---|
422 | std::vector<std::vector<double> > double_m; |
---|
423 | utility::load(iss, double_m, '\0', '\n'); |
---|
424 | suite.add(double_m.size()==2); |
---|
425 | |
---|
426 | suite.out() << "test load<std::string>(matrix)\n"; |
---|
427 | suite.out() << str; |
---|
428 | std::istringstream iss2(str); |
---|
429 | std::vector<std::vector<std::string> > str_m; |
---|
430 | utility::load(iss2, str_m, '\0', '\n'); |
---|
431 | std::cout << str_m.size() << std::endl; |
---|
432 | for (size_t i=0; i<str_m.size(); ++i) |
---|
433 | for (size_t j=0; j<str_m[i].size(); ++j) |
---|
434 | suite.out() << str_m[i][j] << "\n"; |
---|
435 | suite.add(str_m.size()==2); |
---|
436 | } |
---|
437 | |
---|
438 | |
---|
439 | void test_inverse(test::Suite& suite) |
---|
440 | { |
---|
441 | suite.err() << "Testing inverse\n"; |
---|
442 | std::vector<std::string> vec; |
---|
443 | vec.push_back("No"); |
---|
444 | vec.push_back("one"); |
---|
445 | vec.push_back("shall"); |
---|
446 | vec.push_back("be"); |
---|
447 | vec.push_back("subjected"); |
---|
448 | std::map<std::string, std::vector<size_t> > map; |
---|
449 | utility::inverse(vec.begin(), vec.end(), map); |
---|
450 | test_inverse_validate(vec.begin(), vec.end(), map, suite); |
---|
451 | utility::inverse(vec.begin()+1, vec.end(), map); |
---|
452 | test_inverse_validate(vec.begin()+1, vec.end(), map, suite); |
---|
453 | const std::vector<std::string> vec2(vec); |
---|
454 | utility::inverse(vec2.begin(), vec2.end(), map); |
---|
455 | test_inverse_validate(vec2.begin(), vec2.end(), map, suite); |
---|
456 | std::multimap<std::string, size_t> mmap; |
---|
457 | utility::inverse(vec.begin(), vec.end(), mmap); |
---|
458 | test_inverse_validate(vec.begin(), mmap, suite); |
---|
459 | utility::inverse(vec.begin()+1, vec.end(), mmap); |
---|
460 | test_inverse_validate(vec.begin()+1, mmap, suite); |
---|
461 | // do not run compile tests |
---|
462 | if (false) { |
---|
463 | std::map<std::string, std::vector<size_t> > m; |
---|
464 | boost::input_iterator_archetype<std::string> start; |
---|
465 | boost::input_iterator_archetype<std::string> end; |
---|
466 | utility::inverse(start, end, m); |
---|
467 | std::map<std::string, std::vector<size_t>, std::greater<std::string> > m2; |
---|
468 | utility::inverse(start, end, m2); |
---|
469 | std::multimap<std::string, size_t> m3; |
---|
470 | utility::inverse(start, end, m3); |
---|
471 | std::multimap<std::string, size_t, std::greater<std::string> > m4; |
---|
472 | utility::inverse(start, end, m4); |
---|
473 | |
---|
474 | std::map<std::string, size_t> unique_map; |
---|
475 | utility::inverse(start, end, unique_map); |
---|
476 | } |
---|
477 | } |
---|
478 | |
---|
479 | |
---|
480 | template<typename InputIterator, typename Key> |
---|
481 | void test_inverse_validate(InputIterator first, InputIterator last, |
---|
482 | const std::map<Key, std::vector<size_t> >& m, |
---|
483 | test::Suite& suite) |
---|
484 | { |
---|
485 | typedef typename std::map<Key, std::vector<size_t> >::const_iterator Iter; |
---|
486 | for ( InputIterator iter=first; iter != last; ++iter) { |
---|
487 | Iter map_iter = m.find(*iter); |
---|
488 | if (!suite.add(map_iter!=m.end())) { |
---|
489 | suite.err() << "test_inverse_validate() failed\n"; |
---|
490 | suite.err() << " could not find: " << *first << " in map m\n"; |
---|
491 | } |
---|
492 | for (size_t i=0; i<map_iter->second.size(); ++i) |
---|
493 | if (!suite.add(map_iter->second[i] == |
---|
494 | static_cast<size_t>(distance(first, iter))) ) { |
---|
495 | suite.err() << "test_inverse_validate() failed\n"; |
---|
496 | suite.err() << " comparing: " << map_iter->second[i] |
---|
497 | << " and " << distance(first, iter) |
---|
498 | << " expected them to be equal\n"; |
---|
499 | } |
---|
500 | } |
---|
501 | } |
---|
502 | |
---|
503 | |
---|
504 | template<typename InputIterator, typename Key> |
---|
505 | void test_inverse_validate(InputIterator first, |
---|
506 | const std::multimap<Key, size_t>& m, |
---|
507 | test::Suite& suite) |
---|
508 | { |
---|
509 | for (typename std::multimap<Key, size_t>::const_iterator iter=m.begin(); |
---|
510 | iter!=m.end(); ++iter) { |
---|
511 | suite.add(*(first + iter->second) == iter->first); |
---|
512 | } |
---|
513 | |
---|
514 | } |
---|
515 | |
---|
516 | |
---|
517 | template<typename Access, typename Traversal> |
---|
518 | void test_sort_index2(Access access, Traversal traversal) |
---|
519 | { |
---|
520 | typedef boost::default_constructible_archetype< |
---|
521 | boost::sgi_assignable_archetype< |
---|
522 | boost::less_than_comparable_archetype<> > > Value; |
---|
523 | boost::iterator_archetype<Value, Access, Traversal> iterator; |
---|
524 | std::vector<size_t> index; |
---|
525 | utility::sort_index(iterator, iterator, index); |
---|
526 | |
---|
527 | typedef boost::default_constructible_archetype< |
---|
528 | boost::sgi_assignable_archetype<> > Value2; |
---|
529 | boost::iterator_archetype<Value2, Access, Traversal> iterator2; |
---|
530 | typedef boost::default_constructible_archetype< |
---|
531 | boost::binary_predicate_archetype<Value2, Value2> |
---|
532 | > Compare; |
---|
533 | Compare comp; |
---|
534 | utility::sort_index(iterator2, iterator2, index, comp); |
---|
535 | } |
---|
536 | |
---|
537 | |
---|
538 | template<typename Access> |
---|
539 | void test_sort_index2(Access access) |
---|
540 | { |
---|
541 | // cannot create iterator_archetype w incrementable_traversal_tag |
---|
542 | //test_sort_index2(access, boost::incrementable_traversal_tag()); |
---|
543 | test_sort_index2(access, boost::forward_traversal_tag()); |
---|
544 | test_sort_index2(access, boost::bidirectional_traversal_tag()); |
---|
545 | test_sort_index2(access, boost::random_access_traversal_tag()); |
---|
546 | } |
---|
547 | |
---|
548 | |
---|
549 | void test_sort_index2(void) |
---|
550 | { |
---|
551 | using namespace boost::iterator_archetypes; |
---|
552 | |
---|
553 | test_sort_index2(readable_iterator_t()); |
---|
554 | test_sort_index2(readable_writable_iterator_t()); |
---|
555 | test_sort_index2(writable_lvalue_iterator_t()); |
---|
556 | } |
---|
557 | |
---|
558 | |
---|
559 | void test_sort_index(test::Suite& suite) |
---|
560 | { |
---|
561 | suite.err() << "testing sort_index" << std::endl; |
---|
562 | utility::Vector a(10); |
---|
563 | for (size_t i=0; i<a.size(); ++i) |
---|
564 | a(i) = std::pow(i-4.2,2); |
---|
565 | std::vector<size_t> vec; |
---|
566 | utility::sort_index(vec, a); |
---|
567 | |
---|
568 | std::vector<size_t> vec2; |
---|
569 | utility::sort_index(a.begin(), a.end(), vec2); |
---|
570 | if (vec.size()==vec2.size()) { |
---|
571 | if (!suite.equal_range(vec.begin(), vec.end(), vec2.begin())) { |
---|
572 | suite.add(false); |
---|
573 | } |
---|
574 | } |
---|
575 | else { |
---|
576 | suite.add(false); |
---|
577 | suite.err() << "size mismatch: vec.size()=" << vec.size() |
---|
578 | << "vec2.size()=" << vec2.size() << "\n"; |
---|
579 | } |
---|
580 | const utility::VectorConstView b(a, 0, 5, 2); |
---|
581 | |
---|
582 | std::vector<size_t> vec3; |
---|
583 | utility::sort_index(vec3, b); |
---|
584 | std::vector<size_t> vec4; |
---|
585 | utility::sort_index(b.begin(), b.end(), vec4); |
---|
586 | if (vec3.size()!=vec4.size()) { |
---|
587 | suite.add(false); |
---|
588 | suite.err() << "size mismatch: vec3.size()=" << vec3.size() |
---|
589 | << " vec4.size()=" << vec4.size() << "\n"; |
---|
590 | } |
---|
591 | else { |
---|
592 | if (!suite.equal_range(vec3.begin(), vec3.end(), vec4.begin())){ |
---|
593 | suite.add(false); |
---|
594 | } |
---|
595 | } |
---|
596 | |
---|
597 | std::list<double> list; |
---|
598 | for (size_t i=0; i<a.size(); ++i) |
---|
599 | list.push_back(a(i)); |
---|
600 | std::vector<size_t> vec5; |
---|
601 | utility::sort_index(list.begin(), list.end(), vec5); |
---|
602 | if (vec.size()!=vec5.size()) { |
---|
603 | suite.add(false); |
---|
604 | suite.err() << "size mismatch: vec.size()=" << vec.size() |
---|
605 | << " vec5.size()=" << vec5.size() << "\n"; |
---|
606 | } |
---|
607 | else { |
---|
608 | if (!suite.equal_range(vec.begin(), vec.end(), vec5.begin())){ |
---|
609 | suite.add(false); |
---|
610 | } |
---|
611 | } |
---|
612 | |
---|
613 | test_sort_index2(); |
---|
614 | } |
---|
615 | |
---|
616 | |
---|
617 | void test_sum_weight(test::Suite& suite) |
---|
618 | { |
---|
619 | // do not run compile test |
---|
620 | if (false) { |
---|
621 | boost::input_iterator_archetype<double> iter; |
---|
622 | utility::sum_weight(iter, iter); |
---|
623 | boost::input_iterator_archetype_no_proxy<utility::DataWeight> iter2; |
---|
624 | utility::sum_weight(iter2, iter2); |
---|
625 | } |
---|
626 | } |
---|
627 | |
---|
628 | |
---|
629 | void test_ptr_compare(test::Suite& suite) |
---|
630 | { |
---|
631 | using utility::make_ptr_compare; |
---|
632 | std::vector<double*> vec(10); |
---|
633 | std::greater<double> d_greater; |
---|
634 | for (size_t i=0; i<vec.size(); ++i) |
---|
635 | vec[i] = new double(i); |
---|
636 | std::sort(vec.begin(), vec.end(), make_ptr_compare(*vec.begin(), d_greater)); |
---|
637 | for (size_t i=1; i<vec.size(); ++i) { |
---|
638 | suite.add( *vec[i-1] > *vec[i]); |
---|
639 | } |
---|
640 | std::sort(vec.begin(), vec.end(), make_ptr_compare(*vec.begin())); |
---|
641 | for (size_t i=1; i<vec.size(); ++i) { |
---|
642 | suite.add( *vec[i-1] < *vec[i]); |
---|
643 | } |
---|
644 | for (size_t i=0; i<vec.size(); ++i) |
---|
645 | delete vec[i]; |
---|
646 | |
---|
647 | } |
---|
648 | |
---|
649 | |
---|
650 | void test_fnmatch(test::Suite& suite) |
---|
651 | { |
---|
652 | bool ok=true; |
---|
653 | ok &= test_fnmatch(true,"peter", "peter"); |
---|
654 | ok &= test_fnmatch(false,"peter", "peterj"); |
---|
655 | |
---|
656 | ok &= test_fnmatch(true,"*", "peterj"); |
---|
657 | ok &= test_fnmatch(true,"p*", "peterj"); |
---|
658 | ok &= test_fnmatch(true, "p*", "peter"); |
---|
659 | ok &= test_fnmatch(false, "p*j", "peter"); |
---|
660 | ok &= test_fnmatch(true, "p*j", "peterj"); |
---|
661 | ok &= test_fnmatch(true, "*peter", "peter"); |
---|
662 | |
---|
663 | ok &= test_fnmatch(true, "p?ter", "peter"); |
---|
664 | ok &= test_fnmatch(false, "p?er", "peter"); |
---|
665 | ok &= test_fnmatch(false, "p?eter", "peter"); |
---|
666 | |
---|
667 | ok &= test_fnmatch(true, "filename", "filename"); |
---|
668 | ok &= test_fnmatch(true, "*name", "filename"); |
---|
669 | ok &= test_fnmatch(true, "[fa]il?name", "filename"); |
---|
670 | ok &= test_fnmatch(true, "[fa]*il?name", "ffilename"); |
---|
671 | |
---|
672 | ok &= test_fnmatch(true, "[fa]*il?name", "fafafailename"); |
---|
673 | ok &= test_fnmatch(false, "[fa]?il?name", "ilename"); |
---|
674 | ok &= test_fnmatch(false, "?[fa]il?name", "ilename"); |
---|
675 | ok &= test_fnmatch(true, "[fa]il?name", "filename"); |
---|
676 | ok &= test_fnmatch(false, "[fa]?il?name", "fafafailename"); |
---|
677 | |
---|
678 | ok &= test_fnmatch(true, "*name", "/path/to/filename"); |
---|
679 | ok &= test_fnmatch(true, "*name", "file.name"); |
---|
680 | ok &= test_fnmatch(true, "*.txt", ".file.txt"); |
---|
681 | suite.add(ok); |
---|
682 | } |
---|
683 | |
---|
684 | |
---|
685 | void test_dirname(test::Suite& suite) |
---|
686 | { |
---|
687 | std::vector<std::string> path; |
---|
688 | path.push_back("/usr/lib"); |
---|
689 | path.push_back("/usr/"); |
---|
690 | path.push_back("usr"); |
---|
691 | path.push_back("/"); |
---|
692 | path.push_back("."); |
---|
693 | path.push_back(".."); |
---|
694 | path.push_back(""); |
---|
695 | std::vector<std::string> dir; |
---|
696 | dir.push_back("/usr"); |
---|
697 | dir.push_back("/"); |
---|
698 | dir.push_back("."); |
---|
699 | dir.push_back("/"); |
---|
700 | dir.push_back("."); |
---|
701 | dir.push_back("."); |
---|
702 | dir.push_back("."); |
---|
703 | assert(dir.size()==path.size()); |
---|
704 | using utility::dirname; |
---|
705 | for (size_t i=0; i<dir.size(); ++i) { |
---|
706 | if (dir[i] != dirname(path[i])) { |
---|
707 | suite.add(false); |
---|
708 | suite.out() << "error:\n"; |
---|
709 | suite.out() << "path: " << path[i] << "\n"; |
---|
710 | suite.out() << "directory_name: " << dirname(path[i]) << "\n"; |
---|
711 | suite.out() << "expected: " << dir[i] << "\n"; |
---|
712 | } |
---|
713 | } |
---|
714 | } |
---|
715 | |
---|
716 | |
---|
717 | void test_basename(test::Suite& suite) |
---|
718 | { |
---|
719 | std::vector<std::string> path; |
---|
720 | path.push_back("/usr/lib"); |
---|
721 | path.push_back("/usr/"); |
---|
722 | path.push_back("usr"); |
---|
723 | path.push_back("/"); |
---|
724 | path.push_back("."); |
---|
725 | path.push_back(".."); |
---|
726 | path.push_back(""); |
---|
727 | std::vector<std::string> file; |
---|
728 | file.push_back("lib"); |
---|
729 | file.push_back("usr"); |
---|
730 | file.push_back("usr"); |
---|
731 | file.push_back("/"); |
---|
732 | file.push_back("."); |
---|
733 | file.push_back(".."); |
---|
734 | file.push_back("."); |
---|
735 | assert(file.size()==path.size()); |
---|
736 | using utility::basename; |
---|
737 | for (size_t i=0; i<file.size(); ++i) { |
---|
738 | if (file[i] != basename(path[i])) { |
---|
739 | suite.add(false); |
---|
740 | suite.out() << "error:\n"; |
---|
741 | suite.out() << "path: " << path[i] << "\n"; |
---|
742 | suite.out() << "basename: " << basename(path[i]) << "\n"; |
---|
743 | suite.out() << "expected: " << file[i] << "\n"; |
---|
744 | } |
---|
745 | } |
---|
746 | } |
---|
747 | |
---|
748 | |
---|
749 | bool test_fnmatch(bool answ, std::string a, std::string b) |
---|
750 | { |
---|
751 | using namespace utility; |
---|
752 | bool res = fnmatch(a, b); |
---|
753 | // check that fnmatch and regexp agree |
---|
754 | if (res == answ) |
---|
755 | return true; |
---|
756 | if (res!=answ) |
---|
757 | std::cerr << "fnmatch(" << a << ", " << b << ") results " |
---|
758 | << res |
---|
759 | << ". Expects " << answ << std::endl; |
---|
760 | return false; |
---|
761 | } |
---|
762 | |
---|
763 | |
---|
764 | void test_getcwd(test::Suite& suite) |
---|
765 | { |
---|
766 | std::string cwd = utility::getcwd(); |
---|
767 | suite.out() << "cwd: " << cwd << "\n"; |
---|
768 | if (cwd.find("/test/testSubDir/") == std::string::npos) { |
---|
769 | suite.add(false); |
---|
770 | suite.err() << "error: cwd: expect path to contain 'test/testSubDir/'\n"; |
---|
771 | } |
---|
772 | } |
---|
773 | |
---|
774 | |
---|
775 | void test_remove(test::Suite& suite) |
---|
776 | { |
---|
777 | // at least test linking |
---|
778 | if (false) |
---|
779 | utility::remove("foo"); |
---|
780 | } |
---|
781 | |
---|
782 | |
---|
783 | void test_rename(test::Suite& suite) |
---|
784 | { |
---|
785 | // at least test linking |
---|
786 | if (false) |
---|
787 | utility::rename("foo", "bar"); |
---|
788 | } |
---|
789 | |
---|
790 | |
---|
791 | void test_replace(test::Suite& suite) |
---|
792 | { |
---|
793 | std::string s = "Some magic string!!!"; |
---|
794 | utility::replace(s, "magic", "arbitrary"); |
---|
795 | if (!suite.add(s=="Some arbitrary string!!!")) |
---|
796 | suite.err() << "error: " << s << "\n"; |
---|
797 | } |
---|
798 | |
---|
799 | |
---|
800 | void test_symlink(test::Suite& suite) |
---|
801 | { |
---|
802 | // at least test function links |
---|
803 | if (false) |
---|
804 | utility::symlink("/from/to/path", "my/symlink"); |
---|
805 | } |
---|
806 | |
---|
807 | |
---|
808 | void test_ticket842(test::Suite& suite) |
---|
809 | { |
---|
810 | suite.out() << "test_ticket842\n"; |
---|
811 | std::vector<std::pair<std::string, int> > data; |
---|
812 | data.push_back(std::make_pair("Orange", 2)); |
---|
813 | data.push_back(std::make_pair("Orange", 1)); |
---|
814 | data.push_back(std::make_pair("Apple", 10)); |
---|
815 | |
---|
816 | typedef utility::PairFirst<const std::pair<std::string, int> > PF; |
---|
817 | typedef std::less<std::string> strLess; |
---|
818 | utility::compose_f_gx_hy<strLess, PF, PF> compare; |
---|
819 | // stable sort should shuffle 'Apple' first, and then keep the order |
---|
820 | // of the equaivalent 'Orange' elements, i.e., <Orange, 2> should stay |
---|
821 | // before <Orange, 1>. |
---|
822 | std::stable_sort(data.begin(), data.end(), compare); |
---|
823 | if (!suite.add(data[0].first == "Apple")) |
---|
824 | suite.err() << "incorrect data[0].first: " << data[0].first << "\n"; |
---|
825 | if (!suite.add(data[1].first == "Orange")) |
---|
826 | suite.err() << "incorrect data[1].first: " << data[1].first << "\n"; |
---|
827 | if (!suite.add(data[1].second == 2)) |
---|
828 | suite.err() << "incorrect data[1].second: " << data[1].second << "\n"; |
---|
829 | } |
---|