Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_34_1/libs/spirit/test/while_tests.cpp @ 33

Last change on this file since 33 was 29, checked in by landauf, 17 years ago

updated boost from 1_33_1 to 1_34_1

File size: 4.9 KB
Line 
1/*=============================================================================
2    Phoenix V1.0
3    Copyright (c) 2002-2003 Martin Wille
4
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
19
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>
25
26namespace local
27{
28    template <typename T>
29    struct var_wrapper
30        : public ::boost::reference_wrapper<T>
31    {
32        typedef boost::reference_wrapper<T> parent;
33
34        explicit inline var_wrapper(T& t) : parent(t) {}
35
36        inline T& operator()() const { return parent::get(); }
37    };
38
39    template <typename T>
40    inline var_wrapper<T>
41    var(T& t)
42    {
43        return var_wrapper<T>(t);
44    }
45}
46
47namespace
48{
49    template <typename T>
50    class add_actor
51    {
52    public:
53        explicit add_actor(T &ref_) : ref(ref_) {}
54
55        template <typename T2>
56        void operator()(T2 const &val) const
57        { ref += val; }
58
59    private:
60        T& ref;
61    };
62
63    template <typename T>
64    inline add_actor<T> const
65    add(T& ref)
66    {
67        return add_actor<T>(ref);
68    }
69}
70
71typedef boost::spirit::rule<> rule_t;
72
73unsigned int test_count = 0;
74unsigned int error_count = 0;
75unsigned int iterations_performed;
76
77unsigned int number_result;
78static const unsigned int kError = 999;
79static const bool good = true;
80static const bool bad = false;
81
82rule_t while_rule;
83rule_t do_while_rule;
84
85void
86test_while(
87    char const *s,
88    unsigned int wanted,
89    rule_t const &r,
90    unsigned int iterations_wanted)
91{
92    using namespace std;
93   
94    ++test_count;
95
96    number_result = 0;
97    iterations_performed = 0;
98
99    boost::spirit::parse_info<> m = boost::spirit::parse(s, s+ test_impl::string_length(s), r);
100
101    bool result = wanted == kError?(m.full?bad:good): (number_result==wanted);
102
103    result &= iterations_performed == iterations_wanted;
104
105    if (m.full && (m.length != test_impl::string_length(s)))
106        result = bad;
107
108    if (result==good)
109        cout << "PASSED";
110    else
111    {
112        ++error_count;
113        cout << "FAILED";
114    }
115
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";
123}
124
125template<typename T>
126struct inc_actor
127{
128    explicit inc_actor(T &t) : var(t) {}
129    template<typename IteratorT>
130    void operator()(IteratorT const &, IteratorT const &) const
131    {
132        ++var;
133    }
134    template<typename U>
135    void operator()(U) const
136    {
137        ++var;
138    }
139    T &var;
140};
141
142template<typename T>
143inc_actor<T>
144inc(T &t)
145{
146    return inc_actor<T>(t);
147}
148
149int
150main()
151{
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;
157
158#if qDebug
159    BOOST_SPIRIT_DEBUG_RULE(while_rule);
160    BOOST_SPIRIT_DEBUG_RULE(do_while_rule);
161#endif
162
163    while_rule
164        =   uint_p[assign_a(number_result)]
165        >>  while_p('+')
166            [
167                uint_p[add(number_result)][inc(iterations_performed)]
168            ];
169
170    do_while_rule
171        =   do_p
172            [
173                uint_p[add(number_result)][inc(iterations_performed)]
174            ].while_p('+');
175
176    cout << "/////////////////////////////////////////////////////////\n";
177    cout << "\n";
178    cout << "          while_p test\n";
179    cout << "\n";
180    cout << "/////////////////////////////////////////////////////////\n";
181    cout << "\n";
182
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);
189
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);
196
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";
204
205    return error_count!=0;
206}
Note: See TracBrowser for help on using the repository browser.