source: trunk/test/utility_test.cc @ 1098

Last change on this file since 1098 was 1098, checked in by Peter Johansson, 12 years ago

closes #450. extend filename matching tp allow [xyz] and ?. Also fixed a bug when having asterisk (*) in end of pattern string

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.5 KB
Line 
1// $Id: utility_test.cc 1098 2010-06-13 16:46:29Z peter $
2
3/*
4  Copyright (C) 2007, 2008 Jari Häkkinen, Peter Johansson
5  Copyright (C) 2009, 2010 Peter Johansson
6
7  This file is part of svndigest, http://dev.thep.lu.se/svndigest
8
9  svndigest is free software; you can redistribute it and/or modify it
10  under the terms of the GNU General Public License as published by
11  the Free Software Foundation; either version 3 of the License, or
12  (at your option) any later version.
13
14  svndigest is distributed in the hope that it will be useful, but
15  WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  General Public License for more details.
18
19  You should have received a copy of the GNU General Public License
20  along with svndigest. If not, see <http://www.gnu.org/licenses/>.
21*/
22
23#include "../lib/utility.h"
24
25#include <algorithm>
26#include <iterator>
27#include <iostream>
28#include <string>
29
30bool test_hex(int, unsigned int, std::string);
31bool test_fnmatch(bool, std::string, std::string);
32bool test_regexp(bool, std::string, std::string, 
33                 const std::vector<std::string>&);
34
35int main(const int argc,const char* argv[])
36{
37  bool ok=true;
38 
39  ok &= test_hex(15,2, "0f"); 
40  ok &= test_hex(17,1, "1"); 
41  ok &= test_hex(16,2, "10"); 
42
43  ok &= test_fnmatch(true,"peter", "peter"); 
44  ok &= test_fnmatch(false,"peter", "peterj"); 
45
46  ok &= test_fnmatch(true,"*", "peterj"); 
47  ok &= test_fnmatch(true,"p*", "peterj"); 
48  ok &= test_fnmatch(true, "p*", "peter"); 
49  ok &= test_fnmatch(false, "p*j", "peter"); 
50  ok &= test_fnmatch(true, "p*j", "peterj"); 
51  ok &= test_fnmatch(true, "*peter", "peter"); 
52
53  ok &= test_fnmatch(true, "p?ter", "peter"); 
54  ok &= test_fnmatch(false, "p?er", "peter"); 
55  ok &= test_fnmatch(false, "p?eter", "peter"); 
56
57  ok &= test_fnmatch(true, "filename", "filename");
58  ok &= test_fnmatch(true, "*name", "filename");
59  ok &= test_fnmatch(true, "[fa]il?name", "filename");
60  ok &= test_fnmatch(true, "[fa]*il?name", "ffilename");
61
62  ok &= test_fnmatch(true, "[fa]*il?name", "fafafailename");
63  ok &= test_fnmatch(false, "[fa]?il?name", "ilename");
64  ok &= test_fnmatch(false, "?[fa]il?name", "ilename");
65  ok &= test_fnmatch(true, "[fa]il?name", "filename");
66  ok &= test_fnmatch(false, "[fa]?il?name", "fafafailename");
67
68  ok &= test_fnmatch(true, "*name", "/path/to/filename");
69  ok &= test_fnmatch(true, "*name", "file.name");
70  // posix dictates that leading period can not be matched by
71  // wildcard, but here we allow match
72  ok &= test_fnmatch(true, "*.txt", ".file.txt");
73
74
75  std::vector<std::string> vec;
76  ok &= test_regexp(true,"abcde", "abcde", vec); 
77  vec.push_back("c");
78  ok &= test_regexp(true,"ab?de", "abcde", vec); 
79  vec[0] = "bcd";
80  ok &= test_regexp(true,"a*e", "abcde", vec); 
81  vec.push_back("");
82  ok &= test_regexp(true,"a*d*f", "abcddf", vec); 
83  vec[0] = "bc";
84  vec[1] = "ef";
85  ok &= test_regexp(true,"a*d*g", "abcdefg", vec); 
86  vec.push_back("");
87  vec[1]="e";
88  vec[2]="f";
89  ok &= test_regexp(true,"a*d*?g", "abcdefg", vec); 
90  ok &= test_regexp(true,"a*d??g", "abcdefg", vec); 
91  vec.resize(2);
92  vec[0]="f";
93  vec[1]="e";
94  ok &= test_regexp(true, "[fa]il?name", "filename", vec);
95
96  if (ok)
97    return 0;
98  return 1;
99}
100
101bool test_fnmatch(bool answ, std::string a, std::string b)
102{
103  using namespace theplu::svndigest;
104  bool res = fnmatch(a.c_str(), b.c_str());
105  // check that fnmatch and regexp agree
106  std::vector<std::string> v;
107  bool res2 = regexp(a, b, v);
108  if (res == answ && res2==answ)
109    return true;
110  if (res!=answ)
111    std::cerr << "fnmatch(" << a << ", " << b << ") results "
112              << res
113              << ". Expects " << answ << std::endl;
114  if (res2!=answ)
115    std::cerr << "regexp(" << b << ", " << a << ") results "
116              << res2
117              << ". Expects " << answ << std::endl;
118  return false;
119}
120
121bool test_hex(int x, unsigned int w, std::string facit)
122{
123  if (theplu::svndigest::hex(x,w)==facit)
124    return true;
125  std::cerr << "hex(" << x << ", " << w << ") results "
126            << theplu::svndigest::hex(x,w) << ". Expects " << facit
127            << std::endl;
128  return false;
129}
130
131bool test_regexp(bool ans, std::string a, std::string b, 
132                 const std::vector<std::string>& vec)
133{
134  using namespace theplu::svndigest;
135  std::vector<std::string> v;
136  bool res = regexp(a, b, v);
137  if (res!=ans || v!=vec) {
138    std::cerr << "regexp(" << a << ", " << b << ") results "
139              << res << ". Expected " << ans << "\n"
140              << "resulting vector:\n";
141    std::copy(v.begin(), v.end(), 
142              std::ostream_iterator<std::string>(std::cerr, "\n"));
143    std::cerr << "expected:\n";
144    std::copy(vec.begin(), vec.end(), 
145              std::ostream_iterator<std::string>(std::cerr, "\n"));
146    return false;
147  }
148  return true;
149}
Note: See TracBrowser for help on using the repository browser.