split_test.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. // Boost string_algo library iterator_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/config.hpp>
  8. #include <boost/algorithm/string/split.hpp>
  9. #include <boost/algorithm/string/classification.hpp>
  10. // equals predicate is used for result comparison
  11. #include <boost/algorithm/string/predicate.hpp>
  12. // Include unit test framework
  13. #define BOOST_TEST_MAIN
  14. #include <boost/test/unit_test.hpp>
  15. #include <string>
  16. #include <vector>
  17. #include <list>
  18. #include <iostream>
  19. #include <boost/test/test_tools.hpp>
  20. using namespace std;
  21. using namespace boost;
  22. template< typename T1, typename T2 >
  23. void deep_compare( const T1& X, const T2& Y )
  24. {
  25. BOOST_REQUIRE( X.size() == Y.size() );
  26. for( unsigned int nIndex=0; nIndex<X.size(); ++nIndex )
  27. {
  28. BOOST_CHECK( equals( X[nIndex], Y[nIndex] ) );
  29. }
  30. }
  31. void iterator_test()
  32. {
  33. string str1("xx-abc--xx-abb");
  34. string str2("Xx-abc--xX-abb-xx");
  35. string str3("xx");
  36. string strempty("");
  37. const char* pch1="xx-abc--xx-abb";
  38. vector<string> tokens;
  39. vector< vector<int> > vtokens;
  40. // find_all tests
  41. find_all(
  42. tokens,
  43. pch1,
  44. "xx" );
  45. BOOST_REQUIRE( tokens.size()==2 );
  46. BOOST_CHECK( tokens[0]==string("xx") );
  47. BOOST_CHECK( tokens[1]==string("xx") );
  48. ifind_all(
  49. tokens,
  50. str2,
  51. "xx" );
  52. BOOST_REQUIRE( tokens.size()==3 );
  53. BOOST_CHECK( tokens[0]==string("Xx") );
  54. BOOST_CHECK( tokens[1]==string("xX") );
  55. BOOST_CHECK( tokens[2]==string("xx") );
  56. find_all(
  57. tokens,
  58. str1,
  59. "xx" );
  60. BOOST_REQUIRE( tokens.size()==2 );
  61. BOOST_CHECK( tokens[0]==string("xx") );
  62. BOOST_CHECK( tokens[1]==string("xx") );
  63. find_all(
  64. vtokens,
  65. str1,
  66. string("xx") );
  67. deep_compare( tokens, vtokens );
  68. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  69. // If using a compiler that supports forwarding references, we should be able to use rvalues, too
  70. find_all(
  71. tokens,
  72. string("xx-abc--xx-abb"),
  73. "xx" );
  74. BOOST_REQUIRE( tokens.size()==2 );
  75. BOOST_CHECK( tokens[0]==string("xx") );
  76. BOOST_CHECK( tokens[1]==string("xx") );
  77. ifind_all(
  78. tokens,
  79. string("Xx-abc--xX-abb-xx"),
  80. "xx" );
  81. BOOST_REQUIRE( tokens.size()==3 );
  82. BOOST_CHECK( tokens[0]==string("Xx") );
  83. BOOST_CHECK( tokens[1]==string("xX") );
  84. BOOST_CHECK( tokens[2]==string("xx") );
  85. #endif
  86. // split tests
  87. split(
  88. tokens,
  89. str2,
  90. is_any_of("xX"),
  91. token_compress_on );
  92. BOOST_REQUIRE( tokens.size()==4 );
  93. BOOST_CHECK( tokens[0]==string("") );
  94. BOOST_CHECK( tokens[1]==string("-abc--") );
  95. BOOST_CHECK( tokens[2]==string("-abb-") );
  96. BOOST_CHECK( tokens[3]==string("") );
  97. split(
  98. tokens,
  99. pch1,
  100. is_any_of("x"),
  101. token_compress_on );
  102. BOOST_REQUIRE( tokens.size()==3 );
  103. BOOST_CHECK( tokens[0]==string("") );
  104. BOOST_CHECK( tokens[1]==string("-abc--") );
  105. BOOST_CHECK( tokens[2]==string("-abb") );
  106. split(
  107. vtokens,
  108. str1,
  109. is_any_of("x"),
  110. token_compress_on );
  111. deep_compare( tokens, vtokens );
  112. split(
  113. tokens,
  114. str1,
  115. is_punct(),
  116. token_compress_off );
  117. BOOST_REQUIRE( tokens.size()==5 );
  118. BOOST_CHECK( tokens[0]==string("xx") );
  119. BOOST_CHECK( tokens[1]==string("abc") );
  120. BOOST_CHECK( tokens[2]==string("") );
  121. BOOST_CHECK( tokens[3]==string("xx") );
  122. BOOST_CHECK( tokens[4]==string("abb") );
  123. split(
  124. tokens,
  125. str3,
  126. is_any_of(","),
  127. token_compress_off);
  128. BOOST_REQUIRE( tokens.size()==1 );
  129. BOOST_CHECK( tokens[0]==string("xx") );
  130. split(
  131. tokens,
  132. strempty,
  133. is_punct(),
  134. token_compress_off);
  135. BOOST_REQUIRE( tokens.size()==1 );
  136. BOOST_CHECK( tokens[0]==string("") );
  137. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  138. // If using a compiler that supports forwarding references, we should be able to use rvalues, too
  139. split(
  140. tokens,
  141. string("Xx-abc--xX-abb-xx"),
  142. is_any_of("xX"),
  143. token_compress_on );
  144. BOOST_REQUIRE( tokens.size()==4 );
  145. BOOST_CHECK( tokens[0]==string("") );
  146. BOOST_CHECK( tokens[1]==string("-abc--") );
  147. BOOST_CHECK( tokens[2]==string("-abb-") );
  148. BOOST_CHECK( tokens[3]==string("") );
  149. #endif
  150. find_iterator<string::iterator> fiter=make_find_iterator(str1, first_finder("xx"));
  151. find_iterator<string::iterator> fiter2;
  152. BOOST_CHECK(equals(*fiter, "xx"));
  153. ++fiter;
  154. fiter2 = fiter;
  155. BOOST_CHECK(equals(*fiter, "xx"));
  156. BOOST_CHECK(equals(*fiter2, "xx"));
  157. ++fiter;
  158. BOOST_CHECK(fiter==find_iterator<string::iterator>());
  159. BOOST_CHECK(equals(*fiter2, "xx"));
  160. ++fiter2;
  161. BOOST_CHECK(fiter2==find_iterator<string::iterator>());
  162. split_iterator<string::iterator> siter=make_split_iterator(str1, token_finder(is_any_of("-"), token_compress_on));
  163. split_iterator<string::iterator> siter2;
  164. BOOST_CHECK(equals(*siter, "xx"));
  165. ++siter;
  166. siter2 = siter;
  167. BOOST_CHECK(equals(*siter, "abc"));
  168. BOOST_CHECK(equals(*siter2, "abc"));
  169. ++siter;
  170. BOOST_CHECK(equals(*siter, "xx"));
  171. BOOST_CHECK(equals(*siter2, "abc"));
  172. ++siter;
  173. BOOST_CHECK(equals(*siter, "abb"));
  174. ++siter;
  175. BOOST_CHECK(siter==split_iterator<string::iterator>(siter));
  176. BOOST_CHECK(siter==split_iterator<string::iterator>());
  177. // Make sure we work with forward iterators
  178. // See bug #7989
  179. list<char> l1;
  180. find_iterator<list<char>::iterator> liter=make_find_iterator(l1, first_finder("xx"));
  181. }
  182. BOOST_AUTO_TEST_CASE( test_main )
  183. {
  184. iterator_test();
  185. }