test_dynamic_grammar.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. //////////////////////////////////////////////////////////////////////////////
  2. // test_dynamic_grammar.cpp
  3. //
  4. // (C) Copyright Eric Niebler 2004.
  5. // Use, modification and distribution are subject to the
  6. // Boost Software License, Version 1.0. (See accompanying file
  7. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  8. /*
  9. Revision history:
  10. 5 March 2007 : Initial version.
  11. */
  12. // defining this causes regex_impl objects to be counted, allowing us to detect
  13. // leaks portably.
  14. #define BOOST_XPRESSIVE_DEBUG_CYCLE_TEST
  15. #include <boost/xpressive/xpressive.hpp>
  16. #include <boost/test/unit_test.hpp>
  17. void test_dynamic_grammar()
  18. {
  19. using namespace boost::xpressive;
  20. {
  21. sregex expr;
  22. {
  23. sregex_compiler compiler;
  24. regex_constants::syntax_option_type x = regex_constants::ignore_white_space;
  25. compiler.compile( "(? $group = ) \\( (? $expr ) \\) ", x);
  26. compiler.compile( "(? $factor = ) \\d+ | (? $group ) ", x);
  27. compiler.compile( "(? $term = ) (? $factor ) (?: \\* (? $factor ) | / (? $factor ) )* ", x);
  28. expr = compiler.compile( "(? $expr = ) (? $term ) (?: \\+ (? $term ) | - (? $term ) )* ", x);
  29. }
  30. std::string str("foo 9*(10+3) bar");
  31. smatch what;
  32. if(regex_search(str, what, expr))
  33. {
  34. BOOST_CHECK_EQUAL(what[0].str(), "9*(10+3)");
  35. BOOST_CHECK_EQUAL((*what.nested_results().begin())[0].str(), "9*(10+3)");
  36. BOOST_CHECK_EQUAL((*(*what.nested_results().begin()).nested_results().begin())[0].str(), "9");
  37. BOOST_CHECK_EQUAL((*++(*what.nested_results().begin()).nested_results().begin())[0].str(), "(10+3)");
  38. }
  39. else
  40. {
  41. BOOST_ERROR("regex_search test 1 failed");
  42. }
  43. }
  44. // Test that all regex_impl instances have been cleaned up correctly
  45. BOOST_CHECK_EQUAL(0, detail::regex_impl<std::string::const_iterator>::instances);
  46. }
  47. void test_dynamic_grammar2()
  48. {
  49. using namespace boost::xpressive;
  50. {
  51. sregex expr;
  52. {
  53. sregex_compiler compiler;
  54. regex_constants::syntax_option_type x = regex_constants::ignore_white_space;
  55. compiler["group"] = compiler.compile( "\\( (? $expr ) \\) ", x);
  56. compiler["factor"] = compiler.compile( "\\d+ | (? $group ) ", x);
  57. compiler["term"] = compiler.compile( "(? $factor ) (?: \\* (? $factor ) | / (? $factor ) )* ", x);
  58. compiler["expr"] = compiler.compile( "(? $term ) (?: \\+ (? $term ) | - (? $term ) )* ", x);
  59. expr = compiler["expr"];
  60. }
  61. std::string str("foo 9*(10+3) bar");
  62. smatch what;
  63. if(regex_search(str, what, expr))
  64. {
  65. BOOST_CHECK_EQUAL(what[0].str(), "9*(10+3)");
  66. BOOST_CHECK_EQUAL((*what.nested_results().begin())[0].str(), "9*(10+3)");
  67. BOOST_CHECK_EQUAL((*(*what.nested_results().begin()).nested_results().begin())[0].str(), "9");
  68. BOOST_CHECK_EQUAL((*++(*what.nested_results().begin()).nested_results().begin())[0].str(), "(10+3)");
  69. }
  70. else
  71. {
  72. BOOST_ERROR("regex_search test 2 failed");
  73. }
  74. }
  75. // Test that all regex_impl instances have been cleaned up correctly
  76. BOOST_CHECK_EQUAL(0, detail::regex_impl<std::string::const_iterator>::instances);
  77. }
  78. using namespace boost;
  79. using namespace unit_test;
  80. ///////////////////////////////////////////////////////////////////////////////
  81. // init_unit_test_suite
  82. //
  83. test_suite* init_unit_test_suite( int argc, char* argv[] )
  84. {
  85. test_suite *test = BOOST_TEST_SUITE("testing dynamic grammars");
  86. test->add(BOOST_TEST_CASE(&test_dynamic_grammar));
  87. test->add(BOOST_TEST_CASE(&test_dynamic_grammar2));
  88. return test;
  89. }