1 /*=============================================================================
3 Copyright (c) 2002-2003 Martin Wille
5 Use, modification and distribution is subject to the Boost Software
6 License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
7 http://www.boost.org/LICENSE_1_0.txt)
8 ==============================================================================*/
10 // Tests for boost::spirit::while_p
12 ////////////////////////////////////////////////////////////////////////////////
17 #define BOOST_SPIRIT_DEBUG
20 #include "impl/string_length.hpp"
21 #include <boost/spirit/core.hpp>
22 #include <boost/spirit/actor/assign_actor.hpp>
23 #include <boost/spirit/dynamic/while.hpp>
24 #include <boost/ref.hpp>
30 : public ::boost::reference_wrapper
<T
>
32 typedef boost::reference_wrapper
<T
> parent
;
34 explicit inline var_wrapper(T
& t
) : parent(t
) {}
36 inline T
& operator()() const { return parent::get(); }
43 return var_wrapper
<T
>(t
);
53 explicit add_actor(T
&ref_
) : ref(ref_
) {}
55 template <typename T2
>
56 void operator()(T2
const &val
) const
64 inline add_actor
<T
> const
67 return add_actor
<T
>(ref
);
71 typedef boost::spirit::rule
<> rule_t
;
73 unsigned int test_count
= 0;
74 unsigned int error_count
= 0;
75 unsigned int iterations_performed
;
77 unsigned int number_result
;
78 static const unsigned int kError
= 999;
79 static const bool good
= true;
80 static const bool bad
= false;
90 unsigned int iterations_wanted
)
97 iterations_performed
= 0;
99 boost::spirit::parse_info
<> m
= boost::spirit::parse(s
, s
+ test_impl::string_length(s
), r
);
101 bool result
= wanted
== kError
?(m
.full
?bad
:good
): (number_result
==wanted
);
103 result
&= iterations_performed
== iterations_wanted
;
105 if (m
.full
&& (m
.length
!= test_impl::string_length(s
)))
116 cout
<< ": \"" << s
<< "\" ==> ";
120 cout
<< number_result
;
121 cout
<< " " << iterations_performed
<< " of "
122 << iterations_wanted
<< " iterations\n";
128 explicit inc_actor(T
&t
) : var(t
) {}
129 template<typename IteratorT
>
130 void operator()(IteratorT
const &, IteratorT
const &) const
135 void operator()(U
) const
146 return inc_actor
<T
>(t
);
153 using ::boost::spirit::uint_p
;
154 using ::boost::spirit::while_p
;
155 using ::boost::spirit::do_p
;
156 using ::boost::spirit::assign_a
;
159 BOOST_SPIRIT_DEBUG_RULE(while_rule
);
160 BOOST_SPIRIT_DEBUG_RULE(do_while_rule
);
164 = uint_p
[assign_a(number_result
)]
167 uint_p
[add(number_result
)][inc(iterations_performed
)]
173 uint_p
[add(number_result
)][inc(iterations_performed
)]
176 cout
<< "/////////////////////////////////////////////////////////\n";
178 cout
<< " while_p test\n";
180 cout
<< "/////////////////////////////////////////////////////////\n";
183 cout
<< "while_p()[]\n";
184 test_while("", kError
, while_rule
, 0);
185 test_while("1", 1, while_rule
, 0);
186 test_while("1+1", 2, while_rule
, 1);
187 test_while("1+1+12", 14, while_rule
, 2);
188 test_while("1+1+x", kError
, while_rule
, 1);
190 cout
<< "do_p[].while_p()\n";
191 test_while("", kError
, do_while_rule
, 0);
192 test_while("1", 1, do_while_rule
, 1);
193 test_while("1+1", 2, do_while_rule
, 2);
194 test_while("1+1+12", 14, do_while_rule
, 3);
195 test_while("1+1+x", kError
, do_while_rule
, 2);
199 cout
<< "All " << test_count
<< " while_p-tests passed.\n"
200 << "Test concluded successfully\n";
202 cout
<< error_count
<< " of " << test_count
<< " while_p-tests failed\n"
205 return error_count
!=0;