2 The software in this package is distributed under the GNU General
3 Public License version 2 (with a special exception described below).
5 A copy of GNU General Public License (GPL) is included in this distribution,
6 in the file COPYING.GPL.
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.
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.
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.
20 /***************************************************************************
21 * Copyright (C) 2006-2011 by Intra2net AG *
23 ***************************************************************************/
25 // #include <iostream>
29 // #include <stdexcept>
31 #define BOOST_TEST_DYN_LINK
32 #include <boost/test/unit_test.hpp>
33 #include <boost/foreach.hpp>
34 #include <boost/numeric/conversion/cast.hpp>
36 #include <stringfunc.hxx>
37 #include <containerfunc.hpp>
44 typedef std::list< std::string > StringList;
46 BOOST_AUTO_TEST_SUITE(stringfunc)
48 BOOST_AUTO_TEST_CASE(smart_html_entities1)
50 string output = smart_html_entities("Test");
52 BOOST_CHECK_EQUAL(string("Test"), output);
55 BOOST_AUTO_TEST_CASE(smart_html_entities2)
57 string output = smart_html_entities("Täst");
59 BOOST_CHECK_EQUAL(string("Täst"), output);
62 BOOST_AUTO_TEST_CASE(smart_html_entities3)
64 string output = smart_html_entities("<>");
66 BOOST_CHECK_EQUAL(string("<>"), output);
69 BOOST_AUTO_TEST_CASE(smart_html_entities4)
71 string output = smart_html_entities("<ümlaut>");
73 BOOST_CHECK_EQUAL(string("<ümlaut>"), output);
76 BOOST_AUTO_TEST_CASE(smart_html_entities5)
78 string output = smart_html_entities("Test<ümlaut>Blä");
80 BOOST_CHECK_EQUAL(string("Test<ümlaut>Blä"), output);
83 BOOST_AUTO_TEST_CASE(smart_html_entities6)
85 string output = smart_html_entities("System > Einstellungen");
87 BOOST_CHECK_EQUAL(string("System > Einstellungen"), output);
90 BOOST_AUTO_TEST_CASE(smart_html_entities7)
92 string output = smart_html_entities("Finden Sie <b>auf</b> der Seite <a href=\"fdslfsl\">\"System > Einstellungen\"</a>. Oder etwa nicht?");
94 BOOST_CHECK_EQUAL(string("Finden Sie <b>auf</b> der Seite <a href=\"fdslfsl\">"System > Einstellungen"</a>. Oder etwa nicht?"), output);
97 BOOST_AUTO_TEST_CASE(strip_html_tags1)
99 string output = strip_html_tags("Was für ein schöner Tag, finden Sie nicht?");
101 BOOST_CHECK_EQUAL(string("Was für ein schöner Tag, finden Sie nicht?"), output);
104 BOOST_AUTO_TEST_CASE(strip_html_tags2)
106 string output = strip_html_tags("Was für ein <a href=\"wikipedia\" target=\"new\">schöner Tag</a>, finden Sie nicht?");
108 BOOST_CHECK_EQUAL(string("Was für ein schöner Tag, finden Sie nicht?"), output);
113 BOOST_AUTO_TEST_CASE(html_entities1)
115 string output = html_entities("\xC3\xA4\xC3\xB6\xC3\xBC");
116 BOOST_CHECK_EQUAL(string("äöü"), output);
119 BOOST_AUTO_TEST_CASE(html_entities2)
121 string output = html_entities("\xC3\xA5 \xC3\xB5 \xC3\xBF");
122 BOOST_CHECK_EQUAL(string("å õ ÿ"), output);
125 BOOST_AUTO_TEST_CASE(html_entities3)
127 string output = html_entities("\xC4\x8E \xE0\xBC\xB1 \xE8\x82\x88");
128 BOOST_CHECK_EQUAL(string("Ď ༱ 肈"), output);
131 BOOST_AUTO_TEST_CASE(entities_latin1)
133 BOOST_CHECK_EQUAL((std::string)"noop", html_entities_iso ("noop"));
134 BOOST_CHECK_EQUAL((std::string)"täst", html_entities_iso ("t\xe4st"));
135 BOOST_CHECK_EQUAL((std::string)"TÄST", html_entities_iso ("T\xc4ST"));
139 BOOST_AUTO_TEST_CASE(nice_unit_format1)
141 const int64_t two_bytes = 2;
143 string output = nice_unit_format(two_bytes, LongUnitFormat, UnitBase1000);
144 BOOST_CHECK_EQUAL(string("2.00 Bytes"), output);
146 output = nice_unit_format(two_bytes);
147 BOOST_CHECK_EQUAL(string("2.0 B"), output);
150 BOOST_AUTO_TEST_CASE(nice_unit_format2)
152 const int64_t two_kilobytes = 2000;
154 string output = nice_unit_format(two_kilobytes, LongUnitFormat, UnitBase1000);
155 BOOST_CHECK_EQUAL(string("2.00 KBytes"), output);
157 output = nice_unit_format(two_kilobytes);
158 BOOST_CHECK_EQUAL(string("2.0 KB"), output);
160 output = nice_unit_format(boost::numeric_cast<double>(two_kilobytes), LongUnitFormat, UnitBase1000);
161 BOOST_CHECK_EQUAL(string("2.00 KBytes"), output);
163 output = nice_unit_format(boost::numeric_cast<double>(two_kilobytes));
164 BOOST_CHECK_EQUAL(string("2.0 KB"), output);
166 const int64_t two_and_half_kilobytes = 2500;
168 output = nice_unit_format(two_and_half_kilobytes, LongUnitFormat, UnitBase1000);
169 BOOST_CHECK_EQUAL(string("2.50 KBytes"), output);
171 output = nice_unit_format(two_and_half_kilobytes);
172 BOOST_CHECK_EQUAL(string("2.4 KB"), output);
174 output = nice_unit_format(boost::numeric_cast<double>(two_and_half_kilobytes), LongUnitFormat, UnitBase1000);
175 BOOST_CHECK_EQUAL(string("2.50 KBytes"), output);
177 output = nice_unit_format(boost::numeric_cast<double>(two_and_half_kilobytes));
178 BOOST_CHECK_EQUAL(string("2.4 KB"), output);
181 BOOST_AUTO_TEST_CASE(nice_unit_format3)
183 const int64_t two_megabytes = 2000000;
185 string output = nice_unit_format(two_megabytes, LongUnitFormat, UnitBase1000);
186 BOOST_CHECK_EQUAL(string("2.00 MBytes"), output);
188 output = nice_unit_format(two_megabytes);
189 BOOST_CHECK_EQUAL(string("1.9 MB"), output);
191 output = nice_unit_format(boost::numeric_cast<double>(two_megabytes), LongUnitFormat, UnitBase1000);
192 BOOST_CHECK_EQUAL(string("2.00 MBytes"), output);
194 output = nice_unit_format(boost::numeric_cast<double>(two_megabytes));
195 BOOST_CHECK_EQUAL(string("1.9 MB"), output);
197 const int64_t two_and_half_megabytes = 2500000;
199 output = nice_unit_format(two_and_half_megabytes, LongUnitFormat, UnitBase1000);
200 BOOST_CHECK_EQUAL(string("2.50 MBytes"), output);
202 output = nice_unit_format(two_and_half_megabytes);
203 BOOST_CHECK_EQUAL(string("2.4 MB"), output);
205 output = nice_unit_format(boost::numeric_cast<double>(two_and_half_megabytes), LongUnitFormat, UnitBase1000);
206 BOOST_CHECK_EQUAL(string("2.50 MBytes"), output);
208 output = nice_unit_format(boost::numeric_cast<double>(two_and_half_megabytes));
209 BOOST_CHECK_EQUAL(string("2.4 MB"), output);
213 BOOST_AUTO_TEST_CASE(nice_unit_format4)
215 const int64_t two_gigabytes = 2000000000LL;
217 string output = nice_unit_format(two_gigabytes, LongUnitFormat, UnitBase1000);
218 BOOST_CHECK_EQUAL(string("2.00 GBytes"), output);
220 output = nice_unit_format(two_gigabytes);
221 BOOST_CHECK_EQUAL(string("1.9 GB"), output);
223 output = nice_unit_format(boost::numeric_cast<double>(two_gigabytes), LongUnitFormat, UnitBase1000);
224 BOOST_CHECK_EQUAL(string("2.00 GBytes"), output);
226 output = nice_unit_format(boost::numeric_cast<double>(two_gigabytes));
227 BOOST_CHECK_EQUAL(string("1.9 GB"), output);
229 const int64_t two_and_half_gigabytes = 2500000000LL;
231 output = nice_unit_format(two_and_half_gigabytes, LongUnitFormat, UnitBase1000);
232 BOOST_CHECK_EQUAL(string("2.50 GBytes"), output);
234 output = nice_unit_format(two_and_half_gigabytes);
235 BOOST_CHECK_EQUAL(string("2.3 GB"), output);
237 output = nice_unit_format(boost::numeric_cast<double>(two_and_half_gigabytes), LongUnitFormat, UnitBase1000);
238 BOOST_CHECK_EQUAL(string("2.50 GBytes"), output);
240 output = nice_unit_format(boost::numeric_cast<double>(two_and_half_gigabytes));
241 BOOST_CHECK_EQUAL(string("2.3 GB"), output);
244 BOOST_AUTO_TEST_CASE(nice_unit_format5)
246 const int64_t two_terabytes = 2000000000000LL;
248 string output = nice_unit_format(two_terabytes, LongUnitFormat, UnitBase1000);
249 BOOST_CHECK_EQUAL(string("2.00 TBytes"), output);
251 output = nice_unit_format(two_terabytes);
252 BOOST_CHECK_EQUAL(string("1.8 TB"), output);
254 output = nice_unit_format(boost::numeric_cast<double>(two_terabytes), LongUnitFormat, UnitBase1000);
255 BOOST_CHECK_EQUAL(string("2.00 TBytes"), output);
257 output = nice_unit_format(boost::numeric_cast<double>(two_terabytes));
258 BOOST_CHECK_EQUAL(string("1.8 TB"), output);
260 const int64_t two_and_half_terabytes = 2500000000000LL;
262 output = nice_unit_format(two_and_half_terabytes, LongUnitFormat, UnitBase1000);
263 BOOST_CHECK_EQUAL(string("2.50 TBytes"), output);
265 output = nice_unit_format(two_and_half_terabytes);
266 BOOST_CHECK_EQUAL(string("2.3 TB"), output);
268 output = nice_unit_format(boost::numeric_cast<double>(two_and_half_terabytes), LongUnitFormat, UnitBase1000);
269 BOOST_CHECK_EQUAL(string("2.50 TBytes"), output);
271 output = nice_unit_format(boost::numeric_cast<double>(two_and_half_terabytes));
272 BOOST_CHECK_EQUAL(string("2.3 TB"), output);
275 BOOST_AUTO_TEST_CASE(nice_unit_format6)
277 const int64_t two_petabytes = 2000000000000000LL;
279 string output = nice_unit_format(two_petabytes, LongUnitFormat, UnitBase1000);
280 BOOST_CHECK_EQUAL(string("2.00 PBytes"), output);
282 output = nice_unit_format(two_petabytes);
283 BOOST_CHECK_EQUAL(string("1.8 PB"), output);
285 output = nice_unit_format(boost::numeric_cast<double>(two_petabytes), LongUnitFormat, UnitBase1000);
286 BOOST_CHECK_EQUAL(string("2.00 PBytes"), output);
288 output = nice_unit_format(boost::numeric_cast<double>(two_petabytes));
289 BOOST_CHECK_EQUAL(string("1.8 PB"), output);
291 const int64_t two_and_half_petabytes = 2500000000000000LL;
293 output = nice_unit_format(two_and_half_petabytes, LongUnitFormat, UnitBase1000);
294 BOOST_CHECK_EQUAL(string("2.50 PBytes"), output);
296 output = nice_unit_format(two_and_half_petabytes);
297 BOOST_CHECK_EQUAL(string("2.2 PB"), output);
299 output = nice_unit_format(boost::numeric_cast<double>(two_and_half_petabytes), LongUnitFormat, UnitBase1000);
300 BOOST_CHECK_EQUAL(string("2.50 PBytes"), output);
302 output = nice_unit_format(boost::numeric_cast<double>(two_and_half_petabytes));
303 BOOST_CHECK_EQUAL(string("2.2 PB"), output);
306 BOOST_AUTO_TEST_CASE(nice_unit_format7)
308 const int64_t two_exabytes = 2000000000000000000LL;
310 string output = nice_unit_format(two_exabytes, LongUnitFormat, UnitBase1000);
311 BOOST_CHECK_EQUAL(string("2000.00 PBytes"), output);
313 output = nice_unit_format(two_exabytes);
314 BOOST_CHECK_EQUAL(string("1776.4 PB"), output);
316 output = nice_unit_format(boost::numeric_cast<double>(two_exabytes), LongUnitFormat, UnitBase1000);
317 BOOST_CHECK_EQUAL(string("2000.00 PBytes"), output);
319 output = nice_unit_format(boost::numeric_cast<double>(two_exabytes));
320 BOOST_CHECK_EQUAL(string("1776.4 PB"), output);
322 const int64_t two_and_half_exabytes = 2500000000000000000LL;
324 output = nice_unit_format(two_and_half_exabytes, LongUnitFormat, UnitBase1000);
325 BOOST_CHECK_EQUAL(string("2500.00 PBytes"), output);
327 output = nice_unit_format(two_and_half_exabytes);
328 BOOST_CHECK_EQUAL(string("2220.4 PB"), output);
330 output = nice_unit_format(boost::numeric_cast<double>(two_and_half_exabytes), LongUnitFormat, UnitBase1000);
331 BOOST_CHECK_EQUAL(string("2500.00 PBytes"), output);
333 output = nice_unit_format(boost::numeric_cast<double>(two_and_half_exabytes));
334 BOOST_CHECK_EQUAL(string("2220.4 PB"), output);
337 BOOST_AUTO_TEST_CASE(nice_unit_format8)
339 const int64_t max_representable_64bits_number = 9223372036854775807LL;
341 string output = nice_unit_format(max_representable_64bits_number, LongUnitFormat, UnitBase1000);
342 BOOST_CHECK_EQUAL(string("9223.40 PBytes"), output);
344 output = nice_unit_format(max_representable_64bits_number);
345 BOOST_CHECK_EQUAL(string("8192.0 PB"), output);
348 double val = boost::numeric_cast<double>(max_representable_64bits_number);
349 BOOST_MESSAGE("val as int64 is " << max_representable_64bits_number << " and as double is " << val);
350 BOOST_MESSAGE("val rounded is " << round(val));
351 BOOST_CHECK_EQUAL(val, round(val));
352 BOOST_MESSAGE("half as int64 is " << boost::numeric_cast<int64_t>(round(val/2))); // ok
353 BOOST_MESSAGE("cast back is " << boost::numeric_cast<int64_t>(round(val))); // throws a positive overflow
354 output = nice_unit_format(boost::numeric_cast<double>(max_representable_64bits_number), LongUnitFormat, UnitBase1000);
355 BOOST_CHECK_EQUAL(string("9223.40 PBytes"), output);
357 output = nice_unit_format(boost::numeric_cast<double>(max_representable_64bits_number));
358 BOOST_CHECK_EQUAL(string("8192.0 PB"), output);
362 BOOST_AUTO_TEST_CASE(TestTrim)
366 BOOST_CHECK_EQUAL( std::string("s1"), s );
370 BOOST_CHECK_EQUAL( std::string("s2"), s );
374 BOOST_CHECK_EQUAL( std::string("s3"), s );
378 BOOST_CHECK_EQUAL( std::string("s4:s4"), s);
380 /* non modifying version */
383 BOOST_CHECK_EQUAL( std::string("s1"), trim(s) );
386 BOOST_CHECK_EQUAL( std::string("s2"), trim(s) );
387 BOOST_CHECK_EQUAL( std::string(" s2"), s );
390 BOOST_CHECK_EQUAL( std::string("s3"), trim(s) );
391 BOOST_CHECK_EQUAL( std::string("s3 "), s );
394 BOOST_CHECK_EQUAL( std::string("s4:s4"), trim(s,":+-a") );
399 BOOST_AUTO_TEST_CASE(TestChomp)
404 BOOST_CHECK_EQUAL( std::string("s1"), s );
408 BOOST_CHECK_EQUAL( std::string("s2"), s );
412 BOOST_CHECK_EQUAL( std::string("s3"), s );
416 BOOST_CHECK_EQUAL( std::string(":s4:"), s);
418 /* non modifiying versions */
421 BOOST_CHECK_EQUAL( std::string("s1"), chomp(s) );
424 BOOST_CHECK_EQUAL( std::string("s2"), chomp(s) );
425 BOOST_CHECK_EQUAL( std::string("s2\n"), s);
428 BOOST_CHECK_EQUAL( std::string("s3"), chomp(s,":") );
429 BOOST_CHECK_EQUAL( std::string("s3:"), s);
432 BOOST_CHECK_EQUAL( std::string(":s4:"), chomp(s,"s:") );
433 BOOST_CHECK_EQUAL( std::string(":s4::"), s);
438 BOOST_AUTO_TEST_CASE(TestSuffixFuncs)
440 std::string s1("12.cpp");
442 BOOST_CHECK_EQUAL( true, has_suffix(s1,".cpp") );
443 BOOST_CHECK_EQUAL( true, has_suffix(s1,"pp") );
444 BOOST_CHECK_EQUAL( false, has_suffix(s1,"hpp") );
445 BOOST_CHECK_EQUAL( false, has_suffix(s1,"cp") );
446 BOOST_CHECK_EQUAL( false, has_suffix(s1,"") );
448 std::string s1c1= remove_suffix(s1,".cpp");
449 BOOST_CHECK_EQUAL( std::string("12"), s1c1 );
451 std::string s1c2= remove_suffix(s1,"p");
452 BOOST_CHECK_EQUAL( std::string("12.cp"), s1c2 );
454 std::string s1c3= remove_suffix(s1,"cp");
455 BOOST_CHECK_EQUAL( std::string("12.cpp"), s1c3 );
457 std::string s2(".cpp");
458 BOOST_CHECK_EQUAL( true, has_suffix(s2,".cpp") );
460 std::string s2c1= remove_suffix(s2,".cpp");
461 BOOST_CHECK_EQUAL( std::string(""), s2c1 );
463 } // eo TestSuffixFuncs()
467 BOOST_AUTO_TEST_CASE(TestPrefixFuncs)
469 std::string s1("12.cpp");
471 BOOST_CHECK_EQUAL( true, has_prefix(s1,"12") );
472 BOOST_CHECK_EQUAL( true, has_prefix(s1, "1") );
473 BOOST_CHECK_EQUAL( false, has_prefix(s1, "2") );
474 BOOST_CHECK_EQUAL( false, has_prefix(s1, "") );
476 std::string s1c1= remove_prefix(s1, "12");
477 BOOST_CHECK_EQUAL( std::string(".cpp"), s1c1);
478 } // eo TestPrefixFuncs()
482 BOOST_AUTO_TEST_CASE(TestLowerUpperFuncs)
484 std::string u1("CASE CONVERSION TEST..");
485 std::string l1("case conversion test..");
487 std::string test1(l1);
490 BOOST_CHECK_EQUAL( u1, test1 );
493 BOOST_CHECK_EQUAL( l1, test1 );
496 BOOST_CHECK_EQUAL( u1, to_upper(l1) );
497 BOOST_CHECK_EQUAL( l1, to_lower(u1) );
498 } // eo TestLowerUpper
502 BOOST_AUTO_TEST_CASE(PairSplit1)
505 get_push_back_filler(str_list)
506 ("a=11")("a= 11")("a =11 ")("a = 11 ")(" a = 11 ")
508 BOOST_CHECK_EQUAL( 5u, str_list.size() );
509 for(StringList::iterator it= str_list.begin();
510 it != str_list.end();
513 std::string key, value;
514 bool res= pair_split( *it, key, value);
516 BOOST_CHECK_EQUAL( true , res );
517 BOOST_CHECK_EQUAL( std::string("a"), key );
518 BOOST_CHECK_EQUAL( std::string("11"), value );
521 std::string key, value;
524 res= pair_split(" 1 : 2 ", key, value, ':');
525 BOOST_CHECK_EQUAL( true, res );
526 BOOST_CHECK_EQUAL( std::string("1"), key);
527 BOOST_CHECK_EQUAL( std::string("2"), value);
532 BOOST_AUTO_TEST_CASE(SplitString1)
544 split_string(block, list1, "\n");
545 // since the blocks ends with \n we should have 6 lines (the last one empty):
546 BOOST_CHECK_EQUAL( 6u, list1.size() );
547 BOOST_CHECK_EQUAL( std::string(), list1.back() );
548 BOOST_CHECK_EQUAL( std::string("Zeile 1"), list1.front() );
552 split_string(block, list2, "\n", true);
554 // now we omitted empty lines, now we should have only 4 lines left:
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() );
559 list2= split_string(block, "\n", true, "+-");
561 // again, we omitted empty lines, but also trimmed away leading and trailing "+" and "-"
562 BOOST_CHECK_EQUAL( 4u, list2.size() );
563 BOOST_CHECK_EQUAL( std::string("Zeile 5"), list2.back() );
564 BOOST_CHECK_EQUAL( std::string("Zeile 1"), list2.front() );
565 BOOST_CHECK_EQUAL( std::string("Zeile-2"), *(++list2.begin()) );
570 BOOST_AUTO_TEST_CASE(SplitString2)
572 std::string line("172.16.0.0/16 dev eth0 scope link src 172.16.1.111");
576 split_string(line, list1, " ", true, Whitespaces);
578 BOOST_CHECK_EQUAL( 7u, list1.size() );
584 BOOST_AUTO_TEST_CASE(SplitStringEmpty)
586 std::string line("");
590 split_string(line, list1, " ", true, Whitespaces);
592 BOOST_CHECK_EQUAL( 0u, list1.size() );
593 } // eo SplitStringEmpty
596 BOOST_AUTO_TEST_CASE(SplitStringDelimiterOnly)
598 std::string line(" ");
602 split_string(line, list1, " ", true, Whitespaces);
604 BOOST_CHECK_EQUAL( 0u, list1.size() );
605 } // eo SplitStringDelimiterOnly
608 BOOST_AUTO_TEST_CASE(SplitToVector)
610 std::string line("0;1;2;3;4;5;6;7;8;9");
611 std::vector<std::string> result;
612 split_string(line, result, ";");
613 BOOST_REQUIRE_EQUAL(result.size(), 10);
614 BOOST_CHECK_EQUAL(result[0], "0");
615 BOOST_CHECK_EQUAL(result[4], "4");
616 BOOST_CHECK_EQUAL(result[9], "9");
620 BOOST_AUTO_TEST_CASE(JoinString1)
622 std::list< std::string > parts;
623 std::string joined_string= join_string(parts,"/");
624 BOOST_CHECK_EQUAL( std::string("") , joined_string );
626 parts.push_back ("1");
627 joined_string= join_string(parts,"/");
628 // we should have slashes between the strings:
629 BOOST_CHECK_EQUAL( std::string("1") , joined_string );
631 get_push_back_filler(parts)("2")("drei");
633 joined_string= join_string(parts,"/");
634 // we should have slashes between the strings:
635 BOOST_CHECK_EQUAL( std::string("1/2/drei") , joined_string );
637 parts.push_back( std::string() );
638 joined_string= join_string(parts,"/");
639 // now we should have an additional trailing slash:
640 BOOST_CHECK_EQUAL( std::string("1/2/drei/") , joined_string );
642 parts.push_front( std::string() );
643 joined_string= join_string(parts,"/");
644 // now we should have an additional leading slash:
645 BOOST_CHECK_EQUAL( std::string("/1/2/drei/") , joined_string );
650 BOOST_AUTO_TEST_CASE(JoinStringVector)
652 std::vector< std::string > parts;
653 get_push_back_filler(parts)("1")("2")("drei");
655 std::string joined_string= join_string(parts,"/");
656 // we should have slashes between the strings:
657 BOOST_CHECK_EQUAL( std::string("1/2/drei") , joined_string );
659 parts.push_back( std::string() );
660 joined_string= join_string(parts,"/");
661 // now we should have an additional trailing slash:
662 BOOST_CHECK_EQUAL( std::string("1/2/drei/") , joined_string );
664 parts.insert(parts.begin(), "");
665 joined_string= join_string(parts,"/");
666 // now we should have an additional leading slash:
667 BOOST_CHECK_EQUAL( std::string("/1/2/drei/") , joined_string );
669 } // eo JoinStringVector
672 BOOST_AUTO_TEST_CASE(JoinStringSet)
674 std::set< std::string > parts;
676 std::string joined_string= join_string(parts,"/");
677 BOOST_CHECK_EQUAL( std::string() , joined_string );
679 parts.insert ("foo");
680 joined_string= join_string(parts,"/");
681 BOOST_CHECK_EQUAL( std::string("foo") , joined_string );
683 parts.insert ("bar");
684 parts.insert ("baz");
686 joined_string= join_string(parts,"/");
687 // we should have slashes between the strings:
688 BOOST_CHECK_EQUAL( std::string("bar/baz/foo") , joined_string );
690 parts.insert( std::string() );
691 joined_string= join_string(parts,"/");
692 // now we should have an additional trailing slash:
693 BOOST_CHECK_EQUAL( std::string("/bar/baz/foo") , joined_string );
694 } // eo JoinStringSet
697 BOOST_AUTO_TEST_CASE(JoinStringIterSet_Empty)
699 std::set< std::string > parts;
701 // empty sequence → empty string
702 BOOST_CHECK_EQUAL(join_string (parts.begin (), parts.end () ), "");
703 BOOST_CHECK_EQUAL(join_string (parts.begin (), parts.end (), "/"), "");
704 } // eo JoinStringSet
706 BOOST_AUTO_TEST_CASE(JoinStringIterSet_One)
708 std::set< std::string > parts;
710 parts.insert ("foo");
712 // cardinality == 1 → no delimiter
713 BOOST_CHECK_EQUAL(join_string (parts.begin (), parts.end () ), "foo");
714 BOOST_CHECK_EQUAL(join_string (parts.begin (), parts.end (), "/"), "foo");
715 } // eo JoinStringSet
717 BOOST_AUTO_TEST_CASE(JoinStringIterSet)
719 std::set< std::string > parts;
721 parts.insert ("foo");
722 parts.insert ("bar");
723 parts.insert ("baz");
725 std::string joined_string= join_string(parts.begin (), parts.end (), "/");
726 // we should have slashes between the strings:
727 BOOST_CHECK_EQUAL( std::string("bar/baz/foo") , joined_string );
729 parts.insert( std::string() );
730 joined_string= join_string(parts.begin (), parts.end (),"/");
731 // now we should have an additional trailing slash:
732 BOOST_CHECK_EQUAL( std::string("/bar/baz/foo") , joined_string );
733 } // eo JoinStringSet
736 BOOST_AUTO_TEST_CASE(JoinStringIterSet_Default)
737 { /* default delimiter is newline */
738 std::set< std::string > parts;
740 parts.insert ("foo");
741 parts.insert ("bar");
742 parts.insert ("baz");
744 BOOST_CHECK_EQUAL(join_string (parts.begin (), parts.end ()), "bar\nbaz\nfoo");
745 BOOST_CHECK_EQUAL(join_string (parts ), "bar\nbaz\nfoo");
747 } // eo JoinStringSet
750 BOOST_AUTO_TEST_CASE(JoinStringArray_Empty)
752 const char *const parts [] = { NULL };
754 BOOST_CHECK_EQUAL(join_string(&parts [0], &parts [0], "/"), "");
755 BOOST_CHECK_EQUAL(join_string(parts , "/"), "");
757 BOOST_CHECK_EQUAL(join_string(&parts [0], &parts [0]), "");
758 BOOST_CHECK_EQUAL(join_string(parts ), "");
760 } // eo JoinStringSet
762 BOOST_AUTO_TEST_CASE(JoinStringArray_One)
764 const char *const parts [] = { "one", NULL };
766 BOOST_CHECK_EQUAL(join_string(&parts [0], &parts [1], "/"), "one");
767 BOOST_CHECK_EQUAL(join_string(parts , "/"), "one");
769 BOOST_CHECK_EQUAL(join_string(&parts [0], &parts [1]), "one");
770 BOOST_CHECK_EQUAL(join_string(parts ), "one");
772 } // eo JoinStringSet
774 BOOST_AUTO_TEST_CASE(JoinStringArray_Many)
776 const char *const parts [5] = { "one", "two", "three", "many", NULL };
778 BOOST_CHECK_EQUAL(join_string(&parts [0], &parts [4], "/"), "one/two/three/many");
779 BOOST_CHECK_EQUAL(join_string(parts , "/"), "one/two/three/many");
781 BOOST_CHECK_EQUAL(join_string(&parts [0], &parts [4]), "one\ntwo\nthree\nmany");
782 BOOST_CHECK_EQUAL(join_string(parts ), "one\ntwo\nthree\nmany");
784 } // eo JoinStringSet
787 BOOST_AUTO_TEST_CASE(ConversionStringInt)
789 std::string s1("24");
790 std::string s1x("25x");
794 i1= string_to<int>(s1);
795 BOOST_CHECK_EQUAL( 24, i1 );
796 i1= string_to<int>(s1x);
797 BOOST_CHECK_EQUAL( 25, i1 );
799 res= string_to<int>(s1,i1);
800 BOOST_CHECK_EQUAL( true, res );
801 BOOST_CHECK_EQUAL( 24, i1 );
803 res= string_to<int>(s1x,i1);
804 BOOST_CHECK_EQUAL( false, res );
806 std::string ss1= to_string( 24 );
807 BOOST_CHECK_EQUAL( std::string("24"), ss1);
809 } // eo ConversionStringInt()
813 BOOST_AUTO_TEST_CASE(HexBinaryConversion)
815 std::string hex1("49324E");
816 std::string bin1("I2N");
818 BOOST_CHECK_EQUAL( hex1, convert_binary_to_hex(bin1,true) );
819 BOOST_CHECK_EQUAL( bin1, convert_hex_to_binary(hex1) );
820 BOOST_CHECK_EQUAL( to_lower(hex1), convert_binary_to_hex(bin1) );
822 std::string hex2("0001");
823 std::string hex2a("00 01");
824 std::string hex2b("00:01");
825 std::string bin2("\0\1",2);
827 BOOST_CHECK_EQUAL( hex2, convert_binary_to_hex(bin2) );
828 BOOST_CHECK_EQUAL( bin2, convert_hex_to_binary(hex2) );
829 BOOST_CHECK_EQUAL( bin2, convert_hex_to_binary(hex2a) );
830 BOOST_CHECK_EQUAL( bin2, convert_hex_to_binary(hex2b) );
832 BOOST_REQUIRE_THROW( convert_hex_to_binary("01 kein hex"), std::runtime_error);
833 } // eo HexConversion()
835 BOOST_AUTO_TEST_CASE(HexIntConversion)
837 BOOST_CHECK_EQUAL( 255 , hex_string_to<uint32_t>("ff") );
838 BOOST_CHECK_EQUAL( 18866985 , hex_string_to<uint32_t>("11FE329") );
839 BOOST_CHECK_EQUAL( 44 , hex_string_to<uint32_t>("0x2C") );
842 BOOST_AUTO_TEST_CASE(sanitize_for_logging1)
844 string output = sanitize_for_logging("normaler string ohne aerger");
846 BOOST_CHECK_EQUAL(string("normaler string ohne aerger"), output);
849 BOOST_AUTO_TEST_CASE(sanitize_for_logging2)
851 string to_test="fiese";
852 to_test.push_back(0);
855 string output = sanitize_for_logging(to_test);
857 BOOST_CHECK_EQUAL(string("fiese?null"), output);
860 BOOST_AUTO_TEST_CASE(sanitize_for_logging3)
862 string output = sanitize_for_logging("läuter ümlaute utf8");
864 BOOST_CHECK_EQUAL(string("l??uter ??mlaute utf8"), output);
867 BOOST_AUTO_TEST_CASE(find_html_comments_test)
869 string text = "bla-->" // ==> (npos, 6)
871 "bla<!--bla<!--bla-->bla-->" // ==> (16, 26), (9, 32)
873 "bla<!--bla-->" // ==> (35, 45)
875 "--><!----><!--" // ==> (npos, 48), (48, 55), (55, npos)
877 "bla<!--bla-->"; // ==> (62, 72)
879 BOOST_REQUIRE_EQUAL(text.length(), 72);
880 vector<CommentZone> expect;
881 expect.push_back(CommentZone(string::npos, 6));
882 expect.push_back(CommentZone(16, 26));
883 expect.push_back(CommentZone( 9, 32));
884 expect.push_back(CommentZone(35, 45));
885 expect.push_back(CommentZone(string::npos, 48));
886 expect.push_back(CommentZone(48, 55));
887 expect.push_back(CommentZone(55, string::npos));
888 expect.push_back(CommentZone(62, 72));
889 vector<CommentZone> result = find_html_comments(text);
890 //BOOST_CHECK_EQUAL_COLLECTIONS(result.begin(), result.end(), not working, requires ...
891 // expect.begin(), expect.end()); ... operator<<(CommentZone)
892 BOOST_CHECK_EQUAL(result.size(), expect.size());
893 BOOST_FOREACH(const CommentZone &comment, expect)
894 BOOST_CHECK_MESSAGE(find(result.begin(), result.end(), comment) != result.end(),
895 "Find (" << comment.first << "-" << comment.second << ")");
898 BOOST_AUTO_TEST_CASE(remove_html_comments_test)
900 const string original = "First line outside\n"
904 "foo bar, what a surprise.\n"
905 "<!-- short tag-less comment -->\n"
906 " Html cannot handle this, thinks that FOO ended above\n"
908 "This, neither. No nested comments\n"
909 "some text <!--BAZ more text\n"
911 "more text BAZ--> even more\n"
913 "second line outside\n"
914 "<!-- second comment -->";
915 string text = original;
916 string expect = "First line outside\n"
918 "second line outside\n";
919 remove_html_comments(text);
920 BOOST_CHECK_EQUAL(text, expect);
921 remove_html_comments(text); // should not have any effect
922 BOOST_CHECK_EQUAL(text, expect);
924 text = string("test<!--") + original;
925 remove_html_comments(text);
926 BOOST_CHECK_EQUAL(text, "test");
928 text = original + "-->test";
929 remove_html_comments(text);
930 BOOST_CHECK_EQUAL(text, "test");
933 BOOST_AUTO_TEST_CASE(shorten_stl_types_string)
935 BOOST_CHECK_EQUAL(shorten_stl_types("std::basic_string<char, std::char_traits<char>, std::allocator<char> >"),
939 BOOST_AUTO_TEST_CASE(shorten_stl_types_simple)
941 BOOST_CHECK_EQUAL(shorten_stl_types("std::list<some_type, std::allocator<some_type> >"),
942 "std::list<some_type, _alloc_>");
945 BOOST_AUTO_TEST_CASE(shorten_stl_types_multiple)
947 BOOST_CHECK_EQUAL(shorten_stl_types("std::basic_string<char, std::char_traits<char>, std::allocator<char> > my_func(std::list<some_type, std::allocator<some_type> >, std::vector<int, std::allocator<int> >)"),
948 "std::string my_func(std::list<some_type, _alloc_>, std::vector<int, _alloc_>)");
951 BOOST_AUTO_TEST_CASE(shorten_stl_types_complex)
953 BOOST_CHECK_EQUAL(shorten_stl_types("std::list<boost::shared_ptr<some_type>, std::allocator<boost::shared_ptr<some_type> > >"),
954 "std::list<boost::shared_ptr<some_type>, _alloc_>");
957 BOOST_AUTO_TEST_CASE(shorten_stl_types_nested)
959 //"std::list<int, std::allocator<int> >"
960 //"std::list<std::list<int, std::allocator<int> >, std::allocator<std::list<int, std::allocator<int> > > >"
961 BOOST_CHECK_EQUAL(shorten_stl_types("std::list<std::list<int, std::allocator<int> >, std::allocator<std::list<int, std::allocator<int> > > >"),
962 "std::list<std::list<int, _alloc_>, _alloc_>");
965 BOOST_AUTO_TEST_CASE(shorten_stl_types_nothing)
968 BOOST_CHECK_EQUAL(shorten_stl_types(text), text);
969 text = "int f(void)";
970 BOOST_CHECK_EQUAL(shorten_stl_types(text), text);
971 text = "std::cout << \"Test\" << std::endl;";
972 BOOST_CHECK_EQUAL(shorten_stl_types(text), text);
974 BOOST_CHECK_EQUAL(shorten_stl_types(text), text);
975 text = "std::list<> is a class template";
976 BOOST_CHECK_EQUAL(shorten_stl_types(text), text);
977 text = "std::list<int, std::allocator<int>\n>";
978 BOOST_CHECK_EQUAL(shorten_stl_types(text), text);
981 BOOST_AUTO_TEST_CASE(base64_encode_decode)
983 string text = "Hello World\n";
985 string encoded = base64_encode(text);
987 BOOST_CHECK_EQUAL("SGVsbG8gV29ybGQK", encoded);
988 BOOST_CHECK_EQUAL(text, base64_decode(encoded));
991 BOOST_AUTO_TEST_CASE(base64_empty_string)
994 string encoded = base64_encode(text);
996 BOOST_CHECK_EQUAL("", encoded);
997 BOOST_CHECK_EQUAL(text, base64_decode(encoded));
1000 BOOST_AUTO_TEST_CASE(base64_large_string_with_zero)
1003 int data_size = 1024 * 1024 * 10;
1005 string large_binary_data(data_size, 0);
1006 BOOST_CHECK_EQUAL(data_size, large_binary_data.size());
1008 string encoded = base64_encode(large_binary_data);
1010 string decoded = base64_decode(encoded);
1011 BOOST_CHECK_EQUAL(large_binary_data, decoded);
1014 BOOST_AUTO_TEST_CASE(base64_large_string_with_zero_encode_linefeeds)
1017 int data_size = 1024 * 1024 * 10;
1019 string large_binary_data(data_size, 0);
1020 BOOST_CHECK_EQUAL(data_size, large_binary_data.size());
1022 const bool one_line_mode = false;
1023 string encoded = base64_encode(large_binary_data, one_line_mode);
1025 string decoded = base64_decode(encoded, one_line_mode);
1026 BOOST_CHECK_EQUAL(large_binary_data, decoded);
1029 BOOST_AUTO_TEST_CASE(base64_decode_garbage)
1031 std::string data = "Hello World, this is unencoded data";
1032 string decoded = base64_decode(data);
1034 // garbage turns out to be an empty string
1035 BOOST_CHECK_EQUAL(0, decoded.size());
1038 BOOST_AUTO_TEST_CASE(base64_encode_with_linefeeds)
1040 const string data = string("Hello World\n")
1048 const string encoded = base64_encode(data, false);
1050 const std::string expected = string("SGVsbG8gV29ybGQKSGVsbG8gV29ybGQKSGVsbG8gV29ybGQKSGVsbG8gV29ybGQK\n")
1051 + "SGVsbG8gV29ybGQKSGVsbG8gV29ybGQKSGVsbG8gV29ybGQK\n";
1052 BOOST_CHECK_EQUAL(expected, encoded);
1054 // decode and compare
1055 BOOST_CHECK_EQUAL(data, base64_decode(encoded, false));
1057 // expected empty string when switching on single line base64 mode
1058 // (openssl is very strict about this)
1059 BOOST_CHECK_EQUAL("", base64_decode(encoded, true));
1062 BOOST_AUTO_TEST_SUITE_END()