Merge remote branch 'origin/kernel-upgrade'
[libi2ncommon] / test / stringfunc.cpp
CommitLineData
118e216e
TJ
1/***************************************************************************
2 * Copyright (C) 2006 by Intra2net AG *
3 * info@intra2net.com *
4 * *
5 ***************************************************************************/
6
7// #include <iostream>
8#include <string>
9// #include <sstream>
10// #include <stdexcept>
11
12#include <cppunit/extensions/TestFactoryRegistry.h>
13#include <cppunit/ui/text/TestRunner.h>
14#include <cppunit/extensions/HelperMacros.h>
15
16#include <stringfunc.hxx>
57e78ccd 17#include <containerfunc.hpp>
118e216e
TJ
18
19using namespace std;
20using namespace CppUnit;
57e78ccd 21using namespace I2n;
118e216e
TJ
22
23class stringfunc : public TestFixture
24{
25 CPPUNIT_TEST_SUITE(stringfunc);
26
27 CPPUNIT_TEST(smart_html_entites1);
28 CPPUNIT_TEST(smart_html_entites2);
29 CPPUNIT_TEST(smart_html_entites3);
30 CPPUNIT_TEST(smart_html_entites4);
31 CPPUNIT_TEST(smart_html_entites5);
32 CPPUNIT_TEST(smart_html_entites6);
33 CPPUNIT_TEST(smart_html_entites7);
34 CPPUNIT_TEST(strip_html_tags1);
35 CPPUNIT_TEST(strip_html_tags2);
a5f3af6e
GE
36 CPPUNIT_TEST(html_entities1);
37 CPPUNIT_TEST(html_entities2);
38 CPPUNIT_TEST(html_entities3);
6a2b6dd1
TJ
39 CPPUNIT_TEST(imaputf7_to_utf8);
40 CPPUNIT_TEST(utf8_to_imaputf7);
118e216e 41
57e78ccd
RP
42
43 CPPUNIT_TEST(TestTrim);
44 CPPUNIT_TEST(TestChomp);
45 CPPUNIT_TEST(TestSuffixFuncs);
46 CPPUNIT_TEST(TestPrefixFuncs);
47 CPPUNIT_TEST(TestLowerUpperFuncs);
48
49 CPPUNIT_TEST(PairSplit1);
50 CPPUNIT_TEST(SplitString1);
51 CPPUNIT_TEST(SplitString2);
f61aeac1
RP
52 CPPUNIT_TEST(SplitStringEmpty);
53 CPPUNIT_TEST(SplitStringDelimiterOnly);
57e78ccd
RP
54 CPPUNIT_TEST(JoinString1);
55
56 CPPUNIT_TEST(ConversionStringInt);
57
58 CPPUNIT_TEST(HexConversion);
59
60
118e216e
TJ
61 CPPUNIT_TEST_SUITE_END();
62
57e78ccd
RP
63 protected:
64 typedef std::list< std::string > StringList;
65
118e216e
TJ
66 public:
67 void smart_html_entites1()
68 {
69 string output = smart_html_entities("Test");
70
71 CPPUNIT_ASSERT_EQUAL(string("Test"), output);
72 }
73
74 void smart_html_entites2()
75 {
76 string output = smart_html_entities("Täst");
77
78 CPPUNIT_ASSERT_EQUAL(string("T&auml;st"), output);
79 }
80
81 void smart_html_entites3()
82 {
83 string output = smart_html_entities("<>");
84
85 CPPUNIT_ASSERT_EQUAL(string("<>"), output);
86 }
87
88 void smart_html_entites4()
89 {
90 string output = smart_html_entities("<ümlaut>");
91
92 CPPUNIT_ASSERT_EQUAL(string("<ümlaut>"), output);
93 }
94
95 void smart_html_entites5()
96 {
97 string output = smart_html_entities("Test<ümlaut>Blä");
98
99 CPPUNIT_ASSERT_EQUAL(string("Test<ümlaut>Bl&auml;"), output);
100 }
101
102 void smart_html_entites6()
103 {
104 string output = smart_html_entities("System > Einstellungen");
105
106 CPPUNIT_ASSERT_EQUAL(string("System &gt; Einstellungen"), output);
107 }
108
109 void smart_html_entites7()
110 {
111 string output = smart_html_entities("Finden Sie <b>auf</b> der Seite <a href=\"fdslfsl\">\"System > Einstellungen\"</a>. Oder etwa nicht?");
112
113 CPPUNIT_ASSERT_EQUAL(string("Finden Sie <b>auf</b> der Seite <a href=\"fdslfsl\">&quot;System &gt; Einstellungen&quot;</a>. Oder etwa nicht?"), output);
114 }
115
116 void strip_html_tags1()
117 {
118 string output = strip_html_tags("Was für ein schöner Tag, finden Sie nicht?");
119
120 CPPUNIT_ASSERT_EQUAL(string("Was für ein schöner Tag, finden Sie nicht?"), output);
121 }
122
123 void strip_html_tags2()
124 {
125 string output = strip_html_tags("Was für ein <a href=\"wikipedia\" target=\"new\">schöner Tag</a>, finden Sie nicht?");
126
127 CPPUNIT_ASSERT_EQUAL(string("Was für ein schöner Tag, finden Sie nicht?"), output);
128 }
a5f3af6e
GE
129
130 void html_entities1()
131 {
132 string output = html_entities("\xC3\xA4\xC3\xB6\xC3\xBC");
133 CPPUNIT_ASSERT_EQUAL(string("&auml;&ouml;&uuml;"), output);
134 }
135
136 void html_entities2()
137 {
138 string output = html_entities("\xC3\xA5 \xC3\xB5 \xC3\xBF");
139 CPPUNIT_ASSERT_EQUAL(string("&#229; &#245; &#255;"), output);
140 }
141
142 void html_entities3()
143 {
144 string output = html_entities("\xC4\x8E \xE0\xBC\xB1 \xE8\x82\x88");
145 CPPUNIT_ASSERT_EQUAL(string("&#270; &#3889; &#32904;"), output);
146 }
6a2b6dd1
TJ
147
148 void imaputf7_to_utf8()
149 {
150 string output = utf7imap_to_utf8("Sp&AOQ-m");
151 CPPUNIT_ASSERT_EQUAL(string("Späm"), output);
152 }
153
154 void utf8_to_imaputf7()
155 {
156 string output = utf8_to_utf7imap("Späm");
157 CPPUNIT_ASSERT_EQUAL(string("Sp&AOQ-m"), output);
158 }
57e78ccd
RP
159
160 /*
161 **
162 */
163
164 void TestTrim()
165 {
166 std::string s("s1");
167 trim_mod(s);
168 CPPUNIT_ASSERT_EQUAL( std::string("s1"), s );
169
170 s=" s2";
171 trim_mod(s);
172 CPPUNIT_ASSERT_EQUAL( std::string("s2"), s );
173
174 s="s3 ";
175 trim_mod(s);
176 CPPUNIT_ASSERT_EQUAL( std::string("s3"), s );
177
178 s="::s4:s4::++--aa";
179 trim_mod(s,":+-a");
180 CPPUNIT_ASSERT_EQUAL( std::string("s4:s4"), s);
181
182 /* non modifying version */
183
184 s= "s1";
185 CPPUNIT_ASSERT_EQUAL( std::string("s1"), trim(s) );
186
187 s=" s2";
188 CPPUNIT_ASSERT_EQUAL( std::string("s2"), trim(s) );
189 CPPUNIT_ASSERT_EQUAL( std::string(" s2"), s );
190
191 s="s3 ";
192 CPPUNIT_ASSERT_EQUAL( std::string("s3"), trim(s) );
193 CPPUNIT_ASSERT_EQUAL( std::string("s3 "), s );
194
195 s="::s4:s4::++--aa";
196 CPPUNIT_ASSERT_EQUAL( std::string("s4:s4"), trim(s,":+-a") );
197 } // eo TestTrim()
198
199
200
201 void TestChomp()
202 {
203 std::string s("s1");
204
205 chomp_mod(s);
206 CPPUNIT_ASSERT_EQUAL( std::string("s1"), s );
207
208 s="s2\n";
209 chomp_mod(s);
210 CPPUNIT_ASSERT_EQUAL( std::string("s2"), s );
211
212 s="s3:";
213 chomp_mod(s,":");
214 CPPUNIT_ASSERT_EQUAL( std::string("s3"), s );
215
216 s=":s4::";
217 chomp_mod(s,"s:");
218 CPPUNIT_ASSERT_EQUAL( std::string(":s4:"), s);
219
220 /* non modifiying versions */
221 s= "s1";
222
223 CPPUNIT_ASSERT_EQUAL( std::string("s1"), chomp(s) );
224
225 s="s2\n";
226 CPPUNIT_ASSERT_EQUAL( std::string("s2"), chomp(s) );
227 CPPUNIT_ASSERT_EQUAL( std::string("s2\n"), s);
228
229 s="s3:";
230 CPPUNIT_ASSERT_EQUAL( std::string("s3"), chomp(s,":") );
231 CPPUNIT_ASSERT_EQUAL( std::string("s3:"), s);
232
233 s=":s4::";
234 CPPUNIT_ASSERT_EQUAL( std::string(":s4:"), chomp(s,"s:") );
235 CPPUNIT_ASSERT_EQUAL( std::string(":s4::"), s);
236 } // eo TestChomp()
237
238
239
240 void TestSuffixFuncs()
241 {
242 std::string s1("12.cpp");
243
244 CPPUNIT_ASSERT_EQUAL( true, has_suffix(s1,".cpp") );
245 CPPUNIT_ASSERT_EQUAL( true, has_suffix(s1,"pp") );
246 CPPUNIT_ASSERT_EQUAL( false, has_suffix(s1,"hpp") );
247 CPPUNIT_ASSERT_EQUAL( false, has_suffix(s1,"cp") );
248 CPPUNIT_ASSERT_EQUAL( false, has_suffix(s1,"") );
249
250 std::string s1c1= remove_suffix(s1,".cpp");
251 CPPUNIT_ASSERT_EQUAL( std::string("12"), s1c1 );
252
253 std::string s1c2= remove_suffix(s1,"p");
254 CPPUNIT_ASSERT_EQUAL( std::string("12.cp"), s1c2 );
255
256 std::string s1c3= remove_suffix(s1,"cp");
257 CPPUNIT_ASSERT_EQUAL( std::string("12.cpp"), s1c3 );
258
259 std::string s2(".cpp");
260 CPPUNIT_ASSERT_EQUAL( true, has_suffix(s2,".cpp") );
261
262 std::string s2c1= remove_suffix(s2,".cpp");
263 CPPUNIT_ASSERT_EQUAL( std::string(""), s2c1 );
264
265 } // eo TestSuffixFuncs()
266
267
268
269 void TestPrefixFuncs()
270 {
271 std::string s1("12.cpp");
272
273 CPPUNIT_ASSERT_EQUAL( true, has_prefix(s1,"12") );
274 CPPUNIT_ASSERT_EQUAL( true, has_prefix(s1, "1") );
275 CPPUNIT_ASSERT_EQUAL( false, has_prefix(s1, "2") );
276 CPPUNIT_ASSERT_EQUAL( false, has_prefix(s1, "") );
277
278 std::string s1c1= remove_prefix(s1, "12");
279 CPPUNIT_ASSERT_EQUAL( std::string(".cpp"), s1c1);
280 } // eo TestPrefixFuncs()
281
282
283
284 void TestLowerUpperFuncs()
285 {
286 std::string u1("CASE CONVERSION TEST..");
287 std::string l1("case conversion test..");
288
289 std::string test1(l1);
290
291 to_upper_mod(test1);
292 CPPUNIT_ASSERT_EQUAL( u1, test1 );
293
294 to_lower_mod(test1);
295 CPPUNIT_ASSERT_EQUAL( l1, test1 );
296
297
298 CPPUNIT_ASSERT_EQUAL( u1, to_upper(l1) );
299 CPPUNIT_ASSERT_EQUAL( l1, to_lower(u1) );
300 } // eo TestLowerUpper
301
302
303
304 void PairSplit1()
305 {
306 StringList str_list;
307 get_push_back_filler(str_list)
308 ("a=11")("a= 11")("a =11 ")("a = 11 ")(" a = 11 ")
309 ;
310 CPPUNIT_ASSERT_EQUAL( 5u, str_list.size() );
311 for(StringList::iterator it= str_list.begin();
312 it != str_list.end();
313 ++it)
314 {
315 std::string key, value;
316 bool res= pair_split( *it, key, value);
317
318 CPPUNIT_ASSERT_EQUAL( true , res );
319 CPPUNIT_ASSERT_EQUAL( std::string("a"), key );
320 CPPUNIT_ASSERT_EQUAL( std::string("11"), value );
321 }
322
323 std::string key, value;
324 bool res;
325
326 res= pair_split(" 1 : 2 ", key, value, ':');
327 CPPUNIT_ASSERT_EQUAL( true, res );
328 CPPUNIT_ASSERT_EQUAL( std::string("1"), key);
329 CPPUNIT_ASSERT_EQUAL( std::string("2"), value);
330 } // eo PairSplit1
331
332
333
334 void SplitString1()
335 {
336 std::string block(
337 "Zeile 1\n"
338 "++Zeile-2--\n"
339 "Zeile 3\n"
340 "\n"
341 "Zeile 5\n"
342 );
343
344 StringList list1;
345
346 split_string(block, list1, "\n");
347 // since the blocks ends with \n we should have 6 lines (the last one empty):
348 CPPUNIT_ASSERT_EQUAL( 6u, list1.size() );
349 CPPUNIT_ASSERT_EQUAL( std::string(), list1.back() );
350 CPPUNIT_ASSERT_EQUAL( std::string("Zeile 1"), list1.front() );
351
352 StringList list2;
353
354 split_string(block, list2, "\n", true);
355
356 // now we omitted empty lines, now we should have only 4 lines left:
357 CPPUNIT_ASSERT_EQUAL( 4u, list2.size() );
358 CPPUNIT_ASSERT_EQUAL( std::string("Zeile 5"), list2.back() );
359 CPPUNIT_ASSERT_EQUAL( std::string("Zeile 1"), list2.front() );
360
361 list2= split_string(block, "\n", true, "+-");
362
363 // again, we omitted empty lines, but also trimmed away leading and trailing "+" and "-"
364 CPPUNIT_ASSERT_EQUAL( 4u, list2.size() );
365 CPPUNIT_ASSERT_EQUAL( std::string("Zeile 5"), list2.back() );
366 CPPUNIT_ASSERT_EQUAL( std::string("Zeile 1"), list2.front() );
367 CPPUNIT_ASSERT_EQUAL( std::string("Zeile-2"), *(++list2.begin()) );
368 } // eo SplitString1
369
370
371
372 void SplitString2()
373 {
374 std::string line("172.16.0.0/16 dev eth0 scope link src 172.16.1.111");
375
376 StringList list1;
377
378 split_string(line, list1, " ", true, Whitespaces);
379
380 CPPUNIT_ASSERT_EQUAL( 7u, list1.size() );
381
382 } // eo SplitString2
f61aeac1
RP
383
384
385
386 void SplitStringEmpty()
387 {
388 std::string line("");
389
390 StringList list1;
391
392 split_string(line, list1, " ", true, Whitespaces);
393
394 CPPUNIT_ASSERT_EQUAL( 0u, list1.size() );
395 } // eo SplitStringEmpty
396
397
398 void SplitStringDelimiterOnly()
399 {
400 std::string line(" ");
401
402 StringList list1;
403
404 split_string(line, list1, " ", true, Whitespaces);
405
406 CPPUNIT_ASSERT_EQUAL( 0u, list1.size() );
407 } // eo SplitStringDelimiterOnly
57e78ccd
RP
408
409
410
411 void JoinString1()
412 {
413 std::list< std::string > parts;
414 get_push_back_filler(parts)("1")("2")("drei");
415
416 std::string joined_string= join_string(parts,"/");
417 // we should have slashes between the strings:
418 CPPUNIT_ASSERT_EQUAL( std::string("1/2/drei") , joined_string );
419
420 parts.push_back( std::string() );
421 joined_string= join_string(parts,"/");
422 // now we should have an additional trailing slash:
423 CPPUNIT_ASSERT_EQUAL( std::string("1/2/drei/") , joined_string );
424
425 parts.push_front( std::string() );
426 joined_string= join_string(parts,"/");
427 // now we should have an additional leading slash:
428 CPPUNIT_ASSERT_EQUAL( std::string("/1/2/drei/") , joined_string );
429
430 } // eo JoinString1
431
432
433
434 void ConversionStringInt()
435 {
436 std::string s1("24");
437 std::string s1x("25x");
438 int i1=0;
439 bool res= false;
440
441 i1= string_to<int>(s1);
442 CPPUNIT_ASSERT_EQUAL( 24, i1 );
443 i1= string_to<int>(s1x);
444 CPPUNIT_ASSERT_EQUAL( 25, i1 );
445
446 res= string_to<int>(s1,i1);
447 CPPUNIT_ASSERT_EQUAL( true, res );
448 CPPUNIT_ASSERT_EQUAL( 24, i1 );
449
450 res= string_to<int>(s1x,i1);
451 CPPUNIT_ASSERT_EQUAL( false, res );
452
453 std::string ss1= to_string( 24 );
454 CPPUNIT_ASSERT_EQUAL( std::string("24"), ss1);
455
456 } // eo ConversionStringInt()
457
458
459
460 void HexConversion()
461 {
462 std::string hex1("49324E");
463 std::string bin1("I2N");
464
465 CPPUNIT_ASSERT_EQUAL( hex1, convert_binary_to_hex(bin1,true) );
466 CPPUNIT_ASSERT_EQUAL( bin1, convert_hex_to_binary(hex1) );
467 CPPUNIT_ASSERT_EQUAL( to_lower(hex1), convert_binary_to_hex(bin1) );
468
469 std::string hex2("0001");
470 std::string hex2a("00 01");
471 std::string hex2b("00:01");
472 std::string bin2("\0\1",2);
473
474 CPPUNIT_ASSERT_EQUAL( hex2, convert_binary_to_hex(bin2) );
475 CPPUNIT_ASSERT_EQUAL( bin2, convert_hex_to_binary(hex2) );
476 CPPUNIT_ASSERT_EQUAL( bin2, convert_hex_to_binary(hex2a) );
477 CPPUNIT_ASSERT_EQUAL( bin2, convert_hex_to_binary(hex2b) );
478
479 CPPUNIT_ASSERT_THROW( convert_hex_to_binary("01 kein hex"), std::runtime_error);
480 } // eo HexConversion()
481
482
483
118e216e
TJ
484};
485
486CPPUNIT_TEST_SUITE_REGISTRATION(stringfunc);