| 1 | // Boost string_algo library trim_test.cpp file ---------------------------// |
| 2 | |
| 3 | // Copyright Pavol Droba 2002-2003. Use, modification and |
| 4 | // distribution is subject to the Boost Software License, Version |
| 5 | // 1.0. (See accompanying file LICENSE_1_0.txt or copy at |
| 6 | // http://www.boost.org/LICENSE_1_0.txt) |
| 7 | |
| 8 | // See http://www.boost.org for updates, documentation, and revision history. |
| 9 | |
| 10 | #include <boost/algorithm/string/trim.hpp> |
| 11 | #include <boost/algorithm/string/trim_all.hpp> |
| 12 | |
| 13 | // Include unit test framework |
| 14 | #define BOOST_TEST_MAIN |
| 15 | #include <boost/test/unit_test.hpp> |
| 16 | |
| 17 | #include <string> |
| 18 | #include <iostream> |
| 19 | #include <boost/test/test_tools.hpp> |
| 20 | |
| 21 | using namespace std; |
| 22 | using namespace boost; |
| 23 | |
| 24 | void trim_test() |
| 25 | { |
| 26 | string str1(" 1x x x x1 " ); |
| 27 | string str2(" 2x x x x2 " ); |
| 28 | string str3(" " ); |
| 29 | |
| 30 | // *** value passing tests *** // |
| 31 | |
| 32 | // general string test |
| 33 | BOOST_CHECK( trim_left_copy( str1 )=="1x x x x1 " ) ; |
| 34 | BOOST_CHECK( trim_right_copy( str1 )==" 1x x x x1" ) ; |
| 35 | BOOST_CHECK( trim_copy( str1 )=="1x x x x1" ) ; |
| 36 | |
| 37 | // spaces-only string test |
| 38 | BOOST_CHECK( trim_left_copy( str3 )=="" ); |
| 39 | BOOST_CHECK( trim_right_copy( str3 )=="" ); |
| 40 | BOOST_CHECK( trim_copy( str3 )=="" ); |
| 41 | |
| 42 | // empty string check |
| 43 | BOOST_CHECK( trim_left_copy( string("" ) )=="" ); |
| 44 | BOOST_CHECK( trim_right_copy( string("" ) )=="" ); |
| 45 | BOOST_CHECK( trim_copy( string("" ) )=="" ); |
| 46 | |
| 47 | // iterator tests |
| 48 | string str; |
| 49 | trim_left_copy_if( Output: std::back_inserter(x&: str), Input: str1, IsSpace: is_space() ); |
| 50 | BOOST_CHECK( str=="1x x x x1 " ); |
| 51 | |
| 52 | str.clear(); |
| 53 | trim_right_copy_if( Output: std::back_inserter(x&: str), Input: str1, IsSpace: is_space() ); |
| 54 | BOOST_CHECK( str==" 1x x x x1" ); |
| 55 | |
| 56 | str.clear(); |
| 57 | trim_copy_if( Output: std::back_inserter(x&: str), Input: str1, IsSpace: is_space() ); |
| 58 | BOOST_CHECK( str=="1x x x x1" ); |
| 59 | |
| 60 | str.clear(); |
| 61 | trim_left_copy_if( |
| 62 | Output: std::back_inserter(x&: str), |
| 63 | Input: " 1x x x x1 " , |
| 64 | IsSpace: is_space() ); |
| 65 | BOOST_CHECK( str=="1x x x x1 " ); |
| 66 | |
| 67 | str.clear(); |
| 68 | trim_right_copy_if( |
| 69 | Output: std::back_inserter(x&: str), |
| 70 | Input: " 1x x x x1 " , |
| 71 | IsSpace: is_space() ); |
| 72 | BOOST_CHECK( str==" 1x x x x1" ); |
| 73 | |
| 74 | str.clear(); |
| 75 | trim_copy_if( |
| 76 | Output: std::back_inserter(x&: str), |
| 77 | Input: " 1x x x x1 " , |
| 78 | IsSpace: is_space() ); |
| 79 | BOOST_CHECK( str=="1x x x x1" ); |
| 80 | // *** inplace tests *** // |
| 81 | |
| 82 | // general string test |
| 83 | trim_left( Input&: str1 ); |
| 84 | BOOST_CHECK( str1=="1x x x x1 " ); |
| 85 | trim_right( Input&: str1 ); |
| 86 | BOOST_CHECK( str1=="1x x x x1" ); |
| 87 | trim( Input&: str2 ); |
| 88 | BOOST_CHECK( str2=="2x x x x2" ); |
| 89 | |
| 90 | // spaces-only string test |
| 91 | str3 = " " ; trim_left( Input&: str3 ); |
| 92 | BOOST_CHECK( str3=="" ); |
| 93 | str3 = " " ; trim_right( Input&: str3 ); |
| 94 | BOOST_CHECK( str3=="" ); |
| 95 | str3 = " " ; trim( Input&: str3 ); |
| 96 | BOOST_CHECK( str3=="" ); |
| 97 | |
| 98 | // empty string check |
| 99 | str3 = "" ; trim_left( Input&: str3 ); |
| 100 | BOOST_CHECK( str3=="" ); |
| 101 | str3 = "" ; trim_right( Input&: str3 ); |
| 102 | BOOST_CHECK( str3=="" ); |
| 103 | str3 = "" ; trim( Input&: str3 ); |
| 104 | BOOST_CHECK( str3=="" ); |
| 105 | |
| 106 | // *** non-standard predicate tests *** // |
| 107 | BOOST_CHECK( |
| 108 | trim_copy_if( |
| 109 | string("123abc456" ), |
| 110 | is_classified(std::ctype_base::digit) )=="abc" ); |
| 111 | BOOST_CHECK( trim_copy_if( string("<>abc<>" ), is_any_of( "<<>>" ) )=="abc" ); |
| 112 | } |
| 113 | |
| 114 | void trim_all_test() |
| 115 | { |
| 116 | string str1(" 1x x x x1 " ); |
| 117 | string str2("+---...2x+--x--+x-+-x2...---+" ); |
| 118 | string str3(" " ); |
| 119 | |
| 120 | // *** value passing tests *** // |
| 121 | |
| 122 | // general string test |
| 123 | BOOST_CHECK( trim_all_copy( str1 )=="1x x x x1" ) ; |
| 124 | BOOST_CHECK( trim_all_copy_if( str2, is_punct() )=="2x+x-x-x2" ) ; |
| 125 | |
| 126 | // spaces-only string test |
| 127 | BOOST_CHECK( trim_all_copy( str3 )=="" ); |
| 128 | |
| 129 | // empty string check |
| 130 | BOOST_CHECK( trim_all_copy( string("" ) )=="" ); |
| 131 | |
| 132 | // general string test |
| 133 | trim_all( Input&: str1 ); |
| 134 | BOOST_CHECK( str1=="1x x x x1" ) ; |
| 135 | trim_all_if( Input&: str2, IsSpace: is_punct() ); |
| 136 | BOOST_CHECK( str2=="2x+x-x-x2" ) ; |
| 137 | |
| 138 | // spaces-only string test |
| 139 | str3 = " " ; trim_all( Input&: str3 ); |
| 140 | BOOST_CHECK( str3=="" ); |
| 141 | |
| 142 | // empty string check |
| 143 | str3 = "" ; trim_all( Input&: str3 ); |
| 144 | BOOST_CHECK( str3=="" ); |
| 145 | BOOST_CHECK( str3=="" ); |
| 146 | |
| 147 | // *** non-standard predicate tests *** // |
| 148 | BOOST_CHECK( |
| 149 | trim_all_copy_if( |
| 150 | string("123abc127deb456" ), |
| 151 | is_classified(std::ctype_base::digit) )=="abc1deb" ); |
| 152 | BOOST_CHECK( trim_all_copy_if( string("<>abc<>def<>" ), is_any_of( "<<>>" ) )=="abc<def" ); |
| 153 | } |
| 154 | |
| 155 | void trim_fill_test() |
| 156 | { |
| 157 | string str1(" 1x x x x1 " ); |
| 158 | string str2("+---...2x+--x--+x-+-x2...---+" ); |
| 159 | string str3(" " ); |
| 160 | |
| 161 | // *** value passing tests *** // |
| 162 | |
| 163 | // general string test |
| 164 | BOOST_CHECK( trim_fill_copy( str1, "-" )=="1x-x-x-x1" ) ; |
| 165 | BOOST_CHECK( trim_fill_copy_if( str2, " " , is_punct() )=="2x x x x2" ) ; |
| 166 | |
| 167 | // spaces-only string test |
| 168 | BOOST_CHECK( trim_fill_copy( str3, " " )=="" ); |
| 169 | |
| 170 | // empty string check |
| 171 | BOOST_CHECK( trim_fill_copy( string("" ), " " )=="" ); |
| 172 | |
| 173 | // general string test |
| 174 | trim_fill( Input&: str1, Fill: "-" ); |
| 175 | BOOST_CHECK( str1=="1x-x-x-x1" ) ; |
| 176 | trim_fill_if( Input&: str2, Fill: "" , IsSpace: is_punct() ); |
| 177 | BOOST_CHECK( str2=="2xxxx2" ) ; |
| 178 | |
| 179 | // spaces-only string test |
| 180 | str3 = " " ; trim_fill( Input&: str3, Fill: "" ); |
| 181 | BOOST_CHECK( str3=="" ); |
| 182 | |
| 183 | // empty string check |
| 184 | str3 = "" ; trim_fill( Input&: str3, Fill: "" ); |
| 185 | BOOST_CHECK( str3=="" ); |
| 186 | BOOST_CHECK( str3=="" ); |
| 187 | |
| 188 | // *** non-standard predicate tests *** // |
| 189 | BOOST_CHECK( |
| 190 | trim_fill_copy_if( |
| 191 | string("123abc127deb456" ), |
| 192 | "+" , |
| 193 | is_classified(std::ctype_base::digit) )=="abc+deb" ); |
| 194 | BOOST_CHECK( trim_fill_copy_if( string("<>abc<>def<>" ), "-" , is_any_of( "<<>>" ) )=="abc-def" ); |
| 195 | } |
| 196 | |
| 197 | BOOST_AUTO_TEST_CASE( test_main ) |
| 198 | { |
| 199 | trim_test(); |
| 200 | trim_all_test(); |
| 201 | trim_fill_test(); |
| 202 | } |
| 203 | |