source: trunk/test/utility.cc @ 1434

Last change on this file since 1434 was 1434, checked in by Jari Häkkinen, 9 years ago

Added missing include

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