source: trunk/test/utility.cc @ 3285

Last change on this file since 3285 was 3285, checked in by Peter, 9 years ago

refs #803. Use boost concepts in sort_index. Make requirements more
accurate especially when iterator is random access and value_type
doesn't need to be, e.g., default constructible.

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