373 |
19 Jun 07 |
peter |
// $Id$ |
373 |
19 Jun 07 |
peter |
2 |
|
373 |
19 Jun 07 |
peter |
3 |
/* |
1635 |
30 Mar 23 |
peter |
Copyright (C) 2007 Peter Johansson |
1635 |
30 Mar 23 |
peter |
Copyright (C) 2008 Jari Häkkinen, Peter Johansson |
1090 |
12 Jun 10 |
peter |
Copyright (C) 2009, 2010 Peter Johansson |
1515 |
26 Sep 12 |
peter |
Copyright (C) 2011 Jari Häkkinen, Peter Johansson |
1635 |
30 Mar 23 |
peter |
Copyright (C) 2023 Peter Johansson |
373 |
19 Jun 07 |
peter |
9 |
|
687 |
04 Aug 08 |
peter |
This file is part of svndigest, http://dev.thep.lu.se/svndigest |
373 |
19 Jun 07 |
peter |
11 |
|
373 |
19 Jun 07 |
peter |
svndigest is free software; you can redistribute it and/or modify it |
373 |
19 Jun 07 |
peter |
under the terms of the GNU General Public License as published by |
693 |
11 Sep 08 |
jari |
the Free Software Foundation; either version 3 of the License, or |
373 |
19 Jun 07 |
peter |
(at your option) any later version. |
373 |
19 Jun 07 |
peter |
16 |
|
373 |
19 Jun 07 |
peter |
svndigest is distributed in the hope that it will be useful, but |
373 |
19 Jun 07 |
peter |
WITHOUT ANY WARRANTY; without even the implied warranty of |
373 |
19 Jun 07 |
peter |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
373 |
19 Jun 07 |
peter |
General Public License for more details. |
373 |
19 Jun 07 |
peter |
21 |
|
373 |
19 Jun 07 |
peter |
You should have received a copy of the GNU General Public License |
693 |
11 Sep 08 |
jari |
along with svndigest. If not, see <http://www.gnu.org/licenses/>. |
373 |
19 Jun 07 |
peter |
24 |
*/ |
373 |
19 Jun 07 |
peter |
25 |
|
1619 |
12 Mar 23 |
peter |
26 |
#include <config.h> |
1619 |
12 Mar 23 |
peter |
27 |
|
1162 |
13 Aug 10 |
peter |
28 |
#include "Suite.h" |
373 |
19 Jun 07 |
peter |
29 |
|
1162 |
13 Aug 10 |
peter |
30 |
#include "lib/utility.h" |
1162 |
13 Aug 10 |
peter |
31 |
|
516 |
09 Dec 07 |
peter |
32 |
#include <algorithm> |
1434 |
19 Dec 11 |
jari |
33 |
#include <cassert> |
516 |
09 Dec 07 |
peter |
34 |
#include <iterator> |
373 |
19 Jun 07 |
peter |
35 |
#include <iostream> |
373 |
19 Jun 07 |
peter |
36 |
#include <string> |
373 |
19 Jun 07 |
peter |
37 |
|
1432 |
18 Dec 11 |
peter |
38 |
void test_directory_name(theplu::svndigest::test::Suite& suite); |
1432 |
18 Dec 11 |
peter |
39 |
void test_file_name(theplu::svndigest::test::Suite& suite); |
589 |
12 Apr 08 |
jari |
40 |
bool test_hex(int, unsigned int, std::string); |
1052 |
18 Apr 10 |
peter |
41 |
bool test_fnmatch(bool, std::string, std::string); |
1432 |
18 Dec 11 |
peter |
42 |
bool test_regexp(bool, std::string, std::string, |
516 |
09 Dec 07 |
peter |
43 |
const std::vector<std::string>&); |
373 |
19 Jun 07 |
peter |
44 |
|
1162 |
13 Aug 10 |
peter |
45 |
int main(int argc, char* argv[]) |
373 |
19 Jun 07 |
peter |
46 |
{ |
1162 |
13 Aug 10 |
peter |
47 |
theplu::svndigest::test::Suite suite(argc, argv); |
373 |
19 Jun 07 |
peter |
48 |
bool ok=true; |
373 |
19 Jun 07 |
peter |
49 |
|
1432 |
18 Dec 11 |
peter |
50 |
ok &= test_hex(15,2, "0f"); |
1432 |
18 Dec 11 |
peter |
51 |
ok &= test_hex(17,1, "1"); |
1432 |
18 Dec 11 |
peter |
52 |
ok &= test_hex(16,2, "10"); |
1098 |
13 Jun 10 |
peter |
53 |
|
1432 |
18 Dec 11 |
peter |
54 |
ok &= test_fnmatch(true,"peter", "peter"); |
1432 |
18 Dec 11 |
peter |
55 |
ok &= test_fnmatch(false,"peter", "peterj"); |
507 |
08 Dec 07 |
peter |
56 |
|
1432 |
18 Dec 11 |
peter |
57 |
ok &= test_fnmatch(true,"*", "peterj"); |
1432 |
18 Dec 11 |
peter |
58 |
ok &= test_fnmatch(true,"p*", "peterj"); |
1432 |
18 Dec 11 |
peter |
59 |
ok &= test_fnmatch(true, "p*", "peter"); |
1432 |
18 Dec 11 |
peter |
60 |
ok &= test_fnmatch(false, "p*j", "peter"); |
1432 |
18 Dec 11 |
peter |
61 |
ok &= test_fnmatch(true, "p*j", "peterj"); |
1432 |
18 Dec 11 |
peter |
62 |
ok &= test_fnmatch(true, "*peter", "peter"); |
1098 |
13 Jun 10 |
peter |
63 |
|
1432 |
18 Dec 11 |
peter |
64 |
ok &= test_fnmatch(true, "p?ter", "peter"); |
1432 |
18 Dec 11 |
peter |
65 |
ok &= test_fnmatch(false, "p?er", "peter"); |
1432 |
18 Dec 11 |
peter |
66 |
ok &= test_fnmatch(false, "p?eter", "peter"); |
1432 |
18 Dec 11 |
peter |
67 |
|
1052 |
18 Apr 10 |
peter |
68 |
ok &= test_fnmatch(true, "filename", "filename"); |
1052 |
18 Apr 10 |
peter |
69 |
ok &= test_fnmatch(true, "*name", "filename"); |
1052 |
18 Apr 10 |
peter |
70 |
ok &= test_fnmatch(true, "[fa]il?name", "filename"); |
1098 |
13 Jun 10 |
peter |
71 |
ok &= test_fnmatch(true, "[fa]*il?name", "ffilename"); |
1098 |
13 Jun 10 |
peter |
72 |
|
1098 |
13 Jun 10 |
peter |
73 |
ok &= test_fnmatch(true, "[fa]*il?name", "fafafailename"); |
1098 |
13 Jun 10 |
peter |
74 |
ok &= test_fnmatch(false, "[fa]?il?name", "ilename"); |
1098 |
13 Jun 10 |
peter |
75 |
ok &= test_fnmatch(false, "?[fa]il?name", "ilename"); |
1098 |
13 Jun 10 |
peter |
76 |
ok &= test_fnmatch(true, "[fa]il?name", "filename"); |
1098 |
13 Jun 10 |
peter |
77 |
ok &= test_fnmatch(false, "[fa]?il?name", "fafafailename"); |
1098 |
13 Jun 10 |
peter |
78 |
|
1052 |
18 Apr 10 |
peter |
79 |
ok &= test_fnmatch(true, "*name", "/path/to/filename"); |
1052 |
18 Apr 10 |
peter |
80 |
ok &= test_fnmatch(true, "*name", "file.name"); |
1098 |
13 Jun 10 |
peter |
// posix dictates that leading period can not be matched by |
1098 |
13 Jun 10 |
peter |
// wildcard, but here we allow match |
1098 |
13 Jun 10 |
peter |
83 |
ok &= test_fnmatch(true, "*.txt", ".file.txt"); |
1052 |
18 Apr 10 |
peter |
84 |
|
1098 |
13 Jun 10 |
peter |
85 |
|
516 |
09 Dec 07 |
peter |
86 |
std::vector<std::string> vec; |
1432 |
18 Dec 11 |
peter |
87 |
ok &= test_regexp(true,"abcde", "abcde", vec); |
1098 |
13 Jun 10 |
peter |
88 |
vec.push_back("c"); |
1432 |
18 Dec 11 |
peter |
89 |
ok &= test_regexp(true,"ab?de", "abcde", vec); |
516 |
09 Dec 07 |
peter |
90 |
vec[0] = "bcd"; |
1432 |
18 Dec 11 |
peter |
91 |
ok &= test_regexp(true,"a*e", "abcde", vec); |
516 |
09 Dec 07 |
peter |
92 |
vec.push_back(""); |
1432 |
18 Dec 11 |
peter |
93 |
ok &= test_regexp(true,"a*d*f", "abcddf", vec); |
516 |
09 Dec 07 |
peter |
94 |
vec[0] = "bc"; |
516 |
09 Dec 07 |
peter |
95 |
vec[1] = "ef"; |
1432 |
18 Dec 11 |
peter |
96 |
ok &= test_regexp(true,"a*d*g", "abcdefg", vec); |
516 |
09 Dec 07 |
peter |
97 |
vec.push_back(""); |
1096 |
13 Jun 10 |
peter |
98 |
vec[1]="e"; |
1096 |
13 Jun 10 |
peter |
99 |
vec[2]="f"; |
1432 |
18 Dec 11 |
peter |
100 |
ok &= test_regexp(true,"a*d*?g", "abcdefg", vec); |
1432 |
18 Dec 11 |
peter |
101 |
ok &= test_regexp(true,"a*d??g", "abcdefg", vec); |
1098 |
13 Jun 10 |
peter |
102 |
vec.resize(2); |
1098 |
13 Jun 10 |
peter |
103 |
vec[0]="f"; |
1098 |
13 Jun 10 |
peter |
104 |
vec[1]="e"; |
1098 |
13 Jun 10 |
peter |
105 |
ok &= test_regexp(true, "[fa]il?name", "filename", vec); |
516 |
09 Dec 07 |
peter |
106 |
|
1162 |
13 Aug 10 |
peter |
107 |
suite.add(ok); |
1432 |
18 Dec 11 |
peter |
108 |
test_directory_name(suite); |
1432 |
18 Dec 11 |
peter |
109 |
test_file_name(suite); |
1162 |
13 Aug 10 |
peter |
110 |
return suite.exit_status(); |
373 |
19 Jun 07 |
peter |
111 |
} |
373 |
19 Jun 07 |
peter |
112 |
|
1432 |
18 Dec 11 |
peter |
113 |
|
1432 |
18 Dec 11 |
peter |
114 |
void test_directory_name(theplu::svndigest::test::Suite& suite) |
1432 |
18 Dec 11 |
peter |
115 |
{ |
1432 |
18 Dec 11 |
peter |
116 |
std::vector<std::string> path; |
1432 |
18 Dec 11 |
peter |
117 |
path.push_back("/usr/lib"); |
1432 |
18 Dec 11 |
peter |
118 |
path.push_back("/usr/"); |
1432 |
18 Dec 11 |
peter |
119 |
path.push_back("usr"); |
1432 |
18 Dec 11 |
peter |
120 |
path.push_back("/"); |
1432 |
18 Dec 11 |
peter |
121 |
path.push_back("."); |
1432 |
18 Dec 11 |
peter |
122 |
path.push_back(".."); |
1432 |
18 Dec 11 |
peter |
123 |
std::vector<std::string> dir; |
1432 |
18 Dec 11 |
peter |
124 |
dir.push_back("/usr"); |
1432 |
18 Dec 11 |
peter |
125 |
dir.push_back("/"); |
1432 |
18 Dec 11 |
peter |
126 |
dir.push_back("."); |
1432 |
18 Dec 11 |
peter |
127 |
dir.push_back("/"); |
1432 |
18 Dec 11 |
peter |
128 |
dir.push_back("."); |
1432 |
18 Dec 11 |
peter |
129 |
dir.push_back("."); |
1432 |
18 Dec 11 |
peter |
130 |
assert(dir.size()==path.size()); |
1432 |
18 Dec 11 |
peter |
131 |
using theplu::svndigest::directory_name; |
1432 |
18 Dec 11 |
peter |
132 |
for (size_t i=0; i<dir.size(); ++i) { |
1432 |
18 Dec 11 |
peter |
133 |
if (dir[i] != directory_name(path[i])) { |
1432 |
18 Dec 11 |
peter |
134 |
suite.add(false); |
1432 |
18 Dec 11 |
peter |
135 |
suite.out() << "error:\n"; |
1432 |
18 Dec 11 |
peter |
136 |
suite.out() << "path: " << path[i] << "\n"; |
1432 |
18 Dec 11 |
peter |
137 |
suite.out() << "directory_name: " << directory_name(path[i]) << "\n"; |
1432 |
18 Dec 11 |
peter |
138 |
suite.out() << "expected: " << dir[i] << "\n"; |
1432 |
18 Dec 11 |
peter |
139 |
} |
1432 |
18 Dec 11 |
peter |
140 |
} |
1432 |
18 Dec 11 |
peter |
141 |
} |
1432 |
18 Dec 11 |
peter |
142 |
|
1432 |
18 Dec 11 |
peter |
143 |
|
1432 |
18 Dec 11 |
peter |
144 |
void test_file_name(theplu::svndigest::test::Suite& suite) |
1432 |
18 Dec 11 |
peter |
145 |
{ |
1432 |
18 Dec 11 |
peter |
146 |
std::vector<std::string> path; |
1432 |
18 Dec 11 |
peter |
147 |
path.push_back("/usr/lib"); |
1432 |
18 Dec 11 |
peter |
148 |
path.push_back("/usr/"); |
1432 |
18 Dec 11 |
peter |
149 |
path.push_back("usr"); |
1432 |
18 Dec 11 |
peter |
150 |
path.push_back("/"); |
1432 |
18 Dec 11 |
peter |
151 |
path.push_back("."); |
1432 |
18 Dec 11 |
peter |
152 |
path.push_back(".."); |
1432 |
18 Dec 11 |
peter |
153 |
std::vector<std::string> file; |
1432 |
18 Dec 11 |
peter |
154 |
file.push_back("lib"); |
1432 |
18 Dec 11 |
peter |
155 |
file.push_back("usr"); |
1432 |
18 Dec 11 |
peter |
156 |
file.push_back("usr"); |
1432 |
18 Dec 11 |
peter |
157 |
file.push_back("/"); |
1432 |
18 Dec 11 |
peter |
158 |
file.push_back("."); |
1432 |
18 Dec 11 |
peter |
159 |
file.push_back(".."); |
1432 |
18 Dec 11 |
peter |
160 |
assert(file.size()==path.size()); |
1432 |
18 Dec 11 |
peter |
161 |
using theplu::svndigest::file_name; |
1432 |
18 Dec 11 |
peter |
162 |
for (size_t i=0; i<file.size(); ++i) { |
1432 |
18 Dec 11 |
peter |
163 |
if (file[i] != file_name(path[i])) { |
1432 |
18 Dec 11 |
peter |
164 |
suite.add(false); |
1432 |
18 Dec 11 |
peter |
165 |
suite.out() << "error:\n"; |
1432 |
18 Dec 11 |
peter |
166 |
suite.out() << "path: " << path[i] << "\n"; |
1432 |
18 Dec 11 |
peter |
167 |
suite.out() << "file_name: " << file_name(path[i]) << "\n"; |
1432 |
18 Dec 11 |
peter |
168 |
suite.out() << "expected: " << file[i] << "\n"; |
1432 |
18 Dec 11 |
peter |
169 |
} |
1432 |
18 Dec 11 |
peter |
170 |
} |
1432 |
18 Dec 11 |
peter |
171 |
} |
1432 |
18 Dec 11 |
peter |
172 |
|
1432 |
18 Dec 11 |
peter |
173 |
|
1052 |
18 Apr 10 |
peter |
174 |
bool test_fnmatch(bool answ, std::string a, std::string b) |
507 |
08 Dec 07 |
peter |
175 |
{ |
1052 |
18 Apr 10 |
peter |
176 |
using namespace theplu::svndigest; |
1052 |
18 Apr 10 |
peter |
177 |
bool res = fnmatch(a.c_str(), b.c_str()); |
1098 |
13 Jun 10 |
peter |
// check that fnmatch and regexp agree |
1098 |
13 Jun 10 |
peter |
179 |
std::vector<std::string> v; |
1098 |
13 Jun 10 |
peter |
180 |
bool res2 = regexp(a, b, v); |
1098 |
13 Jun 10 |
peter |
181 |
if (res == answ && res2==answ) |
507 |
08 Dec 07 |
peter |
182 |
return true; |
1098 |
13 Jun 10 |
peter |
183 |
if (res!=answ) |
1098 |
13 Jun 10 |
peter |
184 |
std::cerr << "fnmatch(" << a << ", " << b << ") results " |
1098 |
13 Jun 10 |
peter |
185 |
<< res |
1098 |
13 Jun 10 |
peter |
186 |
<< ". Expects " << answ << std::endl; |
1098 |
13 Jun 10 |
peter |
187 |
if (res2!=answ) |
1098 |
13 Jun 10 |
peter |
188 |
std::cerr << "regexp(" << b << ", " << a << ") results " |
1098 |
13 Jun 10 |
peter |
189 |
<< res2 |
1098 |
13 Jun 10 |
peter |
190 |
<< ". Expects " << answ << std::endl; |
507 |
08 Dec 07 |
peter |
191 |
return false; |
507 |
08 Dec 07 |
peter |
192 |
} |
507 |
08 Dec 07 |
peter |
193 |
|
589 |
12 Apr 08 |
jari |
194 |
bool test_hex(int x, unsigned int w, std::string facit) |
373 |
19 Jun 07 |
peter |
195 |
{ |
373 |
19 Jun 07 |
peter |
196 |
if (theplu::svndigest::hex(x,w)==facit) |
373 |
19 Jun 07 |
peter |
197 |
return true; |
373 |
19 Jun 07 |
peter |
198 |
std::cerr << "hex(" << x << ", " << w << ") results " |
1432 |
18 Dec 11 |
peter |
199 |
<< theplu::svndigest::hex(x,w) << ". Expects " << facit |
373 |
19 Jun 07 |
peter |
200 |
<< std::endl; |
373 |
19 Jun 07 |
peter |
201 |
return false; |
373 |
19 Jun 07 |
peter |
202 |
} |
507 |
08 Dec 07 |
peter |
203 |
|
1432 |
18 Dec 11 |
peter |
204 |
bool test_regexp(bool ans, std::string a, std::string b, |
516 |
09 Dec 07 |
peter |
205 |
const std::vector<std::string>& vec) |
516 |
09 Dec 07 |
peter |
206 |
{ |
516 |
09 Dec 07 |
peter |
207 |
using namespace theplu::svndigest; |
516 |
09 Dec 07 |
peter |
208 |
std::vector<std::string> v; |
1098 |
13 Jun 10 |
peter |
209 |
bool res = regexp(a, b, v); |
516 |
09 Dec 07 |
peter |
210 |
if (res!=ans || v!=vec) { |
516 |
09 Dec 07 |
peter |
211 |
std::cerr << "regexp(" << a << ", " << b << ") results " |
516 |
09 Dec 07 |
peter |
212 |
<< res << ". Expected " << ans << "\n" |
516 |
09 Dec 07 |
peter |
213 |
<< "resulting vector:\n"; |
1432 |
18 Dec 11 |
peter |
214 |
std::copy(v.begin(), v.end(), |
516 |
09 Dec 07 |
peter |
215 |
std::ostream_iterator<std::string>(std::cerr, "\n")); |
516 |
09 Dec 07 |
peter |
216 |
std::cerr << "expected:\n"; |
1432 |
18 Dec 11 |
peter |
217 |
std::copy(vec.begin(), vec.end(), |
516 |
09 Dec 07 |
peter |
218 |
std::ostream_iterator<std::string>(std::cerr, "\n")); |
516 |
09 Dec 07 |
peter |
219 |
return false; |
516 |
09 Dec 07 |
peter |
220 |
} |
516 |
09 Dec 07 |
peter |
221 |
return true; |
516 |
09 Dec 07 |
peter |
222 |
} |