1 /***************************************************************************
2 * Copyright (C) 2006 by Intra2net AG *
5 ***************************************************************************/
10 // #include <stdexcept>
12 #define BOOST_TEST_DYN_LINK
13 #include <boost/test/unit_test.hpp>
15 #include <stringfunc.hxx>
16 #include <containerfunc.hpp>
21 typedef std::list< std::string > StringList;
23 BOOST_AUTO_TEST_SUITE(stringfunc)
25 BOOST_AUTO_TEST_CASE(smart_html_entites1)
27 string output = smart_html_entities("Test");
29 BOOST_CHECK_EQUAL(string("Test"), output);
32 BOOST_AUTO_TEST_CASE(smart_html_entites2)
34 string output = smart_html_entities("Täst");
36 BOOST_CHECK_EQUAL(string("Täst"), output);
39 BOOST_AUTO_TEST_CASE(smart_html_entites3)
41 string output = smart_html_entities("<>");
43 BOOST_CHECK_EQUAL(string("<>"), output);
46 BOOST_AUTO_TEST_CASE(smart_html_entites4)
48 string output = smart_html_entities("<ümlaut>");
50 BOOST_CHECK_EQUAL(string("<ümlaut>"), output);
53 BOOST_AUTO_TEST_CASE(smart_html_entites5)
55 string output = smart_html_entities("Test<ümlaut>Blä");
57 BOOST_CHECK_EQUAL(string("Test<ümlaut>Blä"), output);
60 BOOST_AUTO_TEST_CASE(smart_html_entites6)
62 string output = smart_html_entities("System > Einstellungen");
64 BOOST_CHECK_EQUAL(string("System > Einstellungen"), output);
67 BOOST_AUTO_TEST_CASE(smart_html_entites7)
69 string output = smart_html_entities("Finden Sie <b>auf</b> der Seite <a href=\"fdslfsl\">\"System > Einstellungen\"</a>. Oder etwa nicht?");
71 BOOST_CHECK_EQUAL(string("Finden Sie <b>auf</b> der Seite <a href=\"fdslfsl\">"System > Einstellungen"</a>. Oder etwa nicht?"), output);
74 BOOST_AUTO_TEST_CASE(strip_html_tags1)
76 string output = strip_html_tags("Was für ein schöner Tag, finden Sie nicht?");
78 BOOST_CHECK_EQUAL(string("Was für ein schöner Tag, finden Sie nicht?"), output);
81 BOOST_AUTO_TEST_CASE(strip_html_tags2)
83 string output = strip_html_tags("Was für ein <a href=\"wikipedia\" target=\"new\">schöner Tag</a>, finden Sie nicht?");
85 BOOST_CHECK_EQUAL(string("Was für ein schöner Tag, finden Sie nicht?"), output);
90 BOOST_AUTO_TEST_CASE(html_entities1)
92 string output = html_entities("\xC3\xA4\xC3\xB6\xC3\xBC");
93 BOOST_CHECK_EQUAL(string("äöü"), output);
96 BOOST_AUTO_TEST_CASE(html_entities2)
98 string output = html_entities("\xC3\xA5 \xC3\xB5 \xC3\xBF");
99 BOOST_CHECK_EQUAL(string("å õ ÿ"), output);
102 BOOST_AUTO_TEST_CASE(html_entities3)
104 string output = html_entities("\xC4\x8E \xE0\xBC\xB1 \xE8\x82\x88");
105 BOOST_CHECK_EQUAL(string("Ď ༱ 肈"), output);
110 BOOST_AUTO_TEST_CASE(nice_unit_format1)
112 const int64_t two_bytes = 2;
114 string output = nice_unit_format(two_bytes);
115 BOOST_CHECK_EQUAL(string("2.00 Bytes"), output);
117 output = nice_unit_format(two_bytes, UnitBase1024, ShortUnitFormat);
118 BOOST_CHECK_EQUAL(string("2.0 B"), output);
121 BOOST_AUTO_TEST_CASE(nice_unit_format2)
123 const int64_t two_kilobytes = 2000;
125 string output = nice_unit_format(two_kilobytes);
126 BOOST_CHECK_EQUAL(string("2.00 KBytes"), output);
128 output = nice_unit_format(two_kilobytes, UnitBase1024, ShortUnitFormat);
129 BOOST_CHECK_EQUAL(string("2.0 KB"), output);
131 const int64_t two_and_half_kilobytes = 2500;
133 output = nice_unit_format(two_and_half_kilobytes);
134 BOOST_CHECK_EQUAL(string("2.50 KBytes"), output);
136 output = nice_unit_format(two_and_half_kilobytes, UnitBase1024, ShortUnitFormat);
137 BOOST_CHECK_EQUAL(string("2.4 KB"), output);
140 BOOST_AUTO_TEST_CASE(nice_unit_format3)
142 const int64_t two_megabytes = 2000000;
144 string output = nice_unit_format(two_megabytes);
145 BOOST_CHECK_EQUAL(string("2.00 MBytes"), output);
147 output = nice_unit_format(two_megabytes, UnitBase1024, ShortUnitFormat);
148 BOOST_CHECK_EQUAL(string("1.9 MB"), output);
150 const int64_t two_and_half_megabytes = 2500000;
152 output = nice_unit_format(two_and_half_megabytes);
153 BOOST_CHECK_EQUAL(string("2.50 MBytes"), output);
155 output = nice_unit_format(two_and_half_megabytes, UnitBase1024, ShortUnitFormat);
156 BOOST_CHECK_EQUAL(string("2.4 MB"), output);
160 BOOST_AUTO_TEST_CASE(nice_unit_format4)
162 const int64_t two_gigabytes = 2000000000LL;
164 string output = nice_unit_format(two_gigabytes);
165 BOOST_CHECK_EQUAL(string("2.00 GBytes"), output);
167 output = nice_unit_format(two_gigabytes, UnitBase1024, ShortUnitFormat);
168 BOOST_CHECK_EQUAL(string("1.9 GB"), output);
170 const int64_t two_and_half_gigabytes = 2500000000LL;
172 output = nice_unit_format(two_and_half_gigabytes);
173 BOOST_CHECK_EQUAL(string("2.50 GBytes"), output);
175 output = nice_unit_format(two_and_half_gigabytes, UnitBase1024, ShortUnitFormat);
176 BOOST_CHECK_EQUAL(string("2.3 GB"), output);
179 BOOST_AUTO_TEST_CASE(nice_unit_format5)
181 const int64_t two_terabytes = 2000000000000LL;
183 string output = nice_unit_format(two_terabytes);
184 BOOST_CHECK_EQUAL(string("2.00 TBytes"), output);
186 output = nice_unit_format(two_terabytes, UnitBase1024, ShortUnitFormat);
187 BOOST_CHECK_EQUAL(string("1.8 TB"), output);
189 const int64_t two_and_half_terabytes = 2500000000000LL;
191 output = nice_unit_format(two_and_half_terabytes);
192 BOOST_CHECK_EQUAL(string("2.50 TBytes"), output);
194 output = nice_unit_format(two_and_half_terabytes, UnitBase1024, ShortUnitFormat);
195 BOOST_CHECK_EQUAL(string("2.3 TB"), output);
198 BOOST_AUTO_TEST_CASE(nice_unit_format6)
200 const int64_t two_petabytes = 2000000000000000LL;
202 string output = nice_unit_format(two_petabytes);
203 BOOST_CHECK_EQUAL(string("2.00 PBytes"), output);
205 output = nice_unit_format(two_petabytes, UnitBase1024, ShortUnitFormat);
206 BOOST_CHECK_EQUAL(string("1.8 PB"), output);
208 const int64_t two_and_half_petabytes = 2500000000000000LL;
210 output = nice_unit_format(two_and_half_petabytes);
211 BOOST_CHECK_EQUAL(string("2.50 PBytes"), output);
213 output = nice_unit_format(two_and_half_petabytes, UnitBase1024, ShortUnitFormat);
214 BOOST_CHECK_EQUAL(string("2.2 PB"), output);
217 BOOST_AUTO_TEST_CASE(nice_unit_format7)
219 const int64_t two_exabytes = 2000000000000000000LL;
221 string output = nice_unit_format(two_exabytes);
222 BOOST_CHECK_EQUAL(string("2000.00 PBytes"), output);
224 output = nice_unit_format(two_exabytes, UnitBase1024, ShortUnitFormat);
225 BOOST_CHECK_EQUAL(string("1776.4 PB"), output);
227 const int64_t two_and_half_exabytes = 2500000000000000000LL;
229 output = nice_unit_format(two_and_half_exabytes);
230 BOOST_CHECK_EQUAL(string("2500.00 PBytes"), output);
232 output = nice_unit_format(two_and_half_exabytes, UnitBase1024, ShortUnitFormat);
233 BOOST_CHECK_EQUAL(string("2220.4 PB"), output);
236 BOOST_AUTO_TEST_CASE(nice_unit_format8)
238 const int64_t max_representable_64bits_number = 9223372036854775807LL;
240 string output = nice_unit_format(max_representable_64bits_number);
241 BOOST_CHECK_EQUAL(string("9223.40 PBytes"), output);
243 output = nice_unit_format(max_representable_64bits_number, UnitBase1024, ShortUnitFormat);
244 BOOST_CHECK_EQUAL(string("8192.0 PB"), output);
249 BOOST_AUTO_TEST_CASE(imaputf7_to_utf8)
251 string output = utf7imap_to_utf8("Sp&AOQ-m");
252 BOOST_CHECK_EQUAL(string("Späm"), output);
255 BOOST_AUTO_TEST_CASE(utf8_to_imaputf7)
257 string output = utf8_to_utf7imap("Späm");
258 BOOST_CHECK_EQUAL(string("Sp&AOQ-m"), output);
265 BOOST_AUTO_TEST_CASE(TestTrim)
269 BOOST_CHECK_EQUAL( std::string("s1"), s );
273 BOOST_CHECK_EQUAL( std::string("s2"), s );
277 BOOST_CHECK_EQUAL( std::string("s3"), s );
281 BOOST_CHECK_EQUAL( std::string("s4:s4"), s);
283 /* non modifying version */
286 BOOST_CHECK_EQUAL( std::string("s1"), trim(s) );
289 BOOST_CHECK_EQUAL( std::string("s2"), trim(s) );
290 BOOST_CHECK_EQUAL( std::string(" s2"), s );
293 BOOST_CHECK_EQUAL( std::string("s3"), trim(s) );
294 BOOST_CHECK_EQUAL( std::string("s3 "), s );
297 BOOST_CHECK_EQUAL( std::string("s4:s4"), trim(s,":+-a") );
302 BOOST_AUTO_TEST_CASE(TestChomp)
307 BOOST_CHECK_EQUAL( std::string("s1"), s );
311 BOOST_CHECK_EQUAL( std::string("s2"), s );
315 BOOST_CHECK_EQUAL( std::string("s3"), s );
319 BOOST_CHECK_EQUAL( std::string(":s4:"), s);
321 /* non modifiying versions */
324 BOOST_CHECK_EQUAL( std::string("s1"), chomp(s) );
327 BOOST_CHECK_EQUAL( std::string("s2"), chomp(s) );
328 BOOST_CHECK_EQUAL( std::string("s2\n"), s);
331 BOOST_CHECK_EQUAL( std::string("s3"), chomp(s,":") );
332 BOOST_CHECK_EQUAL( std::string("s3:"), s);
335 BOOST_CHECK_EQUAL( std::string(":s4:"), chomp(s,"s:") );
336 BOOST_CHECK_EQUAL( std::string(":s4::"), s);
341 BOOST_AUTO_TEST_CASE(TestSuffixFuncs)
343 std::string s1("12.cpp");
345 BOOST_CHECK_EQUAL( true, has_suffix(s1,".cpp") );
346 BOOST_CHECK_EQUAL( true, has_suffix(s1,"pp") );
347 BOOST_CHECK_EQUAL( false, has_suffix(s1,"hpp") );
348 BOOST_CHECK_EQUAL( false, has_suffix(s1,"cp") );
349 BOOST_CHECK_EQUAL( false, has_suffix(s1,"") );
351 std::string s1c1= remove_suffix(s1,".cpp");
352 BOOST_CHECK_EQUAL( std::string("12"), s1c1 );
354 std::string s1c2= remove_suffix(s1,"p");
355 BOOST_CHECK_EQUAL( std::string("12.cp"), s1c2 );
357 std::string s1c3= remove_suffix(s1,"cp");
358 BOOST_CHECK_EQUAL( std::string("12.cpp"), s1c3 );
360 std::string s2(".cpp");
361 BOOST_CHECK_EQUAL( true, has_suffix(s2,".cpp") );
363 std::string s2c1= remove_suffix(s2,".cpp");
364 BOOST_CHECK_EQUAL( std::string(""), s2c1 );
366 } // eo TestSuffixFuncs()
370 BOOST_AUTO_TEST_CASE(TestPrefixFuncs)
372 std::string s1("12.cpp");
374 BOOST_CHECK_EQUAL( true, has_prefix(s1,"12") );
375 BOOST_CHECK_EQUAL( true, has_prefix(s1, "1") );
376 BOOST_CHECK_EQUAL( false, has_prefix(s1, "2") );
377 BOOST_CHECK_EQUAL( false, has_prefix(s1, "") );
379 std::string s1c1= remove_prefix(s1, "12");
380 BOOST_CHECK_EQUAL( std::string(".cpp"), s1c1);
381 } // eo TestPrefixFuncs()
385 BOOST_AUTO_TEST_CASE(TestLowerUpperFuncs)
387 std::string u1("CASE CONVERSION TEST..");
388 std::string l1("case conversion test..");
390 std::string test1(l1);
393 BOOST_CHECK_EQUAL( u1, test1 );
396 BOOST_CHECK_EQUAL( l1, test1 );
399 BOOST_CHECK_EQUAL( u1, to_upper(l1) );
400 BOOST_CHECK_EQUAL( l1, to_lower(u1) );
401 } // eo TestLowerUpper
405 BOOST_AUTO_TEST_CASE(PairSplit1)
408 get_push_back_filler(str_list)
409 ("a=11")("a= 11")("a =11 ")("a = 11 ")(" a = 11 ")
411 BOOST_CHECK_EQUAL( 5u, str_list.size() );
412 for(StringList::iterator it= str_list.begin();
413 it != str_list.end();
416 std::string key, value;
417 bool res= pair_split( *it, key, value);
419 BOOST_CHECK_EQUAL( true , res );
420 BOOST_CHECK_EQUAL( std::string("a"), key );
421 BOOST_CHECK_EQUAL( std::string("11"), value );
424 std::string key, value;
427 res= pair_split(" 1 : 2 ", key, value, ':');
428 BOOST_CHECK_EQUAL( true, res );
429 BOOST_CHECK_EQUAL( std::string("1"), key);
430 BOOST_CHECK_EQUAL( std::string("2"), value);
435 BOOST_AUTO_TEST_CASE(SplitString1)
447 split_string(block, list1, "\n");
448 // since the blocks ends with \n we should have 6 lines (the last one empty):
449 BOOST_CHECK_EQUAL( 6u, list1.size() );
450 BOOST_CHECK_EQUAL( std::string(), list1.back() );
451 BOOST_CHECK_EQUAL( std::string("Zeile 1"), list1.front() );
455 split_string(block, list2, "\n", true);
457 // now we omitted empty lines, now we should have only 4 lines left:
458 BOOST_CHECK_EQUAL( 4u, list2.size() );
459 BOOST_CHECK_EQUAL( std::string("Zeile 5"), list2.back() );
460 BOOST_CHECK_EQUAL( std::string("Zeile 1"), list2.front() );
462 list2= split_string(block, "\n", true, "+-");
464 // again, we omitted empty lines, but also trimmed away leading and trailing "+" and "-"
465 BOOST_CHECK_EQUAL( 4u, list2.size() );
466 BOOST_CHECK_EQUAL( std::string("Zeile 5"), list2.back() );
467 BOOST_CHECK_EQUAL( std::string("Zeile 1"), list2.front() );
468 BOOST_CHECK_EQUAL( std::string("Zeile-2"), *(++list2.begin()) );
473 BOOST_AUTO_TEST_CASE(SplitString2)
475 std::string line("172.16.0.0/16 dev eth0 scope link src 172.16.1.111");
479 split_string(line, list1, " ", true, Whitespaces);
481 BOOST_CHECK_EQUAL( 7u, list1.size() );
487 BOOST_AUTO_TEST_CASE(SplitStringEmpty)
489 std::string line("");
493 split_string(line, list1, " ", true, Whitespaces);
495 BOOST_CHECK_EQUAL( 0u, list1.size() );
496 } // eo SplitStringEmpty
499 BOOST_AUTO_TEST_CASE(SplitStringDelimiterOnly)
501 std::string line(" ");
505 split_string(line, list1, " ", true, Whitespaces);
507 BOOST_CHECK_EQUAL( 0u, list1.size() );
508 } // eo SplitStringDelimiterOnly
512 BOOST_AUTO_TEST_CASE(JoinString1)
514 std::list< std::string > parts;
515 get_push_back_filler(parts)("1")("2")("drei");
517 std::string joined_string= join_string(parts,"/");
518 // we should have slashes between the strings:
519 BOOST_CHECK_EQUAL( std::string("1/2/drei") , joined_string );
521 parts.push_back( std::string() );
522 joined_string= join_string(parts,"/");
523 // now we should have an additional trailing slash:
524 BOOST_CHECK_EQUAL( std::string("1/2/drei/") , joined_string );
526 parts.push_front( std::string() );
527 joined_string= join_string(parts,"/");
528 // now we should have an additional leading slash:
529 BOOST_CHECK_EQUAL( std::string("/1/2/drei/") , joined_string );
535 BOOST_AUTO_TEST_CASE(ConversionStringInt)
537 std::string s1("24");
538 std::string s1x("25x");
542 i1= string_to<int>(s1);
543 BOOST_CHECK_EQUAL( 24, i1 );
544 i1= string_to<int>(s1x);
545 BOOST_CHECK_EQUAL( 25, i1 );
547 res= string_to<int>(s1,i1);
548 BOOST_CHECK_EQUAL( true, res );
549 BOOST_CHECK_EQUAL( 24, i1 );
551 res= string_to<int>(s1x,i1);
552 BOOST_CHECK_EQUAL( false, res );
554 std::string ss1= to_string( 24 );
555 BOOST_CHECK_EQUAL( std::string("24"), ss1);
557 } // eo ConversionStringInt()
561 BOOST_AUTO_TEST_CASE(HexConversion)
563 std::string hex1("49324E");
564 std::string bin1("I2N");
566 BOOST_CHECK_EQUAL( hex1, convert_binary_to_hex(bin1,true) );
567 BOOST_CHECK_EQUAL( bin1, convert_hex_to_binary(hex1) );
568 BOOST_CHECK_EQUAL( to_lower(hex1), convert_binary_to_hex(bin1) );
570 std::string hex2("0001");
571 std::string hex2a("00 01");
572 std::string hex2b("00:01");
573 std::string bin2("\0\1",2);
575 BOOST_CHECK_EQUAL( hex2, convert_binary_to_hex(bin2) );
576 BOOST_CHECK_EQUAL( bin2, convert_hex_to_binary(hex2) );
577 BOOST_CHECK_EQUAL( bin2, convert_hex_to_binary(hex2a) );
578 BOOST_CHECK_EQUAL( bin2, convert_hex_to_binary(hex2b) );
580 BOOST_REQUIRE_THROW( convert_hex_to_binary("01 kein hex"), std::runtime_error);
581 } // eo HexConversion()
583 BOOST_AUTO_TEST_SUITE_END()