+/*
+The software in this package is distributed under the GNU General
+Public License version 2 (with a special exception described below).
+
+A copy of GNU General Public License (GPL) is included in this distribution,
+in the file COPYING.GPL.
+
+As a special exception, if other files instantiate templates or use macros
+or inline functions from this file, or you compile this file and link it
+with other works to produce a work based on this file, this file
+does not by itself cause the resulting work to be covered
+by the GNU General Public License.
+
+However the source code for this file must still be made available
+in accordance with section (3) of the GNU General Public License.
+
+This exception does not invalidate any other reasons why a work based
+on this file might be covered by the GNU General Public License.
+*/
/***************************************************************************
- * Copyright (C) 2006 by Intra2net AG *
- * info@intra2net.com *
+ * Copyright (C) 2006-2011 by Intra2net AG *
* *
***************************************************************************/
// #include <iostream>
#include <string>
+#include <cmath>
// #include <sstream>
// #include <stdexcept>
-#include <cppunit/extensions/TestFactoryRegistry.h>
-#include <cppunit/ui/text/TestRunner.h>
-#include <cppunit/extensions/HelperMacros.h>
+#define BOOST_TEST_DYN_LINK
+#include <boost/test/unit_test.hpp>
+#include <boost/foreach.hpp>
+#include <boost/numeric/conversion/cast.hpp>
#include <stringfunc.hxx>
#include <containerfunc.hpp>
+#include <iostream>
+#include <stdint.h>
using namespace std;
-using namespace CppUnit;
using namespace I2n;
-class stringfunc : public TestFixture
-{
- CPPUNIT_TEST_SUITE(stringfunc);
-
- CPPUNIT_TEST(smart_html_entites1);
- CPPUNIT_TEST(smart_html_entites2);
- CPPUNIT_TEST(smart_html_entites3);
- CPPUNIT_TEST(smart_html_entites4);
- CPPUNIT_TEST(smart_html_entites5);
- CPPUNIT_TEST(smart_html_entites6);
- CPPUNIT_TEST(smart_html_entites7);
- CPPUNIT_TEST(strip_html_tags1);
- CPPUNIT_TEST(strip_html_tags2);
- CPPUNIT_TEST(html_entities1);
- CPPUNIT_TEST(html_entities2);
- CPPUNIT_TEST(html_entities3);
- CPPUNIT_TEST(imaputf7_to_utf8);
- CPPUNIT_TEST(utf8_to_imaputf7);
-
-
- CPPUNIT_TEST(TestTrim);
- CPPUNIT_TEST(TestChomp);
- CPPUNIT_TEST(TestSuffixFuncs);
- CPPUNIT_TEST(TestPrefixFuncs);
- CPPUNIT_TEST(TestLowerUpperFuncs);
-
- CPPUNIT_TEST(PairSplit1);
- CPPUNIT_TEST(SplitString1);
- CPPUNIT_TEST(SplitString2);
- CPPUNIT_TEST(SplitStringEmpty);
- CPPUNIT_TEST(SplitStringDelimiterOnly);
- CPPUNIT_TEST(JoinString1);
-
- CPPUNIT_TEST(ConversionStringInt);
-
- CPPUNIT_TEST(HexConversion);
-
-
- CPPUNIT_TEST_SUITE_END();
-
- protected:
- typedef std::list< std::string > StringList;
-
- public:
- void smart_html_entites1()
- {
- string output = smart_html_entities("Test");
-
- CPPUNIT_ASSERT_EQUAL(string("Test"), output);
- }
-
- void smart_html_entites2()
- {
- string output = smart_html_entities("Täst");
-
- CPPUNIT_ASSERT_EQUAL(string("Täst"), output);
- }
-
- void smart_html_entites3()
- {
- string output = smart_html_entities("<>");
-
- CPPUNIT_ASSERT_EQUAL(string("<>"), output);
- }
-
- void smart_html_entites4()
- {
- string output = smart_html_entities("<ümlaut>");
-
- CPPUNIT_ASSERT_EQUAL(string("<ümlaut>"), output);
- }
-
- void smart_html_entites5()
- {
- string output = smart_html_entities("Test<ümlaut>Blä");
-
- CPPUNIT_ASSERT_EQUAL(string("Test<ümlaut>Blä"), output);
- }
-
- void smart_html_entites6()
- {
- string output = smart_html_entities("System > Einstellungen");
-
- CPPUNIT_ASSERT_EQUAL(string("System > Einstellungen"), output);
- }
-
- void smart_html_entites7()
- {
- string output = smart_html_entities("Finden Sie <b>auf</b> der Seite <a href=\"fdslfsl\">\"System > Einstellungen\"</a>. Oder etwa nicht?");
-
- CPPUNIT_ASSERT_EQUAL(string("Finden Sie <b>auf</b> der Seite <a href=\"fdslfsl\">"System > Einstellungen"</a>. Oder etwa nicht?"), output);
- }
-
- void strip_html_tags1()
- {
- string output = strip_html_tags("Was für ein schöner Tag, finden Sie nicht?");
-
- CPPUNIT_ASSERT_EQUAL(string("Was für ein schöner Tag, finden Sie nicht?"), output);
- }
-
- void strip_html_tags2()
- {
- string output = strip_html_tags("Was für ein <a href=\"wikipedia\" target=\"new\">schöner Tag</a>, finden Sie nicht?");
-
- CPPUNIT_ASSERT_EQUAL(string("Was für ein schöner Tag, finden Sie nicht?"), output);
- }
-
- void html_entities1()
- {
- string output = html_entities("\xC3\xA4\xC3\xB6\xC3\xBC");
- CPPUNIT_ASSERT_EQUAL(string("äöü"), output);
- }
-
- void html_entities2()
- {
- string output = html_entities("\xC3\xA5 \xC3\xB5 \xC3\xBF");
- CPPUNIT_ASSERT_EQUAL(string("å õ ÿ"), output);
- }
-
- void html_entities3()
- {
- string output = html_entities("\xC4\x8E \xE0\xBC\xB1 \xE8\x82\x88");
- CPPUNIT_ASSERT_EQUAL(string("Ď ༱ 肈"), output);
- }
-
- void imaputf7_to_utf8()
- {
- string output = utf7imap_to_utf8("Sp&AOQ-m");
- CPPUNIT_ASSERT_EQUAL(string("Späm"), output);
- }
-
- void utf8_to_imaputf7()
- {
- string output = utf8_to_utf7imap("Späm");
- CPPUNIT_ASSERT_EQUAL(string("Sp&AOQ-m"), output);
- }
-
- /*
- **
- */
-
- void TestTrim()
- {
- std::string s("s1");
- trim_mod(s);
- CPPUNIT_ASSERT_EQUAL( std::string("s1"), s );
-
- s=" s2";
- trim_mod(s);
- CPPUNIT_ASSERT_EQUAL( std::string("s2"), s );
-
- s="s3 ";
- trim_mod(s);
- CPPUNIT_ASSERT_EQUAL( std::string("s3"), s );
-
- s="::s4:s4::++--aa";
- trim_mod(s,":+-a");
- CPPUNIT_ASSERT_EQUAL( std::string("s4:s4"), s);
-
- /* non modifying version */
-
- s= "s1";
- CPPUNIT_ASSERT_EQUAL( std::string("s1"), trim(s) );
-
- s=" s2";
- CPPUNIT_ASSERT_EQUAL( std::string("s2"), trim(s) );
- CPPUNIT_ASSERT_EQUAL( std::string(" s2"), s );
-
- s="s3 ";
- CPPUNIT_ASSERT_EQUAL( std::string("s3"), trim(s) );
- CPPUNIT_ASSERT_EQUAL( std::string("s3 "), s );
-
- s="::s4:s4::++--aa";
- CPPUNIT_ASSERT_EQUAL( std::string("s4:s4"), trim(s,":+-a") );
- } // eo TestTrim()
-
-
-
- void TestChomp()
- {
- std::string s("s1");
-
- chomp_mod(s);
- CPPUNIT_ASSERT_EQUAL( std::string("s1"), s );
-
- s="s2\n";
- chomp_mod(s);
- CPPUNIT_ASSERT_EQUAL( std::string("s2"), s );
-
- s="s3:";
- chomp_mod(s,":");
- CPPUNIT_ASSERT_EQUAL( std::string("s3"), s );
-
- s=":s4::";
- chomp_mod(s,"s:");
- CPPUNIT_ASSERT_EQUAL( std::string(":s4:"), s);
-
- /* non modifiying versions */
- s= "s1";
-
- CPPUNIT_ASSERT_EQUAL( std::string("s1"), chomp(s) );
-
- s="s2\n";
- CPPUNIT_ASSERT_EQUAL( std::string("s2"), chomp(s) );
- CPPUNIT_ASSERT_EQUAL( std::string("s2\n"), s);
-
- s="s3:";
- CPPUNIT_ASSERT_EQUAL( std::string("s3"), chomp(s,":") );
- CPPUNIT_ASSERT_EQUAL( std::string("s3:"), s);
-
- s=":s4::";
- CPPUNIT_ASSERT_EQUAL( std::string(":s4:"), chomp(s,"s:") );
- CPPUNIT_ASSERT_EQUAL( std::string(":s4::"), s);
- } // eo TestChomp()
-
-
-
- void TestSuffixFuncs()
- {
- std::string s1("12.cpp");
-
- CPPUNIT_ASSERT_EQUAL( true, has_suffix(s1,".cpp") );
- CPPUNIT_ASSERT_EQUAL( true, has_suffix(s1,"pp") );
- CPPUNIT_ASSERT_EQUAL( false, has_suffix(s1,"hpp") );
- CPPUNIT_ASSERT_EQUAL( false, has_suffix(s1,"cp") );
- CPPUNIT_ASSERT_EQUAL( false, has_suffix(s1,"") );
-
- std::string s1c1= remove_suffix(s1,".cpp");
- CPPUNIT_ASSERT_EQUAL( std::string("12"), s1c1 );
-
- std::string s1c2= remove_suffix(s1,"p");
- CPPUNIT_ASSERT_EQUAL( std::string("12.cp"), s1c2 );
-
- std::string s1c3= remove_suffix(s1,"cp");
- CPPUNIT_ASSERT_EQUAL( std::string("12.cpp"), s1c3 );
-
- std::string s2(".cpp");
- CPPUNIT_ASSERT_EQUAL( true, has_suffix(s2,".cpp") );
-
- std::string s2c1= remove_suffix(s2,".cpp");
- CPPUNIT_ASSERT_EQUAL( std::string(""), s2c1 );
-
- } // eo TestSuffixFuncs()
-
-
-
- void TestPrefixFuncs()
- {
- std::string s1("12.cpp");
-
- CPPUNIT_ASSERT_EQUAL( true, has_prefix(s1,"12") );
- CPPUNIT_ASSERT_EQUAL( true, has_prefix(s1, "1") );
- CPPUNIT_ASSERT_EQUAL( false, has_prefix(s1, "2") );
- CPPUNIT_ASSERT_EQUAL( false, has_prefix(s1, "") );
-
- std::string s1c1= remove_prefix(s1, "12");
- CPPUNIT_ASSERT_EQUAL( std::string(".cpp"), s1c1);
- } // eo TestPrefixFuncs()
-
-
-
- void TestLowerUpperFuncs()
- {
- std::string u1("CASE CONVERSION TEST..");
- std::string l1("case conversion test..");
-
- std::string test1(l1);
-
- to_upper_mod(test1);
- CPPUNIT_ASSERT_EQUAL( u1, test1 );
-
- to_lower_mod(test1);
- CPPUNIT_ASSERT_EQUAL( l1, test1 );
-
-
- CPPUNIT_ASSERT_EQUAL( u1, to_upper(l1) );
- CPPUNIT_ASSERT_EQUAL( l1, to_lower(u1) );
- } // eo TestLowerUpper
-
-
-
- void PairSplit1()
- {
- StringList str_list;
- get_push_back_filler(str_list)
- ("a=11")("a= 11")("a =11 ")("a = 11 ")(" a = 11 ")
- ;
- CPPUNIT_ASSERT_EQUAL( 5u, str_list.size() );
- for(StringList::iterator it= str_list.begin();
- it != str_list.end();
- ++it)
- {
- std::string key, value;
- bool res= pair_split( *it, key, value);
-
- CPPUNIT_ASSERT_EQUAL( true , res );
- CPPUNIT_ASSERT_EQUAL( std::string("a"), key );
- CPPUNIT_ASSERT_EQUAL( std::string("11"), value );
- }
-
- std::string key, value;
- bool res;
-
- res= pair_split(" 1 : 2 ", key, value, ':');
- CPPUNIT_ASSERT_EQUAL( true, res );
- CPPUNIT_ASSERT_EQUAL( std::string("1"), key);
- CPPUNIT_ASSERT_EQUAL( std::string("2"), value);
- } // eo PairSplit1
-
-
-
- void SplitString1()
- {
- std::string block(
- "Zeile 1\n"
- "++Zeile-2--\n"
- "Zeile 3\n"
- "\n"
- "Zeile 5\n"
- );
-
- StringList list1;
-
- split_string(block, list1, "\n");
- // since the blocks ends with \n we should have 6 lines (the last one empty):
- CPPUNIT_ASSERT_EQUAL( 6u, list1.size() );
- CPPUNIT_ASSERT_EQUAL( std::string(), list1.back() );
- CPPUNIT_ASSERT_EQUAL( std::string("Zeile 1"), list1.front() );
-
- StringList list2;
-
- split_string(block, list2, "\n", true);
-
- // now we omitted empty lines, now we should have only 4 lines left:
- CPPUNIT_ASSERT_EQUAL( 4u, list2.size() );
- CPPUNIT_ASSERT_EQUAL( std::string("Zeile 5"), list2.back() );
- CPPUNIT_ASSERT_EQUAL( std::string("Zeile 1"), list2.front() );
-
- list2= split_string(block, "\n", true, "+-");
-
- // again, we omitted empty lines, but also trimmed away leading and trailing "+" and "-"
- CPPUNIT_ASSERT_EQUAL( 4u, list2.size() );
- CPPUNIT_ASSERT_EQUAL( std::string("Zeile 5"), list2.back() );
- CPPUNIT_ASSERT_EQUAL( std::string("Zeile 1"), list2.front() );
- CPPUNIT_ASSERT_EQUAL( std::string("Zeile-2"), *(++list2.begin()) );
- } // eo SplitString1
-
-
-
- void SplitString2()
- {
- std::string line("172.16.0.0/16 dev eth0 scope link src 172.16.1.111");
-
- StringList list1;
-
- split_string(line, list1, " ", true, Whitespaces);
-
- CPPUNIT_ASSERT_EQUAL( 7u, list1.size() );
-
- } // eo SplitString2
-
-
-
- void SplitStringEmpty()
- {
- std::string line("");
-
- StringList list1;
-
- split_string(line, list1, " ", true, Whitespaces);
-
- CPPUNIT_ASSERT_EQUAL( 0u, list1.size() );
- } // eo SplitStringEmpty
-
-
- void SplitStringDelimiterOnly()
- {
- std::string line(" ");
-
- StringList list1;
-
- split_string(line, list1, " ", true, Whitespaces);
-
- CPPUNIT_ASSERT_EQUAL( 0u, list1.size() );
- } // eo SplitStringDelimiterOnly
-
-
-
- void JoinString1()
- {
- std::list< std::string > parts;
- get_push_back_filler(parts)("1")("2")("drei");
-
- std::string joined_string= join_string(parts,"/");
- // we should have slashes between the strings:
- CPPUNIT_ASSERT_EQUAL( std::string("1/2/drei") , joined_string );
-
- parts.push_back( std::string() );
- joined_string= join_string(parts,"/");
- // now we should have an additional trailing slash:
- CPPUNIT_ASSERT_EQUAL( std::string("1/2/drei/") , joined_string );
-
- parts.push_front( std::string() );
- joined_string= join_string(parts,"/");
- // now we should have an additional leading slash:
- CPPUNIT_ASSERT_EQUAL( std::string("/1/2/drei/") , joined_string );
-
- } // eo JoinString1
-
-
-
- void ConversionStringInt()
- {
- std::string s1("24");
- std::string s1x("25x");
- int i1=0;
- bool res= false;
-
- i1= string_to<int>(s1);
- CPPUNIT_ASSERT_EQUAL( 24, i1 );
- i1= string_to<int>(s1x);
- CPPUNIT_ASSERT_EQUAL( 25, i1 );
-
- res= string_to<int>(s1,i1);
- CPPUNIT_ASSERT_EQUAL( true, res );
- CPPUNIT_ASSERT_EQUAL( 24, i1 );
-
- res= string_to<int>(s1x,i1);
- CPPUNIT_ASSERT_EQUAL( false, res );
-
- std::string ss1= to_string( 24 );
- CPPUNIT_ASSERT_EQUAL( std::string("24"), ss1);
-
- } // eo ConversionStringInt()
-
-
-
- void HexConversion()
- {
- std::string hex1("49324E");
- std::string bin1("I2N");
-
- CPPUNIT_ASSERT_EQUAL( hex1, convert_binary_to_hex(bin1,true) );
- CPPUNIT_ASSERT_EQUAL( bin1, convert_hex_to_binary(hex1) );
- CPPUNIT_ASSERT_EQUAL( to_lower(hex1), convert_binary_to_hex(bin1) );
-
- std::string hex2("0001");
- std::string hex2a("00 01");
- std::string hex2b("00:01");
- std::string bin2("\0\1",2);
-
- CPPUNIT_ASSERT_EQUAL( hex2, convert_binary_to_hex(bin2) );
- CPPUNIT_ASSERT_EQUAL( bin2, convert_hex_to_binary(hex2) );
- CPPUNIT_ASSERT_EQUAL( bin2, convert_hex_to_binary(hex2a) );
- CPPUNIT_ASSERT_EQUAL( bin2, convert_hex_to_binary(hex2b) );
-
- CPPUNIT_ASSERT_THROW( convert_hex_to_binary("01 kein hex"), std::runtime_error);
- } // eo HexConversion()
-
-
-
-};
-
-CPPUNIT_TEST_SUITE_REGISTRATION(stringfunc);
+typedef std::list< std::string > StringList;
+
+BOOST_AUTO_TEST_SUITE(stringfunc)
+
+BOOST_AUTO_TEST_CASE(smart_html_entites1)
+{
+ string output = smart_html_entities("Test");
+
+ BOOST_CHECK_EQUAL(string("Test"), output);
+}
+
+BOOST_AUTO_TEST_CASE(smart_html_entites2)
+{
+ string output = smart_html_entities("Täst");
+
+ BOOST_CHECK_EQUAL(string("Täst"), output);
+}
+
+BOOST_AUTO_TEST_CASE(smart_html_entites3)
+{
+ string output = smart_html_entities("<>");
+
+ BOOST_CHECK_EQUAL(string("<>"), output);
+}
+
+BOOST_AUTO_TEST_CASE(smart_html_entites4)
+{
+ string output = smart_html_entities("<ümlaut>");
+
+ BOOST_CHECK_EQUAL(string("<ümlaut>"), output);
+}
+
+BOOST_AUTO_TEST_CASE(smart_html_entites5)
+{
+ string output = smart_html_entities("Test<ümlaut>Blä");
+
+ BOOST_CHECK_EQUAL(string("Test<ümlaut>Blä"), output);
+}
+
+BOOST_AUTO_TEST_CASE(smart_html_entites6)
+{
+ string output = smart_html_entities("System > Einstellungen");
+
+ BOOST_CHECK_EQUAL(string("System > Einstellungen"), output);
+}
+
+BOOST_AUTO_TEST_CASE(smart_html_entites7)
+{
+ string output = smart_html_entities("Finden Sie <b>auf</b> der Seite <a href=\"fdslfsl\">\"System > Einstellungen\"</a>. Oder etwa nicht?");
+
+ BOOST_CHECK_EQUAL(string("Finden Sie <b>auf</b> der Seite <a href=\"fdslfsl\">"System > Einstellungen"</a>. Oder etwa nicht?"), output);
+}
+
+BOOST_AUTO_TEST_CASE(strip_html_tags1)
+{
+ string output = strip_html_tags("Was für ein schöner Tag, finden Sie nicht?");
+
+ BOOST_CHECK_EQUAL(string("Was für ein schöner Tag, finden Sie nicht?"), output);
+}
+
+BOOST_AUTO_TEST_CASE(strip_html_tags2)
+{
+ string output = strip_html_tags("Was für ein <a href=\"wikipedia\" target=\"new\">schöner Tag</a>, finden Sie nicht?");
+
+ BOOST_CHECK_EQUAL(string("Was für ein schöner Tag, finden Sie nicht?"), output);
+}
+
+
+
+BOOST_AUTO_TEST_CASE(html_entities1)
+{
+ string output = html_entities("\xC3\xA4\xC3\xB6\xC3\xBC");
+ BOOST_CHECK_EQUAL(string("äöü"), output);
+}
+
+BOOST_AUTO_TEST_CASE(html_entities2)
+{
+ string output = html_entities("\xC3\xA5 \xC3\xB5 \xC3\xBF");
+ BOOST_CHECK_EQUAL(string("å õ ÿ"), output);
+}
+
+BOOST_AUTO_TEST_CASE(html_entities3)
+{
+ string output = html_entities("\xC4\x8E \xE0\xBC\xB1 \xE8\x82\x88");
+ BOOST_CHECK_EQUAL(string("Ď ༱ 肈"), output);
+}
+
+BOOST_AUTO_TEST_CASE(entities_latin1)
+{
+ BOOST_CHECK_EQUAL((std::string)"noop", html_entities_iso ("noop"));
+ BOOST_CHECK_EQUAL((std::string)"täst", html_entities_iso ("t\xe4st"));
+ BOOST_CHECK_EQUAL((std::string)"TÄST", html_entities_iso ("T\xc4ST"));
+}
+
+
+BOOST_AUTO_TEST_CASE(nice_unit_format1)
+{
+ const int64_t two_bytes = 2;
+
+ string output = nice_unit_format(two_bytes, LongUnitFormat, UnitBase1000);
+ BOOST_CHECK_EQUAL(string("2.00 Bytes"), output);
+
+ output = nice_unit_format(two_bytes);
+ BOOST_CHECK_EQUAL(string("2.0 B"), output);
+}
+
+BOOST_AUTO_TEST_CASE(nice_unit_format2)
+{
+ const int64_t two_kilobytes = 2000;
+
+ string output = nice_unit_format(two_kilobytes, LongUnitFormat, UnitBase1000);
+ BOOST_CHECK_EQUAL(string("2.00 KBytes"), output);
+
+ output = nice_unit_format(two_kilobytes);
+ BOOST_CHECK_EQUAL(string("2.0 KB"), output);
+
+ output = nice_unit_format(boost::numeric_cast<double>(two_kilobytes), LongUnitFormat, UnitBase1000);
+ BOOST_CHECK_EQUAL(string("2.00 KBytes"), output);
+
+ output = nice_unit_format(boost::numeric_cast<double>(two_kilobytes));
+ BOOST_CHECK_EQUAL(string("2.0 KB"), output);
+
+ const int64_t two_and_half_kilobytes = 2500;
+
+ output = nice_unit_format(two_and_half_kilobytes, LongUnitFormat, UnitBase1000);
+ BOOST_CHECK_EQUAL(string("2.50 KBytes"), output);
+
+ output = nice_unit_format(two_and_half_kilobytes);
+ BOOST_CHECK_EQUAL(string("2.4 KB"), output);
+
+ output = nice_unit_format(boost::numeric_cast<double>(two_and_half_kilobytes), LongUnitFormat, UnitBase1000);
+ BOOST_CHECK_EQUAL(string("2.50 KBytes"), output);
+
+ output = nice_unit_format(boost::numeric_cast<double>(two_and_half_kilobytes));
+ BOOST_CHECK_EQUAL(string("2.4 KB"), output);
+}
+
+BOOST_AUTO_TEST_CASE(nice_unit_format3)
+{
+ const int64_t two_megabytes = 2000000;
+
+ string output = nice_unit_format(two_megabytes, LongUnitFormat, UnitBase1000);
+ BOOST_CHECK_EQUAL(string("2.00 MBytes"), output);
+
+ output = nice_unit_format(two_megabytes);
+ BOOST_CHECK_EQUAL(string("1.9 MB"), output);
+
+ output = nice_unit_format(boost::numeric_cast<double>(two_megabytes), LongUnitFormat, UnitBase1000);
+ BOOST_CHECK_EQUAL(string("2.00 MBytes"), output);
+
+ output = nice_unit_format(boost::numeric_cast<double>(two_megabytes));
+ BOOST_CHECK_EQUAL(string("1.9 MB"), output);
+
+ const int64_t two_and_half_megabytes = 2500000;
+
+ output = nice_unit_format(two_and_half_megabytes, LongUnitFormat, UnitBase1000);
+ BOOST_CHECK_EQUAL(string("2.50 MBytes"), output);
+
+ output = nice_unit_format(two_and_half_megabytes);
+ BOOST_CHECK_EQUAL(string("2.4 MB"), output);
+
+ output = nice_unit_format(boost::numeric_cast<double>(two_and_half_megabytes), LongUnitFormat, UnitBase1000);
+ BOOST_CHECK_EQUAL(string("2.50 MBytes"), output);
+
+ output = nice_unit_format(boost::numeric_cast<double>(two_and_half_megabytes));
+ BOOST_CHECK_EQUAL(string("2.4 MB"), output);
+
+}
+
+BOOST_AUTO_TEST_CASE(nice_unit_format4)
+{
+ const int64_t two_gigabytes = 2000000000LL;
+
+ string output = nice_unit_format(two_gigabytes, LongUnitFormat, UnitBase1000);
+ BOOST_CHECK_EQUAL(string("2.00 GBytes"), output);
+
+ output = nice_unit_format(two_gigabytes);
+ BOOST_CHECK_EQUAL(string("1.9 GB"), output);
+
+ output = nice_unit_format(boost::numeric_cast<double>(two_gigabytes), LongUnitFormat, UnitBase1000);
+ BOOST_CHECK_EQUAL(string("2.00 GBytes"), output);
+
+ output = nice_unit_format(boost::numeric_cast<double>(two_gigabytes));
+ BOOST_CHECK_EQUAL(string("1.9 GB"), output);
+
+ const int64_t two_and_half_gigabytes = 2500000000LL;
+
+ output = nice_unit_format(two_and_half_gigabytes, LongUnitFormat, UnitBase1000);
+ BOOST_CHECK_EQUAL(string("2.50 GBytes"), output);
+
+ output = nice_unit_format(two_and_half_gigabytes);
+ BOOST_CHECK_EQUAL(string("2.3 GB"), output);
+
+ output = nice_unit_format(boost::numeric_cast<double>(two_and_half_gigabytes), LongUnitFormat, UnitBase1000);
+ BOOST_CHECK_EQUAL(string("2.50 GBytes"), output);
+
+ output = nice_unit_format(boost::numeric_cast<double>(two_and_half_gigabytes));
+ BOOST_CHECK_EQUAL(string("2.3 GB"), output);
+}
+
+BOOST_AUTO_TEST_CASE(nice_unit_format5)
+{
+ const int64_t two_terabytes = 2000000000000LL;
+
+ string output = nice_unit_format(two_terabytes, LongUnitFormat, UnitBase1000);
+ BOOST_CHECK_EQUAL(string("2.00 TBytes"), output);
+
+ output = nice_unit_format(two_terabytes);
+ BOOST_CHECK_EQUAL(string("1.8 TB"), output);
+
+ output = nice_unit_format(boost::numeric_cast<double>(two_terabytes), LongUnitFormat, UnitBase1000);
+ BOOST_CHECK_EQUAL(string("2.00 TBytes"), output);
+
+ output = nice_unit_format(boost::numeric_cast<double>(two_terabytes));
+ BOOST_CHECK_EQUAL(string("1.8 TB"), output);
+
+ const int64_t two_and_half_terabytes = 2500000000000LL;
+
+ output = nice_unit_format(two_and_half_terabytes, LongUnitFormat, UnitBase1000);
+ BOOST_CHECK_EQUAL(string("2.50 TBytes"), output);
+
+ output = nice_unit_format(two_and_half_terabytes);
+ BOOST_CHECK_EQUAL(string("2.3 TB"), output);
+
+ output = nice_unit_format(boost::numeric_cast<double>(two_and_half_terabytes), LongUnitFormat, UnitBase1000);
+ BOOST_CHECK_EQUAL(string("2.50 TBytes"), output);
+
+ output = nice_unit_format(boost::numeric_cast<double>(two_and_half_terabytes));
+ BOOST_CHECK_EQUAL(string("2.3 TB"), output);
+}
+
+BOOST_AUTO_TEST_CASE(nice_unit_format6)
+{
+ const int64_t two_petabytes = 2000000000000000LL;
+
+ string output = nice_unit_format(two_petabytes, LongUnitFormat, UnitBase1000);
+ BOOST_CHECK_EQUAL(string("2.00 PBytes"), output);
+
+ output = nice_unit_format(two_petabytes);
+ BOOST_CHECK_EQUAL(string("1.8 PB"), output);
+
+ output = nice_unit_format(boost::numeric_cast<double>(two_petabytes), LongUnitFormat, UnitBase1000);
+ BOOST_CHECK_EQUAL(string("2.00 PBytes"), output);
+
+ output = nice_unit_format(boost::numeric_cast<double>(two_petabytes));
+ BOOST_CHECK_EQUAL(string("1.8 PB"), output);
+
+ const int64_t two_and_half_petabytes = 2500000000000000LL;
+
+ output = nice_unit_format(two_and_half_petabytes, LongUnitFormat, UnitBase1000);
+ BOOST_CHECK_EQUAL(string("2.50 PBytes"), output);
+
+ output = nice_unit_format(two_and_half_petabytes);
+ BOOST_CHECK_EQUAL(string("2.2 PB"), output);
+
+ output = nice_unit_format(boost::numeric_cast<double>(two_and_half_petabytes), LongUnitFormat, UnitBase1000);
+ BOOST_CHECK_EQUAL(string("2.50 PBytes"), output);
+
+ output = nice_unit_format(boost::numeric_cast<double>(two_and_half_petabytes));
+ BOOST_CHECK_EQUAL(string("2.2 PB"), output);
+}
+
+BOOST_AUTO_TEST_CASE(nice_unit_format7)
+{
+ const int64_t two_exabytes = 2000000000000000000LL;
+
+ string output = nice_unit_format(two_exabytes, LongUnitFormat, UnitBase1000);
+ BOOST_CHECK_EQUAL(string("2000.00 PBytes"), output);
+
+ output = nice_unit_format(two_exabytes);
+ BOOST_CHECK_EQUAL(string("1776.4 PB"), output);
+
+ output = nice_unit_format(boost::numeric_cast<double>(two_exabytes), LongUnitFormat, UnitBase1000);
+ BOOST_CHECK_EQUAL(string("2000.00 PBytes"), output);
+
+ output = nice_unit_format(boost::numeric_cast<double>(two_exabytes));
+ BOOST_CHECK_EQUAL(string("1776.4 PB"), output);
+
+ const int64_t two_and_half_exabytes = 2500000000000000000LL;
+
+ output = nice_unit_format(two_and_half_exabytes, LongUnitFormat, UnitBase1000);
+ BOOST_CHECK_EQUAL(string("2500.00 PBytes"), output);
+
+ output = nice_unit_format(two_and_half_exabytes);
+ BOOST_CHECK_EQUAL(string("2220.4 PB"), output);
+
+ output = nice_unit_format(boost::numeric_cast<double>(two_and_half_exabytes), LongUnitFormat, UnitBase1000);
+ BOOST_CHECK_EQUAL(string("2500.00 PBytes"), output);
+
+ output = nice_unit_format(boost::numeric_cast<double>(two_and_half_exabytes));
+ BOOST_CHECK_EQUAL(string("2220.4 PB"), output);
+}
+
+BOOST_AUTO_TEST_CASE(nice_unit_format8)
+{
+ const int64_t max_representable_64bits_number = 9223372036854775807LL;
+
+ string output = nice_unit_format(max_representable_64bits_number, LongUnitFormat, UnitBase1000);
+ BOOST_CHECK_EQUAL(string("9223.40 PBytes"), output);
+
+ output = nice_unit_format(max_representable_64bits_number);
+ BOOST_CHECK_EQUAL(string("8192.0 PB"), output);
+
+ /*
+ double val = boost::numeric_cast<double>(max_representable_64bits_number);
+ BOOST_MESSAGE("val as int64 is " << max_representable_64bits_number << " and as double is " << val);
+ BOOST_MESSAGE("val rounded is " << round(val));
+ BOOST_CHECK_EQUAL(val, round(val));
+ BOOST_MESSAGE("half as int64 is " << boost::numeric_cast<int64_t>(round(val/2))); // ok
+ BOOST_MESSAGE("cast back is " << boost::numeric_cast<int64_t>(round(val))); // throws a positive overflow
+ output = nice_unit_format(boost::numeric_cast<double>(max_representable_64bits_number), LongUnitFormat, UnitBase1000);
+ BOOST_CHECK_EQUAL(string("9223.40 PBytes"), output);
+
+ output = nice_unit_format(boost::numeric_cast<double>(max_representable_64bits_number));
+ BOOST_CHECK_EQUAL(string("8192.0 PB"), output);
+ */
+}
+
+BOOST_AUTO_TEST_CASE(TestTrim)
+{
+ std::string s("s1");
+ trim_mod(s);
+ BOOST_CHECK_EQUAL( std::string("s1"), s );
+
+ s=" s2";
+ trim_mod(s);
+ BOOST_CHECK_EQUAL( std::string("s2"), s );
+
+ s="s3 ";
+ trim_mod(s);
+ BOOST_CHECK_EQUAL( std::string("s3"), s );
+
+ s="::s4:s4::++--aa";
+ trim_mod(s,":+-a");
+ BOOST_CHECK_EQUAL( std::string("s4:s4"), s);
+
+ /* non modifying version */
+
+ s= "s1";
+ BOOST_CHECK_EQUAL( std::string("s1"), trim(s) );
+
+ s=" s2";
+ BOOST_CHECK_EQUAL( std::string("s2"), trim(s) );
+ BOOST_CHECK_EQUAL( std::string(" s2"), s );
+
+ s="s3 ";
+ BOOST_CHECK_EQUAL( std::string("s3"), trim(s) );
+ BOOST_CHECK_EQUAL( std::string("s3 "), s );
+
+ s="::s4:s4::++--aa";
+ BOOST_CHECK_EQUAL( std::string("s4:s4"), trim(s,":+-a") );
+} // eo TestTrim()
+
+
+
+BOOST_AUTO_TEST_CASE(TestChomp)
+{
+ std::string s("s1");
+
+ chomp_mod(s);
+ BOOST_CHECK_EQUAL( std::string("s1"), s );
+
+ s="s2\n";
+ chomp_mod(s);
+ BOOST_CHECK_EQUAL( std::string("s2"), s );
+
+ s="s3:";
+ chomp_mod(s,":");
+ BOOST_CHECK_EQUAL( std::string("s3"), s );
+
+ s=":s4::";
+ chomp_mod(s,"s:");
+ BOOST_CHECK_EQUAL( std::string(":s4:"), s);
+
+ /* non modifiying versions */
+ s= "s1";
+
+ BOOST_CHECK_EQUAL( std::string("s1"), chomp(s) );
+
+ s="s2\n";
+ BOOST_CHECK_EQUAL( std::string("s2"), chomp(s) );
+ BOOST_CHECK_EQUAL( std::string("s2\n"), s);
+
+ s="s3:";
+ BOOST_CHECK_EQUAL( std::string("s3"), chomp(s,":") );
+ BOOST_CHECK_EQUAL( std::string("s3:"), s);
+
+ s=":s4::";
+ BOOST_CHECK_EQUAL( std::string(":s4:"), chomp(s,"s:") );
+ BOOST_CHECK_EQUAL( std::string(":s4::"), s);
+} // eo TestChomp()
+
+
+
+BOOST_AUTO_TEST_CASE(TestSuffixFuncs)
+{
+ std::string s1("12.cpp");
+
+ BOOST_CHECK_EQUAL( true, has_suffix(s1,".cpp") );
+ BOOST_CHECK_EQUAL( true, has_suffix(s1,"pp") );
+ BOOST_CHECK_EQUAL( false, has_suffix(s1,"hpp") );
+ BOOST_CHECK_EQUAL( false, has_suffix(s1,"cp") );
+ BOOST_CHECK_EQUAL( false, has_suffix(s1,"") );
+
+ std::string s1c1= remove_suffix(s1,".cpp");
+ BOOST_CHECK_EQUAL( std::string("12"), s1c1 );
+
+ std::string s1c2= remove_suffix(s1,"p");
+ BOOST_CHECK_EQUAL( std::string("12.cp"), s1c2 );
+
+ std::string s1c3= remove_suffix(s1,"cp");
+ BOOST_CHECK_EQUAL( std::string("12.cpp"), s1c3 );
+
+ std::string s2(".cpp");
+ BOOST_CHECK_EQUAL( true, has_suffix(s2,".cpp") );
+
+ std::string s2c1= remove_suffix(s2,".cpp");
+ BOOST_CHECK_EQUAL( std::string(""), s2c1 );
+
+} // eo TestSuffixFuncs()
+
+
+
+BOOST_AUTO_TEST_CASE(TestPrefixFuncs)
+{
+ std::string s1("12.cpp");
+
+ BOOST_CHECK_EQUAL( true, has_prefix(s1,"12") );
+ BOOST_CHECK_EQUAL( true, has_prefix(s1, "1") );
+ BOOST_CHECK_EQUAL( false, has_prefix(s1, "2") );
+ BOOST_CHECK_EQUAL( false, has_prefix(s1, "") );
+
+ std::string s1c1= remove_prefix(s1, "12");
+ BOOST_CHECK_EQUAL( std::string(".cpp"), s1c1);
+} // eo TestPrefixFuncs()
+
+
+
+BOOST_AUTO_TEST_CASE(TestLowerUpperFuncs)
+{
+ std::string u1("CASE CONVERSION TEST..");
+ std::string l1("case conversion test..");
+
+ std::string test1(l1);
+
+ to_upper_mod(test1);
+ BOOST_CHECK_EQUAL( u1, test1 );
+
+ to_lower_mod(test1);
+ BOOST_CHECK_EQUAL( l1, test1 );
+
+
+ BOOST_CHECK_EQUAL( u1, to_upper(l1) );
+ BOOST_CHECK_EQUAL( l1, to_lower(u1) );
+} // eo TestLowerUpper
+
+
+
+BOOST_AUTO_TEST_CASE(PairSplit1)
+{
+ StringList str_list;
+ get_push_back_filler(str_list)
+ ("a=11")("a= 11")("a =11 ")("a = 11 ")(" a = 11 ")
+ ;
+ BOOST_CHECK_EQUAL( 5u, str_list.size() );
+ for(StringList::iterator it= str_list.begin();
+ it != str_list.end();
+ ++it)
+ {
+ std::string key, value;
+ bool res= pair_split( *it, key, value);
+
+ BOOST_CHECK_EQUAL( true , res );
+ BOOST_CHECK_EQUAL( std::string("a"), key );
+ BOOST_CHECK_EQUAL( std::string("11"), value );
+ }
+
+ std::string key, value;
+ bool res;
+
+ res= pair_split(" 1 : 2 ", key, value, ':');
+ BOOST_CHECK_EQUAL( true, res );
+ BOOST_CHECK_EQUAL( std::string("1"), key);
+ BOOST_CHECK_EQUAL( std::string("2"), value);
+} // eo PairSplit1
+
+
+
+BOOST_AUTO_TEST_CASE(SplitString1)
+{
+ std::string block(
+ "Zeile 1\n"
+ "++Zeile-2--\n"
+ "Zeile 3\n"
+ "\n"
+ "Zeile 5\n"
+ );
+
+ StringList list1;
+
+ split_string(block, list1, "\n");
+ // since the blocks ends with \n we should have 6 lines (the last one empty):
+ BOOST_CHECK_EQUAL( 6u, list1.size() );
+ BOOST_CHECK_EQUAL( std::string(), list1.back() );
+ BOOST_CHECK_EQUAL( std::string("Zeile 1"), list1.front() );
+
+ StringList list2;
+
+ split_string(block, list2, "\n", true);
+
+ // now we omitted empty lines, now we should have only 4 lines left:
+ BOOST_CHECK_EQUAL( 4u, list2.size() );
+ BOOST_CHECK_EQUAL( std::string("Zeile 5"), list2.back() );
+ BOOST_CHECK_EQUAL( std::string("Zeile 1"), list2.front() );
+
+ list2= split_string(block, "\n", true, "+-");
+
+ // again, we omitted empty lines, but also trimmed away leading and trailing "+" and "-"
+ BOOST_CHECK_EQUAL( 4u, list2.size() );
+ BOOST_CHECK_EQUAL( std::string("Zeile 5"), list2.back() );
+ BOOST_CHECK_EQUAL( std::string("Zeile 1"), list2.front() );
+ BOOST_CHECK_EQUAL( std::string("Zeile-2"), *(++list2.begin()) );
+} // eo SplitString1
+
+
+
+BOOST_AUTO_TEST_CASE(SplitString2)
+{
+ std::string line("172.16.0.0/16 dev eth0 scope link src 172.16.1.111");
+
+ StringList list1;
+
+ split_string(line, list1, " ", true, Whitespaces);
+
+ BOOST_CHECK_EQUAL( 7u, list1.size() );
+
+} // eo SplitString2
+
+
+
+BOOST_AUTO_TEST_CASE(SplitStringEmpty)
+{
+ std::string line("");
+
+ StringList list1;
+
+ split_string(line, list1, " ", true, Whitespaces);
+
+ BOOST_CHECK_EQUAL( 0u, list1.size() );
+} // eo SplitStringEmpty
+
+
+BOOST_AUTO_TEST_CASE(SplitStringDelimiterOnly)
+{
+ std::string line(" ");
+
+ StringList list1;
+
+ split_string(line, list1, " ", true, Whitespaces);
+
+ BOOST_CHECK_EQUAL( 0u, list1.size() );
+} // eo SplitStringDelimiterOnly
+
+
+BOOST_AUTO_TEST_CASE(SplitToVector)
+{
+ std::string line("0;1;2;3;4;5;6;7;8;9");
+ std::vector<std::string> result;
+ split_string(line, result, ";");
+ BOOST_REQUIRE_EQUAL(result.size(), 10);
+ BOOST_CHECK_EQUAL(result[0], "0");
+ BOOST_CHECK_EQUAL(result[4], "4");
+ BOOST_CHECK_EQUAL(result[9], "9");
+}
+
+
+BOOST_AUTO_TEST_CASE(JoinString1)
+{
+ std::list< std::string > parts;
+ std::string joined_string= join_string(parts,"/");
+ BOOST_CHECK_EQUAL( std::string("") , joined_string );
+
+ parts.push_back ("1");
+ joined_string= join_string(parts,"/");
+ // we should have slashes between the strings:
+ BOOST_CHECK_EQUAL( std::string("1") , joined_string );
+
+ get_push_back_filler(parts)("2")("drei");
+
+ joined_string= join_string(parts,"/");
+ // we should have slashes between the strings:
+ BOOST_CHECK_EQUAL( std::string("1/2/drei") , joined_string );
+
+ parts.push_back( std::string() );
+ joined_string= join_string(parts,"/");
+ // now we should have an additional trailing slash:
+ BOOST_CHECK_EQUAL( std::string("1/2/drei/") , joined_string );
+
+ parts.push_front( std::string() );
+ joined_string= join_string(parts,"/");
+ // now we should have an additional leading slash:
+ BOOST_CHECK_EQUAL( std::string("/1/2/drei/") , joined_string );
+
+} // eo JoinString1
+
+
+BOOST_AUTO_TEST_CASE(JoinStringVector)
+{
+ std::vector< std::string > parts;
+ get_push_back_filler(parts)("1")("2")("drei");
+
+ std::string joined_string= join_string(parts,"/");
+ // we should have slashes between the strings:
+ BOOST_CHECK_EQUAL( std::string("1/2/drei") , joined_string );
+
+ parts.push_back( std::string() );
+ joined_string= join_string(parts,"/");
+ // now we should have an additional trailing slash:
+ BOOST_CHECK_EQUAL( std::string("1/2/drei/") , joined_string );
+
+ parts.insert(parts.begin(), "");
+ joined_string= join_string(parts,"/");
+ // now we should have an additional leading slash:
+ BOOST_CHECK_EQUAL( std::string("/1/2/drei/") , joined_string );
+
+} // eo JoinStringVector
+
+
+BOOST_AUTO_TEST_CASE(JoinStringSet)
+{
+ std::set< std::string > parts;
+
+ std::string joined_string= join_string(parts,"/");
+ BOOST_CHECK_EQUAL( std::string() , joined_string );
+
+ parts.insert ("foo");
+ joined_string= join_string(parts,"/");
+ BOOST_CHECK_EQUAL( std::string("foo") , joined_string );
+
+ parts.insert ("bar");
+ parts.insert ("baz");
+
+ joined_string= join_string(parts,"/");
+ // we should have slashes between the strings:
+ BOOST_CHECK_EQUAL( std::string("bar/baz/foo") , joined_string );
+
+ parts.insert( std::string() );
+ joined_string= join_string(parts,"/");
+ // now we should have an additional trailing slash:
+ BOOST_CHECK_EQUAL( std::string("/bar/baz/foo") , joined_string );
+} // eo JoinStringSet
+
+
+BOOST_AUTO_TEST_CASE(JoinStringIterSet_Empty)
+{
+ std::set< std::string > parts;
+
+ // empty sequence → empty string
+ BOOST_CHECK_EQUAL(join_string (parts.begin (), parts.end () ), "");
+ BOOST_CHECK_EQUAL(join_string (parts.begin (), parts.end (), "/"), "");
+} // eo JoinStringSet
+
+BOOST_AUTO_TEST_CASE(JoinStringIterSet_One)
+{
+ std::set< std::string > parts;
+
+ parts.insert ("foo");
+
+ // cardinality == 1 → no delimiter
+ BOOST_CHECK_EQUAL(join_string (parts.begin (), parts.end () ), "foo");
+ BOOST_CHECK_EQUAL(join_string (parts.begin (), parts.end (), "/"), "foo");
+} // eo JoinStringSet
+
+BOOST_AUTO_TEST_CASE(JoinStringIterSet)
+{
+ std::set< std::string > parts;
+
+ parts.insert ("foo");
+ parts.insert ("bar");
+ parts.insert ("baz");
+
+ std::string joined_string= join_string(parts.begin (), parts.end (), "/");
+ // we should have slashes between the strings:
+ BOOST_CHECK_EQUAL( std::string("bar/baz/foo") , joined_string );
+
+ parts.insert( std::string() );
+ joined_string= join_string(parts.begin (), parts.end (),"/");
+ // now we should have an additional trailing slash:
+ BOOST_CHECK_EQUAL( std::string("/bar/baz/foo") , joined_string );
+} // eo JoinStringSet
+
+
+BOOST_AUTO_TEST_CASE(JoinStringIterSet_Default)
+{ /* default delimiter is newline */
+ std::set< std::string > parts;
+
+ parts.insert ("foo");
+ parts.insert ("bar");
+ parts.insert ("baz");
+
+ BOOST_CHECK_EQUAL(join_string (parts.begin (), parts.end ()), "bar\nbaz\nfoo");
+ BOOST_CHECK_EQUAL(join_string (parts ), "bar\nbaz\nfoo");
+
+} // eo JoinStringSet
+
+
+BOOST_AUTO_TEST_CASE(JoinStringArray_Empty)
+{
+ const char *const parts [] = { NULL };
+
+ BOOST_CHECK_EQUAL(join_string(&parts [0], &parts [0], "/"), "");
+ BOOST_CHECK_EQUAL(join_string(parts , "/"), "");
+
+ BOOST_CHECK_EQUAL(join_string(&parts [0], &parts [0]), "");
+ BOOST_CHECK_EQUAL(join_string(parts ), "");
+
+} // eo JoinStringSet
+
+BOOST_AUTO_TEST_CASE(JoinStringArray_One)
+{
+ const char *const parts [] = { "one", NULL };
+
+ BOOST_CHECK_EQUAL(join_string(&parts [0], &parts [1], "/"), "one");
+ BOOST_CHECK_EQUAL(join_string(parts , "/"), "one");
+
+ BOOST_CHECK_EQUAL(join_string(&parts [0], &parts [1]), "one");
+ BOOST_CHECK_EQUAL(join_string(parts ), "one");
+
+} // eo JoinStringSet
+
+BOOST_AUTO_TEST_CASE(JoinStringArray_Many)
+{
+ const char *const parts [5] = { "one", "two", "three", "many", NULL };
+
+ BOOST_CHECK_EQUAL(join_string(&parts [0], &parts [4], "/"), "one/two/three/many");
+ BOOST_CHECK_EQUAL(join_string(parts , "/"), "one/two/three/many");
+
+ BOOST_CHECK_EQUAL(join_string(&parts [0], &parts [4]), "one\ntwo\nthree\nmany");
+ BOOST_CHECK_EQUAL(join_string(parts ), "one\ntwo\nthree\nmany");
+
+} // eo JoinStringSet
+
+
+BOOST_AUTO_TEST_CASE(ConversionStringInt)
+{
+ std::string s1("24");
+ std::string s1x("25x");
+ int i1=0;
+ bool res= false;
+
+ i1= string_to<int>(s1);
+ BOOST_CHECK_EQUAL( 24, i1 );
+ i1= string_to<int>(s1x);
+ BOOST_CHECK_EQUAL( 25, i1 );
+
+ res= string_to<int>(s1,i1);
+ BOOST_CHECK_EQUAL( true, res );
+ BOOST_CHECK_EQUAL( 24, i1 );
+
+ res= string_to<int>(s1x,i1);
+ BOOST_CHECK_EQUAL( false, res );
+
+ std::string ss1= to_string( 24 );
+ BOOST_CHECK_EQUAL( std::string("24"), ss1);
+
+} // eo ConversionStringInt()
+
+
+
+BOOST_AUTO_TEST_CASE(HexBinaryConversion)
+{
+ std::string hex1("49324E");
+ std::string bin1("I2N");
+
+ BOOST_CHECK_EQUAL( hex1, convert_binary_to_hex(bin1,true) );
+ BOOST_CHECK_EQUAL( bin1, convert_hex_to_binary(hex1) );
+ BOOST_CHECK_EQUAL( to_lower(hex1), convert_binary_to_hex(bin1) );
+
+ std::string hex2("0001");
+ std::string hex2a("00 01");
+ std::string hex2b("00:01");
+ std::string bin2("\0\1",2);
+
+ BOOST_CHECK_EQUAL( hex2, convert_binary_to_hex(bin2) );
+ BOOST_CHECK_EQUAL( bin2, convert_hex_to_binary(hex2) );
+ BOOST_CHECK_EQUAL( bin2, convert_hex_to_binary(hex2a) );
+ BOOST_CHECK_EQUAL( bin2, convert_hex_to_binary(hex2b) );
+
+ BOOST_REQUIRE_THROW( convert_hex_to_binary("01 kein hex"), std::runtime_error);
+} // eo HexConversion()
+
+BOOST_AUTO_TEST_CASE(HexIntConversion)
+{
+ BOOST_CHECK_EQUAL( 255 , hex_string_to<uint32_t>("ff") );
+ BOOST_CHECK_EQUAL( 18866985 , hex_string_to<uint32_t>("11FE329") );
+ BOOST_CHECK_EQUAL( 44 , hex_string_to<uint32_t>("0x2C") );
+}
+
+BOOST_AUTO_TEST_CASE(sanitize_for_logging1)
+{
+ string output = sanitize_for_logging("normaler string ohne aerger");
+
+ BOOST_CHECK_EQUAL(string("normaler string ohne aerger"), output);
+}
+
+BOOST_AUTO_TEST_CASE(sanitize_for_logging2)
+{
+ string to_test="fiese";
+ to_test.push_back(0);
+ to_test+="null";
+
+ string output = sanitize_for_logging(to_test);
+
+ BOOST_CHECK_EQUAL(string("fiese?null"), output);
+}
+
+BOOST_AUTO_TEST_CASE(sanitize_for_logging3)
+{
+ string output = sanitize_for_logging("läuter ümlaute utf8");
+
+ BOOST_CHECK_EQUAL(string("l??uter ??mlaute utf8"), output);
+}
+
+BOOST_AUTO_TEST_CASE(find_html_comments_test)
+{
+ string text = "bla-->" // ==> (npos, 6)
+ // 0 6
+ "bla<!--bla<!--bla-->bla-->" // ==> (16, 26), (9, 32)
+ // 6 9 16 26 32
+ "bla<!--bla-->" // ==> (35, 45)
+ // 32 35 45
+ "--><!----><!--" // ==> (npos, 48), (48, 55), (55, npos)
+ // 45 48 55 59
+ "bla<!--bla-->"; // ==> (62, 72)
+ // 59 62 72
+ BOOST_REQUIRE_EQUAL(text.length(), 72);
+ vector<CommentZone> expect;
+ expect.push_back(CommentZone(string::npos, 6));
+ expect.push_back(CommentZone(16, 26));
+ expect.push_back(CommentZone( 9, 32));
+ expect.push_back(CommentZone(35, 45));
+ expect.push_back(CommentZone(string::npos, 48));
+ expect.push_back(CommentZone(48, 55));
+ expect.push_back(CommentZone(55, string::npos));
+ expect.push_back(CommentZone(62, 72));
+ vector<CommentZone> result = find_html_comments(text);
+ //BOOST_CHECK_EQUAL_COLLECTIONS(result.begin(), result.end(), not working, requires ...
+ // expect.begin(), expect.end()); ... operator<<(CommentZone)
+ BOOST_CHECK_EQUAL(result.size(), expect.size());
+ BOOST_FOREACH(const CommentZone &comment, expect)
+ BOOST_CHECK_MESSAGE(find(result.begin(), result.end(), comment) != result.end(),
+ "Find (" << comment.first << "-" << comment.second << ")");
+}
+
+BOOST_AUTO_TEST_CASE(remove_html_comments_test)
+{
+ const string original = "First line outside\n"
+ "text <!--FOO\n"
+ "Inside foo\n"
+ "<!--BAR\n"
+ "foo bar, what a surprise.\n"
+ "<!-- short tag-less comment -->\n"
+ " Html cannot handle this, thinks that FOO ended above\n"
+ "BAR-->\n"
+ "This, neither. No nested comments\n"
+ "some text <!--BAZ more text\n"
+ "Aaarggh!"
+ "more text BAZ--> even more\n"
+ "FOO--> text\n"
+ "second line outside\n"
+ "<!-- second comment -->";
+ string text = original;
+ string expect = "First line outside\n"
+ "text text\n"
+ "second line outside\n";
+ remove_html_comments(text);
+ BOOST_CHECK_EQUAL(text, expect);
+ remove_html_comments(text); // should not have any effect
+ BOOST_CHECK_EQUAL(text, expect);
+
+ text = string("test<!--") + original;
+ remove_html_comments(text);
+ BOOST_CHECK_EQUAL(text, "test");
+
+ text = original + "-->test";
+ remove_html_comments(text);
+ BOOST_CHECK_EQUAL(text, "test");
+}
+
+BOOST_AUTO_TEST_CASE(shorten_stl_types_string)
+{
+ BOOST_CHECK_EQUAL(shorten_stl_types("std::basic_string<char, std::char_traits<char>, std::allocator<char> >"),
+ "std::string");
+}
+
+BOOST_AUTO_TEST_CASE(shorten_stl_types_simple)
+{
+ BOOST_CHECK_EQUAL(shorten_stl_types("std::list<some_type, std::allocator<some_type> >"),
+ "std::list<some_type, _alloc_>");
+}
+
+BOOST_AUTO_TEST_CASE(shorten_stl_types_multiple)
+{
+ 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> >)"),
+ "std::string my_func(std::list<some_type, _alloc_>, std::vector<int, _alloc_>)");
+}
+
+BOOST_AUTO_TEST_CASE(shorten_stl_types_complex)
+{
+ BOOST_CHECK_EQUAL(shorten_stl_types("std::list<boost::shared_ptr<some_type>, std::allocator<boost::shared_ptr<some_type> > >"),
+ "std::list<boost::shared_ptr<some_type>, _alloc_>");
+}
+
+BOOST_AUTO_TEST_CASE(shorten_stl_types_nested)
+{
+ //"std::list<int, std::allocator<int> >"
+ //"std::list<std::list<int, std::allocator<int> >, std::allocator<std::list<int, std::allocator<int> > > >"
+ BOOST_CHECK_EQUAL(shorten_stl_types("std::list<std::list<int, std::allocator<int> >, std::allocator<std::list<int, std::allocator<int> > > >"),
+ "std::list<std::list<int, _alloc_>, _alloc_>");
+}
+
+BOOST_AUTO_TEST_CASE(shorten_stl_types_nothing)
+{
+ string text = "";
+ BOOST_CHECK_EQUAL(shorten_stl_types(text), text);
+ text = "int f(void)";
+ BOOST_CHECK_EQUAL(shorten_stl_types(text), text);
+ text = "std::cout << \"Test\" << std::endl;";
+ BOOST_CHECK_EQUAL(shorten_stl_types(text), text);
+ text = "bla<blubb>";
+ BOOST_CHECK_EQUAL(shorten_stl_types(text), text);
+ text = "std::list<> is a class template";
+ BOOST_CHECK_EQUAL(shorten_stl_types(text), text);
+ text = "std::list<int, std::allocator<int>\n>";
+ BOOST_CHECK_EQUAL(shorten_stl_types(text), text);
+}
+
+BOOST_AUTO_TEST_CASE(base64_encode_decode)
+{
+ string text = "Hello World\n";
+
+ string encoded = base64_encode(text);
+
+ BOOST_CHECK_EQUAL("SGVsbG8gV29ybGQK", encoded);
+ BOOST_CHECK_EQUAL(text, base64_decode(encoded));
+}
+
+BOOST_AUTO_TEST_CASE(base64_empty_string)
+{
+ string text = "";
+ string encoded = base64_encode(text);
+
+ BOOST_CHECK_EQUAL("", encoded);
+ BOOST_CHECK_EQUAL(text, base64_decode(encoded));
+}
+
+BOOST_AUTO_TEST_CASE(base64_large_string_with_zero)
+{
+ // 10 MB data
+ int data_size = 1024 * 1024 * 10;
+
+ string large_binary_data(data_size, 0);
+ BOOST_CHECK_EQUAL(data_size, large_binary_data.size());
+
+ string encoded = base64_encode(large_binary_data);
+
+ string decoded = base64_decode(encoded);
+ BOOST_CHECK_EQUAL(large_binary_data, decoded);
+}
+
+BOOST_AUTO_TEST_CASE(base64_large_string_with_zero_encode_linefeeds)
+{
+ // 10 MB data
+ int data_size = 1024 * 1024 * 10;
+
+ string large_binary_data(data_size, 0);
+ BOOST_CHECK_EQUAL(data_size, large_binary_data.size());
+
+ const bool one_line_mode = false;
+ string encoded = base64_encode(large_binary_data, one_line_mode);
+
+ string decoded = base64_decode(encoded, one_line_mode);
+ BOOST_CHECK_EQUAL(large_binary_data, decoded);
+}
+
+BOOST_AUTO_TEST_CASE(base64_decode_garbage)
+{
+ std::string data = "Hello World, this is unencoded data";
+ string decoded = base64_decode(data);
+
+ // garbage turns out to be an empty string
+ BOOST_CHECK_EQUAL(0, decoded.size());
+}
+
+BOOST_AUTO_TEST_CASE(base64_encode_with_linefeeds)
+{
+ const string data = string("Hello World\n")
+ + "Hello World\n"
+ + "Hello World\n"
+ + "Hello World\n"
+ + "Hello World\n"
+ + "Hello World\n"
+ + "Hello World\n";
+
+ const string encoded = base64_encode(data, false);
+
+ const std::string expected = string("SGVsbG8gV29ybGQKSGVsbG8gV29ybGQKSGVsbG8gV29ybGQKSGVsbG8gV29ybGQK\n")
+ + "SGVsbG8gV29ybGQKSGVsbG8gV29ybGQKSGVsbG8gV29ybGQK\n";
+ BOOST_CHECK_EQUAL(expected, encoded);
+
+ // decode and compare
+ BOOST_CHECK_EQUAL(data, base64_decode(encoded, false));
+
+ // expected empty string when switching on single line base64 mode
+ // (openssl is very strict about this)
+ BOOST_CHECK_EQUAL("", base64_decode(encoded, true));
+}
+
+BOOST_AUTO_TEST_SUITE_END()