source: trunk/test/utility.cc @ 3322

Last change on this file since 3322 was 3322, checked in by Peter, 8 years ago

change name of test function to more descriptive 'avoid_compiler_warning'; test weighted_iterator_traits; refs #803

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 21.0 KB
Line 
1// $Id: utility.cc 3322 2014-10-06 06:41:48Z 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_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  test_basename(suite);
223  test_dirname(suite);
224  test_fnmatch(suite);
225  test_remove(suite);
226  test_rename(suite);
227  test_replace(suite);
228
229  return suite.return_value();
230}
231
232
233void test_errno_error_func(void)
234{
235  errno=1;
236  throw utility::errno_error("");
237}
238
239
240void test_errno_error(test::Suite& suite)
241{
242  suite.out() << "testing errno_error\n";
243  try {
244    test_errno_error_func();
245  }
246  catch ( utility::errno_error& e) {
247    suite.out() << "catching expected exception with what():\n";
248    suite.out() << e.what() << "\n";
249    return;
250  }
251  suite.err() << "error: expected thrown exception\n";
252  suite.add(false);
253}
254
255
256void test_compose_functors(test::Suite& suite)
257{
258  typedef utility::abs<double> U1;
259  // we use float here to check conversion from float to double in functors
260  typedef utility::Exp<float> U2;
261  typedef std::plus<double> B;
262  U1 u1;
263  U2 u2;
264  B b;
265
266  utility::compose_f_gx_hy<B, U1, U2> binary;
267  binary = utility::make_compose_f_gx_hy(b, u1, u2);
268
269  utility::compose_f_gxy<U2, B> binary2;
270  binary2 = utility::make_compose_f_gxy(u2, b);
271
272  utility::compose_f_gx<U1, U2> unary;
273  unary = utility::make_compose_f_gx(u1, u2);
274
275  utility::compose_f_gx_hx<B, U1, U2> unary2;
276  unary2 = utility::make_compose_f_gx_hx(b, u1, u2);
277  if (!suite.add(suite.equal_fix(unary2(-2.0), 2.0 + std::exp(-2.0),1e-8)))
278    suite.err() << "compose_f_gx_hx failed\n";
279}
280
281
282void test_dereferencer(test::Suite& suite)
283{
284  using utility::Dereferencer;
285  Dereferencer<double*> deref;
286  double x = 0;
287  double* px = &x;
288  deref(px) = 1.662;
289  if (!suite.add(x==1.662)) {
290    suite.out() << "test_dereferencer failed: x: " << x << "\n";
291  }
292}
293
294
295void test_get_map(test::Suite& suite)
296{
297  std::map<std::string, int> m;
298  m["one"] = 1;
299  m["two"] = 2;
300  const std::map<std::string, int> m2(m);
301  int i = utility::get(m2, static_cast<std::string>("one"));
302  test::avoid_compiler_warning(i); // avoid compiler warning
303  try {
304    utility::get(m2, static_cast<std::string>("three"));
305    suite.add(false);
306  }
307  catch (std::runtime_error& e) {
308    suite.out() << "expected exception thrown with what() = "
309                << e.what() << "\n";
310  }
311  // test ticket #799
312  if (!suite.add(utility::get(m, "one")==1))
313    suite.err() << "get(m, \"one\")!=1\n";
314
315  // don't run compiler test code
316  if (false) {
317    typedef boost::default_constructible_archetype<
318      boost::sgi_assignable_archetype<> > key_type;
319
320    typedef boost::default_constructible_archetype<
321      boost::sgi_assignable_archetype<
322        boost::null_archetype<std::string> > > value_type;
323
324    typedef boost::default_constructible_archetype<
325      boost::binary_function_archetype<key_type, key_type, bool> > Compare;
326
327    std::map<key_type, value_type, Compare> m3;
328
329    // create an object that is convertible to key_type
330    boost::detail::dummy_constructor x;
331    boost::convertible_to_archetype<key_type> my_key(x);
332    utility::get(m3, my_key);
333  }
334}
335
336void test_less_nan(test::Suite& suite)
337{
338  suite.out() << "test less_nan\n";
339  bool prior_ok=suite.ok();
340  utility::less_nan<double> f;
341  suite.add(f(2.7,3.14));
342  suite.add(!f(2.7,-3.14));
343  suite.add(!f(2.7,2.7));
344  suite.add(f(3.14, std::numeric_limits<double>::quiet_NaN()));
345  suite.add(!f(std::numeric_limits<double>::quiet_NaN(), 2.7));
346  suite.add(!f(std::numeric_limits<double>::quiet_NaN(),
347               std::numeric_limits<double>::quiet_NaN()));
348  utility::less_nan<utility::DataWeight> fw;
349  suite.add(fw(utility::DataWeight(2), utility::DataWeight(3)));
350
351  bool post_ok=suite.ok();
352  if (!post_ok && prior_ok)
353    suite.err() << "test_less_nan failed\n";
354}
355
356
357void test_load(test::Suite& suite)
358{
359  std::istringstream iss(std::string("1.69 3.14"));
360  std::vector<double> double_vec;
361  utility::load(iss, double_vec);
362  suite.add(double_vec.size()==2);
363
364  std::istringstream iss2(std::string("1 2"));
365  std::vector<double> uint_vec;
366  utility::load(iss2, uint_vec);
367  suite.add(uint_vec.size()==2);
368
369  std::istringstream iss3(std::string("1.69 3.14"));
370  std::vector<std::string> str_vec;
371  utility::load(iss3, str_vec);
372  if (str_vec.size()==2) {
373    if (str_vec[1]!="3.14") {
374      suite.out() << "error: load<string>:" << "\n"
375                  << "  str_vec[1] = " << str_vec[1] << "\n"
376                  << "  expected:  3.14n";
377      suite.add(false);
378    }
379  }
380  else {
381    suite.out() << "str_vec.size() is not 2\n";
382    suite.add(false);
383  }
384}
385
386
387void test_load2(test::Suite& suite)
388{
389  suite.out() << "test load<double>(matrix)\n";
390  std::string str("1.69 3.14\n1.23 1.22\n");
391  std::istringstream iss(str);
392  std::vector<std::vector<double> > double_m;
393  utility::load(iss, double_m, '\0', '\n');
394  suite.add(double_m.size()==2);
395
396  suite.out() << "test load<std::string>(matrix)\n";
397  suite.out() << str;
398  std::istringstream iss2(str);
399  std::vector<std::vector<std::string> > str_m;
400  utility::load(iss2, str_m, '\0', '\n');
401  std::cout << str_m.size() << std::endl;
402  for (size_t i=0; i<str_m.size(); ++i)
403    for (size_t j=0; j<str_m[i].size(); ++j)
404      suite.out() << str_m[i][j] << "\n";
405  suite.add(str_m.size()==2);
406}
407
408
409void test_inverse(test::Suite& suite)
410{
411  suite.err() << "Testing inverse\n";
412  std::vector<std::string> vec;
413  vec.push_back("No");
414  vec.push_back("one");
415  vec.push_back("shall");
416  vec.push_back("be");
417  vec.push_back("subjected");
418  std::map<std::string, std::vector<size_t> > map;
419  utility::inverse(vec.begin(), vec.end(), map);
420  test_inverse_validate(vec.begin(), vec.end(), map, suite);
421  utility::inverse(vec.begin()+1, vec.end(), map);
422  test_inverse_validate(vec.begin()+1, vec.end(), map, suite);
423  const std::vector<std::string> vec2(vec);
424  utility::inverse(vec2.begin(), vec2.end(), map);
425  test_inverse_validate(vec2.begin(), vec2.end(), map, suite);
426  std::multimap<std::string, size_t> mmap;
427  utility::inverse(vec.begin(), vec.end(), mmap);
428  test_inverse_validate(vec.begin(), mmap, suite);
429  utility::inverse(vec.begin()+1, vec.end(), mmap);
430  test_inverse_validate(vec.begin()+1, mmap, suite);
431  // do not run compile tests
432  if (false) {
433    std::map<std::string, std::vector<size_t> > m;
434    boost::input_iterator_archetype<std::string> start;
435    boost::input_iterator_archetype<std::string> end;
436    utility::inverse(start, end, m);
437    std::map<std::string, std::vector<size_t>, std::greater<std::string> > m2;
438    utility::inverse(start, end, m2);
439    std::multimap<std::string, size_t> m3;
440    utility::inverse(start, end, m3);
441    std::multimap<std::string, size_t, std::greater<std::string> > m4;
442    utility::inverse(start, end, m4);
443
444    std::map<std::string, size_t> unique_map;
445    utility::inverse(start, end, unique_map);
446  }
447}
448
449
450template<typename InputIterator, typename Key>
451void test_inverse_validate(InputIterator first, InputIterator last,
452                           const std::map<Key, std::vector<size_t> >& m,
453                           test::Suite& suite)
454{
455  typedef typename std::map<Key, std::vector<size_t> >::const_iterator Iter;
456  for ( InputIterator iter=first; iter != last; ++iter) {
457    Iter map_iter = m.find(*iter);
458    if (!suite.add(map_iter!=m.end())) {
459      suite.err() << "test_inverse_validate() failed\n";
460      suite.err() << "  could not find: " << *first << " in map m\n";
461    }
462    for (size_t i=0; i<map_iter->second.size(); ++i)
463      if (!suite.add(map_iter->second[i] ==
464                     static_cast<size_t>(distance(first, iter))) ) {
465        suite.err() << "test_inverse_validate() failed\n";
466        suite.err() << "  comparing: " << map_iter->second[i]
467                    << " and " << distance(first, iter)
468                    << " expected them to be equal\n";
469      }
470  }
471}
472
473
474template<typename InputIterator, typename Key>
475void test_inverse_validate(InputIterator first,
476                           const std::multimap<Key, size_t>& m,
477                           test::Suite& suite)
478{
479  for (typename std::multimap<Key, size_t>::const_iterator iter=m.begin();
480       iter!=m.end(); ++iter) {
481    suite.add(*(first + iter->second) == iter->first);
482  }
483
484}
485
486void test_sort_index(test::Suite& suite)
487{
488  suite.err() << "testing sort_index" << std::endl;
489  utility::Vector a(10);
490  for (size_t i=0; i<a.size(); ++i)
491    a(i) = std::pow(i-4.2,2);
492  std::vector<size_t> vec;
493  utility::sort_index(vec, a);
494
495  std::vector<size_t> vec2;
496  utility::sort_index(a.begin(), a.end(), vec2);
497  if (vec.size()==vec2.size()) {
498    if (!suite.equal_range(vec.begin(), vec.end(), vec2.begin())) {
499      suite.add(false);
500    }
501  }
502  else {
503    suite.add(false);
504    suite.err() << "size mismatch: vec.size()=" << vec.size()
505                << "vec2.size()=" << vec2.size() << "\n";
506  }
507  const utility::VectorConstView b(a, 0, 5, 2);
508
509  std::vector<size_t> vec3;
510  utility::sort_index(vec3, b);
511  std::vector<size_t> vec4;
512  utility::sort_index(b.begin(), b.end(), vec4);
513  if (vec3.size()!=vec4.size()) {
514    suite.add(false);
515    suite.err() << "size mismatch: vec3.size()=" << vec3.size()
516                << " vec4.size()=" << vec4.size() << "\n";
517  }
518  else {
519    if (!suite.equal_range(vec3.begin(), vec3.end(), vec4.begin())){
520      suite.add(false);
521    }
522  }
523
524  std::list<double> list;
525  for (size_t i=0; i<a.size(); ++i)
526    list.push_back(a(i));
527  std::vector<size_t> vec5;
528  utility::sort_index(list.begin(), list.end(), vec5);
529  if (vec.size()!=vec5.size()) {
530    suite.add(false);
531    suite.err() << "size mismatch: vec.size()=" << vec.size()
532                << " vec5.size()=" << vec5.size() << "\n";
533  }
534  else {
535    if (!suite.equal_range(vec.begin(), vec.end(), vec5.begin())){
536      suite.add(false);
537    }
538  }
539
540  // do not run compiler tests
541  if (false) {
542    // value type must be possible to store in a vector so has to be
543    // default constructible and assignable
544    typedef boost::default_constructible_archetype<
545      boost::sgi_assignable_archetype<
546        boost::less_than_comparable_archetype<> > > value_type;
547    boost::input_iterator_archetype<value_type> iter;
548    utility::sort_index(iter, iter, vec);
549
550    // try with a random access container as well
551    typedef boost::iterator_archetypes::readable_iterator_t access_type;
552    typedef boost::random_access_traversal_tag traversal_type;
553    typedef boost::copy_constructible_archetype<
554      boost::less_than_comparable_archetype<> > value_type2;
555    boost::iterator_archetype<value_type2, access_type, traversal_type> iter2;
556    utility::sort_index(iter2, iter2, vec);
557  }
558  // same thing with four argument version
559  if (false) {
560    // value type must be possible to store in a vector so has to be
561    // default constructible and assignable
562    typedef boost::default_constructible_archetype<
563      boost::sgi_assignable_archetype<> > value_type;
564
565    boost::input_iterator_archetype<value_type> iter;
566
567    // Create a binary predicate that is copy constructible
568    boost::copy_constructible_archetype<
569        boost::binary_predicate_archetype<value_type, value_type>
570      > comp;
571
572    utility::sort_index(iter, iter, vec, comp);
573
574    // try with a random access iterator as well
575    typedef boost::iterator_archetypes::readable_iterator_t access_type;
576    typedef boost::random_access_traversal_tag traversal_type;
577    typedef boost::copy_constructible_archetype<> value_type2;
578    boost::iterator_archetype<value_type2, access_type, traversal_type> iter2;
579    // Create a binary predicate that is copy constructible
580    boost::copy_constructible_archetype<
581        boost::binary_predicate_archetype<value_type2, value_type2>
582      > comp2;
583    utility::sort_index(iter2, iter2, vec, comp2);
584  }
585}
586
587
588void test_sum_weight(test::Suite& suite)
589{
590  // do not run compile test
591  if (false) {
592    boost::input_iterator_archetype<double> iter;
593    utility::sum_weight(iter, iter);
594    boost::input_iterator_archetype_no_proxy<utility::DataWeight> iter2;
595    utility::sum_weight(iter2, iter2);
596  }
597}
598
599
600void test_ptr_compare(test::Suite& suite)
601{
602  using utility::make_ptr_compare;
603  std::vector<double*> vec(10);
604  std::greater<double> d_greater;
605  for (size_t i=0; i<vec.size(); ++i)
606    vec[i] = new double(i);
607  std::sort(vec.begin(), vec.end(), make_ptr_compare(*vec.begin(), d_greater));
608  for (size_t i=1; i<vec.size(); ++i) {
609    suite.add( *vec[i-1] > *vec[i]);
610  }
611  std::sort(vec.begin(), vec.end(), make_ptr_compare(*vec.begin()));
612  for (size_t i=1; i<vec.size(); ++i) {
613    suite.add( *vec[i-1] < *vec[i]);
614  }
615  for (size_t i=0; i<vec.size(); ++i)
616    delete vec[i];
617
618}
619
620
621void test_fnmatch(test::Suite& suite)
622{
623  bool ok=true;
624  ok &= test_fnmatch(true,"peter", "peter");
625  ok &= test_fnmatch(false,"peter", "peterj");
626
627  ok &= test_fnmatch(true,"*", "peterj");
628  ok &= test_fnmatch(true,"p*", "peterj");
629  ok &= test_fnmatch(true, "p*", "peter");
630  ok &= test_fnmatch(false, "p*j", "peter");
631  ok &= test_fnmatch(true, "p*j", "peterj");
632  ok &= test_fnmatch(true, "*peter", "peter");
633
634  ok &= test_fnmatch(true, "p?ter", "peter");
635  ok &= test_fnmatch(false, "p?er", "peter");
636  ok &= test_fnmatch(false, "p?eter", "peter");
637
638  ok &= test_fnmatch(true, "filename", "filename");
639  ok &= test_fnmatch(true, "*name", "filename");
640  ok &= test_fnmatch(true, "[fa]il?name", "filename");
641  ok &= test_fnmatch(true, "[fa]*il?name", "ffilename");
642
643  ok &= test_fnmatch(true, "[fa]*il?name", "fafafailename");
644  ok &= test_fnmatch(false, "[fa]?il?name", "ilename");
645  ok &= test_fnmatch(false, "?[fa]il?name", "ilename");
646  ok &= test_fnmatch(true, "[fa]il?name", "filename");
647  ok &= test_fnmatch(false, "[fa]?il?name", "fafafailename");
648
649  ok &= test_fnmatch(true, "*name", "/path/to/filename");
650  ok &= test_fnmatch(true, "*name", "file.name");
651  ok &= test_fnmatch(true, "*.txt", ".file.txt");
652  suite.add(ok);
653}
654
655
656void test_dirname(test::Suite& suite)
657{
658  std::vector<std::string> path;
659  path.push_back("/usr/lib");
660  path.push_back("/usr/");
661  path.push_back("usr");
662  path.push_back("/");
663  path.push_back(".");
664  path.push_back("..");
665  std::vector<std::string> dir;
666  dir.push_back("/usr");
667  dir.push_back("/");
668  dir.push_back(".");
669  dir.push_back("/");
670  dir.push_back(".");
671  dir.push_back(".");
672  assert(dir.size()==path.size());
673  using utility::dirname;
674  for (size_t i=0; i<dir.size(); ++i) {
675    if (dir[i] != dirname(path[i])) {
676      suite.add(false);
677      suite.out() << "error:\n";
678      suite.out() << "path:           " << path[i] << "\n";
679      suite.out() << "directory_name: " << dirname(path[i]) << "\n";
680      suite.out() << "expected:       " << dir[i] << "\n";
681    }
682  }
683}
684
685
686void test_basename(test::Suite& suite)
687{
688  std::vector<std::string> path;
689  path.push_back("/usr/lib");
690  path.push_back("/usr/");
691  path.push_back("usr");
692  path.push_back("/");
693  path.push_back(".");
694  path.push_back("..");
695  std::vector<std::string> file;
696  file.push_back("lib");
697  file.push_back("usr");
698  file.push_back("usr");
699  file.push_back("/");
700  file.push_back(".");
701  file.push_back("..");
702  assert(file.size()==path.size());
703  using utility::basename;
704  for (size_t i=0; i<file.size(); ++i) {
705    if (file[i] != basename(path[i])) {
706      suite.add(false);
707      suite.out() << "error:\n";
708      suite.out() << "path:           " << path[i] << "\n";
709      suite.out() << "basename:       " << basename(path[i]) << "\n";
710      suite.out() << "expected:       " << file[i] << "\n";
711    }
712  }
713}
714
715
716bool test_fnmatch(bool answ, std::string a, std::string b)
717{
718  using namespace utility;
719  bool res = fnmatch(a, b);
720  // check that fnmatch and regexp agree
721  if (res == answ)
722    return true;
723  if (res!=answ)
724    std::cerr << "fnmatch(" << a << ", " << b << ") results "
725              << res
726              << ". Expects " << answ << std::endl;
727  return false;
728}
729
730
731void test_remove(test::Suite& suite)
732{
733  // at least test linking
734  if (false)
735    utility::remove("foo");
736}
737
738
739void test_rename(test::Suite& suite)
740{
741  // at least test linking
742  if (false)
743    utility::rename("foo", "bar");
744}
745
746
747void test_replace(test::Suite& suite)
748{
749  std::string s = "Some magic string!!!";
750  utility::replace(s, "magic", "arbitrary");
751  if (!suite.add(s=="Some arbitrary string!!!"))
752    suite.err() << "error: " << s << "\n";
753}
Note: See TracBrowser for help on using the repository browser.