]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/xpressive/test/test_dynamic_grammar.cpp
1 //////////////////////////////////////////////////////////////////////////////
2 // test_dynamic_grammar.cpp
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)
11 5 March 2007 : Initial version.
14 // defining this causes regex_impl objects to be counted, allowing us to detect
16 #define BOOST_XPRESSIVE_DEBUG_CYCLE_TEST
18 #include <boost/xpressive/xpressive.hpp>
19 #include <boost/test/unit_test.hpp>
21 void test_dynamic_grammar()
23 using namespace boost::xpressive
;
28 sregex_compiler compiler
;
29 regex_constants::syntax_option_type x
= regex_constants::ignore_white_space
;
31 compiler
.compile( "(? $group = ) \\( (? $expr ) \\) ", x
);
32 compiler
.compile( "(? $factor = ) \\d+ | (? $group ) ", x
);
33 compiler
.compile( "(? $term = ) (? $factor ) (?: \\* (? $factor ) | / (? $factor ) )* ", x
);
34 expr
= compiler
.compile( "(? $expr = ) (? $term ) (?: \\+ (? $term ) | - (? $term ) )* ", x
);
37 std::string
str("foo 9*(10+3) bar");
40 if(regex_search(str
, what
, expr
))
42 BOOST_CHECK_EQUAL(what
[0].str(), "9*(10+3)");
43 BOOST_CHECK_EQUAL((*what
.nested_results().begin())[0].str(), "9*(10+3)");
44 BOOST_CHECK_EQUAL((*(*what
.nested_results().begin()).nested_results().begin())[0].str(), "9");
45 BOOST_CHECK_EQUAL((*++(*what
.nested_results().begin()).nested_results().begin())[0].str(), "(10+3)");
49 BOOST_ERROR("regex_search test 1 failed");
53 // Test that all regex_impl instances have been cleaned up correctly
54 BOOST_CHECK_EQUAL(0, detail::regex_impl
<std::string::const_iterator
>::instances
);
57 void test_dynamic_grammar2()
59 using namespace boost::xpressive
;
64 sregex_compiler compiler
;
65 regex_constants::syntax_option_type x
= regex_constants::ignore_white_space
;
67 compiler
["group"] = compiler
.compile( "\\( (? $expr ) \\) ", x
);
68 compiler
["factor"] = compiler
.compile( "\\d+ | (? $group ) ", x
);
69 compiler
["term"] = compiler
.compile( "(? $factor ) (?: \\* (? $factor ) | / (? $factor ) )* ", x
);
70 compiler
["expr"] = compiler
.compile( "(? $term ) (?: \\+ (? $term ) | - (? $term ) )* ", x
);
72 expr
= compiler
["expr"];
75 std::string
str("foo 9*(10+3) bar");
78 if(regex_search(str
, what
, expr
))
80 BOOST_CHECK_EQUAL(what
[0].str(), "9*(10+3)");
81 BOOST_CHECK_EQUAL((*what
.nested_results().begin())[0].str(), "9*(10+3)");
82 BOOST_CHECK_EQUAL((*(*what
.nested_results().begin()).nested_results().begin())[0].str(), "9");
83 BOOST_CHECK_EQUAL((*++(*what
.nested_results().begin()).nested_results().begin())[0].str(), "(10+3)");
87 BOOST_ERROR("regex_search test 2 failed");
91 // Test that all regex_impl instances have been cleaned up correctly
92 BOOST_CHECK_EQUAL(0, detail::regex_impl
<std::string::const_iterator
>::instances
);
95 using namespace boost
;
96 using namespace unit_test
;
98 ///////////////////////////////////////////////////////////////////////////////
99 // init_unit_test_suite
101 test_suite
* init_unit_test_suite( int argc
, char* argv
[] )
103 test_suite
*test
= BOOST_TEST_SUITE("testing dynamic grammars");
104 test
->add(BOOST_TEST_CASE(&test_dynamic_grammar
));
105 test
->add(BOOST_TEST_CASE(&test_dynamic_grammar2
));