Renamed enumeration items to improve readability
[libi2ncommon] / test / stringfunc.cpp
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 #define BOOST_TEST_DYN_LINK
13 #include <boost/test/unit_test.hpp>
14
15 #include <stringfunc.hxx>
16 #include <containerfunc.hpp>
17
18 using namespace std;
19 using namespace I2n;
20
21 typedef std::list< std::string > StringList;
22
23 BOOST_AUTO_TEST_SUITE(stringfunc)
24
25 BOOST_AUTO_TEST_CASE(smart_html_entites1)
26 {
27     string output = smart_html_entities("Test");
28
29     BOOST_CHECK_EQUAL(string("Test"), output);
30 }
31
32 BOOST_AUTO_TEST_CASE(smart_html_entites2)
33 {
34     string output = smart_html_entities("Täst");
35
36     BOOST_CHECK_EQUAL(string("T&auml;st"), output);
37 }
38
39 BOOST_AUTO_TEST_CASE(smart_html_entites3)
40 {
41     string output = smart_html_entities("<>");
42
43     BOOST_CHECK_EQUAL(string("<>"), output);
44 }
45
46 BOOST_AUTO_TEST_CASE(smart_html_entites4)
47 {
48     string output = smart_html_entities("<ümlaut>");
49
50     BOOST_CHECK_EQUAL(string("<ümlaut>"), output);
51 }
52
53 BOOST_AUTO_TEST_CASE(smart_html_entites5)
54 {
55     string output = smart_html_entities("Test<ümlaut>Blä");
56
57     BOOST_CHECK_EQUAL(string("Test<ümlaut>Bl&auml;"), output);
58 }
59
60 BOOST_AUTO_TEST_CASE(smart_html_entites6)
61 {
62     string output = smart_html_entities("System > Einstellungen");
63
64     BOOST_CHECK_EQUAL(string("System &gt; Einstellungen"), output);
65 }
66
67 BOOST_AUTO_TEST_CASE(smart_html_entites7)
68 {
69     string output = smart_html_entities("Finden Sie <b>auf</b> der Seite <a href=\"fdslfsl\">\"System > Einstellungen\"</a>. Oder etwa nicht?");
70
71     BOOST_CHECK_EQUAL(string("Finden Sie <b>auf</b> der Seite <a href=\"fdslfsl\">&quot;System &gt; Einstellungen&quot;</a>. Oder etwa nicht?"), output);
72 }
73
74 BOOST_AUTO_TEST_CASE(strip_html_tags1)
75 {
76     string output = strip_html_tags("Was für ein schöner Tag, finden Sie nicht?");
77
78     BOOST_CHECK_EQUAL(string("Was für ein schöner Tag, finden Sie nicht?"), output);
79 }
80
81 BOOST_AUTO_TEST_CASE(strip_html_tags2)
82 {
83     string output = strip_html_tags("Was für ein <a href=\"wikipedia\" target=\"new\">schöner Tag</a>, finden Sie nicht?");
84
85     BOOST_CHECK_EQUAL(string("Was für ein schöner Tag, finden Sie nicht?"), output);
86 }
87
88
89
90 BOOST_AUTO_TEST_CASE(html_entities1)
91 {
92     string output = html_entities("\xC3\xA4\xC3\xB6\xC3\xBC");
93     BOOST_CHECK_EQUAL(string("&auml;&ouml;&uuml;"), output);
94 }
95
96 BOOST_AUTO_TEST_CASE(html_entities2)
97 {
98     string output = html_entities("\xC3\xA5 \xC3\xB5 \xC3\xBF");
99     BOOST_CHECK_EQUAL(string("&#229; &#245; &#255;"), output);
100 }
101
102 BOOST_AUTO_TEST_CASE(html_entities3)
103 {
104     string output = html_entities("\xC4\x8E \xE0\xBC\xB1 \xE8\x82\x88");
105     BOOST_CHECK_EQUAL(string("&#270; &#3889; &#32904;"), output);
106 }
107
108
109
110 BOOST_AUTO_TEST_CASE(nice_unit_format1)
111 {
112     const int64_t two_bytes = 2;
113
114     string output = nice_unit_format(two_bytes);
115     BOOST_CHECK_EQUAL(string("2.00 Bytes"), output);
116
117     output = nice_unit_format(two_bytes, UnitBase1024, ShortUnitFormat);
118     BOOST_CHECK_EQUAL(string("2.0 B"), output);
119 }
120
121 BOOST_AUTO_TEST_CASE(nice_unit_format2)
122 {
123     const int64_t two_kilobytes = 2000;
124
125     string output = nice_unit_format(two_kilobytes);
126     BOOST_CHECK_EQUAL(string("2.00 KBytes"), output);
127
128     output = nice_unit_format(two_kilobytes, UnitBase1024, ShortUnitFormat);
129     BOOST_CHECK_EQUAL(string("2.0 KB"), output);
130
131     const int64_t two_and_half_kilobytes = 2500;
132
133     output = nice_unit_format(two_and_half_kilobytes);
134     BOOST_CHECK_EQUAL(string("2.50 KBytes"), output);
135
136     output = nice_unit_format(two_and_half_kilobytes, UnitBase1024, ShortUnitFormat);
137     BOOST_CHECK_EQUAL(string("2.4 KB"), output);
138 }
139
140 BOOST_AUTO_TEST_CASE(nice_unit_format3)
141 {
142     const int64_t two_megabytes = 2000000;
143
144     string output = nice_unit_format(two_megabytes);
145     BOOST_CHECK_EQUAL(string("2.00 MBytes"), output);
146
147     output = nice_unit_format(two_megabytes, UnitBase1024, ShortUnitFormat);
148     BOOST_CHECK_EQUAL(string("1.9 MB"), output);
149
150     const int64_t two_and_half_megabytes = 2500000;
151
152     output = nice_unit_format(two_and_half_megabytes);
153     BOOST_CHECK_EQUAL(string("2.50 MBytes"), output);
154
155     output = nice_unit_format(two_and_half_megabytes, UnitBase1024, ShortUnitFormat);
156     BOOST_CHECK_EQUAL(string("2.4 MB"), output);
157
158 }
159
160 BOOST_AUTO_TEST_CASE(nice_unit_format4)
161 {
162     const int64_t two_gigabytes = 2000000000LL;
163
164     string output = nice_unit_format(two_gigabytes);
165     BOOST_CHECK_EQUAL(string("2.00 GBytes"), output);
166
167     output = nice_unit_format(two_gigabytes, UnitBase1024, ShortUnitFormat);
168     BOOST_CHECK_EQUAL(string("1.9 GB"), output);
169
170     const int64_t two_and_half_gigabytes = 2500000000LL;
171
172     output = nice_unit_format(two_and_half_gigabytes);
173     BOOST_CHECK_EQUAL(string("2.50 GBytes"), output);
174
175     output = nice_unit_format(two_and_half_gigabytes, UnitBase1024, ShortUnitFormat);
176     BOOST_CHECK_EQUAL(string("2.3 GB"), output);
177 }
178
179 BOOST_AUTO_TEST_CASE(nice_unit_format5)
180 {
181     const int64_t two_terabytes = 2000000000000LL;
182
183     string output = nice_unit_format(two_terabytes);
184     BOOST_CHECK_EQUAL(string("2.00 TBytes"), output);
185
186     output = nice_unit_format(two_terabytes, UnitBase1024, ShortUnitFormat);
187     BOOST_CHECK_EQUAL(string("1.8 TB"), output);
188
189     const int64_t two_and_half_terabytes = 2500000000000LL;
190
191     output = nice_unit_format(two_and_half_terabytes);
192     BOOST_CHECK_EQUAL(string("2.50 TBytes"), output);
193
194     output = nice_unit_format(two_and_half_terabytes, UnitBase1024, ShortUnitFormat);
195     BOOST_CHECK_EQUAL(string("2.3 TB"), output);
196 }
197
198 BOOST_AUTO_TEST_CASE(nice_unit_format6)
199 {
200     const int64_t two_petabytes = 2000000000000000LL;
201
202     string output = nice_unit_format(two_petabytes);
203     BOOST_CHECK_EQUAL(string("2.00 PBytes"), output);
204
205     output = nice_unit_format(two_petabytes, UnitBase1024, ShortUnitFormat);
206     BOOST_CHECK_EQUAL(string("1.8 PB"), output);
207
208     const int64_t two_and_half_petabytes = 2500000000000000LL;
209
210     output = nice_unit_format(two_and_half_petabytes);
211     BOOST_CHECK_EQUAL(string("2.50 PBytes"), output);
212
213     output = nice_unit_format(two_and_half_petabytes, UnitBase1024, ShortUnitFormat);
214     BOOST_CHECK_EQUAL(string("2.2 PB"), output);
215 }
216
217 BOOST_AUTO_TEST_CASE(nice_unit_format7)
218 {
219     const int64_t two_exabytes = 2000000000000000000LL;
220
221     string output = nice_unit_format(two_exabytes);
222     BOOST_CHECK_EQUAL(string("2.00 EBytes"), output);
223
224     output = nice_unit_format(two_exabytes, UnitBase1024, ShortUnitFormat);
225     BOOST_CHECK_EQUAL(string("1.7 EB"), output);
226
227     const int64_t two_and_half_exabytes = 2500000000000000000LL;
228
229     output = nice_unit_format(two_and_half_exabytes);
230     BOOST_CHECK_EQUAL(string("2.50 EBytes"), output);
231
232     output = nice_unit_format(two_and_half_exabytes, UnitBase1024, ShortUnitFormat);
233     BOOST_CHECK_EQUAL(string("2.2 EB"), output);
234 }
235
236
237
238 BOOST_AUTO_TEST_CASE(imaputf7_to_utf8)
239 {
240     string output = utf7imap_to_utf8("Sp&AOQ-m");
241     BOOST_CHECK_EQUAL(string("Späm"), output);
242 }
243
244 BOOST_AUTO_TEST_CASE(utf8_to_imaputf7)
245 {
246     string output = utf8_to_utf7imap("Späm");
247     BOOST_CHECK_EQUAL(string("Sp&AOQ-m"), output);
248 }
249
250 /*
251 **
252 */
253
254 BOOST_AUTO_TEST_CASE(TestTrim)
255 {
256     std::string s("s1");
257     trim_mod(s);
258     BOOST_CHECK_EQUAL( std::string("s1"), s );
259
260     s="  s2";
261     trim_mod(s);
262     BOOST_CHECK_EQUAL( std::string("s2"), s );
263
264     s="s3   ";
265     trim_mod(s);
266     BOOST_CHECK_EQUAL( std::string("s3"), s );
267
268     s="::s4:s4::++--aa";
269     trim_mod(s,":+-a");
270     BOOST_CHECK_EQUAL( std::string("s4:s4"), s);
271
272     /* non modifying version */
273
274     s= "s1";
275     BOOST_CHECK_EQUAL( std::string("s1"), trim(s) );
276
277     s="  s2";
278     BOOST_CHECK_EQUAL( std::string("s2"), trim(s) );
279     BOOST_CHECK_EQUAL( std::string("  s2"), s );
280
281     s="s3   ";
282     BOOST_CHECK_EQUAL( std::string("s3"), trim(s) );
283     BOOST_CHECK_EQUAL( std::string("s3   "), s );
284
285     s="::s4:s4::++--aa";
286     BOOST_CHECK_EQUAL( std::string("s4:s4"), trim(s,":+-a") );
287 } // eo TestTrim()
288
289
290
291 BOOST_AUTO_TEST_CASE(TestChomp)
292 {
293     std::string s("s1");
294
295     chomp_mod(s);
296     BOOST_CHECK_EQUAL( std::string("s1"), s );
297
298     s="s2\n";
299     chomp_mod(s);
300     BOOST_CHECK_EQUAL( std::string("s2"), s );
301
302     s="s3:";
303     chomp_mod(s,":");
304     BOOST_CHECK_EQUAL( std::string("s3"), s );
305
306     s=":s4::";
307     chomp_mod(s,"s:");
308     BOOST_CHECK_EQUAL( std::string(":s4:"), s);
309
310     /* non modifiying versions */
311     s= "s1";
312
313     BOOST_CHECK_EQUAL( std::string("s1"), chomp(s) );
314
315     s="s2\n";
316     BOOST_CHECK_EQUAL( std::string("s2"), chomp(s) );
317     BOOST_CHECK_EQUAL( std::string("s2\n"), s);
318
319     s="s3:";
320     BOOST_CHECK_EQUAL( std::string("s3"), chomp(s,":") );
321     BOOST_CHECK_EQUAL( std::string("s3:"), s);
322
323     s=":s4::";
324     BOOST_CHECK_EQUAL( std::string(":s4:"), chomp(s,"s:") );
325     BOOST_CHECK_EQUAL( std::string(":s4::"), s);
326 } // eo TestChomp()
327
328
329
330 BOOST_AUTO_TEST_CASE(TestSuffixFuncs)
331 {
332     std::string s1("12.cpp");
333
334     BOOST_CHECK_EQUAL( true, has_suffix(s1,".cpp") );
335     BOOST_CHECK_EQUAL( true, has_suffix(s1,"pp") );
336     BOOST_CHECK_EQUAL( false, has_suffix(s1,"hpp") );
337     BOOST_CHECK_EQUAL( false, has_suffix(s1,"cp") );
338     BOOST_CHECK_EQUAL( false, has_suffix(s1,"") );
339
340     std::string s1c1= remove_suffix(s1,".cpp");
341     BOOST_CHECK_EQUAL( std::string("12"), s1c1 );
342
343     std::string s1c2= remove_suffix(s1,"p");
344     BOOST_CHECK_EQUAL( std::string("12.cp"), s1c2 );
345
346     std::string s1c3= remove_suffix(s1,"cp");
347     BOOST_CHECK_EQUAL( std::string("12.cpp"), s1c3 );
348
349     std::string s2(".cpp");
350     BOOST_CHECK_EQUAL( true, has_suffix(s2,".cpp") );
351
352     std::string s2c1= remove_suffix(s2,".cpp");
353     BOOST_CHECK_EQUAL( std::string(""), s2c1 );
354
355 } // eo TestSuffixFuncs()
356
357
358
359 BOOST_AUTO_TEST_CASE(TestPrefixFuncs)
360 {
361     std::string s1("12.cpp");
362
363     BOOST_CHECK_EQUAL( true, has_prefix(s1,"12") );
364     BOOST_CHECK_EQUAL( true, has_prefix(s1, "1") );
365     BOOST_CHECK_EQUAL( false, has_prefix(s1, "2") );
366     BOOST_CHECK_EQUAL( false, has_prefix(s1, "") );
367
368     std::string s1c1= remove_prefix(s1, "12");
369     BOOST_CHECK_EQUAL( std::string(".cpp"), s1c1);
370 } // eo TestPrefixFuncs()
371
372
373
374 BOOST_AUTO_TEST_CASE(TestLowerUpperFuncs)
375 {
376     std::string u1("CASE CONVERSION TEST..");
377     std::string l1("case conversion test..");
378
379     std::string test1(l1);
380
381     to_upper_mod(test1);
382     BOOST_CHECK_EQUAL( u1, test1 );
383
384     to_lower_mod(test1);
385     BOOST_CHECK_EQUAL( l1, test1 );
386
387
388     BOOST_CHECK_EQUAL( u1, to_upper(l1) );
389     BOOST_CHECK_EQUAL( l1, to_lower(u1) );
390 } // eo TestLowerUpper
391
392
393
394 BOOST_AUTO_TEST_CASE(PairSplit1)
395 {
396     StringList str_list;
397     get_push_back_filler(str_list)
398         ("a=11")("a= 11")("a =11 ")("a =  11 ")("  a    =    11   ")
399     ;
400     BOOST_CHECK_EQUAL( 5u, str_list.size() );
401     for(StringList::iterator it= str_list.begin();
402         it != str_list.end();
403         ++it)
404     {
405         std::string key, value;
406         bool res= pair_split( *it, key, value);
407
408         BOOST_CHECK_EQUAL( true , res );
409         BOOST_CHECK_EQUAL( std::string("a"), key );
410         BOOST_CHECK_EQUAL( std::string("11"), value );
411     }
412
413     std::string key, value;
414     bool res;
415
416     res= pair_split(" 1 : 2 ", key, value, ':');
417     BOOST_CHECK_EQUAL( true, res );
418     BOOST_CHECK_EQUAL( std::string("1"), key);
419     BOOST_CHECK_EQUAL( std::string("2"), value);
420 } // eo PairSplit1
421
422
423
424 BOOST_AUTO_TEST_CASE(SplitString1)
425 {
426     std::string block(
427         "Zeile 1\n"
428         "++Zeile-2--\n"
429         "Zeile 3\n"
430         "\n"
431         "Zeile 5\n"
432     );
433
434     StringList list1;
435
436     split_string(block, list1, "\n");
437     // since the blocks ends with \n we should have 6 lines (the last one empty):
438     BOOST_CHECK_EQUAL( 6u, list1.size() );
439     BOOST_CHECK_EQUAL( std::string(), list1.back() );
440     BOOST_CHECK_EQUAL( std::string("Zeile 1"), list1.front() );
441
442     StringList list2;
443
444     split_string(block, list2, "\n", true);
445
446     // now we omitted empty lines, now we should have only 4 lines left:
447     BOOST_CHECK_EQUAL( 4u, list2.size() );
448     BOOST_CHECK_EQUAL( std::string("Zeile 5"), list2.back() );
449     BOOST_CHECK_EQUAL( std::string("Zeile 1"), list2.front() );
450
451     list2= split_string(block, "\n", true, "+-");
452
453     // again, we omitted empty lines, but also trimmed away leading and trailing "+" and "-"
454     BOOST_CHECK_EQUAL( 4u, list2.size() );
455     BOOST_CHECK_EQUAL( std::string("Zeile 5"), list2.back() );
456     BOOST_CHECK_EQUAL( std::string("Zeile 1"), list2.front() );
457     BOOST_CHECK_EQUAL( std::string("Zeile-2"), *(++list2.begin()) );
458 } // eo SplitString1
459
460
461
462 BOOST_AUTO_TEST_CASE(SplitString2)
463 {
464     std::string line("172.16.0.0/16 dev eth0  scope link  src 172.16.1.111");
465
466     StringList list1;
467
468     split_string(line, list1, " ", true, Whitespaces);
469
470     BOOST_CHECK_EQUAL( 7u, list1.size() );
471
472 } // eo SplitString2
473
474
475
476 BOOST_AUTO_TEST_CASE(SplitStringEmpty)
477 {
478     std::string line("");
479
480     StringList list1;
481
482     split_string(line, list1, " ", true, Whitespaces);
483
484     BOOST_CHECK_EQUAL( 0u, list1.size() );
485 } // eo SplitStringEmpty
486
487
488 BOOST_AUTO_TEST_CASE(SplitStringDelimiterOnly)
489 {
490     std::string line(" ");
491
492     StringList list1;
493
494     split_string(line, list1, " ", true, Whitespaces);
495
496     BOOST_CHECK_EQUAL( 0u, list1.size() );
497 } // eo SplitStringDelimiterOnly
498
499
500
501 BOOST_AUTO_TEST_CASE(JoinString1)
502 {
503     std::list< std::string > parts;
504     get_push_back_filler(parts)("1")("2")("drei");
505
506     std::string joined_string= join_string(parts,"/");
507     // we should have slashes between the strings:
508     BOOST_CHECK_EQUAL( std::string("1/2/drei") , joined_string );
509
510     parts.push_back( std::string() );
511     joined_string= join_string(parts,"/");
512     // now we should have an additional trailing slash:
513     BOOST_CHECK_EQUAL( std::string("1/2/drei/") , joined_string );
514
515     parts.push_front( std::string() );
516     joined_string= join_string(parts,"/");
517     // now we should have an additional leading slash:
518     BOOST_CHECK_EQUAL( std::string("/1/2/drei/") , joined_string );
519
520 } // eo JoinString1
521
522
523
524 BOOST_AUTO_TEST_CASE(ConversionStringInt)
525 {
526     std::string s1("24");
527     std::string s1x("25x");
528     int i1=0;
529     bool res= false;
530
531     i1= string_to<int>(s1);
532     BOOST_CHECK_EQUAL( 24, i1 );
533     i1= string_to<int>(s1x);
534     BOOST_CHECK_EQUAL( 25, i1 );
535
536     res= string_to<int>(s1,i1);
537     BOOST_CHECK_EQUAL( true, res );
538     BOOST_CHECK_EQUAL( 24, i1 );
539
540     res= string_to<int>(s1x,i1);
541     BOOST_CHECK_EQUAL( false, res );
542
543     std::string ss1= to_string( 24 );
544     BOOST_CHECK_EQUAL( std::string("24"), ss1);
545
546 } // eo ConversionStringInt()
547
548
549
550 BOOST_AUTO_TEST_CASE(HexConversion)
551 {
552     std::string hex1("49324E");
553     std::string bin1("I2N");
554
555     BOOST_CHECK_EQUAL( hex1, convert_binary_to_hex(bin1,true) );
556     BOOST_CHECK_EQUAL( bin1, convert_hex_to_binary(hex1) );
557     BOOST_CHECK_EQUAL( to_lower(hex1), convert_binary_to_hex(bin1) );
558
559     std::string hex2("0001");
560     std::string hex2a("00 01");
561     std::string hex2b("00:01");
562     std::string bin2("\0\1",2);
563
564     BOOST_CHECK_EQUAL( hex2, convert_binary_to_hex(bin2) );
565     BOOST_CHECK_EQUAL( bin2, convert_hex_to_binary(hex2) );
566     BOOST_CHECK_EQUAL( bin2, convert_hex_to_binary(hex2a) );
567     BOOST_CHECK_EQUAL( bin2, convert_hex_to_binary(hex2b) );
568
569     BOOST_REQUIRE_THROW( convert_hex_to_binary("01 kein hex"), std::runtime_error);
570 } // eo HexConversion()
571
572 BOOST_AUTO_TEST_SUITE_END()