trim_test.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. // Boost string_algo library trim_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/trim.hpp>
  8. #include <boost/algorithm/string/trim_all.hpp>
  9. // Include unit test framework
  10. #define BOOST_TEST_MAIN
  11. #include <boost/test/unit_test.hpp>
  12. #include <string>
  13. #include <iostream>
  14. #include <boost/test/test_tools.hpp>
  15. using namespace std;
  16. using namespace boost;
  17. void trim_test()
  18. {
  19. string str1(" 1x x x x1 ");
  20. string str2(" 2x x x x2 ");
  21. string str3(" ");
  22. // *** value passing tests *** //
  23. // general string test
  24. BOOST_CHECK( trim_left_copy( str1 )=="1x x x x1 " ) ;
  25. BOOST_CHECK( trim_right_copy( str1 )==" 1x x x x1" ) ;
  26. BOOST_CHECK( trim_copy( str1 )=="1x x x x1" ) ;
  27. // spaces-only string test
  28. BOOST_CHECK( trim_left_copy( str3 )=="" );
  29. BOOST_CHECK( trim_right_copy( str3 )=="" );
  30. BOOST_CHECK( trim_copy( str3 )=="" );
  31. // empty string check
  32. BOOST_CHECK( trim_left_copy( string("") )=="" );
  33. BOOST_CHECK( trim_right_copy( string("") )=="" );
  34. BOOST_CHECK( trim_copy( string("") )=="" );
  35. // iterator tests
  36. string str;
  37. trim_left_copy_if( std::back_inserter(str), str1, is_space() );
  38. BOOST_CHECK( str=="1x x x x1 " );
  39. str.clear();
  40. trim_right_copy_if( std::back_inserter(str), str1, is_space() );
  41. BOOST_CHECK( str==" 1x x x x1" );
  42. str.clear();
  43. trim_copy_if( std::back_inserter(str), str1, is_space() );
  44. BOOST_CHECK( str=="1x x x x1" );
  45. str.clear();
  46. trim_left_copy_if(
  47. std::back_inserter(str),
  48. " 1x x x x1 ",
  49. is_space() );
  50. BOOST_CHECK( str=="1x x x x1 " );
  51. str.clear();
  52. trim_right_copy_if(
  53. std::back_inserter(str),
  54. " 1x x x x1 ",
  55. is_space() );
  56. BOOST_CHECK( str==" 1x x x x1" );
  57. str.clear();
  58. trim_copy_if(
  59. std::back_inserter(str),
  60. " 1x x x x1 ",
  61. is_space() );
  62. BOOST_CHECK( str=="1x x x x1" );
  63. // *** inplace tests *** //
  64. // general string test
  65. trim_left( str1 );
  66. BOOST_CHECK( str1=="1x x x x1 " );
  67. trim_right( str1 );
  68. BOOST_CHECK( str1=="1x x x x1" );
  69. trim( str2 );
  70. BOOST_CHECK( str2=="2x x x x2" );
  71. // spaces-only string test
  72. str3 = " "; trim_left( str3 );
  73. BOOST_CHECK( str3=="" );
  74. str3 = " "; trim_right( str3 );
  75. BOOST_CHECK( str3=="" );
  76. str3 = " "; trim( str3 );
  77. BOOST_CHECK( str3=="" );
  78. // empty string check
  79. str3 = ""; trim_left( str3 );
  80. BOOST_CHECK( str3=="" );
  81. str3 = ""; trim_right( str3 );
  82. BOOST_CHECK( str3=="" );
  83. str3 = ""; trim( str3 );
  84. BOOST_CHECK( str3=="" );
  85. // *** non-standard predicate tests *** //
  86. BOOST_CHECK(
  87. trim_copy_if(
  88. string("123abc456"),
  89. is_classified(std::ctype_base::digit) )=="abc" );
  90. BOOST_CHECK( trim_copy_if( string("<>abc<>"), is_any_of( "<<>>" ) )=="abc" );
  91. }
  92. void trim_all_test()
  93. {
  94. string str1(" 1x x x x1 ");
  95. string str2("+---...2x+--x--+x-+-x2...---+");
  96. string str3(" ");
  97. // *** value passing tests *** //
  98. // general string test
  99. BOOST_CHECK( trim_all_copy( str1 )=="1x x x x1" ) ;
  100. BOOST_CHECK( trim_all_copy_if( str2, is_punct() )=="2x+x-x-x2" ) ;
  101. // spaces-only string test
  102. BOOST_CHECK( trim_all_copy( str3 )=="" );
  103. // empty string check
  104. BOOST_CHECK( trim_all_copy( string("") )=="" );
  105. // general string test
  106. trim_all( str1 );
  107. BOOST_CHECK( str1=="1x x x x1" ) ;
  108. trim_all_if( str2, is_punct() );
  109. BOOST_CHECK( str2=="2x+x-x-x2" ) ;
  110. // spaces-only string test
  111. str3 = " "; trim_all( str3 );
  112. BOOST_CHECK( str3=="" );
  113. // empty string check
  114. str3 = ""; trim_all( str3 );
  115. BOOST_CHECK( str3=="" );
  116. BOOST_CHECK( str3=="" );
  117. // *** non-standard predicate tests *** //
  118. BOOST_CHECK(
  119. trim_all_copy_if(
  120. string("123abc127deb456"),
  121. is_classified(std::ctype_base::digit) )=="abc1deb" );
  122. BOOST_CHECK( trim_all_copy_if( string("<>abc<>def<>"), is_any_of( "<<>>" ) )=="abc<def" );
  123. }
  124. void trim_fill_test()
  125. {
  126. string str1(" 1x x x x1 ");
  127. string str2("+---...2x+--x--+x-+-x2...---+");
  128. string str3(" ");
  129. // *** value passing tests *** //
  130. // general string test
  131. BOOST_CHECK( trim_fill_copy( str1, "-" )=="1x-x-x-x1" ) ;
  132. BOOST_CHECK( trim_fill_copy_if( str2, " ", is_punct() )=="2x x x x2" ) ;
  133. // spaces-only string test
  134. BOOST_CHECK( trim_fill_copy( str3, " " )=="" );
  135. // empty string check
  136. BOOST_CHECK( trim_fill_copy( string(""), " " )=="" );
  137. // general string test
  138. trim_fill( str1, "-" );
  139. BOOST_CHECK( str1=="1x-x-x-x1" ) ;
  140. trim_fill_if( str2, "", is_punct() );
  141. BOOST_CHECK( str2=="2xxxx2" ) ;
  142. // spaces-only string test
  143. str3 = " "; trim_fill( str3, "" );
  144. BOOST_CHECK( str3=="" );
  145. // empty string check
  146. str3 = ""; trim_fill( str3, "" );
  147. BOOST_CHECK( str3=="" );
  148. BOOST_CHECK( str3=="" );
  149. // *** non-standard predicate tests *** //
  150. BOOST_CHECK(
  151. trim_fill_copy_if(
  152. string("123abc127deb456"),
  153. "+",
  154. is_classified(std::ctype_base::digit) )=="abc+deb" );
  155. BOOST_CHECK( trim_fill_copy_if( string("<>abc<>def<>"), "-", is_any_of( "<<>>" ) )=="abc-def" );
  156. }
  157. BOOST_AUTO_TEST_CASE( test_main )
  158. {
  159. trim_test();
  160. trim_all_test();
  161. trim_fill_test();
  162. }