source: trunk/test/utility_test.cc @ 540

Last change on this file since 540 was 523, checked in by Peter Johansson, 15 years ago

fixes ticket:284 - validate [file-name-dictionary] section in config file

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.2 KB
Line 
1// $Id: utility_test.cc 523 2007-12-25 01:51:54Z peter $
2
3/*
4  Copyright (C) 2007 Peter Johansson
5
6  This file is part of svndigest, http://trac.thep.lu.se/svndigest
7
8  svndigest is free software; you can redistribute it and/or modify it
9  under the terms of the GNU General Public License as published by
10  the Free Software Foundation; either version 2 of the License, or
11  (at your option) any later version.
12
13  svndigest is distributed in the hope that it will be useful, but
14  WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  General Public License for more details.
17
18  You should have received a copy of the GNU General Public License
19  along with this program; if not, write to the Free Software
20  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
21  02111-1307, USA.
22*/
23
24#include "utility.h"
25
26#include <algorithm>
27#include <iterator>
28#include <iostream>
29#include <string>
30
31bool test_hex(int, u_int, std::string);
32bool test_equal(bool, std::string, std::string);
33bool test_regexp(bool, std::string, std::string, 
34                 const std::vector<std::string>&);
35
36int main(const int argc,const char* argv[])
37{
38  bool ok=true;
39 
40  ok &= test_hex(15,2, "0f"); 
41  ok &= test_hex(17,1, "1"); 
42  ok &= test_hex(16,2, "10"); 
43
44  ok &= test_equal(true,"peter", "peter"); 
45  ok &= test_equal(false,"peter", "peterj"); 
46  ok &= test_equal(true,"p*", "peterj"); 
47  ok &= test_equal(true,"peter", "p*"); 
48  ok &= test_equal(false,"peter", "p*j"); 
49  ok &= test_equal(true,"peter", "*peter"); 
50
51  std::vector<std::string> vec;
52  ok &= test_regexp(true,"abcde", "abcde", vec); 
53  vec.push_back("");
54  ok &= test_regexp(true,"abcde", "abcd?e", vec); 
55  vec[0]="c";
56  ok &= test_regexp(true,"abcde", "ab?de", vec); 
57  vec[0] = "bcd";
58  ok &= test_regexp(true,"abcde", "a*e", vec); 
59  vec.push_back("");
60  ok &= test_regexp(true,"abcddf", "a*d*f", vec); 
61  vec[0] = "bc";
62  vec[1] = "ef";
63  ok &= test_regexp(true,"abcdefg", "a*d*g", vec); 
64  vec.push_back("");
65  ok &= test_regexp(true,"abcdefg", "a*d*?g", vec); 
66
67  if (ok)
68    return 0;
69  return 1;
70}
71
72bool test_equal(bool answ, std::string a, std::string b)
73{
74  if (theplu::svndigest::equal(a.begin(), a.end(), b.begin(), b.end())==answ)
75    return true;
76  std::cerr << "equal(" << a << ", " << b << ") results "
77            << theplu::svndigest::equal(a.begin(), a.end(),b.begin(), b.end()) 
78            << ". Expects " << answ << std::endl;
79  return false;
80}
81
82bool test_hex(int x, u_int w, std::string facit)
83{
84  if (theplu::svndigest::hex(x,w)==facit)
85    return true;
86  std::cerr << "hex(" << x << ", " << w << ") results "
87            << theplu::svndigest::hex(x,w) << ". Expects " << facit
88            << std::endl;
89  return false;
90}
91
92bool test_regexp(bool ans, std::string a, std::string b, 
93                 const std::vector<std::string>& vec)
94{
95  using namespace theplu::svndigest;
96  std::vector<std::string> v;
97  bool res = regexp(a.begin(), a.end(), b.begin(), b.end(), v);
98  if (res!=ans || v!=vec) {
99    std::cerr << "regexp(" << a << ", " << b << ") results "
100              << res << ". Expected " << ans << "\n"
101              << "resulting vector:\n";
102    std::copy(v.begin(), v.end(), 
103              std::ostream_iterator<std::string>(std::cerr, "\n"));
104    std::cerr << "expected:\n";
105    std::copy(vec.begin(), vec.end(), 
106              std::ostream_iterator<std::string>(std::cerr, "\n"));
107    return false;
108  }
109  return true;
110
111}
Note: See TracBrowser for help on using the repository browser.