1 /*=============================================================================
2 Copyright (c) 2004 Stefan Slapeta
3 Copyright (c) 2002-2003 Martin Wille
4 http://spirit.sourceforge.net/
6 Use, modification and distribution is subject to the Boost Software
7 License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
8 http://www.boost.org/LICENSE_1_0.txt)
9 =============================================================================*/
11 // Tests for BOOST_SPIRIT_CLASSIC_NS::if_p
13 ////////////////////////////////////////////////////////////////////////////////
16 #include <boost/spirit/include/classic_core.hpp>
17 #include <boost/spirit/include/classic_if.hpp>
18 #include <boost/spirit/include/classic_assign_actor.hpp>
19 #include <boost/ref.hpp>
20 #include "impl/string_length.hpp"
26 : public ::boost::reference_wrapper
<T
>
28 typedef ::boost::reference_wrapper
<T
> parent
;
30 explicit inline var_wrapper(T
& t
) : parent(t
) {}
32 inline T
& operator()() const { return parent::get(); }
39 return var_wrapper
<T
>(t
);
43 typedef ::BOOST_SPIRIT_CLASSIC_NS::rule
<> rule_t
;
44 typedef ::BOOST_SPIRIT_CLASSIC_NS::rule
<BOOST_SPIRIT_CLASSIC_NS::no_actions_scanner
<>::type
>
47 unsigned int test_count
= 0;
48 unsigned int error_count
= 0;
50 unsigned int number_result
;
51 static const unsigned int kError
= 999;
52 static const bool good
= true;
53 static const bool bad
= false;
56 no_actions_rule_t oct_prefix
;
57 rule_t hex_rule
, oct_rule
, dec_rule
;
59 rule_t auto_number_rule
;
60 rule_t hex_or_dec_number_rule
;
63 test_number(char const *s
, unsigned int wanted
, rule_t
const &r
)
69 number_result
= wanted
-1;
70 ::BOOST_SPIRIT_CLASSIC_NS::parse_info
<> m
= ::BOOST_SPIRIT_CLASSIC_NS::parse(s
, s
+ test_impl::string_length(s
), r
);
72 bool result
= wanted
== kError
?(m
.full
?bad
:good
): (number_result
==wanted
);
74 if (m
.full
&& (m
.length
!= test_impl::string_length(s
)))
86 cout
<< ": \"" << s
<< "\" ==> ";
87 if (number_result
==wanted
-1)
90 cout
<< number_result
;
100 using ::BOOST_SPIRIT_CLASSIC_NS::if_p
;
101 using ::BOOST_SPIRIT_CLASSIC_NS::str_p
;
102 using ::BOOST_SPIRIT_CLASSIC_NS::nothing_p
;
104 cout
<< "\nfail enclosed parser:\n";
106 const char *p
= "abc";
108 ::BOOST_SPIRIT_CLASSIC_NS::strlit
<const char*> success_p
= str_p(p
);
109 ::BOOST_SPIRIT_CLASSIC_NS::strlit
<const char*> fail_p
= str_p("xxx");
111 ::BOOST_SPIRIT_CLASSIC_NS::rule
<> r
= if_p(success_p
)[nothing_p
];
113 ::BOOST_SPIRIT_CLASSIC_NS::parse_info
<> m
= ::BOOST_SPIRIT_CLASSIC_NS::parse(p
, r
);
116 cout
<< "FAILED: if --> match" << endl
;
119 cout
<< "PASSED: if --> no_match" << endl
;
122 r
= if_p(fail_p
)[success_p
].else_p
[nothing_p
];
124 m
= ::BOOST_SPIRIT_CLASSIC_NS::parse(p
, r
);
127 cout
<< "FAILED: else --> match" << endl
;
130 cout
<< "PASSED: else --> no_match" << endl
;
138 using ::BOOST_SPIRIT_CLASSIC_NS::if_p
;
139 using ::BOOST_SPIRIT_CLASSIC_NS::uint_p
;
140 using ::BOOST_SPIRIT_CLASSIC_NS::oct_p
;
141 using ::BOOST_SPIRIT_CLASSIC_NS::hex_p
;
142 using ::BOOST_SPIRIT_CLASSIC_NS::str_p
;
143 using ::BOOST_SPIRIT_CLASSIC_NS::ch_p
;
144 using ::BOOST_SPIRIT_CLASSIC_NS::assign_a
;
146 cout
<< "/////////////////////////////////////////////////////////\n";
148 cout
<< " if_p test\n";
150 cout
<< "/////////////////////////////////////////////////////////\n";
156 BOOST_SPIRIT_DEBUG_RULE(hex_prefix
);
157 BOOST_SPIRIT_DEBUG_RULE(hex_rule
);
158 BOOST_SPIRIT_DEBUG_RULE(oct_prefix
);
159 BOOST_SPIRIT_DEBUG_RULE(oct_rule
);
160 BOOST_SPIRIT_DEBUG_RULE(dec_rule
);
161 BOOST_SPIRIT_DEBUG_RULE(auto_number_rule
);
162 BOOST_SPIRIT_DEBUG_RULE(hex_or_dec_number_rule
);
165 hex_prefix
= str_p("0x");
166 oct_prefix
= ch_p('0');
168 hex_rule
= hex_p
[assign_a(number_result
)];
169 oct_rule
= oct_p
[assign_a(number_result
)];
170 dec_rule
= uint_p
[assign_a(number_result
)];
177 if_p(::BOOST_SPIRIT_CLASSIC_NS::eps_p(oct_prefix
))
183 hex_or_dec_number_rule
=
184 if_p(local::var(as_hex
))[hex_prefix
>>hex_rule
].else_p
[dec_rule
];
187 test_number("", kError
, auto_number_rule
);
188 test_number("0", 0, auto_number_rule
);
189 test_number("1", 1, auto_number_rule
);
190 test_number("00", 0, auto_number_rule
);
191 test_number("0x", kError
, auto_number_rule
);
192 test_number("0x0", 0, auto_number_rule
);
193 test_number("0755", 493, auto_number_rule
);
194 test_number("0x100", 256, auto_number_rule
);
196 cout
<< "\ndecimal:\n";
198 test_number("", kError
, hex_or_dec_number_rule
);
199 test_number("100", 100, hex_or_dec_number_rule
);
200 test_number("0x100", kError
, hex_or_dec_number_rule
);
201 test_number("0xff", kError
, hex_or_dec_number_rule
);
203 cout
<< "\nhexadecimal:\n";
205 test_number("", kError
, hex_or_dec_number_rule
);
206 test_number("0x100", 256, hex_or_dec_number_rule
);
207 test_number("0xff", 255, hex_or_dec_number_rule
);
209 //////////////////////////////////
210 // tests for if_p without else-parser
211 cout
<< "\nno-else:\n";
212 rule_t r
= if_p(::BOOST_SPIRIT_CLASSIC_NS::eps_p('0'))[oct_rule
];
214 test_number("0", 0, r
);
217 ::BOOST_SPIRIT_CLASSIC_NS::parse_info
<> m
= ::BOOST_SPIRIT_CLASSIC_NS::parse("", r
);
218 if (!m
.hit
|| !m
.full
|| m
.length
!=0)
220 std::cout
<< "FAILED: \"\" ==> <error>\n";
224 std::cout
<< "PASSED: \"\" ==> <empty match>\n";
227 m
= ::BOOST_SPIRIT_CLASSIC_NS::parse("junk", r
);
228 if (!m
.hit
|| m
.full
|| m
.length
!=0)
230 std::cout
<< "FAILED: \"junk\" ==> <error>\n";
234 std::cout
<< "PASSED: \"junk\" ==> <empty match>\n";
236 test_enclosed_fail();
239 //////////////////////////////////
243 cout
<< "All " << test_count
<< " if_p-tests passed.\n"
244 << "Test concluded successfully\n";
246 cout
<< error_count
<< " of " << test_count
<< " if_p-tests failed\n"
249 return error_count
!=0;