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 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
|
/*=============================================================================
Copyright (c) 2004 Stefan Slapeta
Copyright (c) 2002-2003 Martin Wille
http://spirit.sourceforge.net/
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::if_p
// [28-Dec-2002]
////////////////////////////////////////////////////////////////////////////////
#include <iostream>
#include <cstring>
#include <boost/spirit/include/classic_core.hpp>
#include <boost/spirit/include/classic_if.hpp>
#include <boost/spirit/include/classic_assign_actor.hpp>
#include <boost/ref.hpp>
#include "impl/string_length.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);
}
}
typedef ::BOOST_SPIRIT_CLASSIC_NS::rule<> rule_t;
typedef ::BOOST_SPIRIT_CLASSIC_NS::rule<BOOST_SPIRIT_CLASSIC_NS::no_actions_scanner<>::type >
no_actions_rule_t;
unsigned int test_count = 0;
unsigned int error_count = 0;
unsigned int number_result;
static const unsigned int kError = 999;
static const bool good = true;
static const bool bad = false;
rule_t hex_prefix;
no_actions_rule_t oct_prefix;
rule_t hex_rule, oct_rule, dec_rule;
rule_t auto_number_rule;
rule_t hex_or_dec_number_rule;
void
test_number(char const *s, unsigned int wanted, rule_t const &r)
{
using namespace std;
++test_count;
number_result = wanted-1;
::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);
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 (number_result==wanted-1)
cout << "<error>";
else
cout << number_result;
cout << "\n";
}
void
test_enclosed_fail()
{
using namespace std;
using ::BOOST_SPIRIT_CLASSIC_NS::if_p;
using ::BOOST_SPIRIT_CLASSIC_NS::str_p;
using ::BOOST_SPIRIT_CLASSIC_NS::nothing_p;
cout << "\nfail enclosed parser:\n";
const char *p = "abc";
::BOOST_SPIRIT_CLASSIC_NS::strlit<const char*> success_p = str_p(p);
::BOOST_SPIRIT_CLASSIC_NS::strlit<const char*> fail_p = str_p("xxx");
::BOOST_SPIRIT_CLASSIC_NS::rule<> r = if_p(success_p)[nothing_p];
::BOOST_SPIRIT_CLASSIC_NS::parse_info<> m = ::BOOST_SPIRIT_CLASSIC_NS::parse(p, r);
if (m.full) {
cout << "FAILED: if --> match" << endl;
++error_count;
} else {
cout << "PASSED: if --> no_match" << endl;
}
r = if_p(fail_p)[success_p].else_p[nothing_p];
m = ::BOOST_SPIRIT_CLASSIC_NS::parse(p, r);
if (m.full) {
cout << "FAILED: else --> match" << endl;
++error_count;
} else {
cout << "PASSED: else --> no_match" << endl;
}
}
int
main()
{
using namespace std;
using ::BOOST_SPIRIT_CLASSIC_NS::if_p;
using ::BOOST_SPIRIT_CLASSIC_NS::uint_p;
using ::BOOST_SPIRIT_CLASSIC_NS::oct_p;
using ::BOOST_SPIRIT_CLASSIC_NS::hex_p;
using ::BOOST_SPIRIT_CLASSIC_NS::str_p;
using ::BOOST_SPIRIT_CLASSIC_NS::ch_p;
using ::BOOST_SPIRIT_CLASSIC_NS::assign_a;
cout << "/////////////////////////////////////////////////////////\n";
cout << "\n";
cout << " if_p test\n";
cout << "\n";
cout << "/////////////////////////////////////////////////////////\n";
cout << "\n";
bool as_hex;
#if qDebug
BOOST_SPIRIT_DEBUG_RULE(hex_prefix);
BOOST_SPIRIT_DEBUG_RULE(hex_rule);
BOOST_SPIRIT_DEBUG_RULE(oct_prefix);
BOOST_SPIRIT_DEBUG_RULE(oct_rule);
BOOST_SPIRIT_DEBUG_RULE(dec_rule);
BOOST_SPIRIT_DEBUG_RULE(auto_number_rule);
BOOST_SPIRIT_DEBUG_RULE(hex_or_dec_number_rule);
#endif
hex_prefix = str_p("0x");
oct_prefix = ch_p('0');
hex_rule = hex_p[assign_a(number_result)];
oct_rule = oct_p[assign_a(number_result)];
dec_rule = uint_p[assign_a(number_result)];
auto_number_rule =
if_p(hex_prefix)
[hex_rule]
.else_p
[
if_p(::BOOST_SPIRIT_CLASSIC_NS::eps_p(oct_prefix))
[oct_rule]
.else_p
[dec_rule]
];
hex_or_dec_number_rule =
if_p(local::var(as_hex))[hex_prefix>>hex_rule].else_p[dec_rule];
cout << "auto:\n";
test_number("", kError, auto_number_rule);
test_number("0", 0, auto_number_rule);
test_number("1", 1, auto_number_rule);
test_number("00", 0, auto_number_rule);
test_number("0x", kError, auto_number_rule);
test_number("0x0", 0, auto_number_rule);
test_number("0755", 493, auto_number_rule);
test_number("0x100", 256, auto_number_rule);
cout << "\ndecimal:\n";
as_hex = false;
test_number("", kError, hex_or_dec_number_rule);
test_number("100", 100, hex_or_dec_number_rule);
test_number("0x100", kError, hex_or_dec_number_rule);
test_number("0xff", kError, hex_or_dec_number_rule);
cout << "\nhexadecimal:\n";
as_hex = true;
test_number("", kError, hex_or_dec_number_rule);
test_number("0x100", 256, hex_or_dec_number_rule);
test_number("0xff", 255, hex_or_dec_number_rule);
//////////////////////////////////
// tests for if_p without else-parser
cout << "\nno-else:\n";
rule_t r = if_p(::BOOST_SPIRIT_CLASSIC_NS::eps_p('0'))[oct_rule];
test_number("0", 0, r);
++test_count;
::BOOST_SPIRIT_CLASSIC_NS::parse_info<> m = ::BOOST_SPIRIT_CLASSIC_NS::parse("", r);
if (!m.hit || !m.full || m.length!=0)
{
std::cout << "FAILED: \"\" ==> <error>\n";
++error_count;
}
else
std::cout << "PASSED: \"\" ==> <empty match>\n";
++test_count;
m = ::BOOST_SPIRIT_CLASSIC_NS::parse("junk", r);
if (!m.hit || m.full || m.length!=0)
{
std::cout << "FAILED: \"junk\" ==> <error>\n";
++error_count;
}
else
std::cout << "PASSED: \"junk\" ==> <empty match>\n";
test_enclosed_fail();
//////////////////////////////////
// report results
std::cout << "\n ";
if (error_count==0)
cout << "All " << test_count << " if_p-tests passed.\n"
<< "Test concluded successfully\n";
else
cout << error_count << " of " << test_count << " if_p-tests failed\n"
<< "Test failed\n";
return error_count!=0;
}
|