predicate_test.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // Boost string_algo library predicate_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/predicate.hpp>
  8. #include <boost/algorithm/string/classification.hpp>
  9. // Include unit test framework
  10. #define BOOST_TEST_MAIN
  11. #include <boost/test/unit_test.hpp>
  12. #include <string>
  13. #include <vector>
  14. #include <iostream>
  15. #include <functional>
  16. #include <boost/test/test_tools.hpp>
  17. using namespace std;
  18. using namespace boost;
  19. void predicate_test()
  20. {
  21. string str1("123xxx321");
  22. string str1_prefix("123");
  23. string str2("abc");
  24. string str3("");
  25. string str4("abc");
  26. vector<int> vec1( str1.begin(), str1.end() );
  27. // Basic tests
  28. BOOST_CHECK( starts_with( str1, string("123") ) );
  29. BOOST_CHECK( !starts_with( str1, string("1234") ) );
  30. BOOST_CHECK( istarts_with( "aBCxxx", "abc" ) );
  31. BOOST_CHECK( !istarts_with( "aBCxxx", "abcd" ) );
  32. BOOST_CHECK( ends_with( str1, string("321") ) );
  33. BOOST_CHECK( !ends_with( str1, string("123") ) );
  34. BOOST_CHECK( iends_with( "aBCxXx", "XXX" ) );
  35. BOOST_CHECK( !iends_with( "aBCxxX", "xXXX" ) );
  36. BOOST_CHECK( contains( str1, string("xxx") ) );
  37. BOOST_CHECK( !contains( str1, string("yyy") ) );
  38. BOOST_CHECK( icontains( "123XxX321", "xxx" ) );
  39. BOOST_CHECK( !icontains( "123xXx321", "yyy" ) );
  40. BOOST_CHECK( equals( str2, string("abc") ) );
  41. BOOST_CHECK( !equals( str1, string("yyy") ) );
  42. BOOST_CHECK( iequals( "AbC", "abc" ) );
  43. BOOST_CHECK( !iequals( "aBc", "yyy" ) );
  44. BOOST_CHECK( lexicographical_compare("abc", "abd") );
  45. BOOST_CHECK( !lexicographical_compare("abc", "abc") );
  46. BOOST_CHECK( lexicographical_compare("abc", "abd", is_less()) );
  47. BOOST_CHECK( !ilexicographical_compare("aBD", "AbC") );
  48. BOOST_CHECK( ilexicographical_compare("aBc", "AbD") );
  49. BOOST_CHECK( lexicographical_compare("abC", "aBd", is_iless()) );
  50. // multi-type comparison test
  51. BOOST_CHECK( starts_with( vec1, string("123") ) );
  52. BOOST_CHECK( ends_with( vec1, string("321") ) );
  53. BOOST_CHECK( contains( vec1, string("xxx") ) );
  54. BOOST_CHECK( equals( vec1, str1 ) );
  55. // overflow test
  56. BOOST_CHECK( !starts_with( str2, string("abcd") ) );
  57. BOOST_CHECK( !ends_with( str2, string("abcd") ) );
  58. BOOST_CHECK( !contains( str2, string("abcd") ) );
  59. BOOST_CHECK( !equals( str2, string("abcd") ) );
  60. // equal test
  61. BOOST_CHECK( starts_with( str2, string("abc") ) );
  62. BOOST_CHECK( ends_with( str2, string("abc") ) );
  63. BOOST_CHECK( contains( str2, string("abc") ) );
  64. BOOST_CHECK( equals( str2, string("abc") ) );
  65. //! Empty string test
  66. BOOST_CHECK( starts_with( str2, string("") ) );
  67. BOOST_CHECK( ends_with( str2, string("") ) );
  68. BOOST_CHECK( contains( str2, string("") ) );
  69. BOOST_CHECK( equals( str3, string("") ) );
  70. //! Container compatibility test
  71. BOOST_CHECK( starts_with( "123xxx321", "123" ) );
  72. BOOST_CHECK( ends_with( "123xxx321", "321" ) );
  73. BOOST_CHECK( contains( "123xxx321", "xxx" ) );
  74. BOOST_CHECK( equals( "123xxx321", "123xxx321" ) );
  75. }
  76. template<typename Pred, typename Input>
  77. void test_pred(const Pred& pred, const Input& input, bool bYes)
  78. {
  79. // test assignment operator
  80. Pred pred1=pred;
  81. pred1=pred;
  82. pred1=pred1;
  83. if(bYes)
  84. {
  85. BOOST_CHECK( all( input, pred ) );
  86. BOOST_CHECK( all( input, pred1 ) );
  87. }
  88. else
  89. {
  90. BOOST_CHECK( !all( input, pred ) );
  91. BOOST_CHECK( !all( input, pred1 ) );
  92. }
  93. }
  94. #define TEST_CLASS( Pred, YesInput, NoInput )\
  95. {\
  96. test_pred(Pred, YesInput, true); \
  97. test_pred(Pred, NoInput, false); \
  98. }
  99. void classification_test()
  100. {
  101. TEST_CLASS( is_space(), "\n\r\t ", "..." );
  102. TEST_CLASS( is_alnum(), "ab129ABc", "_ab129ABc" );
  103. TEST_CLASS( is_alpha(), "abc", "abc1" );
  104. TEST_CLASS( is_cntrl(), "\n\t\r", "..." );
  105. TEST_CLASS( is_digit(), "1234567890", "abc" );
  106. TEST_CLASS( is_graph(), "123abc.,", " \t" );
  107. TEST_CLASS( is_lower(), "abc", "Aasdf" );
  108. TEST_CLASS( is_print(), "abs", "\003\004asdf" );
  109. TEST_CLASS( is_punct(), ".,;\"", "abc" );
  110. TEST_CLASS( is_upper(), "ABC", "aBc" );
  111. TEST_CLASS( is_xdigit(), "ABC123", "XFD" );
  112. TEST_CLASS( is_any_of( string("abc") ), "aaabbcc", "aaxb" );
  113. TEST_CLASS( is_any_of( "abc" ), "aaabbcc", "aaxb" );
  114. TEST_CLASS( is_from_range( 'a', 'c' ), "aaabbcc", "aaxb" );
  115. TEST_CLASS( !is_classified(std::ctype_base::space), "...", "..\n\r\t " );
  116. TEST_CLASS( ( !is_any_of("abc") && is_from_range('a','e') ) || is_space(), "d e", "abcde" );
  117. // is_any_of test
  118. // TEST_CLASS( !is_any_of(""), "", "aaa" )
  119. TEST_CLASS( is_any_of("a"), "a", "ab" )
  120. TEST_CLASS( is_any_of("ba"), "ab", "abc" )
  121. TEST_CLASS( is_any_of("cba"), "abc", "abcd" )
  122. TEST_CLASS( is_any_of("hgfedcba"), "abcdefgh", "abcdefghi" )
  123. TEST_CLASS( is_any_of("qponmlkjihgfedcba"), "abcdefghijklmnopq", "zzz" )
  124. }
  125. #undef TEST_CLASS
  126. BOOST_AUTO_TEST_CASE( test_main )
  127. {
  128. predicate_test();
  129. classification_test();
  130. }