wibble  0.1.28
string.test.h
Go to the documentation of this file.
1 /* -*- C++ -*- (c) 2007 Petr Rockai <me@mornfall.net>
2  (c) 2007 Enrico Zini <enrico@enricozini.org> */
3 
4 #include <wibble/test.h>
5 #include <wibble/string.h>
6 #include <wibble/list.h>
7 
8 namespace {
9 
10 using namespace std;
11 using namespace wibble;
12 
13 struct TestString {
14 
15  Test fmt()
16  {
17  assert_eq(str::fmt(5), "5");
18  assert_eq(str::fmt(5.123), "5.123");
19  assert_eq(str::fmtf("ciao"), "ciao");
20  }
21 
22  Test fmtSet()
23  {
24  std::set< int > a;
25  assert_eq(str::fmt(a), "{}");
26  a.insert( a.begin(), 2 );
27  assert_eq(str::fmt(a), "{ 2 }");
28  a.insert( a.begin(), 5 );
29  assert_eq(str::fmt(a), "{ 2, 5 }");
30  a.insert( a.begin(), 1 );
31  assert_eq(str::fmt(a), "{ 1, 2, 5 }");
32  }
33 
34  Test fmtVec()
35  {
36  std::vector< int > a;
37  assert_eq(str::fmt(a), "[]");
38  a.push_back( 2 );
39  assert_eq(str::fmt(a), "[ 2 ]");
40  a.push_back( 5 );
41  assert_eq(str::fmt(a), "[ 2, 5 ]");
42  a.push_back( 1 );
43  assert_eq(str::fmt(a), "[ 2, 5, 1 ]");
44  }
45 
46  Test fmtList()
47  {
49  assert_eq( str::fmt( list::singular( 0 ) ), "[ 0 ]" );
51  list::singular( 0 ),
52  list::singular( 2 ) ) ), "[ 0, 2 ]" );
53  }
54 
55  Test basename()
56  {
57  assert_eq(str::basename("ciao"), "ciao");
58  assert_eq(str::basename("a/ciao"), "ciao");
59  assert_eq(str::basename("a/b/c/c/d/e/ciao"), "ciao");
60  assert_eq(str::basename("/ciao"), "ciao");
61  }
62 
63  Test dirname()
64  {
65  assert_eq(str::dirname("ciao"), "");
66  assert_eq(str::dirname("a/ciao"), "a");
67  assert_eq(str::dirname("a/b/c/c/d/e/ciao"), "a/b/c/c/d/e");
68  assert_eq(str::dirname("/a/ciao"), "/a");
69  assert_eq(str::dirname("/ciao"), "/");
70  }
71 
72  Test trim()
73  {
74  assert_eq(str::trim(" "), "");
75  assert_eq(str::trim(" c "), "c");
76  assert_eq(str::trim("ciao"), "ciao");
77  assert_eq(str::trim(" ciao"), "ciao");
78  assert_eq(str::trim(" ciao"), "ciao");
79  assert_eq(str::trim("ciao "), "ciao");
80  assert_eq(str::trim("ciao "), "ciao");
81  assert_eq(str::trim(" ciao "), "ciao");
82  assert_eq(str::trim(" ciao "), "ciao");
83  }
84 
85  Test trim2()
86  {
87  assert_eq(str::trim(string("ciao"), ::isalpha), "");
88  assert_eq(str::trim(" ", ::isalpha), " ");
89  }
90 
91  Test tolower()
92  {
93  assert_eq(str::tolower("ciao"), "ciao");
94  assert_eq(str::tolower("CIAO"), "ciao");
95  assert_eq(str::tolower("Ciao"), "ciao");
96  assert_eq(str::tolower("cIAO"), "ciao");
97  }
98 
99  Test toupper()
100  {
101  assert_eq(str::toupper("ciao"), "CIAO");
102  assert_eq(str::toupper("CIAO"), "CIAO");
103  assert_eq(str::toupper("Ciao"), "CIAO");
104  assert_eq(str::toupper("cIAO"), "CIAO");
105  }
106 
107  Test ucfirst()
108  {
109  assert_eq(str::ucfirst("ciao"), "Ciao");
110  assert_eq(str::ucfirst("CIAO"), "Ciao");
111  assert_eq(str::ucfirst("Ciao"), "Ciao");
112  assert_eq(str::ucfirst("cIAO"), "Ciao");
113  }
114 
115 // Check startsWith
116  Test startsWith()
117  {
118  assert(str::startsWith("ciao", "ci"));
119  assert(str::startsWith("ciao", ""));
120  assert(str::startsWith("ciao", "ciao"));
121  assert(!str::startsWith("ciao", "ciaoa"));
122  assert(!str::startsWith("ciao", "i"));
123  }
124 
125  Test endsWith()
126  {
127  assert(str::endsWith("ciao", "ao"));
128  assert(str::endsWith("ciao", ""));
129  assert(str::endsWith("ciao", "ciao"));
130  assert(!str::endsWith("ciao", "aciao"));
131  assert(!str::endsWith("ciao", "a"));
132  }
133 
134  Test joinpath()
135  {
136  assert_eq(str::joinpath("a", "b"), "a/b");
137  assert_eq(str::joinpath("a/", "b"), "a/b");
138  assert_eq(str::joinpath("a", "/b"), "a/b");
139  assert_eq(str::joinpath("a/", "/b"), "a/b");
140  }
141 
142  Test urlencode()
143  {
144  assert_eq(str::urlencode(""), "");
145  assert_eq(str::urlencode("antani"), "antani");
146  assert_eq(str::urlencode("a b c"), "a%20b%20c");
147  assert_eq(str::urlencode("a "), "a%20");
148 
149  assert_eq(str::urldecode(""), "");
150  assert_eq(str::urldecode("antani"), "antani");
151  assert_eq(str::urldecode("a%20b"), "a b");
152  assert_eq(str::urldecode("a%20"), "a ");
153  assert_eq(str::urldecode("a%2"), "a");
154  assert_eq(str::urldecode("a%"), "a");
155 
156  assert_eq(str::urldecode(str::urlencode("àá☣☢☠!@#$%^&*(\")/A")), "àá☣☢☠!@#$%^&*(\")/A");
157  assert_eq(str::urldecode(str::urlencode("http://zz:ss@a.b:31/c?d=e&f=g")), "http://zz:ss@a.b:31/c?d=e&f=g");
158  }
159 
160  Test split1()
161  {
162  string val = "";
163  str::Split split("/", val);
164  str::Split::const_iterator i = split.begin();
165  assert(i == split.end());
166  }
167 
168  Test split2()
169  {
170  string val = "foo";
171  str::Split split("/", val);
172  str::Split::const_iterator i = split.begin();
173  assert(i != split.end());
174  assert_eq(*i, "foo");
175  assert_eq(i.remainder(), "");
176  ++i;
177  assert(i == split.end());
178  }
179 
180  Test split3()
181  {
182  string val = "foo";
183  str::Split split("", val);
184  str::Split::const_iterator i = split.begin();
185  assert(i != split.end());
186  assert_eq(*i, "f");
187  assert_eq(i.remainder(), "oo");
188  ++i;
189  assert_eq(*i, "o");
190  assert_eq(i.remainder(), "o");
191  ++i;
192  assert_eq(*i, "o");
193  assert_eq(i.remainder(), "");
194  ++i;
195  assert(i == split.end());
196  }
197 
198  Test split4()
199  {
200  string val = "/a//foo/";
201  str::Split split("/", val);
202  str::Split::const_iterator i = split.begin();
203  assert(i != split.end());
204  assert_eq(*i, "");
205  assert_eq(i.remainder(), "a//foo/");
206  ++i;
207  assert(i != split.end());
208  assert_eq(*i, "a");
209  assert_eq(i.remainder(), "/foo/");
210  ++i;
211  assert(i != split.end());
212  assert_eq(*i, "");
213  assert_eq(i.remainder(), "foo/");
214  ++i;
215  assert(i != split.end());
216  assert_eq(*i, "foo");
217  assert_eq(i.remainder(), "");
218  ++i;
219  assert(i == split.end());
220  }
221 
222  Test join()
223  {
224  string val = "/a//foo/";
225  str::Split split("/", val);
226  string res = str::join(split.begin(), split.end(), ":");
227  assert_eq(res, ":a::foo");
228  }
229 
230  Test normpath()
231  {
232  assert_eq(str::normpath(""), ".");
233  assert_eq(str::normpath("/"), "/");
234  assert_eq(str::normpath("foo"), "foo");
235  assert_eq(str::normpath("foo/"), "foo");
236  assert_eq(str::normpath("/foo"), "/foo");
237  assert_eq(str::normpath("foo/bar"), "foo/bar");
238  assert_eq(str::normpath("foo/./bar"), "foo/bar");
239  assert_eq(str::normpath("././././foo/./././bar/././././"), "foo/bar");
240  assert_eq(str::normpath("/../../../../../foo"), "/foo");
241  assert_eq(str::normpath("foo/../foo/../foo/../foo/../"), ".");
242  assert_eq(str::normpath("foo//bar"), "foo/bar");
243  assert_eq(str::normpath("foo/./bar"), "foo/bar");
244  assert_eq(str::normpath("foo/foo/../bar"), "foo/bar");
245  }
246 
247  Test base64()
248  {
249  using namespace str;
250  assert_eq(encodeBase64(""), "");
251  assert_eq(encodeBase64("c"), "Yw==");
252  assert_eq(encodeBase64("ci"), "Y2k=");
253  assert_eq(encodeBase64("cia"), "Y2lh");
254  assert_eq(encodeBase64("ciao"), "Y2lhbw==");
255  assert_eq(encodeBase64("ciao "), "Y2lhbyA=");
256  assert_eq(encodeBase64("ciao c"), "Y2lhbyBj");
257  assert_eq(encodeBase64("ciao ci"), "Y2lhbyBjaQ==");
258  assert_eq(encodeBase64("ciao cia"), "Y2lhbyBjaWE=");
259  assert_eq(encodeBase64("ciao ciao"), "Y2lhbyBjaWFv");
260 
262  assert_eq(decodeBase64(encodeBase64("c")), "c");
263  assert_eq(decodeBase64(encodeBase64("ci")), "ci");
264  assert_eq(decodeBase64(encodeBase64("cia")), "cia");
265  assert_eq(decodeBase64(encodeBase64("ciao")), "ciao");
266  assert_eq(decodeBase64(encodeBase64("ciao ")), "ciao ");
267  assert_eq(decodeBase64(encodeBase64("ciao c")), "ciao c");
268  assert_eq(decodeBase64(encodeBase64("ciao ci")), "ciao ci");
269  assert_eq(decodeBase64(encodeBase64("ciao cia")), "ciao cia");
270  assert_eq(decodeBase64(encodeBase64("ciao ciao")), "ciao ciao");
271  }
272 
273  Test yaml()
274  {
275  string data =
276  "Name: value\n"
277  "Multiline: value1\n"
278  " value2\n"
279  " value3\n"
280  "Multifield:\n"
281  " Field1: val1\n"
282  " Field2: val2\n"
283  " continue val2\n"
284  "\n"
285  "Name: second record\n";
286  stringstream input(data, ios_base::in);
287  str::YamlStream yamlStream;
288  str::YamlStream::const_iterator i = yamlStream.begin(input);
289  assert(i != yamlStream.end());
290  assert_eq(i->first, "Name");
291  assert_eq(i->second, "value");
292 
293  ++i;
294  assert(i != yamlStream.end());
295  assert_eq(i->first, "Multiline");
296  assert_eq(i->second,
297  "value1\n"
298  "value2\n"
299  " value3\n");
300 
301  ++i;
302  assert(i != yamlStream.end());
303  assert_eq(i->first, "Multifield");
304  assert_eq(i->second,
305  "Field1: val1\n"
306  "Field2: val2\n"
307  " continue val2\n");
308 
309  ++i;
310  assert(i == yamlStream.end());
311 
312  i = yamlStream.begin(input);
313  assert(i != yamlStream.end());
314  assert_eq(i->first, "Name");
315  assert_eq(i->second, "second record");
316 
317  ++i;
318  assert(i == yamlStream.end());
319 
320  i = yamlStream.begin(input);
321  assert(i == yamlStream.end());
322  }
323 
324  Test yamlComments()
325  {
326  string data =
327  "# comment\n"
328  "Name: value # comment\n"
329  "# comment\n"
330  "Multiline: value1 # comment \n"
331  " value2 # a\n"
332  " value3#b\n"
333  "\n"
334  "# comment\n"
335  "\n"
336  "Name: second record\n";
337  stringstream input(data, ios_base::in);
338  str::YamlStream yamlStream;
339  str::YamlStream::const_iterator i = yamlStream.begin(input);
340  assert(i != yamlStream.end());
341  assert_eq(i->first, "Name");
342  assert_eq(i->second, "value");
343 
344  ++i;
345  assert(i != yamlStream.end());
346  assert_eq(i->first, "Multiline");
347  assert_eq(i->second,
348  "value1\n"
349  "value2 # a\n"
350  " value3#b\n");
351 
352  ++i;
353  assert(i == yamlStream.end());
354 
355  i = yamlStream.begin(input);
356  assert(i != yamlStream.end());
357  assert_eq(i->first, "Name");
358  assert_eq(i->second, "second record");
359 
360  ++i;
361  assert(i == yamlStream.end());
362 
363  i = yamlStream.begin(input);
364  assert(i == yamlStream.end());
365  }
366 };
367 
368 }
369 
370 // vim:set ts=4 sw=4: