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