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