source: trunk/test/utility.cc @ 1432

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

implement file_name and directory_name. fixes #511

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 6.3 KB
Line 
1// $Id: utility.cc 1432 2011-12-18 17:36:40Z 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 "Suite.h"
24
25#include "lib/utility.h"
26
27#include <algorithm>
28#include <iterator>
29#include <iostream>
30#include <string>
31
32void test_directory_name(theplu::svndigest::test::Suite& suite);
33void test_file_name(theplu::svndigest::test::Suite& suite);
34bool test_hex(int, unsigned int, std::string);
35bool test_fnmatch(bool, std::string, std::string);
36bool test_regexp(bool, std::string, std::string,
37                 const std::vector<std::string>&);
38
39int main(int argc, char* argv[])
40{
41  theplu::svndigest::test::Suite suite(argc, argv);
42  bool ok=true;
43
44  ok &= test_hex(15,2, "0f");
45  ok &= test_hex(17,1, "1");
46  ok &= test_hex(16,2, "10");
47
48  ok &= test_fnmatch(true,"peter", "peter");
49  ok &= test_fnmatch(false,"peter", "peterj");
50
51  ok &= test_fnmatch(true,"*", "peterj");
52  ok &= test_fnmatch(true,"p*", "peterj");
53  ok &= test_fnmatch(true, "p*", "peter");
54  ok &= test_fnmatch(false, "p*j", "peter");
55  ok &= test_fnmatch(true, "p*j", "peterj");
56  ok &= test_fnmatch(true, "*peter", "peter");
57
58  ok &= test_fnmatch(true, "p?ter", "peter");
59  ok &= test_fnmatch(false, "p?er", "peter");
60  ok &= test_fnmatch(false, "p?eter", "peter");
61
62  ok &= test_fnmatch(true, "filename", "filename");
63  ok &= test_fnmatch(true, "*name", "filename");
64  ok &= test_fnmatch(true, "[fa]il?name", "filename");
65  ok &= test_fnmatch(true, "[fa]*il?name", "ffilename");
66
67  ok &= test_fnmatch(true, "[fa]*il?name", "fafafailename");
68  ok &= test_fnmatch(false, "[fa]?il?name", "ilename");
69  ok &= test_fnmatch(false, "?[fa]il?name", "ilename");
70  ok &= test_fnmatch(true, "[fa]il?name", "filename");
71  ok &= test_fnmatch(false, "[fa]?il?name", "fafafailename");
72
73  ok &= test_fnmatch(true, "*name", "/path/to/filename");
74  ok &= test_fnmatch(true, "*name", "file.name");
75  // posix dictates that leading period can not be matched by
76  // wildcard, but here we allow match
77  ok &= test_fnmatch(true, "*.txt", ".file.txt");
78
79
80  std::vector<std::string> vec;
81  ok &= test_regexp(true,"abcde", "abcde", vec);
82  vec.push_back("c");
83  ok &= test_regexp(true,"ab?de", "abcde", vec);
84  vec[0] = "bcd";
85  ok &= test_regexp(true,"a*e", "abcde", vec);
86  vec.push_back("");
87  ok &= test_regexp(true,"a*d*f", "abcddf", vec);
88  vec[0] = "bc";
89  vec[1] = "ef";
90  ok &= test_regexp(true,"a*d*g", "abcdefg", vec);
91  vec.push_back("");
92  vec[1]="e";
93  vec[2]="f";
94  ok &= test_regexp(true,"a*d*?g", "abcdefg", vec);
95  ok &= test_regexp(true,"a*d??g", "abcdefg", vec);
96  vec.resize(2);
97  vec[0]="f";
98  vec[1]="e";
99  ok &= test_regexp(true, "[fa]il?name", "filename", vec);
100
101  suite.add(ok);
102  test_directory_name(suite);
103  test_file_name(suite);
104  return suite.exit_status();
105}
106
107
108void test_directory_name(theplu::svndigest::test::Suite& suite)
109{
110  std::vector<std::string> path;
111  path.push_back("/usr/lib");
112  path.push_back("/usr/");
113  path.push_back("usr");
114  path.push_back("/");
115  path.push_back(".");
116  path.push_back("..");
117  std::vector<std::string> dir;
118  dir.push_back("/usr");
119  dir.push_back("/");
120  dir.push_back(".");
121  dir.push_back("/");
122  dir.push_back(".");
123  dir.push_back(".");
124  assert(dir.size()==path.size());
125  using theplu::svndigest::directory_name;
126  for (size_t i=0; i<dir.size(); ++i) {
127    if (dir[i] != directory_name(path[i])) {
128      suite.add(false);
129      suite.out() << "error:\n";
130      suite.out() << "path:           " << path[i] << "\n";
131      suite.out() << "directory_name: " << directory_name(path[i]) << "\n";
132      suite.out() << "expected:       " << dir[i] << "\n";
133    }
134  }
135}
136
137
138void test_file_name(theplu::svndigest::test::Suite& suite)
139{
140  std::vector<std::string> path;
141  path.push_back("/usr/lib");
142  path.push_back("/usr/");
143  path.push_back("usr");
144  path.push_back("/");
145  path.push_back(".");
146  path.push_back("..");
147  std::vector<std::string> file;
148  file.push_back("lib");
149  file.push_back("usr");
150  file.push_back("usr");
151  file.push_back("/");
152  file.push_back(".");
153  file.push_back("..");
154  assert(file.size()==path.size());
155  using theplu::svndigest::file_name;
156  for (size_t i=0; i<file.size(); ++i) {
157    if (file[i] != file_name(path[i])) {
158      suite.add(false);
159      suite.out() << "error:\n";
160      suite.out() << "path:           " << path[i] << "\n";
161      suite.out() << "file_name:      " << file_name(path[i]) << "\n";
162      suite.out() << "expected:       " << file[i] << "\n";
163    }
164  }
165}
166
167
168bool test_fnmatch(bool answ, std::string a, std::string b)
169{
170  using namespace theplu::svndigest;
171  bool res = fnmatch(a.c_str(), b.c_str());
172  // check that fnmatch and regexp agree
173  std::vector<std::string> v;
174  bool res2 = regexp(a, b, v);
175  if (res == answ && res2==answ)
176    return true;
177  if (res!=answ)
178    std::cerr << "fnmatch(" << a << ", " << b << ") results "
179              << res
180              << ". Expects " << answ << std::endl;
181  if (res2!=answ)
182    std::cerr << "regexp(" << b << ", " << a << ") results "
183              << res2
184              << ". Expects " << answ << std::endl;
185  return false;
186}
187
188bool test_hex(int x, unsigned int w, std::string facit)
189{
190  if (theplu::svndigest::hex(x,w)==facit)
191    return true;
192  std::cerr << "hex(" << x << ", " << w << ") results "
193            << theplu::svndigest::hex(x,w) << ". Expects " << facit
194            << std::endl;
195  return false;
196}
197
198bool test_regexp(bool ans, std::string a, std::string b,
199                 const std::vector<std::string>& vec)
200{
201  using namespace theplu::svndigest;
202  std::vector<std::string> v;
203  bool res = regexp(a, b, v);
204  if (res!=ans || v!=vec) {
205    std::cerr << "regexp(" << a << ", " << b << ") results "
206              << res << ". Expected " << ans << "\n"
207              << "resulting vector:\n";
208    std::copy(v.begin(), v.end(),
209              std::ostream_iterator<std::string>(std::cerr, "\n"));
210    std::cerr << "expected:\n";
211    std::copy(vec.begin(), vec.end(),
212              std::ostream_iterator<std::string>(std::cerr, "\n"));
213    return false;
214  }
215  return true;
216}
Note: See TracBrowser for help on using the repository browser.