source: trunk/test/utility_test.cc @ 687

Last change on this file since 687 was 687, checked in by Peter Johansson, 13 years ago

Merged patch release 0.6.6 to trunk. Delta 0.6.6 - 0.6.5

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.3 KB
Line 
1// $Id: utility_test.cc 687 2008-08-04 19:37:10Z peter $
2
3/*
4  Copyright (C) 2007 Peter Johansson
5  Copyright (C) 2008 Jari Häkkinen
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 2 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 this program; if not, write to the Free Software
21  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
22  02111-1307, USA.
23*/
24
25#include "utility.h"
26
27#include <algorithm>
28#include <iterator>
29#include <iostream>
30#include <string>
31
32bool test_hex(int, unsigned int, std::string);
33bool test_equal(bool, std::string, std::string);
34bool test_regexp(bool, std::string, std::string, 
35                 const std::vector<std::string>&);
36
37int main(const int argc,const char* argv[])
38{
39  bool ok=true;
40 
41  ok &= test_hex(15,2, "0f"); 
42  ok &= test_hex(17,1, "1"); 
43  ok &= test_hex(16,2, "10"); 
44
45  ok &= test_equal(true,"peter", "peter"); 
46  ok &= test_equal(false,"peter", "peterj"); 
47  ok &= test_equal(true,"p*", "peterj"); 
48  ok &= test_equal(true,"peter", "p*"); 
49  ok &= test_equal(false,"peter", "p*j"); 
50  ok &= test_equal(true,"peter", "*peter"); 
51
52  std::vector<std::string> vec;
53  ok &= test_regexp(true,"abcde", "abcde", vec); 
54  vec.push_back("");
55  ok &= test_regexp(true,"abcde", "abcd?e", vec); 
56  vec[0]="c";
57  ok &= test_regexp(true,"abcde", "ab?de", vec); 
58  vec[0] = "bcd";
59  ok &= test_regexp(true,"abcde", "a*e", vec); 
60  vec.push_back("");
61  ok &= test_regexp(true,"abcddf", "a*d*f", vec); 
62  vec[0] = "bc";
63  vec[1] = "ef";
64  ok &= test_regexp(true,"abcdefg", "a*d*g", vec); 
65  vec.push_back("");
66  ok &= test_regexp(true,"abcdefg", "a*d*?g", vec); 
67
68  if (ok)
69    return 0;
70  return 1;
71}
72
73bool test_equal(bool answ, std::string a, std::string b)
74{
75  if (theplu::svndigest::equal(a.begin(), a.end(), b.begin(), b.end())==answ)
76    return true;
77  std::cerr << "equal(" << a << ", " << b << ") results "
78            << theplu::svndigest::equal(a.begin(), a.end(),b.begin(), b.end()) 
79            << ". Expects " << answ << std::endl;
80  return false;
81}
82
83bool test_hex(int x, unsigned int w, std::string facit)
84{
85  if (theplu::svndigest::hex(x,w)==facit)
86    return true;
87  std::cerr << "hex(" << x << ", " << w << ") results "
88            << theplu::svndigest::hex(x,w) << ". Expects " << facit
89            << std::endl;
90  return false;
91}
92
93bool test_regexp(bool ans, std::string a, std::string b, 
94                 const std::vector<std::string>& vec)
95{
96  using namespace theplu::svndigest;
97  std::vector<std::string> v;
98  bool res = regexp(a.begin(), a.end(), b.begin(), b.end(), v);
99  if (res!=ans || v!=vec) {
100    std::cerr << "regexp(" << a << ", " << b << ") results "
101              << res << ". Expected " << ans << "\n"
102              << "resulting vector:\n";
103    std::copy(v.begin(), v.end(), 
104              std::ostream_iterator<std::string>(std::cerr, "\n"));
105    std::cerr << "expected:\n";
106    std::copy(vec.begin(), vec.end(), 
107              std::ostream_iterator<std::string>(std::cerr, "\n"));
108    return false;
109  }
110  return true;
111
112}
Note: See TracBrowser for help on using the repository browser.