test_skip.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // test_skip.hpp
  3. //
  4. // Copyright 2008 Eric Niebler. Distributed under the Boost
  5. // Software License, Version 1.0. (See accompanying file
  6. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. #include <map>
  8. #include <iostream>
  9. #include <boost/xpressive/xpressive.hpp>
  10. #include <boost/xpressive/regex_actions.hpp>
  11. #include <boost/test/unit_test.hpp>
  12. using namespace boost::unit_test;
  13. using namespace boost::xpressive;
  14. void test1()
  15. {
  16. std::string s = "a a b b c c";
  17. sregex rx =
  18. "a a" >>
  19. skip(_s)
  20. (
  21. (s1= as_xpr('b')) >>
  22. as_xpr('b') >>
  23. *as_xpr('c') // causes backtracking
  24. ) >>
  25. "c c";
  26. smatch what;
  27. BOOST_CHECK( regex_match(s, what, rx) );
  28. s = "123,456,789";
  29. sregex rx2 = skip(',')(+_d);
  30. BOOST_CHECK( regex_match(s, what, rx2) );
  31. s = "foo";
  32. sregex rx3 = skip(_s)(after("fo") >> 'o');
  33. BOOST_CHECK( regex_search(s, what, rx3) );
  34. }
  35. template<typename Expr>
  36. void test_skip_aux(Expr const &expr)
  37. {
  38. sregex rx = skip(_s)(expr);
  39. }
  40. void test_skip()
  41. {
  42. int i=0;
  43. std::map<std::string, int> syms;
  44. std::locale loc;
  45. test_skip_aux( 'a' );
  46. test_skip_aux( _ );
  47. test_skip_aux( +_ );
  48. test_skip_aux( -+_ );
  49. test_skip_aux( !_ );
  50. test_skip_aux( -!_ );
  51. test_skip_aux( repeat<0,42>(_) );
  52. test_skip_aux( -repeat<0,42>(_) );
  53. test_skip_aux( _ >> 'a' );
  54. test_skip_aux( _ >> 'a' | _ );
  55. test_skip_aux( _ >> 'a' | _ >> 'b' );
  56. test_skip_aux( s1= _ >> 'a' | _ >> 'b' );
  57. test_skip_aux( icase(_ >> 'a' | _ >> 'b') );
  58. test_skip_aux( imbue(loc)(_ >> 'a' | _ >> 'b') );
  59. test_skip_aux( (set='a') );
  60. test_skip_aux( (set='a','b') );
  61. test_skip_aux( ~(set='a') );
  62. test_skip_aux( ~(set='a','b') );
  63. test_skip_aux( range('a','b') );
  64. test_skip_aux( ~range('a','b') );
  65. test_skip_aux( set['a' | alpha] );
  66. test_skip_aux( ~set['a' | alpha] );
  67. test_skip_aux( before(_) );
  68. test_skip_aux( ~before(_) );
  69. test_skip_aux( after(_) );
  70. test_skip_aux( ~after(_) );
  71. test_skip_aux( keep(*_) );
  72. test_skip_aux( (*_)[ref(i) = as<int>(_) + 1] );
  73. test_skip_aux( (a1= syms)[ref(i) = a1 + 1] );
  74. }
  75. ///////////////////////////////////////////////////////////////////////////////
  76. // init_unit_test_suite
  77. //
  78. test_suite* init_unit_test_suite( int argc, char* argv[] )
  79. {
  80. test_suite *test = BOOST_TEST_SUITE("test skip()");
  81. test->add(BOOST_TEST_CASE(&test1));
  82. return test;
  83. }