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
8namespace {
9
10using namespace std;
11using namespace wibble;
12
13struct 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
47 {
49 assert_eq( str::fmt( list::singular( 0 ) ), "[ 0 ]" );
51 list::singular( 0 ),
52 list::singular( 2 ) ) ), "[ 0, 2 ]" );
53 }
54
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
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
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
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
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
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
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);
177 assert(i == split.end());
178 }
179
180 Test split2()
181 {
182 string val = "foo";
183 str::Split split("/", val);
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);
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);
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
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);
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
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);
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
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:
Definition string.h:322
std::string remainder() const
Definition string.h:363
Split a string where a given substring is found.
Definition string.h:315
Parse a record of Yaml-style field: value couples.
Definition string.h:436
Singular< X > singular(const X &x)
Definition list.h:331
Append< X, Y > append(const X &x, const Y &y)
Definition list.h:336
std::string join(const ITER &begin, const ITER &end, const std::string &sep=", ")
Definition string.h:406
std::string trim(const std::string &str, const FUN &classifier)
Return the substring of 'str' without all leading and trailing characters for which 'classifier' retu...
Definition string.h:187
std::string normpath(const std::string &pathname)
Normalise a pathname.
Definition string.cpp:133
std::string urlencode(const std::string &str)
Urlencode a string.
Definition string.cpp:160
std::string c_unescape(const std::string &str, size_t &lenParsed)
Unescape a C string, stopping at the first double quotes or at the end of the string.
Definition string.cpp:424
std::string dirname(const std::string &pathname)
Given a pathname, return the directory name without the file name.
Definition string.h:134
std::string toupper(const std::string &str)
Convert a string to uppercase.
Definition string.h:228
std::string urldecode(const std::string &str)
Decode an urlencoded string.
Definition string.cpp:178
bool endsWith(const std::string &str, const std::string &part)
Check if a string ends with the given substring.
Definition string.h:162
std::string encodeBase64(const std::string &str)
Encode a string in Base64.
Definition string.cpp:208
std::string appendpath(const std::string &path1, const std::string &path2)
Definition string.h:277
std::string decodeBase64(const std::string &str)
Decode a string encoded in Base64.
Definition string.cpp:241
bool startsWith(const std::string &str, const std::string &part)
Check if a string starts with the given substring.
Definition string.h:154
std::string c_escape(const std::string &str)
Escape the string so it can safely used as a C string inside double quotes.
Definition string.cpp:400
std::string ucfirst(const std::string &str)
Return the same string, with the first character uppercased.
Definition string.h:248
std::string joinpath(const std::string &path1, const std::string &path2)
Join two paths, adding slashes when appropriate.
Definition string.h:257
std::string fmtf(const char *f,...)
Definition string.cpp:113
std::string tolower(const std::string &str)
Convert a string to lowercase.
Definition string.h:238
std::string basename(const std::string &pathname)
Given a pathname, return the file name without its path.
Definition string.h:124
std::string fmt(const char *f,...)
Definition string.cpp:123
Definition amorph.h:17
Definition amorph.h:30
Definition list.h:284
#define assert_eq(x, y)
Definition test.h:33
#define assert(x)
Definition test.h:30