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