summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/spirit/classic/test/while_tests.cpp
blob: 33219e5ab210ce802a6735b787a5445c9c826dbc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*=============================================================================
    Phoenix V1.0
    Copyright (c) 2002-2003 Martin Wille

    Use, modification and distribution is subject to the Boost Software
    License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
    http://www.boost.org/LICENSE_1_0.txt)
==============================================================================*/
// vi:ts=4:sw=4:et
// Tests for BOOST_SPIRIT_CLASSIC_NS::while_p
// [28-Dec-2002]
////////////////////////////////////////////////////////////////////////////////
#include <iostream>
#include <cstring>

#include "impl/string_length.hpp"
#include <boost/spirit/include/classic_core.hpp>
#include <boost/spirit/include/classic_assign_actor.hpp>
#include <boost/spirit/include/classic_while.hpp>
#include <boost/ref.hpp>

namespace local
{
    template <typename T>
    struct var_wrapper
        : public ::boost::reference_wrapper<T>
    {
        typedef boost::reference_wrapper<T> parent;

        explicit inline var_wrapper(T& t) : parent(t) {}

        inline T& operator()() const { return parent::get(); }
    };

    template <typename T>
    inline var_wrapper<T>
    var(T& t)
    {
        return var_wrapper<T>(t);
    }
}

namespace
{
    template <typename T>
    class add_actor
    {
    public:
        explicit add_actor(T &ref_) : ref(ref_) {}

        template <typename T2>
        void operator()(T2 const &val) const
        { ref += val; }

    private:
        T& ref;
    };

    template <typename T>
    inline add_actor<T> const
    add(T& ref)
    {
        return add_actor<T>(ref);
    }
}

typedef BOOST_SPIRIT_CLASSIC_NS::rule<> rule_t;

unsigned int test_count = 0;
unsigned int error_count = 0;
unsigned int iterations_performed;

unsigned int number_result;
static const unsigned int kError = 999;
static const bool good = true;
static const bool bad = false;

rule_t while_rule;
rule_t do_while_rule;

void
test_while(
    char const *s,
    unsigned int wanted,
    rule_t const &r,
    unsigned int iterations_wanted)
{
    using namespace std;
    
    ++test_count;

    number_result = 0;
    iterations_performed = 0;

    BOOST_SPIRIT_CLASSIC_NS::parse_info<> m = BOOST_SPIRIT_CLASSIC_NS::parse(s, s+ test_impl::string_length(s), r);

    bool result = wanted == kError?(m.full?bad:good): (number_result==wanted);

    result &= iterations_performed == iterations_wanted;

    if (m.full && (m.length != test_impl::string_length(s)))
        result = bad;

    if (result==good)
        cout << "PASSED";
    else
    {
        ++error_count;
        cout << "FAILED";
    }

    cout << ": \"" << s << "\" ==> ";
    if (!m.full)
        cout << "<error>";
    else
        cout << number_result;
    cout << "     " << iterations_performed << " of "
         << iterations_wanted << " iterations\n";
}

template<typename T>
struct inc_actor
{
    explicit inc_actor(T &t) : var(t) {}
    template<typename IteratorT>
    void operator()(IteratorT const &, IteratorT const &) const
    {
        ++var;
    }
    template<typename U>
    void operator()(U) const
    {
        ++var;
    }
    T &var;
};

template<typename T>
inc_actor<T>
inc(T &t)
{
    return inc_actor<T>(t);
}

int
main()
{
    using namespace std;
    using ::BOOST_SPIRIT_CLASSIC_NS::uint_p;
    using ::BOOST_SPIRIT_CLASSIC_NS::while_p;
    using ::BOOST_SPIRIT_CLASSIC_NS::do_p;
    using ::BOOST_SPIRIT_CLASSIC_NS::assign_a;

#if qDebug
    BOOST_SPIRIT_DEBUG_RULE(while_rule);
    BOOST_SPIRIT_DEBUG_RULE(do_while_rule);
#endif

    while_rule
        =   uint_p[assign_a(number_result)]
        >>  while_p('+')
            [
                uint_p[add(number_result)][inc(iterations_performed)]
            ];

    do_while_rule
        =   do_p
            [
                uint_p[add(number_result)][inc(iterations_performed)]
            ].while_p('+');

    cout << "/////////////////////////////////////////////////////////\n";
    cout << "\n";
    cout << "          while_p test\n";
    cout << "\n";
    cout << "/////////////////////////////////////////////////////////\n";
    cout << "\n";

    cout << "while_p()[]\n";
    test_while("",       kError, while_rule, 0);
    test_while("1",           1, while_rule, 0);
    test_while("1+1",         2, while_rule, 1);
    test_while("1+1+12",     14, while_rule, 2);
    test_while("1+1+x",  kError, while_rule, 1);

    cout << "do_p[].while_p()\n";
    test_while("",       kError, do_while_rule, 0);
    test_while("1",           1, do_while_rule, 1);
    test_while("1+1",         2, do_while_rule, 2);
    test_while("1+1+12",     14, do_while_rule, 3);
    test_while("1+1+x",  kError, do_while_rule, 2);

    std::cout << "\n    ";
    if (error_count==0)
        cout << "All " << test_count << " while_p-tests passed.\n"
             << "Test concluded successfully\n";
    else
        cout << error_count << " of " << test_count << " while_p-tests failed\n"
             << "Test failed\n";

    return error_count!=0;
}