wibble  1.1
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 appendpath()
143  {
144  assert_eq(str::appendpath("a", "b"), "a/b");
145  assert_eq(str::appendpath("a/", "b"), "a/b");
146  assert_eq(str::appendpath("a", "/b"), "/b");
147  assert_eq(str::appendpath("a/", "/b"), "/b");
148  assert_eq(str::appendpath("/a", "b"), "/a/b");
149  assert_eq(str::appendpath("/a/", "b"), "/a/b");
150  assert_eq(str::appendpath("/a", "/b"), "/b");
151  assert_eq(str::appendpath("/a/", "/b"), "/b");
152  }
153 
154  Test urlencode()
155  {
156  assert_eq(str::urlencode(""), "");
157  assert_eq(str::urlencode("antani"), "antani");
158  assert_eq(str::urlencode("a b c"), "a%20b%20c");
159  assert_eq(str::urlencode("a "), "a%20");
160 
161  assert_eq(str::urldecode(""), "");
162  assert_eq(str::urldecode("antani"), "antani");
163  assert_eq(str::urldecode("a%20b"), "a b");
164  assert_eq(str::urldecode("a%20"), "a ");
165  assert_eq(str::urldecode("a%2"), "a");
166  assert_eq(str::urldecode("a%"), "a");
167 
168  assert_eq(str::urldecode(str::urlencode("àá☣☢☠!@#$%^&*(\")/A")), "àá☣☢☠!@#$%^&*(\")/A");
169  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");
170  }
171 
172  Test split1()
173  {
174  string val = "";
175  str::Split split("/", val);
176  str::Split::const_iterator i = split.begin();
177  assert(i == split.end());
178  }
179 
180  Test split2()
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, "foo");
187  assert_eq(i.remainder(), "");
188  ++i;
189  assert(i == split.end());
190  }
191 
192  Test split3()
193  {
194  string val = "foo";
195  str::Split split("", val);
196  str::Split::const_iterator i = split.begin();
197  assert(i != split.end());
198  assert_eq(*i, "f");
199  assert_eq(i.remainder(), "oo");
200  ++i;
201  assert_eq(*i, "o");
202  assert_eq(i.remainder(), "o");
203  ++i;
204  assert_eq(*i, "o");
205  assert_eq(i.remainder(), "");
206  ++i;
207  assert(i == split.end());
208  }
209 
210  Test split4()
211  {
212  string val = "/a//foo/";
213  str::Split split("/", val);
214  str::Split::const_iterator i = split.begin();
215  assert(i != split.end());
216  assert_eq(*i, "");
217  assert_eq(i.remainder(), "a//foo/");
218  ++i;
219  assert(i != split.end());
220  assert_eq(*i, "a");
221  assert_eq(i.remainder(), "/foo/");
222  ++i;
223  assert(i != split.end());
224  assert_eq(*i, "");
225  assert_eq(i.remainder(), "foo/");
226  ++i;
227  assert(i != split.end());
228  assert_eq(*i, "foo");
229  assert_eq(i.remainder(), "");
230  ++i;
231  assert(i == split.end());
232  }
233 
234  Test join()
235  {
236  string val = "/a//foo/";
237  str::Split split("/", val);
238  string res = str::join(split.begin(), split.end(), ":");
239  assert_eq(res, ":a::foo");
240  }
241 
242  Test normpath()
243  {
244  assert_eq(str::normpath(""), ".");
245  assert_eq(str::normpath("/"), "/");
246  assert_eq(str::normpath("foo"), "foo");
247  assert_eq(str::normpath("foo/"), "foo");
248  assert_eq(str::normpath("/foo"), "/foo");
249  assert_eq(str::normpath("foo/bar"), "foo/bar");
250  assert_eq(str::normpath("foo/./bar"), "foo/bar");
251  assert_eq(str::normpath("././././foo/./././bar/././././"), "foo/bar");
252  assert_eq(str::normpath("/../../../../../foo"), "/foo");
253  assert_eq(str::normpath("foo/../foo/../foo/../foo/../"), ".");
254  assert_eq(str::normpath("foo//bar"), "foo/bar");
255  assert_eq(str::normpath("foo/./bar"), "foo/bar");
256  assert_eq(str::normpath("foo/foo/../bar"), "foo/bar");
257  }
258 
259  Test base64()
260  {
261  using namespace str;
262  assert_eq(encodeBase64(""), "");
263  assert_eq(encodeBase64("c"), "Yw==");
264  assert_eq(encodeBase64("ci"), "Y2k=");
265  assert_eq(encodeBase64("cia"), "Y2lh");
266  assert_eq(encodeBase64("ciao"), "Y2lhbw==");
267  assert_eq(encodeBase64("ciao "), "Y2lhbyA=");
268  assert_eq(encodeBase64("ciao c"), "Y2lhbyBj");
269  assert_eq(encodeBase64("ciao ci"), "Y2lhbyBjaQ==");
270  assert_eq(encodeBase64("ciao cia"), "Y2lhbyBjaWE=");
271  assert_eq(encodeBase64("ciao ciao"), "Y2lhbyBjaWFv");
272 
274  assert_eq(decodeBase64(encodeBase64("c")), "c");
275  assert_eq(decodeBase64(encodeBase64("ci")), "ci");
276  assert_eq(decodeBase64(encodeBase64("cia")), "cia");
277  assert_eq(decodeBase64(encodeBase64("ciao")), "ciao");
278  assert_eq(decodeBase64(encodeBase64("ciao ")), "ciao ");
279  assert_eq(decodeBase64(encodeBase64("ciao c")), "ciao c");
280  assert_eq(decodeBase64(encodeBase64("ciao ci")), "ciao ci");
281  assert_eq(decodeBase64(encodeBase64("ciao cia")), "ciao cia");
282  assert_eq(decodeBase64(encodeBase64("ciao ciao")), "ciao ciao");
283  }
284 
285  Test yaml()
286  {
287  string data =
288  "Name: value\n"
289  "Multiline: value1\n"
290  " value2\n"
291  " value3\n"
292  "Multifield:\n"
293  " Field1: val1\n"
294  " Field2: val2\n"
295  " continue val2\n"
296  "\n"
297  "Name: second record\n";
298  stringstream input(data, ios_base::in);
299  str::YamlStream yamlStream;
300  str::YamlStream::const_iterator i = yamlStream.begin(input);
301  assert(i != yamlStream.end());
302  assert_eq(i->first, "Name");
303  assert_eq(i->second, "value");
304 
305  ++i;
306  assert(i != yamlStream.end());
307  assert_eq(i->first, "Multiline");
308  assert_eq(i->second,
309  "value1\n"
310  "value2\n"
311  " value3\n");
312 
313  ++i;
314  assert(i != yamlStream.end());
315  assert_eq(i->first, "Multifield");
316  assert_eq(i->second,
317  "Field1: val1\n"
318  "Field2: val2\n"
319  " continue val2\n");
320 
321  ++i;
322  assert(i == yamlStream.end());
323 
324  i = yamlStream.begin(input);
325  assert(i != yamlStream.end());
326  assert_eq(i->first, "Name");
327  assert_eq(i->second, "second record");
328 
329  ++i;
330  assert(i == yamlStream.end());
331 
332  i = yamlStream.begin(input);
333  assert(i == yamlStream.end());
334  }
335 
336  Test yamlComments()
337  {
338  string data =
339  "# comment\n"
340  "Name: value # comment\n"
341  "# comment\n"
342  "Multiline: value1 # comment \n"
343  " value2 # a\n"
344  " value3#b\n"
345  "\n"
346  "# comment\n"
347  "\n"
348  "Name: second record\n";
349  stringstream input(data, ios_base::in);
350  str::YamlStream yamlStream;
351  str::YamlStream::const_iterator i = yamlStream.begin(input);
352  assert(i != yamlStream.end());
353  assert_eq(i->first, "Name");
354  assert_eq(i->second, "value");
355 
356  ++i;
357  assert(i != yamlStream.end());
358  assert_eq(i->first, "Multiline");
359  assert_eq(i->second,
360  "value1\n"
361  "value2 # a\n"
362  " value3#b\n");
363 
364  ++i;
365  assert(i == yamlStream.end());
366 
367  i = yamlStream.begin(input);
368  assert(i != yamlStream.end());
369  assert_eq(i->first, "Name");
370  assert_eq(i->second, "second record");
371 
372  ++i;
373  assert(i == yamlStream.end());
374 
375  i = yamlStream.begin(input);
376  assert(i == yamlStream.end());
377  }
378 
379  Test c_escape_unescape() {
380  size_t len;
381  assert_eq(str::c_unescape("cia\\x00o", len), string("cia\0o", 5));
382  assert_eq(len, 8);
383  assert_eq(str::c_escape(string("cia\0o", 5)), "cia\\x00o");
384  }
385 };
386 
387 }
388 
389 // vim:set ts=4 sw=4: