source: trunk/test/utility_test.cc @ 795

Last change on this file since 795 was 795, checked in by Peter Johansson, 12 years ago

refs #388

Create a sub-directory 'lib/yat' in which files from yat are
placed. The files may be updated via 'make fetch', see file README for
more details on how to change which files are updated/copied through
this mechanism.

The reason we do not use subversion's external mechanism is that we
don't want the files to be synchronised with yat automatically. We
want the update to be moderated by some developer.

I chose to fetch files using the svn client rather than wget because
the svn allows us to get an Id string with information from the yat
repository. 'wget' would just download the file as it appears on the
server that is the string is not expanded.

The file 'config_public.h' is created by configure just as in
yat. This implies that automake adds $(top_builddir)/lib/yat to the
include path so unless in a VPATH build $(top_srcdir)/lib/yat is in
the include path. As we have already added $(top_srcdir)/lib to the
include path, and there are both a lib/utility.h and a
lib/yat/utility.h it is not obvious which file is included when having
'include "utility.h"'. For this reason, when being in bin/ and test/
it is needed to include "../lib/utility.h" rather than "utility.h"
when we want to include 'lib/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 795 2009-06-30 03:57:27Z peter $
2
3/*
4  Copyright (C) 2007 Peter Johansson
5  Copyright (C) 2008 Jari Häkkinen, 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_equal(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_equal(true,"peter", "peter"); 
44  ok &= test_equal(false,"peter", "peterj"); 
45  ok &= test_equal(true,"p*", "peterj"); 
46  ok &= test_equal(true,"peter", "p*"); 
47  ok &= test_equal(false,"peter", "p*j"); 
48  ok &= test_equal(true,"peter", "*peter"); 
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, unsigned 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.