ee183556cbf5470129aba5e7fda771a74773121b
[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-2011 by Intra2net AG                               *
22  *                                                                         *
23  ***************************************************************************/
24
25 // #include <iostream>
26 #include <string>
27 #include <cmath>
28 // #include <sstream>
29 // #include <stdexcept>
30
31 #define BOOST_TEST_DYN_LINK
32 #include <boost/test/unit_test.hpp>
33 #include <boost/numeric/conversion/cast.hpp>
34
35 #include <stringfunc.hxx>
36 #include <containerfunc.hpp>
37 #include <iostream>
38 #include <stdint.h>
39
40 using namespace std;
41 using namespace I2n;
42
43 typedef std::list< std::string > StringList;
44
45 BOOST_AUTO_TEST_SUITE(stringfunc)
46
47 BOOST_AUTO_TEST_CASE(smart_html_entites1)
48 {
49     string output = smart_html_entities("Test");
50
51     BOOST_CHECK_EQUAL(string("Test"), output);
52 }
53
54 BOOST_AUTO_TEST_CASE(smart_html_entites2)
55 {
56     string output = smart_html_entities("Täst");
57
58     BOOST_CHECK_EQUAL(string("T&auml;st"), output);
59 }
60
61 BOOST_AUTO_TEST_CASE(smart_html_entites3)
62 {
63     string output = smart_html_entities("<>");
64
65     BOOST_CHECK_EQUAL(string("<>"), output);
66 }
67
68 BOOST_AUTO_TEST_CASE(smart_html_entites4)
69 {
70     string output = smart_html_entities("<ümlaut>");
71
72     BOOST_CHECK_EQUAL(string("<ümlaut>"), output);
73 }
74
75 BOOST_AUTO_TEST_CASE(smart_html_entites5)
76 {
77     string output = smart_html_entities("Test<ümlaut>Blä");
78
79     BOOST_CHECK_EQUAL(string("Test<ümlaut>Bl&auml;"), output);
80 }
81
82 BOOST_AUTO_TEST_CASE(smart_html_entites6)
83 {
84     string output = smart_html_entities("System > Einstellungen");
85
86     BOOST_CHECK_EQUAL(string("System &gt; Einstellungen"), output);
87 }
88
89 BOOST_AUTO_TEST_CASE(smart_html_entites7)
90 {
91     string output = smart_html_entities("Finden Sie <b>auf</b> der Seite <a href=\"fdslfsl\">\"System > Einstellungen\"</a>. Oder etwa nicht?");
92
93     BOOST_CHECK_EQUAL(string("Finden Sie <b>auf</b> der Seite <a href=\"fdslfsl\">&quot;System &gt; Einstellungen&quot;</a>. Oder etwa nicht?"), output);
94 }
95
96 BOOST_AUTO_TEST_CASE(strip_html_tags1)
97 {
98     string output = strip_html_tags("Was für ein schöner Tag, finden Sie nicht?");
99
100     BOOST_CHECK_EQUAL(string("Was für ein schöner Tag, finden Sie nicht?"), output);
101 }
102
103 BOOST_AUTO_TEST_CASE(strip_html_tags2)
104 {
105     string output = strip_html_tags("Was für ein <a href=\"wikipedia\" target=\"new\">schöner Tag</a>, finden Sie nicht?");
106
107     BOOST_CHECK_EQUAL(string("Was für ein schöner Tag, finden Sie nicht?"), output);
108 }
109
110
111
112 BOOST_AUTO_TEST_CASE(html_entities1)
113 {
114     string output = html_entities("\xC3\xA4\xC3\xB6\xC3\xBC");
115     BOOST_CHECK_EQUAL(string("&auml;&ouml;&uuml;"), output);
116 }
117
118 BOOST_AUTO_TEST_CASE(html_entities2)
119 {
120     string output = html_entities("\xC3\xA5 \xC3\xB5 \xC3\xBF");
121     BOOST_CHECK_EQUAL(string("&#229; &#245; &#255;"), output);
122 }
123
124 BOOST_AUTO_TEST_CASE(html_entities3)
125 {
126     string output = html_entities("\xC4\x8E \xE0\xBC\xB1 \xE8\x82\x88");
127     BOOST_CHECK_EQUAL(string("&#270; &#3889; &#32904;"), output);
128 }
129
130
131
132 BOOST_AUTO_TEST_CASE(nice_unit_format1)
133 {
134     const int64_t two_bytes = 2;
135
136     string output = nice_unit_format(two_bytes, LongUnitFormat, UnitBase1000);
137     BOOST_CHECK_EQUAL(string("2.00 Bytes"), output);
138
139     output = nice_unit_format(two_bytes);
140     BOOST_CHECK_EQUAL(string("2.0 B"), output);
141 }
142
143 BOOST_AUTO_TEST_CASE(nice_unit_format2)
144 {
145     const int64_t two_kilobytes = 2000;
146
147     string output = nice_unit_format(two_kilobytes, LongUnitFormat, UnitBase1000);
148     BOOST_CHECK_EQUAL(string("2.00 KBytes"), output);
149
150     output = nice_unit_format(two_kilobytes);
151     BOOST_CHECK_EQUAL(string("2.0 KB"), output);
152
153     output = nice_unit_format(boost::numeric_cast<double>(two_kilobytes), LongUnitFormat, UnitBase1000);
154     BOOST_CHECK_EQUAL(string("2.00 KBytes"), output);
155
156     output = nice_unit_format(boost::numeric_cast<double>(two_kilobytes));
157     BOOST_CHECK_EQUAL(string("2.0 KB"), output);
158
159     const int64_t two_and_half_kilobytes = 2500;
160
161     output = nice_unit_format(two_and_half_kilobytes, LongUnitFormat, UnitBase1000);
162     BOOST_CHECK_EQUAL(string("2.50 KBytes"), output);
163
164     output = nice_unit_format(two_and_half_kilobytes);
165     BOOST_CHECK_EQUAL(string("2.4 KB"), output);
166
167     output = nice_unit_format(boost::numeric_cast<double>(two_and_half_kilobytes), LongUnitFormat, UnitBase1000);
168     BOOST_CHECK_EQUAL(string("2.50 KBytes"), output);
169
170     output = nice_unit_format(boost::numeric_cast<double>(two_and_half_kilobytes));
171     BOOST_CHECK_EQUAL(string("2.4 KB"), output);
172 }
173
174 BOOST_AUTO_TEST_CASE(nice_unit_format3)
175 {
176     const int64_t two_megabytes = 2000000;
177
178     string output = nice_unit_format(two_megabytes, LongUnitFormat, UnitBase1000);
179     BOOST_CHECK_EQUAL(string("2.00 MBytes"), output);
180
181     output = nice_unit_format(two_megabytes);
182     BOOST_CHECK_EQUAL(string("1.9 MB"), output);
183
184     output = nice_unit_format(boost::numeric_cast<double>(two_megabytes), LongUnitFormat, UnitBase1000);
185     BOOST_CHECK_EQUAL(string("2.00 MBytes"), output);
186
187     output = nice_unit_format(boost::numeric_cast<double>(two_megabytes));
188     BOOST_CHECK_EQUAL(string("1.9 MB"), output);
189
190     const int64_t two_and_half_megabytes = 2500000;
191
192     output = nice_unit_format(two_and_half_megabytes, LongUnitFormat, UnitBase1000);
193     BOOST_CHECK_EQUAL(string("2.50 MBytes"), output);
194
195     output = nice_unit_format(two_and_half_megabytes);
196     BOOST_CHECK_EQUAL(string("2.4 MB"), output);
197
198     output = nice_unit_format(boost::numeric_cast<double>(two_and_half_megabytes), LongUnitFormat, UnitBase1000);
199     BOOST_CHECK_EQUAL(string("2.50 MBytes"), output);
200
201     output = nice_unit_format(boost::numeric_cast<double>(two_and_half_megabytes));
202     BOOST_CHECK_EQUAL(string("2.4 MB"), output);
203
204 }
205
206 BOOST_AUTO_TEST_CASE(nice_unit_format4)
207 {
208     const int64_t two_gigabytes = 2000000000LL;
209
210     string output = nice_unit_format(two_gigabytes, LongUnitFormat, UnitBase1000);
211     BOOST_CHECK_EQUAL(string("2.00 GBytes"), output);
212
213     output = nice_unit_format(two_gigabytes);
214     BOOST_CHECK_EQUAL(string("1.9 GB"), output);
215
216     output = nice_unit_format(boost::numeric_cast<double>(two_gigabytes), LongUnitFormat, UnitBase1000);
217     BOOST_CHECK_EQUAL(string("2.00 GBytes"), output);
218
219     output = nice_unit_format(boost::numeric_cast<double>(two_gigabytes));
220     BOOST_CHECK_EQUAL(string("1.9 GB"), output);
221
222     const int64_t two_and_half_gigabytes = 2500000000LL;
223
224     output = nice_unit_format(two_and_half_gigabytes, LongUnitFormat, UnitBase1000);
225     BOOST_CHECK_EQUAL(string("2.50 GBytes"), output);
226
227     output = nice_unit_format(two_and_half_gigabytes);
228     BOOST_CHECK_EQUAL(string("2.3 GB"), output);
229
230     output = nice_unit_format(boost::numeric_cast<double>(two_and_half_gigabytes), LongUnitFormat, UnitBase1000);
231     BOOST_CHECK_EQUAL(string("2.50 GBytes"), output);
232
233     output = nice_unit_format(boost::numeric_cast<double>(two_and_half_gigabytes));
234     BOOST_CHECK_EQUAL(string("2.3 GB"), output);
235 }
236
237 BOOST_AUTO_TEST_CASE(nice_unit_format5)
238 {
239     const int64_t two_terabytes = 2000000000000LL;
240
241     string output = nice_unit_format(two_terabytes, LongUnitFormat, UnitBase1000);
242     BOOST_CHECK_EQUAL(string("2.00 TBytes"), output);
243
244     output = nice_unit_format(two_terabytes);
245     BOOST_CHECK_EQUAL(string("1.8 TB"), output);
246
247     output = nice_unit_format(boost::numeric_cast<double>(two_terabytes), LongUnitFormat, UnitBase1000);
248     BOOST_CHECK_EQUAL(string("2.00 TBytes"), output);
249
250     output = nice_unit_format(boost::numeric_cast<double>(two_terabytes));
251     BOOST_CHECK_EQUAL(string("1.8 TB"), output);
252
253     const int64_t two_and_half_terabytes = 2500000000000LL;
254
255     output = nice_unit_format(two_and_half_terabytes, LongUnitFormat, UnitBase1000);
256     BOOST_CHECK_EQUAL(string("2.50 TBytes"), output);
257
258     output = nice_unit_format(two_and_half_terabytes);
259     BOOST_CHECK_EQUAL(string("2.3 TB"), output);
260
261     output = nice_unit_format(boost::numeric_cast<double>(two_and_half_terabytes), LongUnitFormat, UnitBase1000);
262     BOOST_CHECK_EQUAL(string("2.50 TBytes"), output);
263
264     output = nice_unit_format(boost::numeric_cast<double>(two_and_half_terabytes));
265     BOOST_CHECK_EQUAL(string("2.3 TB"), output);
266 }
267
268 BOOST_AUTO_TEST_CASE(nice_unit_format6)
269 {
270     const int64_t two_petabytes = 2000000000000000LL;
271
272     string output = nice_unit_format(two_petabytes, LongUnitFormat, UnitBase1000);
273     BOOST_CHECK_EQUAL(string("2.00 PBytes"), output);
274
275     output = nice_unit_format(two_petabytes);
276     BOOST_CHECK_EQUAL(string("1.8 PB"), output);
277
278     output = nice_unit_format(boost::numeric_cast<double>(two_petabytes), LongUnitFormat, UnitBase1000);
279     BOOST_CHECK_EQUAL(string("2.00 PBytes"), output);
280
281     output = nice_unit_format(boost::numeric_cast<double>(two_petabytes));
282     BOOST_CHECK_EQUAL(string("1.8 PB"), output);
283
284     const int64_t two_and_half_petabytes = 2500000000000000LL;
285
286     output = nice_unit_format(two_and_half_petabytes, LongUnitFormat, UnitBase1000);
287     BOOST_CHECK_EQUAL(string("2.50 PBytes"), output);
288
289     output = nice_unit_format(two_and_half_petabytes);
290     BOOST_CHECK_EQUAL(string("2.2 PB"), output);
291
292     output = nice_unit_format(boost::numeric_cast<double>(two_and_half_petabytes), LongUnitFormat, UnitBase1000);
293     BOOST_CHECK_EQUAL(string("2.50 PBytes"), output);
294
295     output = nice_unit_format(boost::numeric_cast<double>(two_and_half_petabytes));
296     BOOST_CHECK_EQUAL(string("2.2 PB"), output);
297 }
298
299 BOOST_AUTO_TEST_CASE(nice_unit_format7)
300 {
301     const int64_t two_exabytes = 2000000000000000000LL;
302
303     string output = nice_unit_format(two_exabytes, LongUnitFormat, UnitBase1000);
304     BOOST_CHECK_EQUAL(string("2000.00 PBytes"), output);
305
306     output = nice_unit_format(two_exabytes);
307     BOOST_CHECK_EQUAL(string("1776.4 PB"), output);
308
309     output = nice_unit_format(boost::numeric_cast<double>(two_exabytes), LongUnitFormat, UnitBase1000);
310     BOOST_CHECK_EQUAL(string("2000.00 PBytes"), output);
311
312     output = nice_unit_format(boost::numeric_cast<double>(two_exabytes));
313     BOOST_CHECK_EQUAL(string("1776.4 PB"), output);
314
315     const int64_t two_and_half_exabytes = 2500000000000000000LL;
316
317     output = nice_unit_format(two_and_half_exabytes, LongUnitFormat, UnitBase1000);
318     BOOST_CHECK_EQUAL(string("2500.00 PBytes"), output);
319
320     output = nice_unit_format(two_and_half_exabytes);
321     BOOST_CHECK_EQUAL(string("2220.4 PB"), output);
322
323     output = nice_unit_format(boost::numeric_cast<double>(two_and_half_exabytes), LongUnitFormat, UnitBase1000);
324     BOOST_CHECK_EQUAL(string("2500.00 PBytes"), output);
325
326     output = nice_unit_format(boost::numeric_cast<double>(two_and_half_exabytes));
327     BOOST_CHECK_EQUAL(string("2220.4 PB"), output);
328 }
329
330 BOOST_AUTO_TEST_CASE(nice_unit_format8)
331 {
332     const int64_t max_representable_64bits_number = 9223372036854775807LL;
333
334     string output = nice_unit_format(max_representable_64bits_number, LongUnitFormat, UnitBase1000);
335     BOOST_CHECK_EQUAL(string("9223.40 PBytes"), output);
336
337     output = nice_unit_format(max_representable_64bits_number);
338     BOOST_CHECK_EQUAL(string("8192.0 PB"), output);
339
340     /*
341     double val = boost::numeric_cast<double>(max_representable_64bits_number);
342     BOOST_MESSAGE("val as int64 is " << max_representable_64bits_number << " and as double is " << val);
343     BOOST_MESSAGE("val rounded is " << round(val));
344     BOOST_CHECK_EQUAL(val, round(val));
345     BOOST_MESSAGE("half as int64 is " << boost::numeric_cast<int64_t>(round(val/2)));   // ok
346     BOOST_MESSAGE("cast back is " << boost::numeric_cast<int64_t>(round(val)));  // throws a positive overflow
347     output = nice_unit_format(boost::numeric_cast<double>(max_representable_64bits_number), LongUnitFormat, UnitBase1000);
348     BOOST_CHECK_EQUAL(string("9223.40 PBytes"), output);
349
350     output = nice_unit_format(boost::numeric_cast<double>(max_representable_64bits_number));
351     BOOST_CHECK_EQUAL(string("8192.0 PB"), output);
352     */
353 }
354
355 BOOST_AUTO_TEST_CASE(TestTrim)
356 {
357     std::string s("s1");
358     trim_mod(s);
359     BOOST_CHECK_EQUAL( std::string("s1"), s );
360
361     s="  s2";
362     trim_mod(s);
363     BOOST_CHECK_EQUAL( std::string("s2"), s );
364
365     s="s3   ";
366     trim_mod(s);
367     BOOST_CHECK_EQUAL( std::string("s3"), s );
368
369     s="::s4:s4::++--aa";
370     trim_mod(s,":+-a");
371     BOOST_CHECK_EQUAL( std::string("s4:s4"), s);
372
373     /* non modifying version */
374
375     s= "s1";
376     BOOST_CHECK_EQUAL( std::string("s1"), trim(s) );
377
378     s="  s2";
379     BOOST_CHECK_EQUAL( std::string("s2"), trim(s) );
380     BOOST_CHECK_EQUAL( std::string("  s2"), s );
381
382     s="s3   ";
383     BOOST_CHECK_EQUAL( std::string("s3"), trim(s) );
384     BOOST_CHECK_EQUAL( std::string("s3   "), s );
385
386     s="::s4:s4::++--aa";
387     BOOST_CHECK_EQUAL( std::string("s4:s4"), trim(s,":+-a") );
388 } // eo TestTrim()
389
390
391
392 BOOST_AUTO_TEST_CASE(TestChomp)
393 {
394     std::string s("s1");
395
396     chomp_mod(s);
397     BOOST_CHECK_EQUAL( std::string("s1"), s );
398
399     s="s2\n";
400     chomp_mod(s);
401     BOOST_CHECK_EQUAL( std::string("s2"), s );
402
403     s="s3:";
404     chomp_mod(s,":");
405     BOOST_CHECK_EQUAL( std::string("s3"), s );
406
407     s=":s4::";
408     chomp_mod(s,"s:");
409     BOOST_CHECK_EQUAL( std::string(":s4:"), s);
410
411     /* non modifiying versions */
412     s= "s1";
413
414     BOOST_CHECK_EQUAL( std::string("s1"), chomp(s) );
415
416     s="s2\n";
417     BOOST_CHECK_EQUAL( std::string("s2"), chomp(s) );
418     BOOST_CHECK_EQUAL( std::string("s2\n"), s);
419
420     s="s3:";
421     BOOST_CHECK_EQUAL( std::string("s3"), chomp(s,":") );
422     BOOST_CHECK_EQUAL( std::string("s3:"), s);
423
424     s=":s4::";
425     BOOST_CHECK_EQUAL( std::string(":s4:"), chomp(s,"s:") );
426     BOOST_CHECK_EQUAL( std::string(":s4::"), s);
427 } // eo TestChomp()
428
429
430
431 BOOST_AUTO_TEST_CASE(TestSuffixFuncs)
432 {
433     std::string s1("12.cpp");
434
435     BOOST_CHECK_EQUAL( true, has_suffix(s1,".cpp") );
436     BOOST_CHECK_EQUAL( true, has_suffix(s1,"pp") );
437     BOOST_CHECK_EQUAL( false, has_suffix(s1,"hpp") );
438     BOOST_CHECK_EQUAL( false, has_suffix(s1,"cp") );
439     BOOST_CHECK_EQUAL( false, has_suffix(s1,"") );
440
441     std::string s1c1= remove_suffix(s1,".cpp");
442     BOOST_CHECK_EQUAL( std::string("12"), s1c1 );
443
444     std::string s1c2= remove_suffix(s1,"p");
445     BOOST_CHECK_EQUAL( std::string("12.cp"), s1c2 );
446
447     std::string s1c3= remove_suffix(s1,"cp");
448     BOOST_CHECK_EQUAL( std::string("12.cpp"), s1c3 );
449
450     std::string s2(".cpp");
451     BOOST_CHECK_EQUAL( true, has_suffix(s2,".cpp") );
452
453     std::string s2c1= remove_suffix(s2,".cpp");
454     BOOST_CHECK_EQUAL( std::string(""), s2c1 );
455
456 } // eo TestSuffixFuncs()
457
458
459
460 BOOST_AUTO_TEST_CASE(TestPrefixFuncs)
461 {
462     std::string s1("12.cpp");
463
464     BOOST_CHECK_EQUAL( true, has_prefix(s1,"12") );
465     BOOST_CHECK_EQUAL( true, has_prefix(s1, "1") );
466     BOOST_CHECK_EQUAL( false, has_prefix(s1, "2") );
467     BOOST_CHECK_EQUAL( false, has_prefix(s1, "") );
468
469     std::string s1c1= remove_prefix(s1, "12");
470     BOOST_CHECK_EQUAL( std::string(".cpp"), s1c1);
471 } // eo TestPrefixFuncs()
472
473
474
475 BOOST_AUTO_TEST_CASE(TestLowerUpperFuncs)
476 {
477     std::string u1("CASE CONVERSION TEST..");
478     std::string l1("case conversion test..");
479
480     std::string test1(l1);
481
482     to_upper_mod(test1);
483     BOOST_CHECK_EQUAL( u1, test1 );
484
485     to_lower_mod(test1);
486     BOOST_CHECK_EQUAL( l1, test1 );
487
488
489     BOOST_CHECK_EQUAL( u1, to_upper(l1) );
490     BOOST_CHECK_EQUAL( l1, to_lower(u1) );
491 } // eo TestLowerUpper
492
493
494
495 BOOST_AUTO_TEST_CASE(PairSplit1)
496 {
497     StringList str_list;
498     get_push_back_filler(str_list)
499         ("a=11")("a= 11")("a =11 ")("a =  11 ")("  a    =    11   ")
500     ;
501     BOOST_CHECK_EQUAL( 5u, str_list.size() );
502     for(StringList::iterator it= str_list.begin();
503         it != str_list.end();
504         ++it)
505     {
506         std::string key, value;
507         bool res= pair_split( *it, key, value);
508
509         BOOST_CHECK_EQUAL( true , res );
510         BOOST_CHECK_EQUAL( std::string("a"), key );
511         BOOST_CHECK_EQUAL( std::string("11"), value );
512     }
513
514     std::string key, value;
515     bool res;
516
517     res= pair_split(" 1 : 2 ", key, value, ':');
518     BOOST_CHECK_EQUAL( true, res );
519     BOOST_CHECK_EQUAL( std::string("1"), key);
520     BOOST_CHECK_EQUAL( std::string("2"), value);
521 } // eo PairSplit1
522
523
524
525 BOOST_AUTO_TEST_CASE(SplitString1)
526 {
527     std::string block(
528         "Zeile 1\n"
529         "++Zeile-2--\n"
530         "Zeile 3\n"
531         "\n"
532         "Zeile 5\n"
533     );
534
535     StringList list1;
536
537     split_string(block, list1, "\n");
538     // since the blocks ends with \n we should have 6 lines (the last one empty):
539     BOOST_CHECK_EQUAL( 6u, list1.size() );
540     BOOST_CHECK_EQUAL( std::string(), list1.back() );
541     BOOST_CHECK_EQUAL( std::string("Zeile 1"), list1.front() );
542
543     StringList list2;
544
545     split_string(block, list2, "\n", true);
546
547     // now we omitted empty lines, now we should have only 4 lines left:
548     BOOST_CHECK_EQUAL( 4u, list2.size() );
549     BOOST_CHECK_EQUAL( std::string("Zeile 5"), list2.back() );
550     BOOST_CHECK_EQUAL( std::string("Zeile 1"), list2.front() );
551
552     list2= split_string(block, "\n", true, "+-");
553
554     // again, we omitted empty lines, but also trimmed away leading and trailing "+" and "-"
555     BOOST_CHECK_EQUAL( 4u, list2.size() );
556     BOOST_CHECK_EQUAL( std::string("Zeile 5"), list2.back() );
557     BOOST_CHECK_EQUAL( std::string("Zeile 1"), list2.front() );
558     BOOST_CHECK_EQUAL( std::string("Zeile-2"), *(++list2.begin()) );
559 } // eo SplitString1
560
561
562
563 BOOST_AUTO_TEST_CASE(SplitString2)
564 {
565     std::string line("172.16.0.0/16 dev eth0  scope link  src 172.16.1.111");
566
567     StringList list1;
568
569     split_string(line, list1, " ", true, Whitespaces);
570
571     BOOST_CHECK_EQUAL( 7u, list1.size() );
572
573 } // eo SplitString2
574
575
576
577 BOOST_AUTO_TEST_CASE(SplitStringEmpty)
578 {
579     std::string line("");
580
581     StringList list1;
582
583     split_string(line, list1, " ", true, Whitespaces);
584
585     BOOST_CHECK_EQUAL( 0u, list1.size() );
586 } // eo SplitStringEmpty
587
588
589 BOOST_AUTO_TEST_CASE(SplitStringDelimiterOnly)
590 {
591     std::string line(" ");
592
593     StringList list1;
594
595     split_string(line, list1, " ", true, Whitespaces);
596
597     BOOST_CHECK_EQUAL( 0u, list1.size() );
598 } // eo SplitStringDelimiterOnly
599
600
601
602 BOOST_AUTO_TEST_CASE(JoinString1)
603 {
604     std::list< std::string > parts;
605     get_push_back_filler(parts)("1")("2")("drei");
606
607     std::string joined_string= join_string(parts,"/");
608     // we should have slashes between the strings:
609     BOOST_CHECK_EQUAL( std::string("1/2/drei") , joined_string );
610
611     parts.push_back( std::string() );
612     joined_string= join_string(parts,"/");
613     // now we should have an additional trailing slash:
614     BOOST_CHECK_EQUAL( std::string("1/2/drei/") , joined_string );
615
616     parts.push_front( std::string() );
617     joined_string= join_string(parts,"/");
618     // now we should have an additional leading slash:
619     BOOST_CHECK_EQUAL( std::string("/1/2/drei/") , joined_string );
620
621 } // eo JoinString1
622
623
624 BOOST_AUTO_TEST_CASE(JoinStringVector)
625 {
626     std::vector< std::string > parts;
627     get_push_back_filler(parts)("1")("2")("drei");
628
629     std::string joined_string= join_string(parts,"/");
630     // we should have slashes between the strings:
631     BOOST_CHECK_EQUAL( std::string("1/2/drei") , joined_string );
632
633     parts.push_back( std::string() );
634     joined_string= join_string(parts,"/");
635     // now we should have an additional trailing slash:
636     BOOST_CHECK_EQUAL( std::string("1/2/drei/") , joined_string );
637
638     parts.insert(parts.begin(), "");
639     joined_string= join_string(parts,"/");
640     // now we should have an additional leading slash:
641     BOOST_CHECK_EQUAL( std::string("/1/2/drei/") , joined_string );
642
643 } // eo JoinStringVector
644
645
646 BOOST_AUTO_TEST_CASE(ConversionStringInt)
647 {
648     std::string s1("24");
649     std::string s1x("25x");
650     int i1=0;
651     bool res= false;
652
653     i1= string_to<int>(s1);
654     BOOST_CHECK_EQUAL( 24, i1 );
655     i1= string_to<int>(s1x);
656     BOOST_CHECK_EQUAL( 25, i1 );
657
658     res= string_to<int>(s1,i1);
659     BOOST_CHECK_EQUAL( true, res );
660     BOOST_CHECK_EQUAL( 24, i1 );
661
662     res= string_to<int>(s1x,i1);
663     BOOST_CHECK_EQUAL( false, res );
664
665     std::string ss1= to_string( 24 );
666     BOOST_CHECK_EQUAL( std::string("24"), ss1);
667
668 } // eo ConversionStringInt()
669
670
671
672 BOOST_AUTO_TEST_CASE(HexBinaryConversion)
673 {
674     std::string hex1("49324E");
675     std::string bin1("I2N");
676
677     BOOST_CHECK_EQUAL( hex1, convert_binary_to_hex(bin1,true) );
678     BOOST_CHECK_EQUAL( bin1, convert_hex_to_binary(hex1) );
679     BOOST_CHECK_EQUAL( to_lower(hex1), convert_binary_to_hex(bin1) );
680
681     std::string hex2("0001");
682     std::string hex2a("00 01");
683     std::string hex2b("00:01");
684     std::string bin2("\0\1",2);
685
686     BOOST_CHECK_EQUAL( hex2, convert_binary_to_hex(bin2) );
687     BOOST_CHECK_EQUAL( bin2, convert_hex_to_binary(hex2) );
688     BOOST_CHECK_EQUAL( bin2, convert_hex_to_binary(hex2a) );
689     BOOST_CHECK_EQUAL( bin2, convert_hex_to_binary(hex2b) );
690
691     BOOST_REQUIRE_THROW( convert_hex_to_binary("01 kein hex"), std::runtime_error);
692 } // eo HexConversion()
693
694 BOOST_AUTO_TEST_CASE(HexIntConversion)
695 {
696     BOOST_CHECK_EQUAL( 255 , hex_string_to<uint32_t>("ff") );
697     BOOST_CHECK_EQUAL( 18866985 , hex_string_to<uint32_t>("11FE329") );
698     BOOST_CHECK_EQUAL( 44 , hex_string_to<uint32_t>("0x2C") );
699 }
700
701 BOOST_AUTO_TEST_CASE(sanitize_for_logging1)
702 {
703     string output = sanitize_for_logging("normaler string ohne aerger");
704
705     BOOST_CHECK_EQUAL(string("normaler string ohne aerger"), output);
706 }
707
708 BOOST_AUTO_TEST_CASE(sanitize_for_logging2)
709 {
710     string to_test="fiese";
711     to_test.push_back(0);
712     to_test+="null";
713
714     string output = sanitize_for_logging(to_test);
715
716     BOOST_CHECK_EQUAL(string("fiese?null"), output);
717 }
718
719 BOOST_AUTO_TEST_CASE(sanitize_for_logging3)
720 {
721     string output = sanitize_for_logging("läuter ümlaute utf8");
722
723     BOOST_CHECK_EQUAL(string("l??uter ??mlaute utf8"), output);
724 }
725
726 BOOST_AUTO_TEST_SUITE_END()