Improve vacpp support.
[boost.git] / boost / libs / spirit / test / while_tests.cpp
blob020313cf786af4658e67af4b2166abdd84105774
1 /*=============================================================================
2 Phoenix V1.0
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 ==============================================================================*/
9 // vi:ts=4:sw=4:et
10 // Tests for boost::spirit::while_p
11 // [28-Dec-2002]
12 ////////////////////////////////////////////////////////////////////////////////
13 #define qDebug 0
14 #include <iostream>
15 #include <cstring>
16 #if qDebug
17 #define BOOST_SPIRIT_DEBUG
18 #endif
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>
26 namespace local
28 template <typename T>
29 struct var_wrapper
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(); }
39 template <typename T>
40 inline var_wrapper<T>
41 var(T& t)
43 return var_wrapper<T>(t);
47 namespace
49 template <typename T>
50 class add_actor
52 public:
53 explicit add_actor(T &ref_) : ref(ref_) {}
55 template <typename T2>
56 void operator()(T2 const &val) const
57 { ref += val; }
59 private:
60 T& ref;
63 template <typename T>
64 inline add_actor<T> const
65 add(T& ref)
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;
82 rule_t while_rule;
83 rule_t do_while_rule;
85 void
86 test_while(
87 char const *s,
88 unsigned int wanted,
89 rule_t const &r,
90 unsigned int iterations_wanted)
92 using namespace std;
94 ++test_count;
96 number_result = 0;
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)))
106 result = bad;
108 if (result==good)
109 cout << "PASSED";
110 else
112 ++error_count;
113 cout << "FAILED";
116 cout << ": \"" << s << "\" ==> ";
117 if (!m.full)
118 cout << "<error>";
119 else
120 cout << number_result;
121 cout << " " << iterations_performed << " of "
122 << iterations_wanted << " iterations\n";
125 template<typename T>
126 struct inc_actor
128 explicit inc_actor(T &t) : var(t) {}
129 template<typename IteratorT>
130 void operator()(IteratorT const &, IteratorT const &) const
132 ++var;
134 template<typename U>
135 void operator()(U) const
137 ++var;
139 T &var;
142 template<typename T>
143 inc_actor<T>
144 inc(T &t)
146 return inc_actor<T>(t);
150 main()
152 using namespace std;
153 using ::boost::spirit::uint_p;
154 using ::boost::spirit::while_p;
155 using ::boost::spirit::do_p;
156 using ::boost::spirit::assign_a;
158 #if qDebug
159 BOOST_SPIRIT_DEBUG_RULE(while_rule);
160 BOOST_SPIRIT_DEBUG_RULE(do_while_rule);
161 #endif
163 while_rule
164 = uint_p[assign_a(number_result)]
165 >> while_p('+')
167 uint_p[add(number_result)][inc(iterations_performed)]
170 do_while_rule
171 = do_p
173 uint_p[add(number_result)][inc(iterations_performed)]
174 ].while_p('+');
176 cout << "/////////////////////////////////////////////////////////\n";
177 cout << "\n";
178 cout << " while_p test\n";
179 cout << "\n";
180 cout << "/////////////////////////////////////////////////////////\n";
181 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);
197 std::cout << "\n ";
198 if (error_count==0)
199 cout << "All " << test_count << " while_p-tests passed.\n"
200 << "Test concluded successfully\n";
201 else
202 cout << error_count << " of " << test_count << " while_p-tests failed\n"
203 << "Test failed\n";
205 return error_count!=0;