OpenWalnut  1.4.0
WStringUtils_test.h
00001 //---------------------------------------------------------------------------
00002 //
00003 // Project: OpenWalnut ( http://www.openwalnut.org )
00004 //
00005 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
00006 // For more information see http://www.openwalnut.org/copying
00007 //
00008 // This file is part of OpenWalnut.
00009 //
00010 // OpenWalnut is free software: you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as published by
00012 // the Free Software Foundation, either version 3 of the License, or
00013 // (at your option) any later version.
00014 //
00015 // OpenWalnut is distributed in the hope that it will be useful,
00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 // GNU Lesser General Public License for more details.
00019 //
00020 // You should have received a copy of the GNU Lesser General Public License
00021 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
00022 //
00023 //---------------------------------------------------------------------------
00024 
00025 #ifndef WSTRINGUTILS_TEST_H
00026 #define WSTRINGUTILS_TEST_H
00027 
00028 #include <string>
00029 #include <vector>
00030 
00031 #include <cxxtest/TestSuite.h>
00032 
00033 #include "../WStringUtils.h"
00034 
00035 namespace su = string_utils;
00036 
00037 /**
00038  * Testing some boundary cases and basic behaviour of those helper functions.
00039  */
00040 class WStringUtilsTest : public CxxTest::TestSuite
00041 {
00042 public:
00043     /**
00044      * Every vector with elements which may be passed to an ostream, should
00045      * be correctly written to that stream.
00046      */
00047     void testVectorOutputOperator( void )
00048     {
00049         std::stringstream ss;
00050         std::vector< double > pansen;
00051         using string_utils::operator<<;
00052         ss << pansen;
00053         TS_ASSERT_EQUALS( ss.str(), "[]" );
00054         ss.str( "" );
00055         ss.clear();
00056         pansen = std::vector< double >( 2, 3.1415 );
00057         pansen[1] = 1.414;
00058         std::string expected( "[3.1415000000000002e+00, 1.4139999999999999e+00]" );
00059         ss << pansen;
00060         TS_ASSERT_EQUALS( ss.str(), expected );
00061     }
00062 
00063     /**
00064      * Trimming from the right side means that the left side stays unmodified
00065      * and each character which is in the given character set (WHITESPACE on
00066      * default) and occurs on the right side will be removed.
00067      */
00068     void testRightTrimming( void )
00069     {
00070         std::string str( " abc\t  \r\n  \t   \n\n\n" );
00071         std::string expected( " abc" );
00072         std::string actual = su::rTrim( str );
00073         TS_ASSERT_EQUALS( expected, actual );
00074         TS_ASSERT_EQUALS( str, " abc\t  \r\n  \t   \n\n\n" );
00075 
00076         // check the boundaries
00077         TS_ASSERT_EQUALS( su::rTrim( std::string( "" ) ), "" );
00078         TS_ASSERT_EQUALS( su::rTrim( std::string( " " ) ), "" );
00079         TS_ASSERT_EQUALS( su::rTrim( std::string( "abc" ) ), "abc" );
00080 
00081         // check different character set
00082         str = " pansenn";
00083         TS_ASSERT_EQUALS( su::rTrim( str, "pn" ), " panse" );
00084     }
00085 
00086     /**
00087      * Same testing like right side trimming but now from the left side.
00088      */
00089     void testLeftTrimming( void )
00090     {
00091         std::string str( "\t  \r\n  \t   \n\n\nabc " );
00092         std::string expected( "abc " );
00093         std::string actual = su::lTrim( str );
00094         TS_ASSERT_EQUALS( expected, actual );
00095         TS_ASSERT_EQUALS( str, "\t  \r\n  \t   \n\n\nabc " );
00096 
00097         // check the boundaries
00098         TS_ASSERT_EQUALS( su::lTrim( std::string( "" ) ), "" );
00099         TS_ASSERT_EQUALS( su::lTrim( std::string( " " ) ), "" );
00100         TS_ASSERT_EQUALS( su::lTrim( std::string( "abc" ) ), "abc" );
00101 
00102         // check different character set
00103         str = "splendor pansen ";
00104         TS_ASSERT_EQUALS( su::lTrim( str, "splendor " ), "ansen " );
00105     }
00106 
00107     // we don't test trim since it just uses rTrim and lTrim... => not breakable
00108 
00109 
00110     /**
00111      * Switching to upper case means that all chars [a-z] will be transformed
00112      * into [A-Z]. This does explicitly not include umlauts.
00113      */
00114     void testCaseTransformations( void )
00115     {
00116         std::string str( "loWeR text\t with some ü\n" );
00117         std::string expected( "LOWER TEXT\t WITH SOME ü\n" );
00118 
00119         TS_ASSERT_EQUALS( su::toUpper( str ), expected );
00120         TS_ASSERT_EQUALS( str, "loWeR text\t with some ü\n" );
00121         expected = "lower text\t with some ü\n";
00122         TS_ASSERT_EQUALS( su::toLower( str ), expected );
00123         TS_ASSERT_EQUALS( str, "loWeR text\t with some ü\n" );
00124     }
00125 
00126     /**
00127      * Tokenizers break of a string or other character sequence into a series
00128      * of tokens.
00129      */
00130     void testTokenizer( void )
00131     {
00132         std::string source;
00133         std::vector< std::string > expected;
00134         TS_ASSERT_EQUALS( su::tokenize( source ), expected );
00135         source = "Foo bar-foo \r\n\t blubb ";
00136         expected.push_back( "Foo" );
00137         expected.push_back( "bar-foo" );
00138         expected.push_back( "blubb" );
00139         TS_ASSERT_EQUALS( su::tokenize( source ), expected );
00140         TS_ASSERT_EQUALS( source, "Foo bar-foo \r\n\t blubb " );
00141         expected.clear();
00142         expected.push_back( "Foo " );
00143         expected.push_back( "ar-foo \r\n\t " );
00144         expected.push_back( "lu" );
00145         expected.push_back( "" );
00146         expected.push_back( " " );
00147         TS_ASSERT_EQUALS( su::tokenize( source, "b", false ), expected );
00148     }
00149 };
00150 
00151 #endif  // WSTRINGUTILS_TEST_H