find_test.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. // Boost string_algo library substr_test.cpp file ------------------//
  2. // Copyright Pavol Droba 2002-2003. Use, modification and
  3. // distribution is subject to the Boost Software License, Version
  4. // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. // See http://www.boost.org for updates, documentation, and revision history.
  7. #include <boost/algorithm/string/find.hpp>
  8. #include <boost/algorithm/string/classification.hpp>
  9. #include <boost/algorithm/string/split.hpp>
  10. // Include unit test framework
  11. #define BOOST_TEST_MAIN
  12. #include <boost/test/unit_test.hpp>
  13. #include <string>
  14. #include <vector>
  15. #include <iostream>
  16. #include <iterator>
  17. #include <sstream>
  18. #include <boost/test/test_tools.hpp>
  19. using namespace std;
  20. using namespace boost;
  21. void find_test()
  22. {
  23. string str1("123abcxXxabcXxXabc321");
  24. string str2("abc");
  25. string str3("");
  26. const char* pch1="123abcxxxabcXXXabc321";
  27. vector<int> vec1( str1.begin(), str1.end() );
  28. // find results ------------------------------------------------------------//
  29. iterator_range<string::iterator> nc_result;
  30. iterator_range<string::const_iterator> cv_result;
  31. iterator_range<vector<int>::iterator> nc_vresult;
  32. iterator_range<vector<int>::const_iterator> cv_vresult;
  33. iterator_range<const char*> ch_result;
  34. // basic tests ------------------------------------------------------------//
  35. // find_first
  36. BOOST_TEST_CHECKPOINT( "find_first" );
  37. nc_result=find_first( str1, string("abc") );
  38. BOOST_CHECK(
  39. ( (nc_result.begin()-str1.begin()) == 3) &&
  40. ( (nc_result.end()-str1.begin()) == 6) );
  41. cv_result=find_first( const_cast<const string&>(str1), str2 );
  42. BOOST_CHECK(
  43. ( (cv_result.begin()-str1.begin()) == 3) &&
  44. ( (cv_result.end()-str1.begin()) == 6) );
  45. cv_result=ifind_first( const_cast<const string&>(str1), "xXX" );
  46. BOOST_CHECK(
  47. ( (cv_result.begin()-str1.begin()) == 6) &&
  48. ( (cv_result.end()-str1.begin()) == 9) );
  49. ch_result=find_first( pch1, "abc" );
  50. BOOST_CHECK(( (ch_result.begin() - pch1 ) == 3) && ( (ch_result.end() - pch1 ) == 6 ) );
  51. // find_last
  52. BOOST_TEST_CHECKPOINT( "find_last" );
  53. nc_result=find_last( str1, string("abc") );
  54. BOOST_CHECK(
  55. ( (nc_result.begin()-str1.begin()) == 15) &&
  56. ( (nc_result.end()-str1.begin()) == 18) );
  57. cv_result=find_last( const_cast<const string&>(str1), str2 );
  58. BOOST_CHECK(
  59. ( (cv_result.begin()-str1.begin()) == 15) &&
  60. ( (cv_result.end()-str1.begin()) == 18) );
  61. cv_result=ifind_last( const_cast<const string&>(str1), "XXx" );
  62. BOOST_CHECK(
  63. ( (cv_result.begin()-str1.begin()) == 12) &&
  64. ( (cv_result.end()-str1.begin()) == 15) );
  65. ch_result=find_last( pch1, "abc" );
  66. BOOST_CHECK(( (ch_result.begin() - pch1 ) == 15) && ( (ch_result.end() - pch1 ) == 18 ) );
  67. // find_nth
  68. BOOST_TEST_CHECKPOINT( "find_nth" );
  69. nc_result=find_nth( str1, string("abc"), 1 );
  70. BOOST_CHECK(
  71. ( (nc_result.begin()-str1.begin()) == 9) &&
  72. ( (nc_result.end()-str1.begin()) == 12) );
  73. nc_result=find_nth( str1, string("abc"), -1 );
  74. BOOST_CHECK(
  75. ( (nc_result.begin()-str1.begin()) == 15) &&
  76. ( (nc_result.end()-str1.begin()) == 18) );
  77. cv_result=find_nth( const_cast<const string&>(str1), str2, 1 );
  78. BOOST_CHECK(
  79. ( (cv_result.begin()-str1.begin()) == 9) &&
  80. ( (cv_result.end()-str1.begin()) == 12) );
  81. cv_result=find_nth( const_cast<const string&>(str1), str2, -1 );
  82. BOOST_CHECK(
  83. ( (cv_result.begin()-str1.begin()) == 15) &&
  84. ( (cv_result.end()-str1.begin()) == 18) );
  85. cv_result=ifind_nth( const_cast<const string&>(str1), "xxx", 1 );
  86. BOOST_CHECK(
  87. ( (cv_result.begin()-str1.begin()) == 12) &&
  88. ( (cv_result.end()-str1.begin()) == 15) );
  89. cv_result=ifind_nth( const_cast<const string&>(str1), "xxx", 1 );
  90. BOOST_CHECK(
  91. ( (cv_result.begin()-str1.begin()) == 12) &&
  92. ( (cv_result.end()-str1.begin()) == 15) );
  93. ch_result=find_nth( pch1, "abc", 1 );
  94. BOOST_CHECK(( (ch_result.begin() - pch1 ) == 9) && ( (ch_result.end() - pch1 ) == 12 ) );
  95. // find_head
  96. BOOST_TEST_CHECKPOINT( "find_head" );
  97. nc_result=find_head( str1, 6 );
  98. BOOST_CHECK(
  99. ( (nc_result.begin()-str1.begin()) == 0) &&
  100. ( (nc_result.end()-str1.begin()) == 6) );
  101. nc_result=find_head( str1, -6 );
  102. BOOST_CHECK(
  103. ( (nc_result.begin()-str1.begin()) == 0) &&
  104. ( (str1.end()-nc_result.end()) == 6 ) );
  105. cv_result=find_head( const_cast<const string&>(str1), 6 );
  106. BOOST_CHECK(
  107. ( (cv_result.begin()-str1.begin()) == 0) &&
  108. ( (cv_result.end()-str1.begin()) == 6) );
  109. ch_result=find_head( pch1, 6 );
  110. BOOST_CHECK( ( (ch_result.begin() - pch1 ) == 0 ) && ( (ch_result.end() - pch1 ) == 6 ) );
  111. // find_tail
  112. BOOST_TEST_CHECKPOINT( "find_tail" );
  113. nc_result=find_tail( str1, 6 );
  114. BOOST_CHECK(
  115. ( (nc_result.begin()-str1.begin()) == 15) &&
  116. ( (nc_result.end()-str1.begin()) == 21) );
  117. nc_result=find_tail( str1, -6 );
  118. BOOST_CHECK(
  119. ( (nc_result.begin()-str1.begin()) == 6) &&
  120. ( (nc_result.end()-str1.begin()) == 21) );
  121. cv_result=find_tail( const_cast<const string&>(str1), 6 );
  122. BOOST_CHECK(
  123. ( (cv_result.begin()-str1.begin()) == 15) &&
  124. ( (cv_result.end()-str1.begin()) == 21) );
  125. ch_result=find_tail( pch1, 6 );
  126. BOOST_CHECK( ( (ch_result.begin() - pch1 ) == 15 ) && ( (ch_result.end() - pch1 ) == 21 ) );
  127. // find_token
  128. BOOST_TEST_CHECKPOINT( "find_token" );
  129. nc_result=find_token( str1, is_any_of("abc"), token_compress_on );
  130. BOOST_CHECK(
  131. ( (nc_result.begin()-str1.begin()) == 3) &&
  132. ( (nc_result.end()-str1.begin()) == 6) );
  133. cv_result=find_token( const_cast<const string&>(str1), is_any_of("abc"), token_compress_on );
  134. BOOST_CHECK(
  135. ( (cv_result.begin()-str1.begin()) == 3) &&
  136. ( (cv_result.end()-str1.begin()) == 6) );
  137. string s1("abc def ghi jkl");
  138. find_iterator<string::iterator> fEnd;
  139. find_iterator<string::iterator> fxIt = make_find_iterator(s1,
  140. token_finder(is_alnum(), token_compress_on));
  141. BOOST_CHECK((fxIt != fEnd) && (*fxIt == string("abc")));
  142. ++fxIt;
  143. BOOST_CHECK((fxIt != fEnd) && (*fxIt == string("def")));
  144. ++fxIt;
  145. BOOST_CHECK((fxIt != fEnd) && (*fxIt == string("ghi")));
  146. ++fxIt;
  147. BOOST_CHECK((fxIt != fEnd) && (*fxIt == string("jkl")));
  148. ++fxIt;
  149. BOOST_CHECK(fxIt == fEnd);
  150. nc_result=find_token( str1, is_any_of("abc"), token_compress_off );
  151. BOOST_CHECK(
  152. ( (nc_result.begin()-str1.begin()) == 3) &&
  153. ( (nc_result.end()-str1.begin()) == 4) );
  154. cv_result=find_token( const_cast<const string&>(str1), is_any_of("abc"), token_compress_off );
  155. BOOST_CHECK(
  156. ( (cv_result.begin()-str1.begin()) == 3) &&
  157. ( (cv_result.end()-str1.begin()) == 4) );
  158. ch_result=find_token( pch1, is_any_of("abc"), token_compress_off );
  159. BOOST_CHECK( ( (ch_result.begin() - pch1 ) == 3 ) && ( (ch_result.end() - pch1 ) == 4 ) );
  160. // generic find
  161. BOOST_TEST_CHECKPOINT( "generic find" );
  162. nc_result=find(str1, first_finder(string("abc")));
  163. BOOST_CHECK(
  164. ( (nc_result.begin()-str1.begin()) == 3) &&
  165. ( (nc_result.end()-str1.begin()) == 6) );
  166. cv_result=find(const_cast<const string&>(str1), first_finder(str2) );
  167. BOOST_CHECK(
  168. ( (cv_result.begin()-str1.begin()) == 3) &&
  169. ( (cv_result.end()-str1.begin()) == 6) );
  170. // multi-type comparison test
  171. BOOST_TEST_CHECKPOINT( "multi-type" );
  172. nc_vresult=find_first( vec1, string("abc") );
  173. BOOST_CHECK(
  174. ( (nc_result.begin()-str1.begin()) == 3) &&
  175. ( (nc_result.end()-str1.begin()) == 6) );
  176. cv_vresult=find_first( const_cast<const vector<int>&>(vec1), str2 );
  177. BOOST_CHECK(
  178. ( (cv_result.begin()-str1.begin()) == 3) &&
  179. ( (cv_result.end()-str1.begin()) == 6) );
  180. // overflow test
  181. BOOST_TEST_CHECKPOINT( "overflow" );
  182. nc_result=find_first( str2, string("abcd") );
  183. BOOST_CHECK( nc_result.begin()==nc_result.end() );
  184. cv_result=find_first( const_cast<const string&>(str2), string("abcd") );
  185. BOOST_CHECK( cv_result.begin()==cv_result.end() );
  186. cv_result=find_head( const_cast<const string&>(str2), 4 );
  187. BOOST_CHECK( string( cv_result.begin(), cv_result.end() )== string("abc") );
  188. cv_result=find_tail( const_cast<const string&>(str2), 4 );
  189. BOOST_CHECK( string( cv_result.begin(), cv_result.end() )== string("abc") );
  190. // Empty string test
  191. BOOST_TEST_CHECKPOINT( "empty" );
  192. nc_result=find_first( str3, string("abcd") );
  193. BOOST_CHECK( nc_result.begin()==nc_result.end() );
  194. nc_result=find_first( str1, string("") );
  195. BOOST_CHECK( nc_result.begin()==nc_result.end() );
  196. cv_result=find_first( const_cast<const string&>(str3), string("abcd") );
  197. BOOST_CHECK( cv_result.begin()==cv_result.end() );
  198. cv_result=find_first( const_cast<const string&>(str1), string("") );
  199. BOOST_CHECK( cv_result.begin()==cv_result.end() );
  200. // iterator_range specific tests
  201. ostringstream osstr;
  202. osstr << find_first( str1, "abc" );
  203. BOOST_CHECK( osstr.str()=="abc" );
  204. }
  205. // test main
  206. BOOST_AUTO_TEST_CASE( test_main )
  207. {
  208. find_test();
  209. }