source: trunk/test/utility_test.cc @ 1090

Last change on this file since 1090 was 1090, checked in by Peter Johansson, 11 years ago

update copyrights

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.6 KB
Line 
1// $Id: utility_test.cc 1090 2010-06-12 17:27:00Z 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  ok &= test_fnmatch(true,"p*", "peterj"); 
46  ok &= test_fnmatch(true, "p*", "peter"); 
47  ok &= test_fnmatch(false, "p*j", "peter"); 
48  ok &= test_fnmatch(true, "*peter", "peter"); 
49
50  ok &= test_fnmatch(true, "filename", "filename");
51  ok &= test_fnmatch(true, "*name", "filename");
52  ok &= test_fnmatch(true, "[fa]il?name", "filename");
53  ok &= test_fnmatch(true, "*name", "/path/to/filename");
54  ok &= test_fnmatch(true, "*name", "file.name");
55  // posix dictates that leading persion can not be matched by wildcard
56  ok &= test_fnmatch(false, "*.txt", ".file.txt");
57
58  std::vector<std::string> vec;
59  ok &= test_regexp(true,"abcde", "abcde", vec); 
60  vec.push_back("");
61  ok &= test_regexp(true,"abcde", "abcd?e", vec); 
62  vec[0]="c";
63  ok &= test_regexp(true,"abcde", "ab?de", vec); 
64  vec[0] = "bcd";
65  ok &= test_regexp(true,"abcde", "a*e", vec); 
66  vec.push_back("");
67  ok &= test_regexp(true,"abcddf", "a*d*f", vec); 
68  vec[0] = "bc";
69  vec[1] = "ef";
70  ok &= test_regexp(true,"abcdefg", "a*d*g", vec); 
71  vec.push_back("");
72  ok &= test_regexp(true,"abcdefg", "a*d*?g", vec); 
73
74  if (ok)
75    return 0;
76  return 1;
77}
78
79bool test_fnmatch(bool answ, std::string a, std::string b)
80{
81  using namespace theplu::svndigest;
82  bool res = fnmatch(a.c_str(), b.c_str());
83  if (res == answ)
84    return true;
85  std::cerr << "fnmatch(" << a << ", " << b << ") results "
86            << res
87            << ". Expects " << answ << std::endl;
88  return false;
89}
90
91bool test_hex(int x, unsigned int w, std::string facit)
92{
93  if (theplu::svndigest::hex(x,w)==facit)
94    return true;
95  std::cerr << "hex(" << x << ", " << w << ") results "
96            << theplu::svndigest::hex(x,w) << ". Expects " << facit
97            << std::endl;
98  return false;
99}
100
101bool test_regexp(bool ans, std::string a, std::string b, 
102                 const std::vector<std::string>& vec)
103{
104  using namespace theplu::svndigest;
105  std::vector<std::string> v;
106  bool res = regexp(a.begin(), a.end(), b.begin(), b.end(), v);
107  if (res!=ans || v!=vec) {
108    std::cerr << "regexp(" << a << ", " << b << ") results "
109              << res << ". Expected " << ans << "\n"
110              << "resulting vector:\n";
111    std::copy(v.begin(), v.end(), 
112              std::ostream_iterator<std::string>(std::cerr, "\n"));
113    std::cerr << "expected:\n";
114    std::copy(vec.begin(), vec.end(), 
115              std::ostream_iterator<std::string>(std::cerr, "\n"));
116    return false;
117  }
118  return true;
119}
Note: See TracBrowser for help on using the repository browser.