add functions to convert from hex strings to other types
[libi2ncommon] / test / stringfunc.cpp
CommitLineData
0e23f538
TJ
1/*
2The software in this package is distributed under the GNU General
3Public License version 2 (with a special exception described below).
4
5A copy of GNU General Public License (GPL) is included in this distribution,
6in the file COPYING.GPL.
7
8As a special exception, if other files instantiate templates or use macros
9or inline functions from this file, or you compile this file and link it
10with other works to produce a work based on this file, this file
11does not by itself cause the resulting work to be covered
12by the GNU General Public License.
13
14However the source code for this file must still be made available
15in accordance with section (3) of the GNU General Public License.
16
17This exception does not invalidate any other reasons why a work based
18on this file might be covered by the GNU General Public License.
19*/
118e216e 20/***************************************************************************
652f47a7 21 * Copyright (C) 2006-2011 by Intra2net AG *
118e216e
TJ
22 * *
23 ***************************************************************************/
24
25// #include <iostream>
26#include <string>
d435d600 27#include <cmath>
118e216e
TJ
28// #include <sstream>
29// #include <stdexcept>
30
9fe0853b
TJ
31#define BOOST_TEST_DYN_LINK
32#include <boost/test/unit_test.hpp>
d435d600 33#include <boost/numeric/conversion/cast.hpp>
118e216e
TJ
34
35#include <stringfunc.hxx>
57e78ccd 36#include <containerfunc.hpp>
3bcc713f 37#include <iostream>
118e216e
TJ
38
39using namespace std;
57e78ccd 40using namespace I2n;
118e216e 41
9fe0853b
TJ
42typedef std::list< std::string > StringList;
43
44BOOST_AUTO_TEST_SUITE(stringfunc)
45
46BOOST_AUTO_TEST_CASE(smart_html_entites1)
47{
48 string output = smart_html_entities("Test");
49
50 BOOST_CHECK_EQUAL(string("Test"), output);
51}
52
53BOOST_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
60BOOST_AUTO_TEST_CASE(smart_html_entites3)
61{
62 string output = smart_html_entities("<>");
63
64 BOOST_CHECK_EQUAL(string("<>"), output);
65}
66
67BOOST_AUTO_TEST_CASE(smart_html_entites4)
68{
69 string output = smart_html_entities("<ümlaut>");
70
71 BOOST_CHECK_EQUAL(string("<ümlaut>"), output);
72}
73
74BOOST_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
81BOOST_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
88BOOST_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
95BOOST_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
102BOOST_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
8b06a5c9
GMF
109
110
9fe0853b
TJ
111BOOST_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
117BOOST_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
123BOOST_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
8b06a5c9
GMF
129
130
388626dc
GMF
131BOOST_AUTO_TEST_CASE(nice_unit_format1)
132{
133 const int64_t two_bytes = 2;
8b06a5c9 134
70fc0674 135 string output = nice_unit_format(two_bytes, LongUnitFormat, UnitBase1000);
388626dc 136 BOOST_CHECK_EQUAL(string("2.00 Bytes"), output);
8b06a5c9 137
4388cc08 138 output = nice_unit_format(two_bytes);
8b06a5c9 139 BOOST_CHECK_EQUAL(string("2.0 B"), output);
388626dc
GMF
140}
141
142BOOST_AUTO_TEST_CASE(nice_unit_format2)
143{
144 const int64_t two_kilobytes = 2000;
8b06a5c9 145
70fc0674 146 string output = nice_unit_format(two_kilobytes, LongUnitFormat, UnitBase1000);
388626dc
GMF
147 BOOST_CHECK_EQUAL(string("2.00 KBytes"), output);
148
4388cc08 149 output = nice_unit_format(two_kilobytes);
8b06a5c9
GMF
150 BOOST_CHECK_EQUAL(string("2.0 KB"), output);
151
d435d600
CH
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
388626dc 158 const int64_t two_and_half_kilobytes = 2500;
8b06a5c9 159
70fc0674 160 output = nice_unit_format(two_and_half_kilobytes, LongUnitFormat, UnitBase1000);
388626dc 161 BOOST_CHECK_EQUAL(string("2.50 KBytes"), output);
8b06a5c9 162
4388cc08 163 output = nice_unit_format(two_and_half_kilobytes);
8b06a5c9 164 BOOST_CHECK_EQUAL(string("2.4 KB"), output);
d435d600
CH
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);
388626dc
GMF
171}
172
173BOOST_AUTO_TEST_CASE(nice_unit_format3)
174{
175 const int64_t two_megabytes = 2000000;
8b06a5c9 176
70fc0674 177 string output = nice_unit_format(two_megabytes, LongUnitFormat, UnitBase1000);
388626dc
GMF
178 BOOST_CHECK_EQUAL(string("2.00 MBytes"), output);
179
4388cc08 180 output = nice_unit_format(two_megabytes);
8b06a5c9
GMF
181 BOOST_CHECK_EQUAL(string("1.9 MB"), output);
182
d435d600
CH
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
388626dc 189 const int64_t two_and_half_megabytes = 2500000;
8b06a5c9 190
70fc0674 191 output = nice_unit_format(two_and_half_megabytes, LongUnitFormat, UnitBase1000);
388626dc 192 BOOST_CHECK_EQUAL(string("2.50 MBytes"), output);
8b06a5c9 193
4388cc08 194 output = nice_unit_format(two_and_half_megabytes);
8b06a5c9
GMF
195 BOOST_CHECK_EQUAL(string("2.4 MB"), output);
196
d435d600
CH
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
388626dc
GMF
203}
204
205BOOST_AUTO_TEST_CASE(nice_unit_format4)
206{
207 const int64_t two_gigabytes = 2000000000LL;
8b06a5c9 208
70fc0674 209 string output = nice_unit_format(two_gigabytes, LongUnitFormat, UnitBase1000);
388626dc
GMF
210 BOOST_CHECK_EQUAL(string("2.00 GBytes"), output);
211
4388cc08 212 output = nice_unit_format(two_gigabytes);
8b06a5c9
GMF
213 BOOST_CHECK_EQUAL(string("1.9 GB"), output);
214
d435d600
CH
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
388626dc 221 const int64_t two_and_half_gigabytes = 2500000000LL;
8b06a5c9 222
70fc0674 223 output = nice_unit_format(two_and_half_gigabytes, LongUnitFormat, UnitBase1000);
388626dc 224 BOOST_CHECK_EQUAL(string("2.50 GBytes"), output);
8b06a5c9 225
4388cc08 226 output = nice_unit_format(two_and_half_gigabytes);
8b06a5c9 227 BOOST_CHECK_EQUAL(string("2.3 GB"), output);
d435d600
CH
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);
388626dc
GMF
234}
235
236BOOST_AUTO_TEST_CASE(nice_unit_format5)
237{
238 const int64_t two_terabytes = 2000000000000LL;
8b06a5c9 239
70fc0674 240 string output = nice_unit_format(two_terabytes, LongUnitFormat, UnitBase1000);
388626dc
GMF
241 BOOST_CHECK_EQUAL(string("2.00 TBytes"), output);
242
4388cc08 243 output = nice_unit_format(two_terabytes);
8b06a5c9
GMF
244 BOOST_CHECK_EQUAL(string("1.8 TB"), output);
245
d435d600
CH
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
388626dc 252 const int64_t two_and_half_terabytes = 2500000000000LL;
8b06a5c9 253
70fc0674 254 output = nice_unit_format(two_and_half_terabytes, LongUnitFormat, UnitBase1000);
388626dc 255 BOOST_CHECK_EQUAL(string("2.50 TBytes"), output);
8b06a5c9 256
4388cc08 257 output = nice_unit_format(two_and_half_terabytes);
8b06a5c9 258 BOOST_CHECK_EQUAL(string("2.3 TB"), output);
d435d600
CH
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);
388626dc
GMF
265}
266
267BOOST_AUTO_TEST_CASE(nice_unit_format6)
268{
269 const int64_t two_petabytes = 2000000000000000LL;
8b06a5c9 270
70fc0674 271 string output = nice_unit_format(two_petabytes, LongUnitFormat, UnitBase1000);
388626dc
GMF
272 BOOST_CHECK_EQUAL(string("2.00 PBytes"), output);
273
4388cc08 274 output = nice_unit_format(two_petabytes);
8b06a5c9
GMF
275 BOOST_CHECK_EQUAL(string("1.8 PB"), output);
276
d435d600
CH
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
388626dc 283 const int64_t two_and_half_petabytes = 2500000000000000LL;
8b06a5c9 284
70fc0674 285 output = nice_unit_format(two_and_half_petabytes, LongUnitFormat, UnitBase1000);
388626dc 286 BOOST_CHECK_EQUAL(string("2.50 PBytes"), output);
8b06a5c9 287
4388cc08 288 output = nice_unit_format(two_and_half_petabytes);
8b06a5c9 289 BOOST_CHECK_EQUAL(string("2.2 PB"), output);
d435d600
CH
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);
388626dc
GMF
296}
297
298BOOST_AUTO_TEST_CASE(nice_unit_format7)
299{
300 const int64_t two_exabytes = 2000000000000000000LL;
8b06a5c9 301
70fc0674 302 string output = nice_unit_format(two_exabytes, LongUnitFormat, UnitBase1000);
83809f5e 303 BOOST_CHECK_EQUAL(string("2000.00 PBytes"), output);
388626dc 304
4388cc08 305 output = nice_unit_format(two_exabytes);
83809f5e 306 BOOST_CHECK_EQUAL(string("1776.4 PB"), output);
8b06a5c9 307
d435d600
CH
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
388626dc 314 const int64_t two_and_half_exabytes = 2500000000000000000LL;
8b06a5c9 315
70fc0674 316 output = nice_unit_format(two_and_half_exabytes, LongUnitFormat, UnitBase1000);
83809f5e 317 BOOST_CHECK_EQUAL(string("2500.00 PBytes"), output);
8b06a5c9 318
4388cc08 319 output = nice_unit_format(two_and_half_exabytes);
83809f5e 320 BOOST_CHECK_EQUAL(string("2220.4 PB"), output);
d435d600
CH
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);
83809f5e
GMF
327}
328
329BOOST_AUTO_TEST_CASE(nice_unit_format8)
330{
331 const int64_t max_representable_64bits_number = 9223372036854775807LL;
332
70fc0674 333 string output = nice_unit_format(max_representable_64bits_number, LongUnitFormat, UnitBase1000);
83809f5e
GMF
334 BOOST_CHECK_EQUAL(string("9223.40 PBytes"), output);
335
4388cc08 336 output = nice_unit_format(max_representable_64bits_number);
83809f5e 337 BOOST_CHECK_EQUAL(string("8192.0 PB"), output);
d435d600
CH
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 */
388626dc
GMF
352}
353
9fe0853b
TJ
354BOOST_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
391BOOST_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
430BOOST_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
459BOOST_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
474BOOST_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
494BOOST_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
524BOOST_AUTO_TEST_CASE(SplitString1)
118e216e 525{
9fe0853b
TJ
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
562BOOST_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
576BOOST_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
588BOOST_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
601BOOST_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
624BOOST_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
3bcc713f 650BOOST_AUTO_TEST_CASE(HexBinaryConversion)
9fe0853b
TJ
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
3bcc713f
GE
672BOOST_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
b953bf36
GE
679BOOST_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
686BOOST_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
697BOOST_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
9fe0853b 704BOOST_AUTO_TEST_SUITE_END()