Changeset 2908


Ignore:
Timestamp:
Dec 15, 2012, 9:30:23 AM (9 years ago)
Author:
Peter
Message:

copy tests from http://dev.thep.lu.se/svndigest code. refs #717

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/utility.cc

    r2881 r2908  
    5252void test_load2(test::Suite&);
    5353void test_inverse(test::Suite&);
     54
     55void test_dirname(test::Suite& suite);
     56void test_basename(test::Suite& suite);
     57void test_fnmatch(test::Suite& suite);
     58bool test_fnmatch(bool, std::string, std::string);
    5459
    5560template<typename InputIterator, typename Key>
     
    205210  long double xld = 8.0;
    206211  suite.add(suite.equal(utility::log2(xld), 3.0));
     212
     213  test_basename(suite);
     214  test_dirname(suite);
     215  test_fnmatch(suite);
    207216
    208217  return suite.return_value();
     
    517526
    518527}
     528
     529
     530void test_fnmatch(test::Suite& suite)
     531{
     532  bool ok=true;
     533  ok &= test_fnmatch(true,"peter", "peter");
     534  ok &= test_fnmatch(false,"peter", "peterj");
     535
     536  ok &= test_fnmatch(true,"*", "peterj");
     537  ok &= test_fnmatch(true,"p*", "peterj");
     538  ok &= test_fnmatch(true, "p*", "peter");
     539  ok &= test_fnmatch(false, "p*j", "peter");
     540  ok &= test_fnmatch(true, "p*j", "peterj");
     541  ok &= test_fnmatch(true, "*peter", "peter");
     542
     543  ok &= test_fnmatch(true, "p?ter", "peter");
     544  ok &= test_fnmatch(false, "p?er", "peter");
     545  ok &= test_fnmatch(false, "p?eter", "peter");
     546
     547  ok &= test_fnmatch(true, "filename", "filename");
     548  ok &= test_fnmatch(true, "*name", "filename");
     549  ok &= test_fnmatch(true, "[fa]il?name", "filename");
     550  ok &= test_fnmatch(true, "[fa]*il?name", "ffilename");
     551
     552  ok &= test_fnmatch(true, "[fa]*il?name", "fafafailename");
     553  ok &= test_fnmatch(false, "[fa]?il?name", "ilename");
     554  ok &= test_fnmatch(false, "?[fa]il?name", "ilename");
     555  ok &= test_fnmatch(true, "[fa]il?name", "filename");
     556  ok &= test_fnmatch(false, "[fa]?il?name", "fafafailename");
     557
     558  ok &= test_fnmatch(true, "*name", "/path/to/filename");
     559  ok &= test_fnmatch(true, "*name", "file.name");
     560  ok &= test_fnmatch(true, "*.txt", ".file.txt");
     561  suite.add(ok);
     562}
     563
     564
     565void test_dirname(test::Suite& suite)
     566{
     567  std::vector<std::string> path;
     568  path.push_back("/usr/lib");
     569  path.push_back("/usr/");
     570  path.push_back("usr");
     571  path.push_back("/");
     572  path.push_back(".");
     573  path.push_back("..");
     574  std::vector<std::string> dir;
     575  dir.push_back("/usr");
     576  dir.push_back("/");
     577  dir.push_back(".");
     578  dir.push_back("/");
     579  dir.push_back(".");
     580  dir.push_back(".");
     581  assert(dir.size()==path.size());
     582  using utility::dirname;
     583  for (size_t i=0; i<dir.size(); ++i) {
     584    if (dir[i] != dirname(path[i])) {
     585      suite.add(false);
     586      suite.out() << "error:\n";
     587      suite.out() << "path:           " << path[i] << "\n";
     588      suite.out() << "directory_name: " << dirname(path[i]) << "\n";
     589      suite.out() << "expected:       " << dir[i] << "\n";
     590    }
     591  }
     592}
     593
     594
     595void test_basename(test::Suite& suite)
     596{
     597  std::vector<std::string> path;
     598  path.push_back("/usr/lib");
     599  path.push_back("/usr/");
     600  path.push_back("usr");
     601  path.push_back("/");
     602  path.push_back(".");
     603  path.push_back("..");
     604  std::vector<std::string> file;
     605  file.push_back("lib");
     606  file.push_back("usr");
     607  file.push_back("usr");
     608  file.push_back("/");
     609  file.push_back(".");
     610  file.push_back("..");
     611  assert(file.size()==path.size());
     612  using utility::basename;
     613  for (size_t i=0; i<file.size(); ++i) {
     614    if (file[i] != basename(path[i])) {
     615      suite.add(false);
     616      suite.out() << "error:\n";
     617      suite.out() << "path:           " << path[i] << "\n";
     618      suite.out() << "basename:       " << basename(path[i]) << "\n";
     619      suite.out() << "expected:       " << file[i] << "\n";
     620    }
     621  }
     622}
     623
     624
     625bool test_fnmatch(bool answ, std::string a, std::string b)
     626{
     627  using namespace utility;
     628  bool res = fnmatch(a, b);
     629  // check that fnmatch and regexp agree
     630  if (res == answ)
     631    return true;
     632  if (res!=answ)
     633    std::cerr << "fnmatch(" << a << ", " << b << ") results "
     634              << res
     635              << ". Expects " << answ << std::endl;
     636  return false;
     637}
  • trunk/yat/utility/utility.h

    r2907 r2908  
    144144     \return true if \a str matches \a pattern
    145145
    146      \see fnmatch(3)
     146     \see http://linux.die.net/man/3/fnmatch
     147
     148     \throw runtime_error if returned value from underlying fnmatch is
     149     neither zero nor \c FNM_NOMATCH.
    147150
    148151     \since New in yat 0.10
Note: See TracChangeset for help on using the changeset viewer.