source: trunk/test/utility.cc @ 3417

Last change on this file since 3417 was 3417, checked in by Peter, 4 years ago

updating copyright statements

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