OpenWalnut  1.3.1
WStringUtils_test.h
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #ifndef WSTRINGUTILS_TEST_H
26 #define WSTRINGUTILS_TEST_H
27 
28 #include <string>
29 #include <vector>
30 
31 #include <cxxtest/TestSuite.h>
32 
33 #include "../WStringUtils.h"
34 
35 namespace su = string_utils;
36 
37 /**
38  * Testing some boundary cases and basic behaviour of those helper functions.
39  */
40 class WStringUtilsTest : public CxxTest::TestSuite
41 {
42 public:
43  /**
44  * Every vector with elements which may be passed to an ostream, should
45  * be correctly written to that stream.
46  */
48  {
49  std::stringstream ss;
50  std::vector< double > pansen;
51  using string_utils::operator<<;
52  ss << pansen;
53  TS_ASSERT_EQUALS( ss.str(), "[]" );
54  ss.str( "" );
55  ss.clear();
56  pansen = std::vector< double >( 2, 3.1415 );
57  pansen[1] = 1.414;
58  std::string expected( "[3.1415000000000002e+00, 1.4139999999999999e+00]" );
59  ss << pansen;
60  TS_ASSERT_EQUALS( ss.str(), expected );
61  }
62 
63  /**
64  * Trimming from the right side means that the left side stays unmodified
65  * and each character which is in the given character set (WHITESPACE on
66  * default) and occurs on the right side will be removed.
67  */
68  void testRightTrimming( void )
69  {
70  std::string str( " abc\t \r\n \t \n\n\n" );
71  std::string expected( " abc" );
72  std::string actual = su::rTrim( str );
73  TS_ASSERT_EQUALS( expected, actual );
74  TS_ASSERT_EQUALS( str, " abc\t \r\n \t \n\n\n" );
75 
76  // check the boundaries
77  TS_ASSERT_EQUALS( su::rTrim( std::string( "" ) ), "" );
78  TS_ASSERT_EQUALS( su::rTrim( std::string( " " ) ), "" );
79  TS_ASSERT_EQUALS( su::rTrim( std::string( "abc" ) ), "abc" );
80 
81  // check different character set
82  str = " pansenn";
83  TS_ASSERT_EQUALS( su::rTrim( str, "pn" ), " panse" );
84  }
85 
86  /**
87  * Same testing like right side trimming but now from the left side.
88  */
89  void testLeftTrimming( void )
90  {
91  std::string str( "\t \r\n \t \n\n\nabc " );
92  std::string expected( "abc " );
93  std::string actual = su::lTrim( str );
94  TS_ASSERT_EQUALS( expected, actual );
95  TS_ASSERT_EQUALS( str, "\t \r\n \t \n\n\nabc " );
96 
97  // check the boundaries
98  TS_ASSERT_EQUALS( su::lTrim( std::string( "" ) ), "" );
99  TS_ASSERT_EQUALS( su::lTrim( std::string( " " ) ), "" );
100  TS_ASSERT_EQUALS( su::lTrim( std::string( "abc" ) ), "abc" );
101 
102  // check different character set
103  str = "splendor pansen ";
104  TS_ASSERT_EQUALS( su::lTrim( str, "splendor " ), "ansen " );
105  }
106 
107  // we don't test trim since it just uses rTrim and lTrim... => not breakable
108 
109 
110  /**
111  * Switching to upper case means that all chars [a-z] will be transformed
112  * into [A-Z]. This does explicitly not include umlauts.
113  */
115  {
116  std::string str( "loWeR text\t with some ü\n" );
117  std::string expected( "LOWER TEXT\t WITH SOME ü\n" );
118 
119  TS_ASSERT_EQUALS( su::toUpper( str ), expected );
120  TS_ASSERT_EQUALS( str, "loWeR text\t with some ü\n" );
121  expected = "lower text\t with some ü\n";
122  TS_ASSERT_EQUALS( su::toLower( str ), expected );
123  TS_ASSERT_EQUALS( str, "loWeR text\t with some ü\n" );
124  }
125 
126  /**
127  * Tokenizers break of a string or other character sequence into a series
128  * of tokens.
129  */
130  void testTokenizer( void )
131  {
132  std::string source;
133  std::vector< std::string > expected;
134  TS_ASSERT_EQUALS( su::tokenize( source ), expected );
135  source = "Foo bar-foo \r\n\t blubb ";
136  expected.push_back( "Foo" );
137  expected.push_back( "bar-foo" );
138  expected.push_back( "blubb" );
139  TS_ASSERT_EQUALS( su::tokenize( source ), expected );
140  TS_ASSERT_EQUALS( source, "Foo bar-foo \r\n\t blubb " );
141  expected.clear();
142  expected.push_back( "Foo " );
143  expected.push_back( "ar-foo \r\n\t " );
144  expected.push_back( "lu" );
145  expected.push_back( "" );
146  expected.push_back( " " );
147  TS_ASSERT_EQUALS( su::tokenize( source, "b", false ), expected );
148  }
149 };
150 
151 #endif // WSTRINGUTILS_TEST_H