test_utils.cpp
4.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/**
* tests/test_utils/test_utils.cpp
* Part of the NV Virtual Buffer Library
* This library is copyright 2007, 2008 NV Virtual Buffer Library Contributors
* This library is licensed under the GNU Lesser General Public Licence. See license.txt which is included with this library, or see
* http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
*/
#include <iostream>
#include <base/utils.h>
using namespace std;
int failCount=0;
#define test(expr, msg, input, output) if (!(expr)) { wcerr << L"fail: " << msg << L": " << input << L" -> " << output << endl; failCount++;}
#define testNoIO(expr, msg) if (!(expr)) { wcerr << L"fail: " << msg << endl; failCount++;}
void test_getNameForURL() {
wstring s, n;
n = getNameForURL(s);
test(n.empty(), L"empty", s, n);
s = L"javascript:foo";
n = getNameForURL(s);
test(n == L"foo", L"javascript", s, n);
s = L"http://www";
n = getNameForURL(s);
test(n == L"www", L"hostname only", s, n);
s = L"http://www.domain.com";
n = getNameForURL(s);
test(n == L"www.domain.com", L"domain only", s, n);
s = L"http://www.domain.com/";
n = getNameForURL(s);
test(n == L"www.domain.com", L"domain only with trailing slash", s, n);
s = L"http://www.domain.com/path/";
n = getNameForURL(s);
test(n == L"path", L"path", s, n);
s = L"http://www.domain.com/path/file";
n = getNameForURL(s);
test(n == L"file", L"file", s, n);
s = L"http://www.domain.com/file.ext";
n = getNameForURL(s);
test(n == L"file", L"file with extension", s, n);
s = L"http://www.domain.com/file.ext#anchor";
n = getNameForURL(s);
test(n == L"file anchor", L"anchor", s, n);
s = L"http://www.domain.com/file.ext#";
n = getNameForURL(s);
test(n == L"file ", L"empty anchor", s, n);
s = L"http://www.domain.com/#";
n = getNameForURL(s);
test(n == L"www.domain.com ", L"empty anchor no file", s, n);
s = L"http://www.domain.com/file.ext?query";
n = getNameForURL(s);
test(n == L"file query", L"query string", s, n);
s = L"http://www.domain.com/file.ext?query#anchor";
n = getNameForURL(s);
test(n == L"file query anchor", L"query string and anchor", s, n);
s = L"file.ext";
n = getNameForURL(s);
test(n == L"file", "no proto, file only", s, n);
s = L"/";
n = getNameForURL(s);
test(n == L"", L"no proto, slash only", s, n);
s = L"/path/file.ext";
n = getNameForURL(s);
test(n == L"file", L"no proto, path and file", s, n);
s = L"file.ext?query";
n = getNameForURL(s);
test(n == L"file query", L"no proto, file and query only", s, n);
s = L"?query";
n = getNameForURL(s);
test(n == L" query", L"no proto, query only", s, n);
s = L"/?query";
n = getNameForURL(s);
test(n == L" query", L"no proto, slash and query only", s, n);
}
void test_multiValueAttribsStringToMap() {
wstring s;
multiValueAttribsMap m;
multiValueAttribsMap::iterator it;
multiValueAttribsStringToMap(s, m);
testNoIO(m.size() == 0, L"empty");
m.clear();
s = L"a:1;b:2;c:3;";
multiValueAttribsStringToMap(s, m);
testNoIO(m.size() == 3 && m.find(L"a")->second == L"1" && m.find(L"b")->second == L"2" && m.find(L"c")->second == L"3", L"normal: " << s);
m.clear();
s = L"a\\:b:1;b:2\\;3;c\\;d:4\\:5;";
multiValueAttribsStringToMap(s, m);
testNoIO(m.size() == 3 && m.find(L"a:b")->second == L"1" && m.find(L"b")->second == L"2;3" && m.find(L"c;d")->second == L"4:5", L"escaping: " << s);
m.clear();
s = L":;";
multiValueAttribsStringToMap(s, m);
testNoIO(m.size() == 0, L"empty key: " << s);
m.clear();
s = L"a:;";
multiValueAttribsStringToMap(s, m);
testNoIO(m.size() == 1 && m.find(L"a")->second == L"", L"empty value: " << s);
m.clear();
s = L"a:1,2;";
multiValueAttribsStringToMap(s, m);
testNoIO(m.size() == 2 && m.count(L"a") == 2 && (it = m.find(L"a"))->second == L"1" && (++it)->second == L"2", L"single multi value: " << s);
m.clear();
s = L"a:a1,a2;b:b1;";
multiValueAttribsStringToMap(s, m);
testNoIO(m.size() == 3 && m.count(L"a") == 2 && m.count(L"b") == 1 && (it = m.find(L"a"))->second == L"a1" && (++it)->second == L"a2" && m.find(L"b")->second == L"b1", L"mixed multi and non-multi values: " << s);
m.clear();
s = L"a:a1,a2;b:;";
multiValueAttribsStringToMap(s, m);
testNoIO(m.size() == 3 && m.count(L"a") == 2 && m.count(L"b") == 1 && m.find(L"a")->second == L"a1" && m.find(L"b")->second == L"", L"mixed empty and multi values: " << s);
m.clear();
}
int main(int argc, char *argv[]) {
test_getNameForURL();
test_multiValueAttribsStringToMap();
if(failCount>0) {
wcerr<<L"number of failed tests: "<<failCount<<endl;
}
return failCount;
}