source: trunk/test/utility_test.cc @ 516

Last change on this file since 516 was 516, checked in by Peter Johansson, 14 years ago

adding filename translations in Configuration class. Before checking what type parsing to use, filename is translated according to rules set in config file. Default rule (as before) is to remove trailing .in from filenames. To solve the parsing I added a simple regexp function - see utility.h

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.2 KB
Line 
1// $Id: utility_test.cc 516 2007-12-09 07:58:54Z peter $
2
3/*
4  Copyright (C) 2007 Peter Johansson
5
6  This file is part of svndigest, http://trac.thep.lu.se/trac/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
50  std::vector<std::string> vec;
51  ok &= test_regexp(true,"abcde", "abcde", vec); 
52  vec.push_back("");
53  ok &= test_regexp(true,"abcde", "abcd?e", vec); 
54  vec[0]="c";
55  ok &= test_regexp(true,"abcde", "ab?de", vec); 
56  vec[0] = "bcd";
57  ok &= test_regexp(true,"abcde", "a*e", vec); 
58  vec.push_back("");
59  ok &= test_regexp(true,"abcddf", "a*d*f", vec); 
60  vec[0] = "bc";
61  vec[1] = "ef";
62  ok &= test_regexp(true,"abcdefg", "a*d*g", vec); 
63  vec.push_back("");
64  ok &= test_regexp(true,"abcdefg", "a*d*?g", vec); 
65
66  if (ok)
67    return 0;
68  return 1;
69}
70
71bool test_equal(bool answ, std::string a, std::string b)
72{
73  if (theplu::svndigest::equal(a.begin(), a.end(), b.begin(), b.end())==answ)
74    return true;
75  std::cerr << "equal(" << a << ", " << b << ") results "
76            << theplu::svndigest::equal(a.begin(), a.end(),b.begin(), b.end()) 
77            << ". Expects " << answ << std::endl;
78  return false;
79}
80
81bool test_hex(int x, u_int w, std::string facit)
82{
83  if (theplu::svndigest::hex(x,w)==facit)
84    return true;
85  std::cerr << "hex(" << x << ", " << w << ") results "
86            << theplu::svndigest::hex(x,w) << ". Expects " << facit
87            << std::endl;
88  return false;
89}
90
91bool test_regexp(bool ans, std::string a, std::string b, 
92                 const std::vector<std::string>& vec)
93{
94  using namespace theplu::svndigest;
95  std::vector<std::string> v;
96  bool res = regexp(a.begin(), a.end(), b.begin(), b.end(), v);
97  if (res!=ans || v!=vec) {
98    std::cerr << "regexp(" << a << ", " << b << ") results "
99              << res << ". Expected " << ans << "\n"
100              << "resulting vector:\n";
101    std::copy(v.begin(), v.end(), 
102              std::ostream_iterator<std::string>(std::cerr, "\n"));
103    std::cerr << "expected:\n";
104    std::copy(vec.begin(), vec.end(), 
105              std::ostream_iterator<std::string>(std::cerr, "\n"));
106    return false;
107  }
108  return true;
109
110}
Note: See TracBrowser for help on using the repository browser.