| 1 | // Boost string_algo library substr_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/find.hpp> |
| 11 | #include <boost/algorithm/string/classification.hpp> |
| 12 | #include <boost/algorithm/string/split.hpp> |
| 13 | |
| 14 | // Include unit test framework |
| 15 | #define BOOST_TEST_MAIN |
| 16 | #include <boost/test/unit_test.hpp> |
| 17 | |
| 18 | #include <string> |
| 19 | #include <vector> |
| 20 | #include <iostream> |
| 21 | #include <iterator> |
| 22 | #include <sstream> |
| 23 | #include <boost/test/test_tools.hpp> |
| 24 | |
| 25 | using namespace std; |
| 26 | using namespace boost; |
| 27 | |
| 28 | void find_test() |
| 29 | { |
| 30 | string str1("123abcxXxabcXxXabc321" ); |
| 31 | string str2("abc" ); |
| 32 | string str3("" ); |
| 33 | const char* pch1="123abcxxxabcXXXabc321" ; |
| 34 | vector<int> vec1( str1.begin(), str1.end() ); |
| 35 | |
| 36 | // find results ------------------------------------------------------------// |
| 37 | iterator_range<string::iterator> nc_result; |
| 38 | iterator_range<string::const_iterator> cv_result; |
| 39 | |
| 40 | iterator_range<vector<int>::iterator> nc_vresult; |
| 41 | iterator_range<vector<int>::const_iterator> cv_vresult; |
| 42 | |
| 43 | iterator_range<const char*> ch_result; |
| 44 | |
| 45 | // basic tests ------------------------------------------------------------// |
| 46 | |
| 47 | |
| 48 | // find_first |
| 49 | BOOST_TEST_CHECKPOINT( "find_first" ); |
| 50 | |
| 51 | nc_result=find_first( Input&: str1, Search: string("abc" ) ); |
| 52 | BOOST_CHECK( |
| 53 | ( (nc_result.begin()-str1.begin()) == 3) && |
| 54 | ( (nc_result.end()-str1.begin()) == 6) ); |
| 55 | |
| 56 | cv_result=find_first( Input: const_cast<const string&>(str1), Search: str2 ); |
| 57 | BOOST_CHECK( |
| 58 | ( (cv_result.begin()-str1.begin()) == 3) && |
| 59 | ( (cv_result.end()-str1.begin()) == 6) ); |
| 60 | |
| 61 | cv_result=ifind_first( Input: const_cast<const string&>(str1), Search: "xXX" ); |
| 62 | BOOST_CHECK( |
| 63 | ( (cv_result.begin()-str1.begin()) == 6) && |
| 64 | ( (cv_result.end()-str1.begin()) == 9) ); |
| 65 | |
| 66 | ch_result=find_first( Input&: pch1, Search: "abc" ); |
| 67 | BOOST_CHECK(( (ch_result.begin() - pch1 ) == 3) && ( (ch_result.end() - pch1 ) == 6 ) ); |
| 68 | |
| 69 | // find_last |
| 70 | BOOST_TEST_CHECKPOINT( "find_last" ); |
| 71 | |
| 72 | nc_result=find_last( Input&: str1, Search: string("abc" ) ); |
| 73 | BOOST_CHECK( |
| 74 | ( (nc_result.begin()-str1.begin()) == 15) && |
| 75 | ( (nc_result.end()-str1.begin()) == 18) ); |
| 76 | |
| 77 | cv_result=find_last( Input: const_cast<const string&>(str1), Search: str2 ); |
| 78 | BOOST_CHECK( |
| 79 | ( (cv_result.begin()-str1.begin()) == 15) && |
| 80 | ( (cv_result.end()-str1.begin()) == 18) ); |
| 81 | |
| 82 | cv_result=ifind_last( Input: const_cast<const string&>(str1), Search: "XXx" ); |
| 83 | BOOST_CHECK( |
| 84 | ( (cv_result.begin()-str1.begin()) == 12) && |
| 85 | ( (cv_result.end()-str1.begin()) == 15) ); |
| 86 | |
| 87 | ch_result=find_last( Input&: pch1, Search: "abc" ); |
| 88 | BOOST_CHECK(( (ch_result.begin() - pch1 ) == 15) && ( (ch_result.end() - pch1 ) == 18 ) ); |
| 89 | |
| 90 | // find_nth |
| 91 | BOOST_TEST_CHECKPOINT( "find_nth" ); |
| 92 | |
| 93 | nc_result=find_nth( Input&: str1, Search: string("abc" ), Nth: 1 ); |
| 94 | BOOST_CHECK( |
| 95 | ( (nc_result.begin()-str1.begin()) == 9) && |
| 96 | ( (nc_result.end()-str1.begin()) == 12) ); |
| 97 | |
| 98 | nc_result=find_nth( Input&: str1, Search: string("abc" ), Nth: -1 ); |
| 99 | BOOST_CHECK( |
| 100 | ( (nc_result.begin()-str1.begin()) == 15) && |
| 101 | ( (nc_result.end()-str1.begin()) == 18) ); |
| 102 | |
| 103 | |
| 104 | cv_result=find_nth( Input: const_cast<const string&>(str1), Search: str2, Nth: 1 ); |
| 105 | BOOST_CHECK( |
| 106 | ( (cv_result.begin()-str1.begin()) == 9) && |
| 107 | ( (cv_result.end()-str1.begin()) == 12) ); |
| 108 | |
| 109 | cv_result=find_nth( Input: const_cast<const string&>(str1), Search: str2, Nth: -1 ); |
| 110 | BOOST_CHECK( |
| 111 | ( (cv_result.begin()-str1.begin()) == 15) && |
| 112 | ( (cv_result.end()-str1.begin()) == 18) ); |
| 113 | |
| 114 | cv_result=ifind_nth( Input: const_cast<const string&>(str1), Search: "xxx" , Nth: 1 ); |
| 115 | BOOST_CHECK( |
| 116 | ( (cv_result.begin()-str1.begin()) == 12) && |
| 117 | ( (cv_result.end()-str1.begin()) == 15) ); |
| 118 | |
| 119 | cv_result=ifind_nth( Input: const_cast<const string&>(str1), Search: "xxx" , Nth: 1 ); |
| 120 | BOOST_CHECK( |
| 121 | ( (cv_result.begin()-str1.begin()) == 12) && |
| 122 | ( (cv_result.end()-str1.begin()) == 15) ); |
| 123 | |
| 124 | |
| 125 | ch_result=find_nth( Input&: pch1, Search: "abc" , Nth: 1 ); |
| 126 | BOOST_CHECK(( (ch_result.begin() - pch1 ) == 9) && ( (ch_result.end() - pch1 ) == 12 ) ); |
| 127 | |
| 128 | // find_head |
| 129 | BOOST_TEST_CHECKPOINT( "find_head" ); |
| 130 | |
| 131 | nc_result=find_head( Input&: str1, N: 6 ); |
| 132 | BOOST_CHECK( |
| 133 | ( (nc_result.begin()-str1.begin()) == 0) && |
| 134 | ( (nc_result.end()-str1.begin()) == 6) ); |
| 135 | |
| 136 | nc_result=find_head( Input&: str1, N: -6 ); |
| 137 | BOOST_CHECK( |
| 138 | ( (nc_result.begin()-str1.begin()) == 0) && |
| 139 | ( (str1.end()-nc_result.end()) == 6 ) ); |
| 140 | |
| 141 | cv_result=find_head( Input: const_cast<const string&>(str1), N: 6 ); |
| 142 | BOOST_CHECK( |
| 143 | ( (cv_result.begin()-str1.begin()) == 0) && |
| 144 | ( (cv_result.end()-str1.begin()) == 6) ); |
| 145 | |
| 146 | ch_result=find_head( Input&: pch1, N: 6 ); |
| 147 | BOOST_CHECK( ( (ch_result.begin() - pch1 ) == 0 ) && ( (ch_result.end() - pch1 ) == 6 ) ); |
| 148 | |
| 149 | // find_tail |
| 150 | BOOST_TEST_CHECKPOINT( "find_tail" ); |
| 151 | |
| 152 | nc_result=find_tail( Input&: str1, N: 6 ); |
| 153 | BOOST_CHECK( |
| 154 | ( (nc_result.begin()-str1.begin()) == 15) && |
| 155 | ( (nc_result.end()-str1.begin()) == 21) ); |
| 156 | |
| 157 | nc_result=find_tail( Input&: str1, N: -6 ); |
| 158 | BOOST_CHECK( |
| 159 | ( (nc_result.begin()-str1.begin()) == 6) && |
| 160 | ( (nc_result.end()-str1.begin()) == 21) ); |
| 161 | |
| 162 | |
| 163 | cv_result=find_tail( Input: const_cast<const string&>(str1), N: 6 ); |
| 164 | BOOST_CHECK( |
| 165 | ( (cv_result.begin()-str1.begin()) == 15) && |
| 166 | ( (cv_result.end()-str1.begin()) == 21) ); |
| 167 | |
| 168 | ch_result=find_tail( Input&: pch1, N: 6 ); |
| 169 | BOOST_CHECK( ( (ch_result.begin() - pch1 ) == 15 ) && ( (ch_result.end() - pch1 ) == 21 ) ); |
| 170 | |
| 171 | // find_token |
| 172 | BOOST_TEST_CHECKPOINT( "find_token" ); |
| 173 | |
| 174 | nc_result=find_token( Input&: str1, Pred: is_any_of(Set: "abc" ), eCompress: token_compress_on ); |
| 175 | BOOST_CHECK( |
| 176 | ( (nc_result.begin()-str1.begin()) == 3) && |
| 177 | ( (nc_result.end()-str1.begin()) == 6) ); |
| 178 | |
| 179 | cv_result=find_token( Input: const_cast<const string&>(str1), Pred: is_any_of(Set: "abc" ), eCompress: token_compress_on ); |
| 180 | BOOST_CHECK( |
| 181 | ( (cv_result.begin()-str1.begin()) == 3) && |
| 182 | ( (cv_result.end()-str1.begin()) == 6) ); |
| 183 | |
| 184 | string s1("abc def ghi jkl" ); |
| 185 | find_iterator<string::iterator> fEnd; |
| 186 | |
| 187 | find_iterator<string::iterator> fxIt = make_find_iterator(Collection&: s1, |
| 188 | Finder: token_finder(Pred: is_alnum(), eCompress: token_compress_on)); |
| 189 | BOOST_CHECK((fxIt != fEnd) && (*fxIt == string("abc" ))); |
| 190 | ++fxIt; |
| 191 | BOOST_CHECK((fxIt != fEnd) && (*fxIt == string("def" ))); |
| 192 | ++fxIt; |
| 193 | BOOST_CHECK((fxIt != fEnd) && (*fxIt == string("ghi" ))); |
| 194 | ++fxIt; |
| 195 | BOOST_CHECK((fxIt != fEnd) && (*fxIt == string("jkl" ))); |
| 196 | ++fxIt; |
| 197 | BOOST_CHECK(fxIt == fEnd); |
| 198 | |
| 199 | nc_result=find_token( Input&: str1, Pred: is_any_of(Set: "abc" ), eCompress: token_compress_off ); |
| 200 | BOOST_CHECK( |
| 201 | ( (nc_result.begin()-str1.begin()) == 3) && |
| 202 | ( (nc_result.end()-str1.begin()) == 4) ); |
| 203 | |
| 204 | cv_result=find_token( Input: const_cast<const string&>(str1), Pred: is_any_of(Set: "abc" ), eCompress: token_compress_off ); |
| 205 | BOOST_CHECK( |
| 206 | ( (cv_result.begin()-str1.begin()) == 3) && |
| 207 | ( (cv_result.end()-str1.begin()) == 4) ); |
| 208 | |
| 209 | ch_result=find_token( Input&: pch1, Pred: is_any_of(Set: "abc" ), eCompress: token_compress_off ); |
| 210 | BOOST_CHECK( ( (ch_result.begin() - pch1 ) == 3 ) && ( (ch_result.end() - pch1 ) == 4 ) ); |
| 211 | |
| 212 | // generic find |
| 213 | BOOST_TEST_CHECKPOINT( "generic find" ); |
| 214 | |
| 215 | nc_result=find(Input&: str1, Finder: first_finder(Search: string("abc" ))); |
| 216 | BOOST_CHECK( |
| 217 | ( (nc_result.begin()-str1.begin()) == 3) && |
| 218 | ( (nc_result.end()-str1.begin()) == 6) ); |
| 219 | |
| 220 | cv_result=find(Input: const_cast<const string&>(str1), Finder: first_finder(Search: str2) ); |
| 221 | BOOST_CHECK( |
| 222 | ( (cv_result.begin()-str1.begin()) == 3) && |
| 223 | ( (cv_result.end()-str1.begin()) == 6) ); |
| 224 | |
| 225 | // multi-type comparison test |
| 226 | BOOST_TEST_CHECKPOINT( "multi-type" ); |
| 227 | |
| 228 | nc_vresult=find_first( Input&: vec1, Search: string("abc" ) ); |
| 229 | BOOST_CHECK( |
| 230 | ( (nc_result.begin()-str1.begin()) == 3) && |
| 231 | ( (nc_result.end()-str1.begin()) == 6) ); |
| 232 | |
| 233 | cv_vresult=find_first( Input: const_cast<const vector<int>&>(vec1), Search: str2 ); |
| 234 | BOOST_CHECK( |
| 235 | ( (cv_result.begin()-str1.begin()) == 3) && |
| 236 | ( (cv_result.end()-str1.begin()) == 6) ); |
| 237 | |
| 238 | // overflow test |
| 239 | BOOST_TEST_CHECKPOINT( "overflow" ); |
| 240 | |
| 241 | nc_result=find_first( Input&: str2, Search: string("abcd" ) ); |
| 242 | BOOST_CHECK( nc_result.begin()==nc_result.end() ); |
| 243 | cv_result=find_first( Input: const_cast<const string&>(str2), Search: string("abcd" ) ); |
| 244 | BOOST_CHECK( cv_result.begin()==cv_result.end() ); |
| 245 | |
| 246 | cv_result=find_head( Input: const_cast<const string&>(str2), N: 4 ); |
| 247 | BOOST_CHECK( string( cv_result.begin(), cv_result.end() )== string("abc" ) ); |
| 248 | cv_result=find_tail( Input: const_cast<const string&>(str2), N: 4 ); |
| 249 | BOOST_CHECK( string( cv_result.begin(), cv_result.end() )== string("abc" ) ); |
| 250 | |
| 251 | // Empty string test |
| 252 | BOOST_TEST_CHECKPOINT( "empty" ); |
| 253 | |
| 254 | nc_result=find_first( Input&: str3, Search: string("abcd" ) ); |
| 255 | BOOST_CHECK( nc_result.begin()==nc_result.end() ); |
| 256 | nc_result=find_first( Input&: str1, Search: string("" ) ); |
| 257 | BOOST_CHECK( nc_result.begin()==nc_result.end() ); |
| 258 | |
| 259 | cv_result=find_first( Input: const_cast<const string&>(str3), Search: string("abcd" ) ); |
| 260 | BOOST_CHECK( cv_result.begin()==cv_result.end() ); |
| 261 | cv_result=find_first( Input: const_cast<const string&>(str1), Search: string("" ) ); |
| 262 | BOOST_CHECK( cv_result.begin()==cv_result.end() ); |
| 263 | |
| 264 | // iterator_range specific tests |
| 265 | ostringstream osstr; |
| 266 | osstr << find_first( Input&: str1, Search: "abc" ); |
| 267 | BOOST_CHECK( osstr.str()=="abc" ); |
| 268 | |
| 269 | } |
| 270 | |
| 271 | // test main |
| 272 | BOOST_AUTO_TEST_CASE( test_main ) |
| 273 | { |
| 274 | find_test(); |
| 275 | } |
| 276 | |