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