Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_33_1/libs/spirit/test/if_tests.cpp @ 12

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

added boost

File size: 6.7 KB
Line 
1/*=============================================================================
2    Copyright (c) 2004 Stefan Slapeta
3    Copyright (c) 2002-2003 Martin Wille
4    http://spirit.sourceforge.net/
5
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=============================================================================*/
10// vi:ts=4:sw=4:et
11// Tests for boost::spirit::if_p
12// [28-Dec-2002]
13////////////////////////////////////////////////////////////////////////////////
14#define qDebug 0
15#include <iostream>
16#include <cstring>
17#if qDebug
18#define BOOST_SPIRIT_DEBUG
19#endif
20#include <boost/spirit/core.hpp>
21#include <boost/spirit/dynamic/if.hpp>
22#include <boost/spirit/actor/assign_actor.hpp>
23#include <boost/ref.hpp>
24#include "impl/string_length.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
47typedef ::boost::spirit::rule<> rule_t;
48typedef ::boost::spirit::rule<boost::spirit::no_actions_scanner<>::type >
49    no_actions_rule_t;
50
51unsigned int test_count = 0;
52unsigned int error_count = 0;
53
54unsigned int number_result;
55static const unsigned int kError = 999;
56static const bool good = true;
57static const bool bad = false;
58
59rule_t hex_prefix;
60no_actions_rule_t oct_prefix;
61rule_t hex_rule, oct_rule, dec_rule;
62
63rule_t auto_number_rule;
64rule_t hex_or_dec_number_rule;
65
66void
67test_number(char const *s, unsigned int wanted, rule_t const &r)
68{
69    using namespace std;
70   
71    ++test_count;
72
73    number_result = wanted-1;
74    ::boost::spirit::parse_info<> m = ::boost::spirit::parse(s, s+string_length(s), r);
75
76    bool result = wanted == kError?(m.full?bad:good): (number_result==wanted);
77
78    if (m.full && (m.length != string_length(s)))
79        result = bad;
80
81
82    if (result==good)
83        cout << "PASSED";
84    else
85    {
86        ++error_count;
87        cout << "FAILED";
88    }
89
90    cout << ": \"" << s << "\" ==> ";
91    if (number_result==wanted-1)
92        cout << "<error>";
93    else
94        cout << number_result;
95
96    cout << "\n";
97}
98
99void
100test_enclosed_fail()
101{
102    using namespace std;
103
104    using ::boost::spirit::if_p;
105    using ::boost::spirit::str_p;
106    using ::boost::spirit::nothing_p;
107
108  cout << "\nfail enclosed parser:\n";
109
110    const char *p = "abc";
111
112    ::boost::spirit::strlit<const char*> success_p = str_p(p);
113    ::boost::spirit::strlit<const char*> fail_p = str_p("xxx");
114
115    ::boost::spirit::rule<> r = if_p(success_p)[nothing_p];
116
117    ::boost::spirit::parse_info<> m = ::boost::spirit::parse(p, r);
118
119    if (m.full) {
120        cout << "FAILED: if --> match" << endl;
121        ++error_count;
122    } else {
123        cout << "PASSED: if --> no_match" << endl;
124    }
125
126    r = if_p(fail_p)[success_p].else_p[nothing_p];
127
128    m = ::boost::spirit::parse(p, r);
129
130    if (m.full) {
131        cout << "FAILED: else --> match" << endl;
132        ++error_count;
133    } else {
134        cout << "PASSED: else --> no_match" << endl;
135    }
136}
137
138int
139main()
140{
141    using namespace std;
142    using ::boost::spirit::if_p;
143    using ::boost::spirit::uint_p;
144    using ::boost::spirit::oct_p;
145    using ::boost::spirit::hex_p;
146    using ::boost::spirit::str_p;
147    using ::boost::spirit::ch_p;
148    using ::boost::spirit::assign_a;
149
150    cout << "/////////////////////////////////////////////////////////\n";
151    cout << "\n";
152    cout << "          if_p test\n";
153    cout << "\n";
154    cout << "/////////////////////////////////////////////////////////\n";
155    cout << "\n";
156
157    bool    as_hex;
158
159#if qDebug
160    BOOST_SPIRIT_DEBUG_RULE(hex_prefix);
161    BOOST_SPIRIT_DEBUG_RULE(hex_rule);
162    BOOST_SPIRIT_DEBUG_RULE(oct_prefix);
163    BOOST_SPIRIT_DEBUG_RULE(oct_rule);
164    BOOST_SPIRIT_DEBUG_RULE(dec_rule);
165    BOOST_SPIRIT_DEBUG_RULE(auto_number_rule);
166    BOOST_SPIRIT_DEBUG_RULE(hex_or_dec_number_rule);
167#endif
168
169    hex_prefix = str_p("0x");
170    oct_prefix = ch_p('0');
171
172    hex_rule = hex_p[assign_a(number_result)];
173    oct_rule = oct_p[assign_a(number_result)];
174    dec_rule = uint_p[assign_a(number_result)];
175
176    auto_number_rule =
177        if_p(hex_prefix)
178            [hex_rule]
179        .else_p
180        [
181            if_p(::boost::spirit::eps_p(oct_prefix))
182                [oct_rule]
183            .else_p
184                [dec_rule]
185        ];
186
187    hex_or_dec_number_rule =
188        if_p(local::var(as_hex))[hex_prefix>>hex_rule].else_p[dec_rule];
189
190    cout << "auto:\n";
191    test_number("",   kError, auto_number_rule);
192    test_number("0",       0, auto_number_rule);
193    test_number("1",       1, auto_number_rule);
194    test_number("00",      0, auto_number_rule);
195    test_number("0x", kError, auto_number_rule);
196    test_number("0x0",     0, auto_number_rule);
197    test_number("0755",  493, auto_number_rule);
198    test_number("0x100", 256, auto_number_rule);
199
200    cout << "\ndecimal:\n";
201    as_hex = false;
202    test_number("",      kError, hex_or_dec_number_rule);
203    test_number("100",      100, hex_or_dec_number_rule);
204    test_number("0x100", kError, hex_or_dec_number_rule);
205    test_number("0xff",  kError, hex_or_dec_number_rule);
206
207    cout << "\nhexadecimal:\n";
208    as_hex = true;
209    test_number("",      kError, hex_or_dec_number_rule);
210    test_number("0x100",    256, hex_or_dec_number_rule);
211    test_number("0xff",     255, hex_or_dec_number_rule);
212
213    //////////////////////////////////
214    // tests for if_p without else-parser
215    cout << "\nno-else:\n";
216    rule_t r = if_p(::boost::spirit::eps_p('0'))[oct_rule];
217
218    test_number("0", 0, r);
219
220    ++test_count;
221    ::boost::spirit::parse_info<> m = ::boost::spirit::parse("", r);
222    if (!m.hit || !m.full || m.length!=0)
223    {
224        std::cout << "FAILED: \"\" ==> <error>\n";
225        ++error_count;
226    }
227    else
228        std::cout << "PASSED: \"\" ==> <empty match>\n";
229
230    ++test_count;
231    m = ::boost::spirit::parse("junk", r);
232    if (!m.hit || m.full || m.length!=0)
233    {
234        std::cout << "FAILED: \"junk\" ==> <error>\n";
235        ++error_count;
236    }
237    else
238        std::cout << "PASSED: \"junk\" ==> <empty match>\n";
239
240    test_enclosed_fail();
241
242
243    //////////////////////////////////
244    // report results
245    std::cout << "\n    ";
246    if (error_count==0)
247        cout << "All " << test_count << " if_p-tests passed.\n"
248             << "Test concluded successfully\n";
249    else
250        cout << error_count << " of " << test_count << " if_p-tests failed\n"
251             << "Test failed\n";
252
253    return error_count!=0;
254}
Note: See TracBrowser for help on using the repository browser.