1 | // $Id: utility_test.cc 845 2009-11-16 22:27:19Z jari $ |
---|
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 | |
---|
30 | bool test_hex(int, unsigned int, std::string); |
---|
31 | bool test_equal(bool, std::string, std::string); |
---|
32 | bool test_regexp(bool, std::string, std::string, |
---|
33 | const std::vector<std::string>&); |
---|
34 | |
---|
35 | int 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 | |
---|
71 | bool 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 | |
---|
81 | bool 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 | |
---|
91 | bool 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 | } |
---|