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_entites1)
50 string output = smart_html_entities("Test");
52 BOOST_CHECK_EQUAL(string("Test"), output);
55 BOOST_AUTO_TEST_CASE(smart_html_entites2)
57 string output = smart_html_entities("Täst");
59 BOOST_CHECK_EQUAL(string("Täst"), output);
62 BOOST_AUTO_TEST_CASE(smart_html_entites3)
64 string output = smart_html_entities("<>");
66 BOOST_CHECK_EQUAL(string("<>"), output);
69 BOOST_AUTO_TEST_CASE(smart_html_entites4)
71 string output = smart_html_entities("<ümlaut>");
73 BOOST_CHECK_EQUAL(string("<ümlaut>"), output);
76 BOOST_AUTO_TEST_CASE(smart_html_entites5)
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_entites6)
85 string output = smart_html_entities("System > Einstellungen");
87 BOOST_CHECK_EQUAL(string("System > Einstellungen"), output);
90 BOOST_AUTO_TEST_CASE(smart_html_entites7)
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);
133 BOOST_AUTO_TEST_CASE(nice_unit_format1)
135 const int64_t two_bytes = 2;
137 string output = nice_unit_format(two_bytes, LongUnitFormat, UnitBase1000);
138 BOOST_CHECK_EQUAL(string("2.00 Bytes"), output);
140 output = nice_unit_format(two_bytes);
141 BOOST_CHECK_EQUAL(string("2.0 B"), output);
144 BOOST_AUTO_TEST_CASE(nice_unit_format2)
146 const int64_t two_kilobytes = 2000;
148 string output = nice_unit_format(two_kilobytes, LongUnitFormat, UnitBase1000);
149 BOOST_CHECK_EQUAL(string("2.00 KBytes"), output);
151 output = nice_unit_format(two_kilobytes);
152 BOOST_CHECK_EQUAL(string("2.0 KB"), output);
154 output = nice_unit_format(boost::numeric_cast<double>(two_kilobytes), LongUnitFormat, UnitBase1000);
155 BOOST_CHECK_EQUAL(string("2.00 KBytes"), output);
157 output = nice_unit_format(boost::numeric_cast<double>(two_kilobytes));
158 BOOST_CHECK_EQUAL(string("2.0 KB"), output);
160 const int64_t two_and_half_kilobytes = 2500;
162 output = nice_unit_format(two_and_half_kilobytes, LongUnitFormat, UnitBase1000);
163 BOOST_CHECK_EQUAL(string("2.50 KBytes"), output);
165 output = nice_unit_format(two_and_half_kilobytes);
166 BOOST_CHECK_EQUAL(string("2.4 KB"), output);
168 output = nice_unit_format(boost::numeric_cast<double>(two_and_half_kilobytes), LongUnitFormat, UnitBase1000);
169 BOOST_CHECK_EQUAL(string("2.50 KBytes"), output);
171 output = nice_unit_format(boost::numeric_cast<double>(two_and_half_kilobytes));
172 BOOST_CHECK_EQUAL(string("2.4 KB"), output);
175 BOOST_AUTO_TEST_CASE(nice_unit_format3)
177 const int64_t two_megabytes = 2000000;
179 string output = nice_unit_format(two_megabytes, LongUnitFormat, UnitBase1000);
180 BOOST_CHECK_EQUAL(string("2.00 MBytes"), output);
182 output = nice_unit_format(two_megabytes);
183 BOOST_CHECK_EQUAL(string("1.9 MB"), output);
185 output = nice_unit_format(boost::numeric_cast<double>(two_megabytes), LongUnitFormat, UnitBase1000);
186 BOOST_CHECK_EQUAL(string("2.00 MBytes"), output);
188 output = nice_unit_format(boost::numeric_cast<double>(two_megabytes));
189 BOOST_CHECK_EQUAL(string("1.9 MB"), output);
191 const int64_t two_and_half_megabytes = 2500000;
193 output = nice_unit_format(two_and_half_megabytes, LongUnitFormat, UnitBase1000);
194 BOOST_CHECK_EQUAL(string("2.50 MBytes"), output);
196 output = nice_unit_format(two_and_half_megabytes);
197 BOOST_CHECK_EQUAL(string("2.4 MB"), output);
199 output = nice_unit_format(boost::numeric_cast<double>(two_and_half_megabytes), LongUnitFormat, UnitBase1000);
200 BOOST_CHECK_EQUAL(string("2.50 MBytes"), output);
202 output = nice_unit_format(boost::numeric_cast<double>(two_and_half_megabytes));
203 BOOST_CHECK_EQUAL(string("2.4 MB"), output);
207 BOOST_AUTO_TEST_CASE(nice_unit_format4)
209 const int64_t two_gigabytes = 2000000000LL;
211 string output = nice_unit_format(two_gigabytes, LongUnitFormat, UnitBase1000);
212 BOOST_CHECK_EQUAL(string("2.00 GBytes"), output);
214 output = nice_unit_format(two_gigabytes);
215 BOOST_CHECK_EQUAL(string("1.9 GB"), output);
217 output = nice_unit_format(boost::numeric_cast<double>(two_gigabytes), LongUnitFormat, UnitBase1000);
218 BOOST_CHECK_EQUAL(string("2.00 GBytes"), output);
220 output = nice_unit_format(boost::numeric_cast<double>(two_gigabytes));
221 BOOST_CHECK_EQUAL(string("1.9 GB"), output);
223 const int64_t two_and_half_gigabytes = 2500000000LL;
225 output = nice_unit_format(two_and_half_gigabytes, LongUnitFormat, UnitBase1000);
226 BOOST_CHECK_EQUAL(string("2.50 GBytes"), output);
228 output = nice_unit_format(two_and_half_gigabytes);
229 BOOST_CHECK_EQUAL(string("2.3 GB"), output);
231 output = nice_unit_format(boost::numeric_cast<double>(two_and_half_gigabytes), LongUnitFormat, UnitBase1000);
232 BOOST_CHECK_EQUAL(string("2.50 GBytes"), output);
234 output = nice_unit_format(boost::numeric_cast<double>(two_and_half_gigabytes));
235 BOOST_CHECK_EQUAL(string("2.3 GB"), output);
238 BOOST_AUTO_TEST_CASE(nice_unit_format5)
240 const int64_t two_terabytes = 2000000000000LL;
242 string output = nice_unit_format(two_terabytes, LongUnitFormat, UnitBase1000);
243 BOOST_CHECK_EQUAL(string("2.00 TBytes"), output);
245 output = nice_unit_format(two_terabytes);
246 BOOST_CHECK_EQUAL(string("1.8 TB"), output);
248 output = nice_unit_format(boost::numeric_cast<double>(two_terabytes), LongUnitFormat, UnitBase1000);
249 BOOST_CHECK_EQUAL(string("2.00 TBytes"), output);
251 output = nice_unit_format(boost::numeric_cast<double>(two_terabytes));
252 BOOST_CHECK_EQUAL(string("1.8 TB"), output);
254 const int64_t two_and_half_terabytes = 2500000000000LL;
256 output = nice_unit_format(two_and_half_terabytes, LongUnitFormat, UnitBase1000);
257 BOOST_CHECK_EQUAL(string("2.50 TBytes"), output);
259 output = nice_unit_format(two_and_half_terabytes);
260 BOOST_CHECK_EQUAL(string("2.3 TB"), output);
262 output = nice_unit_format(boost::numeric_cast<double>(two_and_half_terabytes), LongUnitFormat, UnitBase1000);
263 BOOST_CHECK_EQUAL(string("2.50 TBytes"), output);
265 output = nice_unit_format(boost::numeric_cast<double>(two_and_half_terabytes));
266 BOOST_CHECK_EQUAL(string("2.3 TB"), output);
269 BOOST_AUTO_TEST_CASE(nice_unit_format6)
271 const int64_t two_petabytes = 2000000000000000LL;
273 string output = nice_unit_format(two_petabytes, LongUnitFormat, UnitBase1000);
274 BOOST_CHECK_EQUAL(string("2.00 PBytes"), output);
276 output = nice_unit_format(two_petabytes);
277 BOOST_CHECK_EQUAL(string("1.8 PB"), output);
279 output = nice_unit_format(boost::numeric_cast<double>(two_petabytes), LongUnitFormat, UnitBase1000);
280 BOOST_CHECK_EQUAL(string("2.00 PBytes"), output);
282 output = nice_unit_format(boost::numeric_cast<double>(two_petabytes));
283 BOOST_CHECK_EQUAL(string("1.8 PB"), output);
285 const int64_t two_and_half_petabytes = 2500000000000000LL;
287 output = nice_unit_format(two_and_half_petabytes, LongUnitFormat, UnitBase1000);
288 BOOST_CHECK_EQUAL(string("2.50 PBytes"), output);
290 output = nice_unit_format(two_and_half_petabytes);
291 BOOST_CHECK_EQUAL(string("2.2 PB"), output);
293 output = nice_unit_format(boost::numeric_cast<double>(two_and_half_petabytes), LongUnitFormat, UnitBase1000);
294 BOOST_CHECK_EQUAL(string("2.50 PBytes"), output);
296 output = nice_unit_format(boost::numeric_cast<double>(two_and_half_petabytes));
297 BOOST_CHECK_EQUAL(string("2.2 PB"), output);
300 BOOST_AUTO_TEST_CASE(nice_unit_format7)
302 const int64_t two_exabytes = 2000000000000000000LL;
304 string output = nice_unit_format(two_exabytes, LongUnitFormat, UnitBase1000);
305 BOOST_CHECK_EQUAL(string("2000.00 PBytes"), output);
307 output = nice_unit_format(two_exabytes);
308 BOOST_CHECK_EQUAL(string("1776.4 PB"), output);
310 output = nice_unit_format(boost::numeric_cast<double>(two_exabytes), LongUnitFormat, UnitBase1000);
311 BOOST_CHECK_EQUAL(string("2000.00 PBytes"), output);
313 output = nice_unit_format(boost::numeric_cast<double>(two_exabytes));
314 BOOST_CHECK_EQUAL(string("1776.4 PB"), output);
316 const int64_t two_and_half_exabytes = 2500000000000000000LL;
318 output = nice_unit_format(two_and_half_exabytes, LongUnitFormat, UnitBase1000);
319 BOOST_CHECK_EQUAL(string("2500.00 PBytes"), output);
321 output = nice_unit_format(two_and_half_exabytes);
322 BOOST_CHECK_EQUAL(string("2220.4 PB"), output);
324 output = nice_unit_format(boost::numeric_cast<double>(two_and_half_exabytes), LongUnitFormat, UnitBase1000);
325 BOOST_CHECK_EQUAL(string("2500.00 PBytes"), output);
327 output = nice_unit_format(boost::numeric_cast<double>(two_and_half_exabytes));
328 BOOST_CHECK_EQUAL(string("2220.4 PB"), output);
331 BOOST_AUTO_TEST_CASE(nice_unit_format8)
333 const int64_t max_representable_64bits_number = 9223372036854775807LL;
335 string output = nice_unit_format(max_representable_64bits_number, LongUnitFormat, UnitBase1000);
336 BOOST_CHECK_EQUAL(string("9223.40 PBytes"), output);
338 output = nice_unit_format(max_representable_64bits_number);
339 BOOST_CHECK_EQUAL(string("8192.0 PB"), output);
342 double val = boost::numeric_cast<double>(max_representable_64bits_number);
343 BOOST_MESSAGE("val as int64 is " << max_representable_64bits_number << " and as double is " << val);
344 BOOST_MESSAGE("val rounded is " << round(val));
345 BOOST_CHECK_EQUAL(val, round(val));
346 BOOST_MESSAGE("half as int64 is " << boost::numeric_cast<int64_t>(round(val/2))); // ok
347 BOOST_MESSAGE("cast back is " << boost::numeric_cast<int64_t>(round(val))); // throws a positive overflow
348 output = nice_unit_format(boost::numeric_cast<double>(max_representable_64bits_number), LongUnitFormat, UnitBase1000);
349 BOOST_CHECK_EQUAL(string("9223.40 PBytes"), output);
351 output = nice_unit_format(boost::numeric_cast<double>(max_representable_64bits_number));
352 BOOST_CHECK_EQUAL(string("8192.0 PB"), output);
356 BOOST_AUTO_TEST_CASE(TestTrim)
360 BOOST_CHECK_EQUAL( std::string("s1"), s );
364 BOOST_CHECK_EQUAL( std::string("s2"), s );
368 BOOST_CHECK_EQUAL( std::string("s3"), s );
372 BOOST_CHECK_EQUAL( std::string("s4:s4"), s);
374 /* non modifying version */
377 BOOST_CHECK_EQUAL( std::string("s1"), trim(s) );
380 BOOST_CHECK_EQUAL( std::string("s2"), trim(s) );
381 BOOST_CHECK_EQUAL( std::string(" s2"), s );
384 BOOST_CHECK_EQUAL( std::string("s3"), trim(s) );
385 BOOST_CHECK_EQUAL( std::string("s3 "), s );
388 BOOST_CHECK_EQUAL( std::string("s4:s4"), trim(s,":+-a") );
393 BOOST_AUTO_TEST_CASE(TestChomp)
398 BOOST_CHECK_EQUAL( std::string("s1"), s );
402 BOOST_CHECK_EQUAL( std::string("s2"), s );
406 BOOST_CHECK_EQUAL( std::string("s3"), s );
410 BOOST_CHECK_EQUAL( std::string(":s4:"), s);
412 /* non modifiying versions */
415 BOOST_CHECK_EQUAL( std::string("s1"), chomp(s) );
418 BOOST_CHECK_EQUAL( std::string("s2"), chomp(s) );
419 BOOST_CHECK_EQUAL( std::string("s2\n"), s);
422 BOOST_CHECK_EQUAL( std::string("s3"), chomp(s,":") );
423 BOOST_CHECK_EQUAL( std::string("s3:"), s);
426 BOOST_CHECK_EQUAL( std::string(":s4:"), chomp(s,"s:") );
427 BOOST_CHECK_EQUAL( std::string(":s4::"), s);
432 BOOST_AUTO_TEST_CASE(TestSuffixFuncs)
434 std::string s1("12.cpp");
436 BOOST_CHECK_EQUAL( true, has_suffix(s1,".cpp") );
437 BOOST_CHECK_EQUAL( true, has_suffix(s1,"pp") );
438 BOOST_CHECK_EQUAL( false, has_suffix(s1,"hpp") );
439 BOOST_CHECK_EQUAL( false, has_suffix(s1,"cp") );
440 BOOST_CHECK_EQUAL( false, has_suffix(s1,"") );
442 std::string s1c1= remove_suffix(s1,".cpp");
443 BOOST_CHECK_EQUAL( std::string("12"), s1c1 );
445 std::string s1c2= remove_suffix(s1,"p");
446 BOOST_CHECK_EQUAL( std::string("12.cp"), s1c2 );
448 std::string s1c3= remove_suffix(s1,"cp");
449 BOOST_CHECK_EQUAL( std::string("12.cpp"), s1c3 );
451 std::string s2(".cpp");
452 BOOST_CHECK_EQUAL( true, has_suffix(s2,".cpp") );
454 std::string s2c1= remove_suffix(s2,".cpp");
455 BOOST_CHECK_EQUAL( std::string(""), s2c1 );
457 } // eo TestSuffixFuncs()
461 BOOST_AUTO_TEST_CASE(TestPrefixFuncs)
463 std::string s1("12.cpp");
465 BOOST_CHECK_EQUAL( true, has_prefix(s1,"12") );
466 BOOST_CHECK_EQUAL( true, has_prefix(s1, "1") );
467 BOOST_CHECK_EQUAL( false, has_prefix(s1, "2") );
468 BOOST_CHECK_EQUAL( false, has_prefix(s1, "") );
470 std::string s1c1= remove_prefix(s1, "12");
471 BOOST_CHECK_EQUAL( std::string(".cpp"), s1c1);
472 } // eo TestPrefixFuncs()
476 BOOST_AUTO_TEST_CASE(TestLowerUpperFuncs)
478 std::string u1("CASE CONVERSION TEST..");
479 std::string l1("case conversion test..");
481 std::string test1(l1);
484 BOOST_CHECK_EQUAL( u1, test1 );
487 BOOST_CHECK_EQUAL( l1, test1 );
490 BOOST_CHECK_EQUAL( u1, to_upper(l1) );
491 BOOST_CHECK_EQUAL( l1, to_lower(u1) );
492 } // eo TestLowerUpper
496 BOOST_AUTO_TEST_CASE(PairSplit1)
499 get_push_back_filler(str_list)
500 ("a=11")("a= 11")("a =11 ")("a = 11 ")(" a = 11 ")
502 BOOST_CHECK_EQUAL( 5u, str_list.size() );
503 for(StringList::iterator it= str_list.begin();
504 it != str_list.end();
507 std::string key, value;
508 bool res= pair_split( *it, key, value);
510 BOOST_CHECK_EQUAL( true , res );
511 BOOST_CHECK_EQUAL( std::string("a"), key );
512 BOOST_CHECK_EQUAL( std::string("11"), value );
515 std::string key, value;
518 res= pair_split(" 1 : 2 ", key, value, ':');
519 BOOST_CHECK_EQUAL( true, res );
520 BOOST_CHECK_EQUAL( std::string("1"), key);
521 BOOST_CHECK_EQUAL( std::string("2"), value);
526 BOOST_AUTO_TEST_CASE(SplitString1)
538 split_string(block, list1, "\n");
539 // since the blocks ends with \n we should have 6 lines (the last one empty):
540 BOOST_CHECK_EQUAL( 6u, list1.size() );
541 BOOST_CHECK_EQUAL( std::string(), list1.back() );
542 BOOST_CHECK_EQUAL( std::string("Zeile 1"), list1.front() );
546 split_string(block, list2, "\n", true);
548 // now we omitted empty lines, now we should have only 4 lines left:
549 BOOST_CHECK_EQUAL( 4u, list2.size() );
550 BOOST_CHECK_EQUAL( std::string("Zeile 5"), list2.back() );
551 BOOST_CHECK_EQUAL( std::string("Zeile 1"), list2.front() );
553 list2= split_string(block, "\n", true, "+-");
555 // again, we omitted empty lines, but also trimmed away leading and trailing "+" and "-"
556 BOOST_CHECK_EQUAL( 4u, list2.size() );
557 BOOST_CHECK_EQUAL( std::string("Zeile 5"), list2.back() );
558 BOOST_CHECK_EQUAL( std::string("Zeile 1"), list2.front() );
559 BOOST_CHECK_EQUAL( std::string("Zeile-2"), *(++list2.begin()) );
564 BOOST_AUTO_TEST_CASE(SplitString2)
566 std::string line("172.16.0.0/16 dev eth0 scope link src 172.16.1.111");
570 split_string(line, list1, " ", true, Whitespaces);
572 BOOST_CHECK_EQUAL( 7u, list1.size() );
578 BOOST_AUTO_TEST_CASE(SplitStringEmpty)
580 std::string line("");
584 split_string(line, list1, " ", true, Whitespaces);
586 BOOST_CHECK_EQUAL( 0u, list1.size() );
587 } // eo SplitStringEmpty
590 BOOST_AUTO_TEST_CASE(SplitStringDelimiterOnly)
592 std::string line(" ");
596 split_string(line, list1, " ", true, Whitespaces);
598 BOOST_CHECK_EQUAL( 0u, list1.size() );
599 } // eo SplitStringDelimiterOnly
602 BOOST_AUTO_TEST_CASE(SplitToVector)
604 std::string line("0;1;2;3;4;5;6;7;8;9");
605 std::vector<std::string> result;
606 split_string(line, result, ";");
607 BOOST_REQUIRE_EQUAL(result.size(), 10);
608 BOOST_CHECK_EQUAL(result[0], "0");
609 BOOST_CHECK_EQUAL(result[4], "4");
610 BOOST_CHECK_EQUAL(result[9], "9");
614 BOOST_AUTO_TEST_CASE(JoinString1)
616 std::list< std::string > parts;
617 std::string joined_string= join_string(parts,"/");
618 BOOST_CHECK_EQUAL( std::string("") , joined_string );
620 parts.push_back ("1");
621 joined_string= join_string(parts,"/");
622 // we should have slashes between the strings:
623 BOOST_CHECK_EQUAL( std::string("1") , joined_string );
625 get_push_back_filler(parts)("2")("drei");
627 joined_string= join_string(parts,"/");
628 // we should have slashes between the strings:
629 BOOST_CHECK_EQUAL( std::string("1/2/drei") , joined_string );
631 parts.push_back( std::string() );
632 joined_string= join_string(parts,"/");
633 // now we should have an additional trailing slash:
634 BOOST_CHECK_EQUAL( std::string("1/2/drei/") , joined_string );
636 parts.push_front( std::string() );
637 joined_string= join_string(parts,"/");
638 // now we should have an additional leading slash:
639 BOOST_CHECK_EQUAL( std::string("/1/2/drei/") , joined_string );
644 BOOST_AUTO_TEST_CASE(JoinStringVector)
646 std::vector< std::string > parts;
647 get_push_back_filler(parts)("1")("2")("drei");
649 std::string joined_string= join_string(parts,"/");
650 // we should have slashes between the strings:
651 BOOST_CHECK_EQUAL( std::string("1/2/drei") , joined_string );
653 parts.push_back( std::string() );
654 joined_string= join_string(parts,"/");
655 // now we should have an additional trailing slash:
656 BOOST_CHECK_EQUAL( std::string("1/2/drei/") , joined_string );
658 parts.insert(parts.begin(), "");
659 joined_string= join_string(parts,"/");
660 // now we should have an additional leading slash:
661 BOOST_CHECK_EQUAL( std::string("/1/2/drei/") , joined_string );
663 } // eo JoinStringVector
666 BOOST_AUTO_TEST_CASE(JoinStringSet)
668 std::set< std::string > parts;
670 std::string joined_string= join_string(parts,"/");
671 BOOST_CHECK_EQUAL( std::string() , joined_string );
673 parts.insert ("foo");
674 joined_string= join_string(parts,"/");
675 BOOST_CHECK_EQUAL( std::string("foo") , joined_string );
677 parts.insert ("bar");
678 parts.insert ("baz");
680 joined_string= join_string(parts,"/");
681 // we should have slashes between the strings:
682 BOOST_CHECK_EQUAL( std::string("bar/baz/foo") , joined_string );
684 parts.insert( std::string() );
685 joined_string= join_string(parts,"/");
686 // now we should have an additional trailing slash:
687 BOOST_CHECK_EQUAL( std::string("/bar/baz/foo") , joined_string );
688 } // eo JoinStringSet
691 BOOST_AUTO_TEST_CASE(JoinStringIterSet_Empty)
693 std::set< std::string > parts;
695 // empty sequence → empty string
696 BOOST_CHECK_EQUAL(join_string (parts.begin (), parts.end () ), "");
697 BOOST_CHECK_EQUAL(join_string (parts.begin (), parts.end (), "/"), "");
698 } // eo JoinStringSet
700 BOOST_AUTO_TEST_CASE(JoinStringIterSet_One)
702 std::set< std::string > parts;
704 parts.insert ("foo");
706 // cardinality == 1 → no delimiter
707 BOOST_CHECK_EQUAL(join_string (parts.begin (), parts.end () ), "foo");
708 BOOST_CHECK_EQUAL(join_string (parts.begin (), parts.end (), "/"), "foo");
709 } // eo JoinStringSet
711 BOOST_AUTO_TEST_CASE(JoinStringIterSet)
713 std::set< std::string > parts;
715 parts.insert ("foo");
716 parts.insert ("bar");
717 parts.insert ("baz");
719 std::string joined_string= join_string(parts.begin (), parts.end (), "/");
720 // we should have slashes between the strings:
721 BOOST_CHECK_EQUAL( std::string("bar/baz/foo") , joined_string );
723 parts.insert( std::string() );
724 joined_string= join_string(parts.begin (), parts.end (),"/");
725 // now we should have an additional trailing slash:
726 BOOST_CHECK_EQUAL( std::string("/bar/baz/foo") , joined_string );
727 } // eo JoinStringSet
730 BOOST_AUTO_TEST_CASE(ConversionStringInt)
732 std::string s1("24");
733 std::string s1x("25x");
737 i1= string_to<int>(s1);
738 BOOST_CHECK_EQUAL( 24, i1 );
739 i1= string_to<int>(s1x);
740 BOOST_CHECK_EQUAL( 25, i1 );
742 res= string_to<int>(s1,i1);
743 BOOST_CHECK_EQUAL( true, res );
744 BOOST_CHECK_EQUAL( 24, i1 );
746 res= string_to<int>(s1x,i1);
747 BOOST_CHECK_EQUAL( false, res );
749 std::string ss1= to_string( 24 );
750 BOOST_CHECK_EQUAL( std::string("24"), ss1);
752 } // eo ConversionStringInt()
756 BOOST_AUTO_TEST_CASE(HexBinaryConversion)
758 std::string hex1("49324E");
759 std::string bin1("I2N");
761 BOOST_CHECK_EQUAL( hex1, convert_binary_to_hex(bin1,true) );
762 BOOST_CHECK_EQUAL( bin1, convert_hex_to_binary(hex1) );
763 BOOST_CHECK_EQUAL( to_lower(hex1), convert_binary_to_hex(bin1) );
765 std::string hex2("0001");
766 std::string hex2a("00 01");
767 std::string hex2b("00:01");
768 std::string bin2("\0\1",2);
770 BOOST_CHECK_EQUAL( hex2, convert_binary_to_hex(bin2) );
771 BOOST_CHECK_EQUAL( bin2, convert_hex_to_binary(hex2) );
772 BOOST_CHECK_EQUAL( bin2, convert_hex_to_binary(hex2a) );
773 BOOST_CHECK_EQUAL( bin2, convert_hex_to_binary(hex2b) );
775 BOOST_REQUIRE_THROW( convert_hex_to_binary("01 kein hex"), std::runtime_error);
776 } // eo HexConversion()
778 BOOST_AUTO_TEST_CASE(HexIntConversion)
780 BOOST_CHECK_EQUAL( 255 , hex_string_to<uint32_t>("ff") );
781 BOOST_CHECK_EQUAL( 18866985 , hex_string_to<uint32_t>("11FE329") );
782 BOOST_CHECK_EQUAL( 44 , hex_string_to<uint32_t>("0x2C") );
785 BOOST_AUTO_TEST_CASE(sanitize_for_logging1)
787 string output = sanitize_for_logging("normaler string ohne aerger");
789 BOOST_CHECK_EQUAL(string("normaler string ohne aerger"), output);
792 BOOST_AUTO_TEST_CASE(sanitize_for_logging2)
794 string to_test="fiese";
795 to_test.push_back(0);
798 string output = sanitize_for_logging(to_test);
800 BOOST_CHECK_EQUAL(string("fiese?null"), output);
803 BOOST_AUTO_TEST_CASE(sanitize_for_logging3)
805 string output = sanitize_for_logging("läuter ümlaute utf8");
807 BOOST_CHECK_EQUAL(string("l??uter ??mlaute utf8"), output);
810 BOOST_AUTO_TEST_CASE(find_html_comments_test)
812 string text = "bla-->" // ==> (npos, 6)
814 "bla<!--bla<!--bla-->bla-->" // ==> (16, 26), (9, 32)
816 "bla<!--bla-->" // ==> (35, 45)
818 "--><!----><!--" // ==> (npos, 48), (48, 55), (55, npos)
820 "bla<!--bla-->"; // ==> (62, 72)
822 BOOST_REQUIRE_EQUAL(text.length(), 72);
823 vector<CommentZone> expect;
824 expect.push_back(CommentZone(string::npos, 6));
825 expect.push_back(CommentZone(16, 26));
826 expect.push_back(CommentZone( 9, 32));
827 expect.push_back(CommentZone(35, 45));
828 expect.push_back(CommentZone(string::npos, 48));
829 expect.push_back(CommentZone(48, 55));
830 expect.push_back(CommentZone(55, string::npos));
831 expect.push_back(CommentZone(62, 72));
832 vector<CommentZone> result = find_html_comments(text);
833 //BOOST_CHECK_EQUAL_COLLECTIONS(result.begin(), result.end(), not working, requires ...
834 // expect.begin(), expect.end()); ... operator<<(CommentZone)
835 BOOST_CHECK_EQUAL(result.size(), expect.size());
836 BOOST_FOREACH(const CommentZone &comment, expect)
837 BOOST_CHECK_MESSAGE(find(result.begin(), result.end(), comment) != result.end(),
838 "Find (" << comment.first << "-" << comment.second << ")");
841 BOOST_AUTO_TEST_CASE(remove_html_comments_test)
843 const string original = "First line outside\n"
847 "foo bar, what a surprise.\n"
848 "<!-- short tag-less comment -->\n"
849 " Html cannot handle this, thinks that FOO ended above\n"
851 "This, neither. No nested comments\n"
852 "some text <!--BAZ more text\n"
854 "more text BAZ--> even more\n"
856 "second line outside\n"
857 "<!-- second comment -->";
858 string text = original;
859 string expect = "First line outside\n"
861 "second line outside\n";
862 remove_html_comments(text);
863 BOOST_CHECK_EQUAL(text, expect);
864 remove_html_comments(text); // should not have any effect
865 BOOST_CHECK_EQUAL(text, expect);
867 text = string("test<!--") + original;
868 remove_html_comments(text);
869 BOOST_CHECK_EQUAL(text, "test");
871 text = original + "-->test";
872 remove_html_comments(text);
873 BOOST_CHECK_EQUAL(text, "test");
876 BOOST_AUTO_TEST_CASE(shorten_stl_types_string)
878 BOOST_CHECK_EQUAL(shorten_stl_types("std::basic_string<char, std::char_traits<char>, std::allocator<char> >"),
882 BOOST_AUTO_TEST_CASE(shorten_stl_types_simple)
884 BOOST_CHECK_EQUAL(shorten_stl_types("std::list<some_type, std::allocator<some_type> >"),
885 "std::list<some_type, _alloc_>");
888 BOOST_AUTO_TEST_CASE(shorten_stl_types_multiple)
890 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> >)"),
891 "std::string my_func(std::list<some_type, _alloc_>, std::vector<int, _alloc_>)");
894 BOOST_AUTO_TEST_CASE(shorten_stl_types_complex)
896 BOOST_CHECK_EQUAL(shorten_stl_types("std::list<boost::shared_ptr<some_type>, std::allocator<boost::shared_ptr<some_type> > >"),
897 "std::list<boost::shared_ptr<some_type>, _alloc_>");
900 BOOST_AUTO_TEST_CASE(shorten_stl_types_nested)
902 //"std::list<int, std::allocator<int> >"
903 //"std::list<std::list<int, std::allocator<int> >, std::allocator<std::list<int, std::allocator<int> > > >"
904 BOOST_CHECK_EQUAL(shorten_stl_types("std::list<std::list<int, std::allocator<int> >, std::allocator<std::list<int, std::allocator<int> > > >"),
905 "std::list<std::list<int, _alloc_>, _alloc_>");
908 BOOST_AUTO_TEST_CASE(shorten_stl_types_nothing)
911 BOOST_CHECK_EQUAL(shorten_stl_types(text), text);
912 text = "int f(void)";
913 BOOST_CHECK_EQUAL(shorten_stl_types(text), text);
914 text = "std::cout << \"Test\" << std::endl;";
915 BOOST_CHECK_EQUAL(shorten_stl_types(text), text);
917 BOOST_CHECK_EQUAL(shorten_stl_types(text), text);
918 text = "std::list<> is a class template";
919 BOOST_CHECK_EQUAL(shorten_stl_types(text), text);
920 text = "std::list<int, std::allocator<int>\n>";
921 BOOST_CHECK_EQUAL(shorten_stl_types(text), text);
924 BOOST_AUTO_TEST_CASE(base64_encode_decode)
926 string text = "Hello World\n";
928 string encoded = base64_encode(text);
930 BOOST_CHECK_EQUAL("SGVsbG8gV29ybGQK", encoded);
931 BOOST_CHECK_EQUAL(text, base64_decode(encoded));
934 BOOST_AUTO_TEST_CASE(base64_empty_string)
937 string encoded = base64_encode(text);
939 BOOST_CHECK_EQUAL("", encoded);
940 BOOST_CHECK_EQUAL(text, base64_decode(encoded));
943 BOOST_AUTO_TEST_CASE(base64_large_string_with_zero)
946 int data_size = 1024 * 1024 * 10;
948 string large_binary_data(data_size, 0);
949 BOOST_CHECK_EQUAL(data_size, large_binary_data.size());
951 string encoded = base64_encode(large_binary_data);
953 string decoded = base64_decode(encoded);
954 BOOST_CHECK_EQUAL(large_binary_data, decoded);
957 BOOST_AUTO_TEST_CASE(base64_large_string_with_zero_encode_linefeeds)
960 int data_size = 1024 * 1024 * 10;
962 string large_binary_data(data_size, 0);
963 BOOST_CHECK_EQUAL(data_size, large_binary_data.size());
965 const bool one_line_mode = false;
966 string encoded = base64_encode(large_binary_data, one_line_mode);
968 string decoded = base64_decode(encoded, one_line_mode);
969 BOOST_CHECK_EQUAL(large_binary_data, decoded);
972 BOOST_AUTO_TEST_CASE(base64_decode_garbage)
974 std::string data = "Hello World, this is unencoded data";
975 string decoded = base64_decode(data);
977 // garbage turns out to be an empty string
978 BOOST_CHECK_EQUAL(0, decoded.size());
981 BOOST_AUTO_TEST_CASE(base64_encode_with_linefeeds)
983 const string data = string("Hello World\n")
991 const string encoded = base64_encode(data, false);
993 const std::string expected = string("SGVsbG8gV29ybGQKSGVsbG8gV29ybGQKSGVsbG8gV29ybGQKSGVsbG8gV29ybGQK\n")
994 + "SGVsbG8gV29ybGQKSGVsbG8gV29ybGQKSGVsbG8gV29ybGQK\n";
995 BOOST_CHECK_EQUAL(expected, encoded);
997 // decode and compare
998 BOOST_CHECK_EQUAL(data, base64_decode(encoded, false));
1000 // expected empty string when switching on single line base64 mode
1001 // (openssl is very strict about this)
1002 BOOST_CHECK_EQUAL("", base64_decode(encoded, true));
1005 BOOST_AUTO_TEST_SUITE_END()