Create vector-result-version of split_string with unit test
[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>
99e07fab 38#include <stdint.h>
118e216e
TJ
39
40using namespace std;
57e78ccd 41using namespace I2n;
118e216e 42
9fe0853b
TJ
43typedef std::list< std::string > StringList;
44
45BOOST_AUTO_TEST_SUITE(stringfunc)
46
47BOOST_AUTO_TEST_CASE(smart_html_entites1)
48{
49 string output = smart_html_entities("Test");
50
51 BOOST_CHECK_EQUAL(string("Test"), output);
52}
53
54BOOST_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
61BOOST_AUTO_TEST_CASE(smart_html_entites3)
62{
63 string output = smart_html_entities("<>");
64
65 BOOST_CHECK_EQUAL(string("<>"), output);
66}
67
68BOOST_AUTO_TEST_CASE(smart_html_entites4)
69{
70 string output = smart_html_entities("<ümlaut>");
71
72 BOOST_CHECK_EQUAL(string("<ümlaut>"), output);
73}
74
75BOOST_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
82BOOST_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
89BOOST_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
96BOOST_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
103BOOST_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
8b06a5c9
GMF
110
111
9fe0853b
TJ
112BOOST_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
118BOOST_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
124BOOST_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
8b06a5c9
GMF
130
131
388626dc
GMF
132BOOST_AUTO_TEST_CASE(nice_unit_format1)
133{
134 const int64_t two_bytes = 2;
8b06a5c9 135
70fc0674 136 string output = nice_unit_format(two_bytes, LongUnitFormat, UnitBase1000);
388626dc 137 BOOST_CHECK_EQUAL(string("2.00 Bytes"), output);
8b06a5c9 138
4388cc08 139 output = nice_unit_format(two_bytes);
8b06a5c9 140 BOOST_CHECK_EQUAL(string("2.0 B"), output);
388626dc
GMF
141}
142
143BOOST_AUTO_TEST_CASE(nice_unit_format2)
144{
145 const int64_t two_kilobytes = 2000;
8b06a5c9 146
70fc0674 147 string output = nice_unit_format(two_kilobytes, LongUnitFormat, UnitBase1000);
388626dc
GMF
148 BOOST_CHECK_EQUAL(string("2.00 KBytes"), output);
149
4388cc08 150 output = nice_unit_format(two_kilobytes);
8b06a5c9
GMF
151 BOOST_CHECK_EQUAL(string("2.0 KB"), output);
152
d435d600
CH
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
388626dc 159 const int64_t two_and_half_kilobytes = 2500;
8b06a5c9 160
70fc0674 161 output = nice_unit_format(two_and_half_kilobytes, LongUnitFormat, UnitBase1000);
388626dc 162 BOOST_CHECK_EQUAL(string("2.50 KBytes"), output);
8b06a5c9 163
4388cc08 164 output = nice_unit_format(two_and_half_kilobytes);
8b06a5c9 165 BOOST_CHECK_EQUAL(string("2.4 KB"), output);
d435d600
CH
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);
388626dc
GMF
172}
173
174BOOST_AUTO_TEST_CASE(nice_unit_format3)
175{
176 const int64_t two_megabytes = 2000000;
8b06a5c9 177
70fc0674 178 string output = nice_unit_format(two_megabytes, LongUnitFormat, UnitBase1000);
388626dc
GMF
179 BOOST_CHECK_EQUAL(string("2.00 MBytes"), output);
180
4388cc08 181 output = nice_unit_format(two_megabytes);
8b06a5c9
GMF
182 BOOST_CHECK_EQUAL(string("1.9 MB"), output);
183
d435d600
CH
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
388626dc 190 const int64_t two_and_half_megabytes = 2500000;
8b06a5c9 191
70fc0674 192 output = nice_unit_format(two_and_half_megabytes, LongUnitFormat, UnitBase1000);
388626dc 193 BOOST_CHECK_EQUAL(string("2.50 MBytes"), output);
8b06a5c9 194
4388cc08 195 output = nice_unit_format(two_and_half_megabytes);
8b06a5c9
GMF
196 BOOST_CHECK_EQUAL(string("2.4 MB"), output);
197
d435d600
CH
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
388626dc
GMF
204}
205
206BOOST_AUTO_TEST_CASE(nice_unit_format4)
207{
208 const int64_t two_gigabytes = 2000000000LL;
8b06a5c9 209
70fc0674 210 string output = nice_unit_format(two_gigabytes, LongUnitFormat, UnitBase1000);
388626dc
GMF
211 BOOST_CHECK_EQUAL(string("2.00 GBytes"), output);
212
4388cc08 213 output = nice_unit_format(two_gigabytes);
8b06a5c9
GMF
214 BOOST_CHECK_EQUAL(string("1.9 GB"), output);
215
d435d600
CH
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
388626dc 222 const int64_t two_and_half_gigabytes = 2500000000LL;
8b06a5c9 223
70fc0674 224 output = nice_unit_format(two_and_half_gigabytes, LongUnitFormat, UnitBase1000);
388626dc 225 BOOST_CHECK_EQUAL(string("2.50 GBytes"), output);
8b06a5c9 226
4388cc08 227 output = nice_unit_format(two_and_half_gigabytes);
8b06a5c9 228 BOOST_CHECK_EQUAL(string("2.3 GB"), output);
d435d600
CH
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);
388626dc
GMF
235}
236
237BOOST_AUTO_TEST_CASE(nice_unit_format5)
238{
239 const int64_t two_terabytes = 2000000000000LL;
8b06a5c9 240
70fc0674 241 string output = nice_unit_format(two_terabytes, LongUnitFormat, UnitBase1000);
388626dc
GMF
242 BOOST_CHECK_EQUAL(string("2.00 TBytes"), output);
243
4388cc08 244 output = nice_unit_format(two_terabytes);
8b06a5c9
GMF
245 BOOST_CHECK_EQUAL(string("1.8 TB"), output);
246
d435d600
CH
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
388626dc 253 const int64_t two_and_half_terabytes = 2500000000000LL;
8b06a5c9 254
70fc0674 255 output = nice_unit_format(two_and_half_terabytes, LongUnitFormat, UnitBase1000);
388626dc 256 BOOST_CHECK_EQUAL(string("2.50 TBytes"), output);
8b06a5c9 257
4388cc08 258 output = nice_unit_format(two_and_half_terabytes);
8b06a5c9 259 BOOST_CHECK_EQUAL(string("2.3 TB"), output);
d435d600
CH
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);
388626dc
GMF
266}
267
268BOOST_AUTO_TEST_CASE(nice_unit_format6)
269{
270 const int64_t two_petabytes = 2000000000000000LL;
8b06a5c9 271
70fc0674 272 string output = nice_unit_format(two_petabytes, LongUnitFormat, UnitBase1000);
388626dc
GMF
273 BOOST_CHECK_EQUAL(string("2.00 PBytes"), output);
274
4388cc08 275 output = nice_unit_format(two_petabytes);
8b06a5c9
GMF
276 BOOST_CHECK_EQUAL(string("1.8 PB"), output);
277
d435d600
CH
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
388626dc 284 const int64_t two_and_half_petabytes = 2500000000000000LL;
8b06a5c9 285
70fc0674 286 output = nice_unit_format(two_and_half_petabytes, LongUnitFormat, UnitBase1000);
388626dc 287 BOOST_CHECK_EQUAL(string("2.50 PBytes"), output);
8b06a5c9 288
4388cc08 289 output = nice_unit_format(two_and_half_petabytes);
8b06a5c9 290 BOOST_CHECK_EQUAL(string("2.2 PB"), output);
d435d600
CH
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);
388626dc
GMF
297}
298
299BOOST_AUTO_TEST_CASE(nice_unit_format7)
300{
301 const int64_t two_exabytes = 2000000000000000000LL;
8b06a5c9 302
70fc0674 303 string output = nice_unit_format(two_exabytes, LongUnitFormat, UnitBase1000);
83809f5e 304 BOOST_CHECK_EQUAL(string("2000.00 PBytes"), output);
388626dc 305
4388cc08 306 output = nice_unit_format(two_exabytes);
83809f5e 307 BOOST_CHECK_EQUAL(string("1776.4 PB"), output);
8b06a5c9 308
d435d600
CH
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
388626dc 315 const int64_t two_and_half_exabytes = 2500000000000000000LL;
8b06a5c9 316
70fc0674 317 output = nice_unit_format(two_and_half_exabytes, LongUnitFormat, UnitBase1000);
83809f5e 318 BOOST_CHECK_EQUAL(string("2500.00 PBytes"), output);
8b06a5c9 319
4388cc08 320 output = nice_unit_format(two_and_half_exabytes);
83809f5e 321 BOOST_CHECK_EQUAL(string("2220.4 PB"), output);
d435d600
CH
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);
83809f5e
GMF
328}
329
330BOOST_AUTO_TEST_CASE(nice_unit_format8)
331{
332 const int64_t max_representable_64bits_number = 9223372036854775807LL;
333
70fc0674 334 string output = nice_unit_format(max_representable_64bits_number, LongUnitFormat, UnitBase1000);
83809f5e
GMF
335 BOOST_CHECK_EQUAL(string("9223.40 PBytes"), output);
336
4388cc08 337 output = nice_unit_format(max_representable_64bits_number);
83809f5e 338 BOOST_CHECK_EQUAL(string("8192.0 PB"), output);
d435d600
CH
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 */
388626dc
GMF
353}
354
9fe0853b
TJ
355BOOST_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
392BOOST_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
431BOOST_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
460BOOST_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
475BOOST_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
495BOOST_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
525BOOST_AUTO_TEST_CASE(SplitString1)
118e216e 526{
9fe0853b
TJ
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
563BOOST_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
577BOOST_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
589BOOST_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
338da253
CH
601BOOST_AUTO_TEST_CASE(SplitToVector)
602{
603 std::string line("0;1;2;3;4;5;6;7;8;9");
604 std::vector<std::string> result;
605 split_string(line, result, ";");
606 BOOST_REQUIRE_EQUAL(result.size(), 10);
607 BOOST_CHECK_EQUAL(result[0], "0");
608 BOOST_CHECK_EQUAL(result[4], "4");
609 BOOST_CHECK_EQUAL(result[9], "9");
610}
611
9fe0853b
TJ
612
613BOOST_AUTO_TEST_CASE(JoinString1)
614{
615 std::list< std::string > parts;
616 get_push_back_filler(parts)("1")("2")("drei");
617
618 std::string joined_string= join_string(parts,"/");
619 // we should have slashes between the strings:
620 BOOST_CHECK_EQUAL( std::string("1/2/drei") , joined_string );
621
622 parts.push_back( std::string() );
623 joined_string= join_string(parts,"/");
624 // now we should have an additional trailing slash:
625 BOOST_CHECK_EQUAL( std::string("1/2/drei/") , joined_string );
626
627 parts.push_front( std::string() );
628 joined_string= join_string(parts,"/");
629 // now we should have an additional leading slash:
630 BOOST_CHECK_EQUAL( std::string("/1/2/drei/") , joined_string );
631
632} // eo JoinString1
633
634
376ec4fa
CH
635BOOST_AUTO_TEST_CASE(JoinStringVector)
636{
637 std::vector< std::string > parts;
638 get_push_back_filler(parts)("1")("2")("drei");
639
640 std::string joined_string= join_string(parts,"/");
641 // we should have slashes between the strings:
642 BOOST_CHECK_EQUAL( std::string("1/2/drei") , joined_string );
643
644 parts.push_back( std::string() );
645 joined_string= join_string(parts,"/");
646 // now we should have an additional trailing slash:
647 BOOST_CHECK_EQUAL( std::string("1/2/drei/") , joined_string );
648
649 parts.insert(parts.begin(), "");
650 joined_string= join_string(parts,"/");
651 // now we should have an additional leading slash:
652 BOOST_CHECK_EQUAL( std::string("/1/2/drei/") , joined_string );
653
654} // eo JoinStringVector
655
9fe0853b
TJ
656
657BOOST_AUTO_TEST_CASE(ConversionStringInt)
658{
659 std::string s1("24");
660 std::string s1x("25x");
661 int i1=0;
662 bool res= false;
663
664 i1= string_to<int>(s1);
665 BOOST_CHECK_EQUAL( 24, i1 );
666 i1= string_to<int>(s1x);
667 BOOST_CHECK_EQUAL( 25, i1 );
668
669 res= string_to<int>(s1,i1);
670 BOOST_CHECK_EQUAL( true, res );
671 BOOST_CHECK_EQUAL( 24, i1 );
672
673 res= string_to<int>(s1x,i1);
674 BOOST_CHECK_EQUAL( false, res );
675
676 std::string ss1= to_string( 24 );
677 BOOST_CHECK_EQUAL( std::string("24"), ss1);
678
679} // eo ConversionStringInt()
680
681
682
3bcc713f 683BOOST_AUTO_TEST_CASE(HexBinaryConversion)
9fe0853b
TJ
684{
685 std::string hex1("49324E");
686 std::string bin1("I2N");
687
688 BOOST_CHECK_EQUAL( hex1, convert_binary_to_hex(bin1,true) );
689 BOOST_CHECK_EQUAL( bin1, convert_hex_to_binary(hex1) );
690 BOOST_CHECK_EQUAL( to_lower(hex1), convert_binary_to_hex(bin1) );
691
692 std::string hex2("0001");
693 std::string hex2a("00 01");
694 std::string hex2b("00:01");
695 std::string bin2("\0\1",2);
696
697 BOOST_CHECK_EQUAL( hex2, convert_binary_to_hex(bin2) );
698 BOOST_CHECK_EQUAL( bin2, convert_hex_to_binary(hex2) );
699 BOOST_CHECK_EQUAL( bin2, convert_hex_to_binary(hex2a) );
700 BOOST_CHECK_EQUAL( bin2, convert_hex_to_binary(hex2b) );
701
702 BOOST_REQUIRE_THROW( convert_hex_to_binary("01 kein hex"), std::runtime_error);
703} // eo HexConversion()
704
3bcc713f
GE
705BOOST_AUTO_TEST_CASE(HexIntConversion)
706{
707 BOOST_CHECK_EQUAL( 255 , hex_string_to<uint32_t>("ff") );
708 BOOST_CHECK_EQUAL( 18866985 , hex_string_to<uint32_t>("11FE329") );
709 BOOST_CHECK_EQUAL( 44 , hex_string_to<uint32_t>("0x2C") );
710}
711
b953bf36
GE
712BOOST_AUTO_TEST_CASE(sanitize_for_logging1)
713{
714 string output = sanitize_for_logging("normaler string ohne aerger");
715
716 BOOST_CHECK_EQUAL(string("normaler string ohne aerger"), output);
717}
718
719BOOST_AUTO_TEST_CASE(sanitize_for_logging2)
720{
721 string to_test="fiese";
722 to_test.push_back(0);
723 to_test+="null";
724
725 string output = sanitize_for_logging(to_test);
726
727 BOOST_CHECK_EQUAL(string("fiese?null"), output);
728}
729
730BOOST_AUTO_TEST_CASE(sanitize_for_logging3)
731{
732 string output = sanitize_for_logging("läuter ümlaute utf8");
733
734 BOOST_CHECK_EQUAL(string("l??uter ??mlaute utf8"), output);
735}
736
9fe0853b 737BOOST_AUTO_TEST_SUITE_END()