Example #1
0
int
main()
{
    namespace karma = boost::spirit::karma;
    using spirit_test::test;
    using namespace boost::spirit;
    using namespace boost::spirit::karma;
    namespace phx = boost::phoenix;

    {
        BOOST_TEST(test("123", karma::lazy(phx::val(int_)), 123));
    }

    {
        int result = 123;
        BOOST_TEST(test("123", karma::lazy(phx::val(int_))[_1 = phx::ref(result)]));
    }

//     {
//         typedef spirit_test::output_iterator<char>::type outiter_type;
//         rule<outiter_type, void(std::string)> r;
// 
//         r = char_('<') << karma::lazy(_r1) << '>' <<  "</" << karma::lazy(_r1) << '>';
// 
//         std::string tag("tag"), foo("foo");
//         BOOST_TEST(test("<tag></tag>", r (phx::ref(tag))));
//         BOOST_TEST(!test("<foo></bar>", r (phx::ref(foo))));
//     }

    return boost::report_errors();
}
Example #2
0
int
main()
{
    using spirit_test::test_attr;
    using spirit_test::test;

    using boost::spirit::x3::rule;
    using boost::spirit::x3::int_;
    using boost::spirit::x3::with;

    { // injecting data into the context in the grammar

        int val = 0;
        auto r = rule<my_rule_class, char const*>() =
            '(' > int_ > ',' > int_ > ')'
            ;
        
        auto start =
            with<my_tag>(std::ref(val)) [ r ]
            ;

        BOOST_TEST(test("(123,456)", start));
        BOOST_TEST(!test("(abc,def)", start));
        BOOST_TEST(val == 2);
    }

    return boost::report_errors();
}
int main () 
{
    using spirit_test::test;

    {
        using boost::spirit::karma::int_;

        data1 b(800, 600);
        BOOST_TEST(test("width: 800\nheight: 600\n", 
            "width: " << int_ << "\n" << "height: " << int_ << "\n", b));
    }

    {
        using boost::spirit::karma::char_;
        using boost::spirit::karma::string;

        data2 d("test");
        BOOST_TEST(test("data: test\n", "data: " << +char_ << "\n", d));
        BOOST_TEST(test("data: test\n", "data: " << string << "\n", d));
    }

    {
        using boost::spirit::karma::double_;

        BOOST_TEST(test("x=0.0\n", "x=" << double_ << "\n", data3(0)));
        BOOST_TEST(test("x=1.1\n", "x=" << double_ << "\n", data3(1.1)));
        BOOST_TEST(test("x=1.0e10\n", "x=" << double_ << "\n", data3(1e10)));

        BOOST_TEST(test("x=inf\n", "x=" << double_ << "\n", 
            data3(std::numeric_limits<double>::infinity())));
        if (std::numeric_limits<double>::has_quiet_NaN) {
            BOOST_TEST(test("x=nan\n", "x=" << double_ << "\n", 
                data3(std::numeric_limits<double>::quiet_NaN())));
        }
        if (std::numeric_limits<double>::has_signaling_NaN) {
            BOOST_TEST(test("x=nan\n", "x=" << double_ << "\n", 
                data3(std::numeric_limits<double>::signaling_NaN())));
        }
    }

    {
        using boost::spirit::karma::double_;
        using boost::spirit::karma::int_;
        using boost::spirit::karma::string;

        data4 d;
        d.b_ = 10;

        BOOST_TEST(test(
            "Testing: b: 10.0\n", 
            "Testing: " << -("a: " << int_ << "\n")
                        << -("b: " << double_ << "\n")
                        << -("c: " << string << "\n"), d));
    }

    return boost::report_errors();
}
Example #4
0
int
main()
{
    using namespace boost::spirit::ascii;
    using boost::spirit::lit;
    using spirit_test::test;

    {
        BOOST_TEST(test("b", char_ - 'a'));
        BOOST_TEST(!test("a", char_ - 'a'));
        BOOST_TEST(test("/* abcdefghijk */", "/*" >> *(char_ - "*/") >> "*/"));
    }

    {
        BOOST_TEST(test("b", char_ - no_case['a']));
        BOOST_TEST(!test("a", char_ - no_case['a']));
        BOOST_TEST(!test("A", char_ - no_case['a']));

        BOOST_TEST(test("b", no_case[lower - 'a']));
        BOOST_TEST(test("B", no_case[lower - 'a']));
        BOOST_TEST(!test("a", no_case[lower - 'a']));
        BOOST_TEST(!test("A", no_case[lower - 'a']));
    }

    {
        // $$$ See difference.hpp why these tests are not done anymore. $$$

        // BOOST_TEST(test("switcher", lit("switcher") - "switch"));
        // BOOST_TEST(test("    switcher    ", lit("switcher") - "switch", space));

        BOOST_TEST(!test("switch", lit("switch") - "switch"));
    }

    {
        using boost::spirit::_1;
        namespace phx = boost::phoenix;

        std::string s;

        BOOST_TEST(test(
            "/*abcdefghijk*/"
          , "/*" >> *(char_ - "*/")[phx::ref(s) += _1] >> "*/"
        ));
        BOOST_TEST(s == "abcdefghijk");
        s.clear();

        BOOST_TEST(test(
            "    /*abcdefghijk*/"
          , "/*" >> *(char_ - "*/")[phx::ref(s) += _1] >> "*/"
          , space
        ));
        BOOST_TEST(s == "abcdefghijk");
    }

    return boost::report_errors();
}
Example #5
0
int
main()
{
    using boost::spirit::x3::ascii::char_;
    using boost::spirit::x3::ascii::space;
    using boost::spirit::x3::lit;
    using spirit_test::test;
    using spirit_test::test_attr;

    // Basic tests
    {
        BOOST_TEST(test("b", char_ - 'a'));
        BOOST_TEST(!test("a", char_ - 'a'));
        BOOST_TEST(test("/* abcdefghijk */", "/*" >> *(char_ - "*/") >> "*/"));
        BOOST_TEST(!test("switch", lit("switch") - "switch"));
    }

    // Test attributes
    {
        char attr;
        BOOST_TEST(test_attr("xg", (char_ - 'g') >> 'g', attr));
        BOOST_TEST(attr == 'x');
    }

    // Test handling of container attributes
    {
        std::string attr;
        BOOST_TEST(test_attr("abcdefg", *(char_ - 'g') >> 'g', attr));
        BOOST_TEST(attr == "abcdef");
    }

    {
        using boost::spirit::x3::_attr;

        std::string s;

        BOOST_TEST(test(
            "/*abcdefghijk*/"
          , "/*" >> *(char_ - "*/")[([&](auto& ctx){ s += _attr(ctx); })] >> "*/"
        ));
        BOOST_TEST(s == "abcdefghijk");
        s.clear();

        BOOST_TEST(test(
            "    /*abcdefghijk*/"
          , "/*" >> *(char_ - "*/")[([&](auto& ctx){ s += _attr(ctx); })] >> "*/"
          , space
        ));
        BOOST_TEST(s == "abcdefghijk");
    }

    return boost::report_errors();
}
Example #6
0
int
main()
{
    using spirit_test::test;
    using boost::spirit::int_;

    {
        BOOST_TEST((test("1234", &int_, false)));
        BOOST_TEST((!test("abcd", &int_)));
    }

    return boost::report_errors();
}
Example #7
0
int
main()
{
    using spirit_test::test;
    using boost::spirit::x3::eol;

    {
        BOOST_TEST((test("\r\n", eol)));
        BOOST_TEST((test("\r", eol)));
        BOOST_TEST((test("\n", eol)));
        BOOST_TEST((!test("\n\r", eol)));
        BOOST_TEST((!test("", eol)));
    }

    return boost::report_errors();
}
Example #8
0
int
main()
{
    using spirit_test::test;
    using boost::spirit::x3::eoi;

    {
        BOOST_TEST((test("", eoi)));
        BOOST_TEST(!(test("x", eoi)));
    }

    {
        BOOST_TEST(what(eoi) == "eoi");
    }

    return boost::report_errors();
}
Example #9
0
int main()
{
    using spirit_test::test_attr;
    using spirit_test::test;

    using namespace boost::spirit::x3::ascii;
    using boost::spirit::x3::rule;
    using boost::spirit::x3::int_;
    using boost::spirit::x3::lit;


    { // synth attribute value-init

        std::string s;
        typedef rule<class r, std::string> rule_type;

        auto rdef = rule_type()
            = alpha                 [f()]
            ;

        BOOST_TEST(test_attr("abcdef", +rdef, s));
        BOOST_TEST(s == "abcdef");
    }

    { // synth attribute value-init

        std::string s;
        typedef rule<class r, std::string> rule_type;

        auto rdef = rule_type() =
            alpha /
               [](auto& ctx)
               {
                  _val(ctx) += _attr(ctx);
               }
            ;

        BOOST_TEST(test_attr("abcdef", +rdef, s));
        BOOST_TEST(s == "abcdef");
    }

    return boost::report_errors();
}
int
main()
{
    using spirit_test::test;
    using namespace boost::spirit;

    {
        BOOST_TEST(test("", eps));
    }

    {   // test action

        using namespace boost::phoenix;
        BOOST_TEST(test("", eps(val(true))));
        BOOST_TEST(!test("", eps(val(false))));
    }

    return boost::report_errors();
}
Example #11
0
int
main()
{
    using spirit_test::test;
    using spirit_test::test_attr;
    using boost::spirit::x3::ascii::space;
    using boost::spirit::x3::ascii::space_type;
    using boost::spirit::x3::ascii::char_;
    using boost::spirit::x3::ascii::alpha;
    using boost::spirit::x3::lexeme;
    using boost::spirit::x3::skip;
    using boost::spirit::x3::lit;

    {
        BOOST_TEST((test("a b c d", skip(space)[*char_])));
    }

    { // test attribute
        std::string s;
        BOOST_TEST((test_attr("a b c d", skip(space)[*char_], s)));
        BOOST_TEST(s == "abcd");
    }

    { // reskip
        BOOST_TEST((test("ab c d", lexeme[lit('a') >> 'b' >> skip[lit('c') >> 'd']], space)));
        BOOST_TEST((test("abcd", lexeme[lit('a') >> 'b' >> skip[lit('c') >> 'd']], space)));
        BOOST_TEST(!(test("a bcd", lexeme[lit('a') >> 'b' >> skip[lit('c') >> 'd']], space)));

        BOOST_TEST((test("ab c d", lexeme[lexeme[lit('a') >> 'b' >> skip[lit('c') >> 'd']]], space)));
        BOOST_TEST((test("abcd", lexeme[lexeme[lit('a') >> 'b' >> skip[lit('c') >> 'd']]], space)));
        BOOST_TEST(!(test("a bcd", lexeme[lexeme[lit('a') >> 'b' >> skip[lit('c') >> 'd']]], space)));
    }

    //~ { // lazy skip
        //~ using boost::phoenix::val;

        //~ BOOST_TEST((test("a b c d", skip(val(space))[*char_])));
    //~ }

    return boost::report_errors();
}
Example #12
0
int
main()
{
    using spirit_test::test;
    using spirit_test::test_attr;
    using boost::spirit::qi::lit;
    using boost::spirit::qi::no_case;
    using boost::spirit::qi::char_;
    using boost::spirit::qi::encoding;
    namespace char_encoding = boost::spirit::char_encoding;

    encoding<char_encoding::iso8859_1> iso8859_1;

    { // test extended ASCII characters
        BOOST_TEST(test("\xC1", iso8859_1[no_case['\xE1']]));
        BOOST_TEST(test("\xC1", iso8859_1[no_case[char_('\xE1')]]));

        BOOST_TEST(test("\xC9", iso8859_1[no_case[char_("\xE5-\xEF")]]));
        BOOST_TEST(!test("\xFF", iso8859_1[no_case[char_("\xE5-\xEF")]]));

        BOOST_TEST(test("\xC1\xE1", iso8859_1[no_case["\xE1\xC1"]]));
        BOOST_TEST(test("\xC1\xE1", iso8859_1[no_case[lit("\xE1\xC1")]]));
    }

    return boost::report_errors();
}
Example #13
0
int main()
{
    using spirit_test::test;
    using spirit_test::test_attr;
    using namespace boost::spirit::x3::ascii;
    using boost::spirit::x3::raw;
    using boost::spirit::x3::eps;
    using boost::spirit::x3::_attr;

    {
        boost::iterator_range<char const*> range;
        std::string str;
        BOOST_TEST((test_attr("spirit_test_123", raw[alpha >> *(alnum | '_')], range)));
        BOOST_TEST((std::string(range.begin(), range.end()) == "spirit_test_123"));
        BOOST_TEST((test_attr("  spirit", raw[*alpha], range, space)));
        BOOST_TEST((range.size() == 6));
    }

    {
        std::string str;
        BOOST_TEST((test_attr("spirit_test_123", raw[alpha >> *(alnum | '_')], str)));
        BOOST_TEST((str == "spirit_test_123"));
    }

    {
        boost::iterator_range<char const*> range;
        BOOST_TEST((test("x", raw[alpha])));
        BOOST_TEST((test_attr("x", raw[alpha], range)));
    }

    {
        boost::iterator_range<char const*> range;
        BOOST_TEST((test("x", raw[alpha][ ([&](auto& ctx){ range = _attr(ctx); }) ])));
        BOOST_TEST(range.size() == 1 && *range.begin() == 'x');
    }

    return boost::report_errors();
}
Example #14
0
int main()
{
    namespace qi = boost::spirit::qi;
    namespace phoenix = boost::phoenix;
    using spirit_test::test_attr;

    {
        qi::rule<char const*, std::string()> r;
        r %= (+qi::char_)[phoenix::bind(&f, qi::_1)];

        std::string attr;
        BOOST_TEST(test_attr("abcdef", r, attr));
        BOOST_TEST(attr == "abcdef");
    }

    return boost::report_errors();
}
Example #15
0
int
main()
{
    using spirit_test::test_attr;
    namespace qi = boost::spirit::qi;

    using qi::attr;
    using qi::double_;

    {
        double d = 0.0;
        BOOST_TEST(test_attr("", attr(1.0), d) && d == 1.0);

        double d1 = 1.0;
        BOOST_TEST(test_attr("", attr(d1), d) && d == 1.0);

        std::pair<double, double> p;
        BOOST_TEST(test_attr("1.0", double_ >> attr(1.0), p) && 
            p.first == 1.0 && p.second == 1.0);

        char c = '\0';
        BOOST_TEST(test_attr("", attr('a'), c) && c == 'a');
        std::string str;
        BOOST_TEST(test_attr("", attr("test"), str) && str == "test");
    }

    {   // testing lazy constructs
        using boost::phoenix::val;
        using boost::phoenix::ref;

        double d = 0.0;
        BOOST_TEST(test_attr("", attr(val(1.0)), d) && d == 1.0);

        double d1 = 2.0;
        BOOST_TEST(test_attr("", attr(ref(d1)), d) && d == 2.0);
    }

    {
        std::string s;
        BOOST_TEST(test_attr("s", "s" >> qi::attr(std::string("123")), s) && 
            s == "123");
    }

    return boost::report_errors();
}
int
main()
{
    using spirit_test::test;
    using spirit_test::test_attr;
    using boost::spirit::qi::lit;
    using boost::spirit::qi::no_case;
    using boost::spirit::qi::char_;
    using boost::spirit::qi::encoding;
    namespace char_encoding = boost::spirit::char_encoding;

    encoding<char_encoding::iso8859_1> iso8859_1;

// needed for VC7.1 only
#if BOOST_WORKAROUND(BOOST_MSVC, BOOST_TESTED_AT(1310))
#pragma setlocale("french")
#endif

    {
        BOOST_TEST(test("Á", iso8859_1[no_case['á']]));
        BOOST_TEST(test("Á", iso8859_1[no_case[char_('á')]]));
    }

    {
        BOOST_TEST(test("É", iso8859_1[no_case[char_("å-ï")]]));
        BOOST_TEST(!test("ÿ", iso8859_1[no_case[char_("å-ï")]]));
    }

    {
        BOOST_TEST(test("Áá", iso8859_1[no_case["áÁ"]]));
        BOOST_TEST(test("Áá", iso8859_1[no_case[lit("áÁ")]]));
    }


#if BOOST_WORKAROUND(BOOST_MSVC, BOOST_TESTED_AT(1310))
#pragma setlocale("")
#endif

    return boost::report_errors();
}
Example #17
0
int
main()
{
    using spirit_test::test_attr;
    using spirit_test::test;

    using namespace boost::spirit::ascii;
    using namespace boost::spirit::qi::labels;
    using boost::spirit::qi::locals;
    using boost::spirit::qi::rule;
    using boost::spirit::qi::int_;
    using boost::spirit::qi::uint_;
    using boost::spirit::qi::fail;
    using boost::spirit::qi::on_error;
    using boost::spirit::qi::debug;
    using boost::spirit::qi::lit;

    namespace phx = boost::phoenix;

    { // synth attribute value-init

        std::string s;
        rule<char const*, char()> r;
        r = alpha[_val += _1];
        BOOST_TEST(test_attr("abcdef", +r, s));
        BOOST_TEST(s == "abcdef");
    }

    { // auto rules aliasing tests

        char ch = '\0';
        rule<char const*, char()> a, b;
        a %= b;
        b %= alpha;

        BOOST_TEST(test("x", a[phx::ref(ch) = _1]));
        BOOST_TEST(ch == 'x');
        ch = '\0';
        BOOST_TEST(test_attr("z", a, ch)); // attribute is given.
        BOOST_TEST(ch == 'z');

        a = b;            // test deduced auto rule behavior
        b = alpha;

        ch = '\0';
        BOOST_TEST(test("x", a[phx::ref(ch) = _1]));
        BOOST_TEST(ch == 'x');
        ch = '\0';
        BOOST_TEST(test_attr("z", a, ch)); // attribute is given.
        BOOST_TEST(ch == 'z');
    }

    { // context (w/arg) tests

        char ch;
        rule<char const*, char(int)> a; // 1 arg
        a = alpha[_val = _1 + _r1];

        BOOST_TEST(test("x", a(phx::val(1))[phx::ref(ch) = _1]));
        BOOST_TEST(ch == 'x' + 1);

        BOOST_TEST(test_attr("a", a(1), ch)); // allow scalars as rule args too.
        BOOST_TEST(ch == 'a' + 1);

        rule<char const*, char(int, int)> b; // 2 args
        b = alpha[_val = _1 + _r1 + _r2];
        BOOST_TEST(test_attr("a", b(1, 2), ch));
        BOOST_TEST(ch == 'a' + 1 + 2);
    }

    { // context (w/ reference arg) tests

        char ch;
        rule<char const*, void(char&)> a; // 1 arg (reference)
        a = alpha[_r1 = _1];

        BOOST_TEST(test("x", a(phx::ref(ch))));
        BOOST_TEST(ch == 'x');
    }

    { // context (w/locals) tests

        rule<char const*, locals<char> > a; // 1 local
        a = alpha[_a = _1] >> char_(_a);
        BOOST_TEST(test("aa", a));
        BOOST_TEST(!test("ax", a));
    }

    { // context (w/args and locals) tests

        rule<char const*, void(int), locals<char> > a; // 1 arg + 1 local
        a = alpha[_a = _1 + _r1] >> char_(_a);
        BOOST_TEST(test("ab", a(phx::val(1))));
        BOOST_TEST(test("xy", a(phx::val(1))));
        BOOST_TEST(!test("ax", a(phx::val(1))));
    }

    { // void() has unused type (void == unused_type)

        std::pair<int, char> attr;
        rule<char const*, void()> r;
        r = char_;
        BOOST_TEST(test_attr("123ax", int_ >> char_ >> r, attr));
        BOOST_TEST(attr.first == 123);
        BOOST_TEST(attr.second == 'a');
    }

    { // bug: test that injected attributes are ok

        rule<char const*, char(int) > r;

        // problem code:
        r = char_(_r1)[_val = _1];
    }

    return boost::report_errors();
}
Example #18
0
int
main()
{
    using spirit_test::test;
    using spirit_test::test_attr;

    ///////////////////////////////////////////////////////////////////////////
    //  strict real number tests
    ///////////////////////////////////////////////////////////////////////////
    {
        using boost::spirit::x3::real_parser;
        using boost::spirit::x3::parse;
        using boost::spirit::x3::strict_ureal_policies;
        using boost::spirit::x3::strict_real_policies;

        real_parser<double, strict_ureal_policies<double> > strict_udouble;
        real_parser<double, strict_real_policies<double> > strict_double;
        double  d;

        BOOST_TEST(!test("1234", strict_udouble));
        BOOST_TEST(!test_attr("1234", strict_udouble, d));

        BOOST_TEST(test("1.2", strict_udouble));
        BOOST_TEST(test_attr("1.2", strict_udouble, d) && compare(d, 1.2));

        BOOST_TEST(!test("-1234", strict_double));
        BOOST_TEST(!test_attr("-1234", strict_double, d));

        BOOST_TEST(test("123.", strict_double));
        BOOST_TEST(test_attr("123.", strict_double, d) && compare(d, 123));

        BOOST_TEST(test("3.E6", strict_double));
        BOOST_TEST(test_attr("3.E6", strict_double, d) && compare(d, 3e6));

        real_parser<double, no_trailing_dot_policy<double> > notrdot_real;
        real_parser<double, no_leading_dot_policy<double> > nolddot_real;

        BOOST_TEST(!test("1234.", notrdot_real));          //  Bad trailing dot
        BOOST_TEST(!test(".1234", nolddot_real));          //  Bad leading dot
    }

    ///////////////////////////////////////////////////////////////////////////
    //  Special thousands separated numbers
    ///////////////////////////////////////////////////////////////////////////
    {
        using boost::spirit::x3::real_parser;
        using boost::spirit::x3::parse;
        real_parser<double, ts_real_policies<double> > ts_real;
        double  d;

        BOOST_TEST(test("123,456,789.01", ts_real));
        BOOST_TEST(test_attr("123,456,789.01", ts_real, d)
                && compare(d, 123456789.01));

        BOOST_TEST(test("12,345,678.90", ts_real));
        BOOST_TEST(test_attr("12,345,678.90", ts_real, d)
                && compare(d, 12345678.90));

        BOOST_TEST(test("1,234,567.89", ts_real));
        BOOST_TEST(test_attr("1,234,567.89", ts_real, d)
                && compare(d, 1234567.89));

        BOOST_TEST(!test("1234,567,890", ts_real));
        BOOST_TEST(!test("1,234,5678,9", ts_real));
        BOOST_TEST(!test("1,234,567.89e6", ts_real));
        BOOST_TEST(!test("1,66", ts_real));
    }

    return boost::report_errors();
}
Example #19
0
int
main()
{
    using spirit_test::test;
    using spirit_test::test_attr;
    using boost::spirit::qi::symbols;
    using boost::spirit::qi::rule;
    using boost::spirit::qi::lazy;
    using boost::spirit::qi::_r1;

    { // basics
        symbols<char, int> sym;

        sym.add
            ("Joel")
            ("Ruby")
            ("Tenji")
            ("Tutit")
            ("Kim")
            ("Joey")
        ;

        boost::mpl::true_ f = boost::mpl::bool_<boost::spirit::traits::is_parser<symbols<char, int> >::value>();

        // silence stupid compiler warnings 
        // i.e. MSVC warning C4189: 'f' : local variable is initialized but not referenced
        BOOST_TEST((f.value));

        BOOST_TEST((test("Joel", sym)));
        BOOST_TEST((test("Ruby", sym)));
        BOOST_TEST((test("Tenji", sym)));
        BOOST_TEST((test("Tutit", sym)));
        BOOST_TEST((test("Kim", sym)));
        BOOST_TEST((test("Joey", sym)));
        BOOST_TEST((!test("XXX", sym)));

        // test copy
        symbols<char, int> sym2;
        sym2 = sym;
        BOOST_TEST((test("Joel", sym2)));
        BOOST_TEST((test("Ruby", sym2)));
        BOOST_TEST((test("Tenji", sym2)));
        BOOST_TEST((test("Tutit", sym2)));
        BOOST_TEST((test("Kim", sym2)));
        BOOST_TEST((test("Joey", sym2)));
        BOOST_TEST((!test("XXX", sym2)));

        // make sure it plays well with other parsers
        BOOST_TEST((test("Joelyo", sym >> "yo")));

        sym.remove
            ("Joel")
            ("Ruby")
        ;

        BOOST_TEST((!test("Joel", sym)));
        BOOST_TEST((!test("Ruby", sym)));
    }

    { // comma syntax
        symbols<char, int> sym;
        sym += "Joel", "Ruby", "Tenji", "Tutit", "Kim", "Joey";

        BOOST_TEST((test("Joel", sym)));
        BOOST_TEST((test("Ruby", sym)));
        BOOST_TEST((test("Tenji", sym)));
        BOOST_TEST((test("Tutit", sym)));
        BOOST_TEST((test("Kim", sym)));
        BOOST_TEST((test("Joey", sym)));
        BOOST_TEST((!test("XXX", sym)));

        sym -= "Joel", "Ruby";

        BOOST_TEST((!test("Joel", sym)));
        BOOST_TEST((!test("Ruby", sym)));
    }

    { // no-case handling
        using namespace boost::spirit::ascii;

        symbols<char, int> sym;
        // NOTE: make sure all entries are in lower-case!!!
        sym = "joel", "ruby", "tenji", "tutit", "kim", "joey";

        BOOST_TEST((test("joel", no_case[sym])));
        BOOST_TEST((test("ruby", no_case[sym])));
        BOOST_TEST((test("tenji", no_case[sym])));
        BOOST_TEST((test("tutit", no_case[sym])));
        BOOST_TEST((test("kim", no_case[sym])));
        BOOST_TEST((test("joey", no_case[sym])));

        BOOST_TEST((test("JOEL", no_case[sym])));
        BOOST_TEST((test("RUBY", no_case[sym])));
        BOOST_TEST((test("TENJI", no_case[sym])));
        BOOST_TEST((test("TUTIT", no_case[sym])));
        BOOST_TEST((test("KIM", no_case[sym])));
        BOOST_TEST((test("JOEY", no_case[sym])));

        // make sure it plays well with other parsers
        BOOST_TEST((test("Joelyo", no_case[sym] >> "yo")));
    }

    { // attributes
        symbols<char, int> sym;

        sym.add
            ("Joel", 1)
            ("Ruby", 2)
            ("Tenji", 3)
            ("Tutit", 4)
            ("Kim", 5)
            ("Joey", 6)
        ;

        int i;
        BOOST_TEST((test_attr("Joel", sym, i)));
        BOOST_TEST(i == 1);
        BOOST_TEST((test_attr("Ruby", sym, i)));
        BOOST_TEST(i == 2);
        BOOST_TEST((test_attr("Tenji", sym, i)));
        BOOST_TEST(i == 3);
        BOOST_TEST((test_attr("Tutit", sym, i)));
        BOOST_TEST(i == 4);
        BOOST_TEST((test_attr("Kim", sym, i)));
        BOOST_TEST(i == 5);
        BOOST_TEST((test_attr("Joey", sym, i)));
        BOOST_TEST(i == 6);
        BOOST_TEST((!test_attr("XXX", sym, i)));
        
        // double add:
        
        sym.add("Joel", 265);
        BOOST_TEST((test_attr("Joel", sym, i)));
        BOOST_TEST(i == 1);        
    }

    { // actions
        namespace phx = boost::phoenix;
        using boost::spirit::_1;

        symbols<char, int> sym;
        sym.add
            ("Joel", 1)
            ("Ruby", 2)
            ("Tenji", 3)
            ("Tutit", 4)
            ("Kim", 5)
            ("Joey", 6)
        ;

        int i;
        BOOST_TEST((test("Joel", sym[phx::ref(i) = _1])));
        BOOST_TEST(i == 1);
        BOOST_TEST((test("Ruby", sym[phx::ref(i) = _1])));
        BOOST_TEST(i == 2);
        BOOST_TEST((test("Tenji", sym[phx::ref(i) = _1])));
        BOOST_TEST(i == 3);
        BOOST_TEST((test("Tutit", sym[phx::ref(i) = _1])));
        BOOST_TEST(i == 4);
        BOOST_TEST((test("Kim", sym[phx::ref(i) = _1])));
        BOOST_TEST(i == 5);
        BOOST_TEST((test("Joey", sym[phx::ref(i) = _1])));
        BOOST_TEST(i == 6);
        BOOST_TEST((!test("XXX", sym[phx::ref(i) = _1])));
    }

    { // construction from symbol array
        char const* syms[] = {"Joel","Ruby","Tenji","Tutit","Kim","Joey"};
        symbols<char, int> sym(syms);

        BOOST_TEST((test("Joel", sym)));
        BOOST_TEST((test("Ruby", sym)));
        BOOST_TEST((test("Tenji", sym)));
        BOOST_TEST((test("Tutit", sym)));
        BOOST_TEST((test("Kim", sym)));
        BOOST_TEST((test("Joey", sym)));
        BOOST_TEST((!test("XXX", sym)));
    }

    { // construction from 2 arrays

        char const* syms[] = {"Joel","Ruby","Tenji","Tutit","Kim","Joey"};
        int data[] = {1,2,3,4,5,6};
        symbols<char, int> sym(syms, data);

        int i;
        BOOST_TEST((test_attr("Joel", sym, i)));
        BOOST_TEST(i == 1);
        BOOST_TEST((test_attr("Ruby", sym, i)));
        BOOST_TEST(i == 2);
        BOOST_TEST((test_attr("Tenji", sym, i)));
        BOOST_TEST(i == 3);
        BOOST_TEST((test_attr("Tutit", sym, i)));
        BOOST_TEST(i == 4);
        BOOST_TEST((test_attr("Kim", sym, i)));
        BOOST_TEST(i == 5);
        BOOST_TEST((test_attr("Joey", sym, i)));
        BOOST_TEST(i == 6);
        BOOST_TEST((!test_attr("XXX", sym, i)));
    }

    { // allow std::string and other string types
        symbols<> sym;

        // const and non-const std::string
        std::string a("abc");
        std::string const b("def");
        sym += a;
        sym += b;
        BOOST_TEST((test("abc", sym)));
        BOOST_TEST((test("def", sym)));
        sym = a;
        BOOST_TEST((test("abc", sym)));
        BOOST_TEST((!test("def", sym)));

        // non-const C-style string
        char arr[2]; arr[0] = 'a'; arr[1] = '\0';
        sym = arr;
        BOOST_TEST((test("a", sym)));
        BOOST_TEST((!test("b", sym)));

        // const and non-const custom string type
        custom_string_c c('x');
        custom_string_c const cc('y');
        sym = c, cc;
        BOOST_TEST((test("x", sym)));
        BOOST_TEST((test("y", sym)));
        BOOST_TEST((!test("z", sym)));
    }

    {
        namespace phx = boost::phoenix;

        symbols<char, int> sym;
        sym.add
            ("a", 1)
            ("b", 2)
        ;

        rule<char const*, int(symbols<char, int>&)> r;
        r %= lazy(_r1);

        int i = 0;
        BOOST_TEST(test_attr("a", r(phx::ref(sym)), i));
        BOOST_TEST(i == 1);
        BOOST_TEST(test_attr("b", r(phx::ref(sym)), i));
        BOOST_TEST(i == 2);
        BOOST_TEST(!test("c", r(phx::ref(sym))));
    }
    
    { // find
    
        symbols<char, int> sym;
        sym.add("a", 1)("b", 2);
        
        BOOST_TEST(!sym.find("c"));

        BOOST_TEST(sym.find("a") && *sym.find("a") == 1);
        BOOST_TEST(sym.find("b") && *sym.find("b") == 2);

        BOOST_TEST(sym.at("a") == 1);
        BOOST_TEST(sym.at("b") == 2);
        BOOST_TEST(sym.at("c") == 0);

        BOOST_TEST(sym.find("a") && *sym.find("a") == 1);
        BOOST_TEST(sym.find("b") && *sym.find("b") == 2);
        BOOST_TEST(sym.find("c") && *sym.find("c") == 0);
        
        symbols<char, int> const_sym(sym);

        BOOST_TEST(const_sym.find("a") && *const_sym.find("a") == 1);
        BOOST_TEST(const_sym.find("b") && *const_sym.find("b") == 2);
        BOOST_TEST(const_sym.find("c") && *const_sym.find("c") == 0);
        BOOST_TEST(!const_sym.find("d"));
        
        
    }

    return boost::report_errors();
}
Example #20
0
int
main()
{
    using spirit_test::test;
    using spirit_test::test_attr;

    {
        using namespace boost::spirit::ascii;
        BOOST_TEST(test("1", alnum));
        BOOST_TEST(!test(" ", alnum));
        BOOST_TEST(!test("1", alpha));
        BOOST_TEST(test("x", alpha));
        BOOST_TEST(test(" ", blank));
        BOOST_TEST(!test("x", blank));
        BOOST_TEST(test("1", digit));
        BOOST_TEST(!test("x", digit));
        BOOST_TEST(test("a", lower));
        BOOST_TEST(!test("A", lower));
        BOOST_TEST(test("!", punct));
        BOOST_TEST(!test("x", punct));
        BOOST_TEST(test(" ", space));
        BOOST_TEST(test("\n", space));
        BOOST_TEST(test("\r", space));
        BOOST_TEST(test("\t", space));
        BOOST_TEST(test("A", upper));
        BOOST_TEST(!test("a", upper));
        BOOST_TEST(test("A", xdigit));
        BOOST_TEST(test("0", xdigit));
        BOOST_TEST(test("f", xdigit));
        BOOST_TEST(!test("g", xdigit));

        // should fail, not assert!
        // $$$ Removing this test for now $$$
        // BOOST_TEST(!test("\265", space));
    }

    {
        using namespace boost::spirit::ascii;
        BOOST_TEST(!test("1", ~alnum));
        BOOST_TEST(test(" ", ~alnum));
        BOOST_TEST(test("1", ~alpha));
        BOOST_TEST(!test("x", ~alpha));
        BOOST_TEST(!test(" ", ~blank));
        BOOST_TEST(test("x", ~blank));
        BOOST_TEST(!test("1", ~digit));
        BOOST_TEST(test("x", ~digit));
        BOOST_TEST(!test("a", ~lower));
        BOOST_TEST(test("A", ~lower));
        BOOST_TEST(!test("!", ~punct));
        BOOST_TEST(test("x", ~punct));
        BOOST_TEST(!test(" ", ~space));
        BOOST_TEST(!test("\n", ~space));
        BOOST_TEST(!test("\r", ~space));
        BOOST_TEST(!test("\t", ~space));
        BOOST_TEST(!test("A", ~upper));
        BOOST_TEST(test("a", ~upper));
        BOOST_TEST(!test("A", ~xdigit));
        BOOST_TEST(!test("0", ~xdigit));
        BOOST_TEST(!test("f", ~xdigit));
        BOOST_TEST(test("g", ~xdigit));
    }

    {
        // we use the hoisted qi namespace this time
        using namespace boost::spirit::qi::iso8859_1;
        BOOST_TEST(test("1", alnum));
        BOOST_TEST(!test(" ", alnum));
        BOOST_TEST(!test("1", alpha));
        BOOST_TEST(test("x", alpha));
        BOOST_TEST(test(" ", blank));
        BOOST_TEST(!test("x", blank));
        BOOST_TEST(test("1", digit));
        BOOST_TEST(!test("x", digit));
        BOOST_TEST(test("a", lower));
        BOOST_TEST(!test("A", lower));
        BOOST_TEST(test("!", punct));
        BOOST_TEST(!test("x", punct));
        BOOST_TEST(test(" ", space));
        BOOST_TEST(test("\n", space));
        BOOST_TEST(test("\r", space));
        BOOST_TEST(test("\t", space));
        BOOST_TEST(test("A", upper));
        BOOST_TEST(!test("a", upper));
        BOOST_TEST(test("A", xdigit));
        BOOST_TEST(test("0", xdigit));
        BOOST_TEST(test("f", xdigit));
        BOOST_TEST(!test("g", xdigit));

// needed for VC7.1 only
#if BOOST_WORKAROUND(BOOST_MSVC, BOOST_TESTED_AT(1310))
#pragma setlocale("german")
#endif
        BOOST_TEST(test("é", alpha));
        BOOST_TEST(test("é", lower));
        BOOST_TEST(!test("é", upper));
#if BOOST_WORKAROUND(BOOST_MSVC, BOOST_TESTED_AT(1310))
#pragma setlocale("")
#endif
    }

    {
        using namespace boost::spirit::standard;
        BOOST_TEST(test("1", alnum));
        BOOST_TEST(!test(" ", alnum));
        BOOST_TEST(!test("1", alpha));
        BOOST_TEST(test("x", alpha));
        BOOST_TEST(test(" ", blank));
        BOOST_TEST(!test("x", blank));
        BOOST_TEST(test("1", digit));
        BOOST_TEST(!test("x", digit));
        BOOST_TEST(test("a", lower));
        BOOST_TEST(!test("A", lower));
        BOOST_TEST(test("!", punct));
        BOOST_TEST(!test("x", punct));
        BOOST_TEST(test(" ", space));
        BOOST_TEST(test("\n", space));
        BOOST_TEST(test("\r", space));
        BOOST_TEST(test("\t", space));
        BOOST_TEST(test("A", upper));
        BOOST_TEST(!test("a", upper));
        BOOST_TEST(test("A", xdigit));
        BOOST_TEST(test("0", xdigit));
        BOOST_TEST(test("f", xdigit));
        BOOST_TEST(!test("g", xdigit));
    }

    {
        using namespace boost::spirit::standard_wide;
        BOOST_TEST(test(L"1", alnum));
        BOOST_TEST(!test(L" ", alnum));
        BOOST_TEST(!test(L"1", alpha));
        BOOST_TEST(test(L"x", alpha));
        BOOST_TEST(test(L" ", blank));
        BOOST_TEST(!test(L"x", blank));
        BOOST_TEST(test(L"1", digit));
        BOOST_TEST(!test(L"x", digit));
        BOOST_TEST(test(L"a", lower));
        BOOST_TEST(!test(L"A", lower));
        BOOST_TEST(test(L"!", punct));
        BOOST_TEST(!test(L"x", punct));
        BOOST_TEST(test(L" ", space));
        BOOST_TEST(test(L"\n", space));
        BOOST_TEST(test(L"\r", space));
        BOOST_TEST(test(L"\t", space));
        BOOST_TEST(test(L"A", upper));
        BOOST_TEST(!test(L"a", upper));
        BOOST_TEST(test(L"A", xdigit));
        BOOST_TEST(test(L"0", xdigit));
        BOOST_TEST(test(L"f", xdigit));
        BOOST_TEST(!test(L"g", xdigit));
    }

    {
        using namespace boost::spirit::unicode;
        BOOST_TEST(test(L"1", alnum));
        BOOST_TEST(!test(L" ", alnum));
        BOOST_TEST(!test(L"1", alpha));
        BOOST_TEST(test(L"x", alpha));
        BOOST_TEST(test(L" ", blank));
        BOOST_TEST(!test(L"x", blank));
        BOOST_TEST(test(L"1", digit));
        BOOST_TEST(!test(L"x", digit));
        BOOST_TEST(test(L"a", lower));
        BOOST_TEST(!test(L"A", lower));
        BOOST_TEST(test(L"!", punct));
        BOOST_TEST(!test(L"x", punct));
        BOOST_TEST(test(L" ", space));
        BOOST_TEST(test(L"\n", space));
        BOOST_TEST(test(L"\r", space));
        BOOST_TEST(test(L"\t", space));
        BOOST_TEST(test(L"A", upper));
        BOOST_TEST(!test(L"a", upper));
        BOOST_TEST(test(L"A", xdigit));
        BOOST_TEST(test(L"0", xdigit));
        BOOST_TEST(test(L"f", xdigit));
        BOOST_TEST(!test(L"g", xdigit));

// needed for VC7.1 only
#if BOOST_WORKAROUND(BOOST_MSVC, BOOST_TESTED_AT(1310))
#pragma setlocale("german")
#endif
        BOOST_TEST(test("é", alpha));
        BOOST_TEST(test("é", lower));
        BOOST_TEST(!test("é", upper));

#if BOOST_WORKAROUND(BOOST_MSVC, BOOST_TESTED_AT(1310))
#pragma setlocale("")
#endif
    }

    {   // test attribute extraction
        using boost::spirit::qi::domain;
        using boost::spirit::traits::attribute_of;
        using boost::spirit::iso8859_1::alpha;
        using boost::spirit::iso8859_1::alpha_type;
        using boost::spirit::result_of::compile;

        BOOST_STATIC_ASSERT((
            boost::is_same<
                attribute_of<compile<domain, alpha_type>::type>::type
              , unsigned char>::value));

        int attr = 0;
        BOOST_TEST(test_attr("a", alpha, attr));
        BOOST_TEST(attr == 'a');
    }

    {   // test attribute extraction
        using boost::spirit::iso8859_1::alpha;
        using boost::spirit::iso8859_1::space;
        char attr = 0;
        BOOST_TEST(test_attr("     a", alpha, attr, space));
        BOOST_TEST(attr == 'a');
    }

    {   // test action

        using namespace boost::spirit::ascii;
        using boost::phoenix::ref;
        using boost::spirit::_1;
        char ch;

        BOOST_TEST(test("x", alnum[ref(ch) = _1]));
        BOOST_TEST(ch == 'x');
        BOOST_TEST(test("   A", alnum[ref(ch) = _1], space));
        BOOST_TEST(ch == 'A');
    }

    return boost::report_errors();
}
Example #21
0
int
main()
{
    using spirit_test::test;
    using spirit_test::test_attr;

    {
        using namespace boost::spirit::ascii;
        BOOST_TEST(test("1", alnum));
        BOOST_TEST(!test(" ", alnum));
        BOOST_TEST(!test("1", alpha));
        BOOST_TEST(test("x", alpha));
        BOOST_TEST(test(" ", blank));
        BOOST_TEST(!test("x", blank));
        BOOST_TEST(test("1", digit));
        BOOST_TEST(!test("x", digit));
        BOOST_TEST(test("a", lower));
        BOOST_TEST(!test("A", lower));
        BOOST_TEST(test("!", punct));
        BOOST_TEST(!test("x", punct));
        BOOST_TEST(test(" ", space));
        BOOST_TEST(test("\n", space));
        BOOST_TEST(test("\r", space));
        BOOST_TEST(test("\t", space));
        BOOST_TEST(test("A", upper));
        BOOST_TEST(!test("a", upper));
        BOOST_TEST(test("A", xdigit));
        BOOST_TEST(test("0", xdigit));
        BOOST_TEST(test("f", xdigit));
        BOOST_TEST(!test("g", xdigit));
    }

    {
        using namespace boost::spirit::iso8859_1;
        BOOST_TEST(test("1", alnum));
        BOOST_TEST(!test(" ", alnum));
        BOOST_TEST(!test("1", alpha));
        BOOST_TEST(test("x", alpha));
        BOOST_TEST(test(" ", blank));
        BOOST_TEST(!test("x", blank));
        BOOST_TEST(test("1", digit));
        BOOST_TEST(!test("x", digit));
        BOOST_TEST(test("a", lower));
        BOOST_TEST(!test("A", lower));
        BOOST_TEST(test("!", punct));
        BOOST_TEST(!test("x", punct));
        BOOST_TEST(test(" ", space));
        BOOST_TEST(test("\n", space));
        BOOST_TEST(test("\r", space));
        BOOST_TEST(test("\t", space));
        BOOST_TEST(test("A", upper));
        BOOST_TEST(!test("a", upper));
        BOOST_TEST(test("A", xdigit));
        BOOST_TEST(test("0", xdigit));
        BOOST_TEST(test("f", xdigit));
        BOOST_TEST(!test("g", xdigit));

        BOOST_TEST(test("é", alpha));
        BOOST_TEST(test("é", lower));
        BOOST_TEST(!test("é", upper));
    }

    {
        using namespace boost::spirit::standard;
        BOOST_TEST(test("1", alnum));
        BOOST_TEST(!test(" ", alnum));
        BOOST_TEST(!test("1", alpha));
        BOOST_TEST(test("x", alpha));
        BOOST_TEST(test(" ", blank));
        BOOST_TEST(!test("x", blank));
        BOOST_TEST(test("1", digit));
        BOOST_TEST(!test("x", digit));
        BOOST_TEST(test("a", lower));
        BOOST_TEST(!test("A", lower));
        BOOST_TEST(test("!", punct));
        BOOST_TEST(!test("x", punct));
        BOOST_TEST(test(" ", space));
        BOOST_TEST(test("\n", space));
        BOOST_TEST(test("\r", space));
        BOOST_TEST(test("\t", space));
        BOOST_TEST(test("A", upper));
        BOOST_TEST(!test("a", upper));
        BOOST_TEST(test("A", xdigit));
        BOOST_TEST(test("0", xdigit));
        BOOST_TEST(test("f", xdigit));
        BOOST_TEST(!test("g", xdigit));
    }

    {
        using namespace boost::spirit::standard_wide;
        BOOST_TEST(test(L"1", alnum));
        BOOST_TEST(!test(L" ", alnum));
        BOOST_TEST(!test(L"1", alpha));
        BOOST_TEST(test(L"x", alpha));
        BOOST_TEST(test(L" ", blank));
        BOOST_TEST(!test(L"x", blank));
        BOOST_TEST(test(L"1", digit));
        BOOST_TEST(!test(L"x", digit));
        BOOST_TEST(test(L"a", lower));
        BOOST_TEST(!test(L"A", lower));
        BOOST_TEST(test(L"!", punct));
        BOOST_TEST(!test(L"x", punct));
        BOOST_TEST(test(L" ", space));
        BOOST_TEST(test(L"\n", space));
        BOOST_TEST(test(L"\r", space));
        BOOST_TEST(test(L"\t", space));
        BOOST_TEST(test(L"A", upper));
        BOOST_TEST(!test(L"a", upper));
        BOOST_TEST(test(L"A", xdigit));
        BOOST_TEST(test(L"0", xdigit));
        BOOST_TEST(test(L"f", xdigit));
        BOOST_TEST(!test(L"g", xdigit));
    }

    {   // test attribute extraction
        using boost::spirit::qi::domain;
        using boost::spirit::traits::attribute_of;
        using boost::spirit::iso8859_1::alpha;
        using boost::spirit::iso8859_1::alpha_type;

        BOOST_STATIC_ASSERT((
            boost::is_same<
                attribute_of<domain, alpha_type, char const*, int>::type
              , unsigned char>::value));

        int attr = 0;
        BOOST_TEST(test_attr("a", alpha, attr));
        BOOST_TEST(attr == 'a');
    }

    {   // test attribute extraction
        using boost::spirit::iso8859_1::alpha;
        using boost::spirit::iso8859_1::space;
        char attr = 0;
        BOOST_TEST(test_attr("     a", alpha, attr, space));
        BOOST_TEST(attr == 'a');
    }

    {   // test action

        using namespace boost::phoenix;
        using namespace boost::spirit::ascii;
        using boost::spirit::arg_names::_1;
        char ch;

        BOOST_TEST(test("x", alnum[ref(ch) = _1]));
        BOOST_TEST(ch == 'x');
        BOOST_TEST(test("   A", alnum[ref(ch) = _1], space));
        BOOST_TEST(ch == 'A');
    }

    return boost::report_errors();
}
Example #22
0
int
main()
{
    using spirit_test::test;
    //~ using spirit_test::test_attr;
    //~ using spirit_test::print_info;

    {
        using namespace boost::spirit::x3::ascii;

        BOOST_TEST(test("x", 'x'));
        BOOST_TEST(test(L"x", L'x'));
        BOOST_TEST(!test("y", 'x'));
        BOOST_TEST(!test(L"y", L'x'));

        BOOST_TEST(test("x", char_));
        BOOST_TEST(test("x", char_('x')));
        BOOST_TEST(!test("x", char_('y')));
        //~ BOOST_TEST(test("x", char_('a', 'z')));
        //~ BOOST_TEST(!test("x", char_('0', '9')));

        BOOST_TEST(!test("x", ~char_));
        BOOST_TEST(!test("x", ~char_('x')));
        BOOST_TEST(test(" ", ~char_('x')));
        BOOST_TEST(test("X", ~char_('x')));
        //~ BOOST_TEST(!test("x", ~char_('b', 'y')));
        //~ BOOST_TEST(test("a", ~char_('b', 'y')));
        //~ BOOST_TEST(test("z", ~char_('b', 'y')));

        BOOST_TEST(test("x", ~~char_));
        BOOST_TEST(test("x", ~~char_('x')));
        BOOST_TEST(!test(" ", ~~char_('x')));
        BOOST_TEST(!test("X", ~~char_('x')));
        //~ BOOST_TEST(test("x", ~~char_('b', 'y')));
        //~ BOOST_TEST(!test("a", ~~char_('b', 'y')));
        //~ BOOST_TEST(!test("z", ~~char_('b', 'y')));
    }

    {
        using namespace boost::spirit::x3::ascii;

        BOOST_TEST(test("   x", 'x', space));
        BOOST_TEST(test(L"   x", L'x', space));

        BOOST_TEST(test("   x", char_, space));
        BOOST_TEST(test("   x", char_('x'), space));
        BOOST_TEST(!test("   x", char_('y'), space));
        //~ BOOST_TEST(test("   x", char_('a', 'z'), space));
        //~ BOOST_TEST(!test("   x", char_('0', '9'), space));
    }

    {
        using namespace boost::spirit::x3::standard_wide;

        BOOST_TEST(test(L"x", char_));
        BOOST_TEST(test(L"x", char_(L'x')));
        BOOST_TEST(!test(L"x", char_(L'y')));
        //~ BOOST_TEST(test(L"x", char_(L'a', L'z')));
        //~ BOOST_TEST(!test(L"x", char_(L'0', L'9')));

        BOOST_TEST(!test(L"x", ~char_));
        BOOST_TEST(!test(L"x", ~char_(L'x')));
        BOOST_TEST(test(L" ", ~char_(L'x')));
        BOOST_TEST(test(L"X", ~char_(L'x')));
        //~ BOOST_TEST(!test(L"x", ~char_(L'b', L'y')));
        //~ BOOST_TEST(test(L"a", ~char_(L'b', L'y')));
        //~ BOOST_TEST(test(L"z", ~char_(L'b', L'y')));

        BOOST_TEST(test(L"x", ~~char_));
        BOOST_TEST(test(L"x", ~~char_(L'x')));
        BOOST_TEST(!test(L" ", ~~char_(L'x')));
        BOOST_TEST(!test(L"X", ~~char_(L'x')));
        //~ BOOST_TEST(test(L"x", ~~char_(L'b', L'y')));
        //~ BOOST_TEST(!test(L"a", ~~char_(L'b', L'y')));
        //~ BOOST_TEST(!test(L"z", ~~char_(L'b', L'y')));
    }


    //~ {   // single char strings!
        //~ namespace ascii = boost::spirit::x3::ascii;
        //~ namespace wide = boost::spirit::x3::standard_wide;

        //~ BOOST_TEST(test("x", "x"));
        //~ BOOST_TEST(test(L"x", L"x"));
        //~ BOOST_TEST(test("x", ascii::char_("x")));
        //~ BOOST_TEST(test(L"x", wide::char_(L"x")));

        //~ BOOST_TEST(test("x", ascii::char_("a", "z")));
        //~ BOOST_TEST(test(L"x", ascii::char_(L"a", L"z")));
    //~ }

    //~ {
        //~ // chsets
        //~ namespace ascii = boost::spirit::x3::ascii;
        //~ namespace wide = boost::spirit::x3::standard_wide;

        //~ BOOST_TEST(test("x", ascii::char_("a-z")));
        //~ BOOST_TEST(!test("1", ascii::char_("a-z")));
        //~ BOOST_TEST(test("1", ascii::char_("a-z0-9")));

        //~ BOOST_TEST(test("x", wide::char_(L"a-z")));
        //~ BOOST_TEST(!test("1", wide::char_(L"a-z")));
        //~ BOOST_TEST(test("1", wide::char_(L"a-z0-9")));

        //~ std::string set = "a-z0-9";
        //~ BOOST_TEST(test("x", ascii::char_(set)));

//~ #ifdef SPIRIT_NO_COMPILE_CHECK
        //~ test("", ascii::char_(L"a-z0-9"));
//~ #endif
    //~ }

    //~ {   // lazy chars

        //~ using namespace boost::spirit::x3::ascii;

        //~ using boost::phoenix::val;
        //~ using boost::phoenix::ref;
        //~ using boost::spirit::x3::_1;

        //~ BOOST_TEST((test("x", char_(val('x')))));
        //~ BOOST_TEST((test("h", char_(val('a'), val('n')))));
        //~ BOOST_TEST(test("0", char_(val("a-z0-9"))));

        //~ char ch; // make sure lazy chars have an attribute
        //~ BOOST_TEST(test("x", char_(val('x'))[ref(ch) = _1]));
        //~ BOOST_TEST(ch == 'x');
    //~ }

    //~ { // testing "what"

        //~ using boost::spirit::x3::what;
        //~ using boost::spirit::x3::ascii::char_;
        //~ using boost::spirit::x3::ascii::alpha;

        //~ print_info(what('x'));
        //~ print_info(what(char_('a','z')));
        //~ print_info(what(alpha));
    //~ }

    return boost::report_errors();
}
Example #23
0
int main()
{
    using spirit_test::test_attr;
    using spirit_test::test;
    using spirit_test::binary_test;
    using spirit_test::binary_test_attr;

    using boost::spirit::qi::byte_;
    using boost::spirit::qi::word;
    using boost::spirit::qi::dword;
    using boost::spirit::qi::big_word;
    using boost::spirit::qi::big_dword;
    using boost::spirit::qi::little_word;
    using boost::spirit::qi::little_dword;
#ifdef BOOST_HAS_LONG_LONG
    using boost::spirit::qi::qword;
    using boost::spirit::qi::big_qword;
    using boost::spirit::qi::little_qword;
#endif
    using boost::spirit::qi::bin_float;
    using boost::spirit::qi::big_bin_float;
    using boost::spirit::qi::little_bin_float;
    using boost::spirit::qi::bin_double;
    using boost::spirit::qi::big_bin_double;
    using boost::spirit::qi::little_bin_double;

    boost::uint8_t uc;
    boost::uint16_t us;
    boost::uint32_t ui;
#ifdef BOOST_HAS_LONG_LONG
    boost::uint64_t ul;
#endif
    float f;
    double d;

    {   // test native endian binaries
#ifdef BOOST_LITTLE_ENDIAN
        BOOST_TEST(test_attr("\x01", byte_, uc) && uc == 0x01);
        BOOST_TEST(test_attr("\x01\x02", word, us) && us == 0x0201);
        BOOST_TEST(test_attr("\x01\x02\x03\x04", dword, ui) && ui == 0x04030201);
#ifdef BOOST_HAS_LONG_LONG
        BOOST_TEST(test_attr("\x01\x02\x03\x04\x05\x06\x07\x08", qword, ul) &&
            ul == 0x0807060504030201LL);
#endif
        BOOST_TEST(binary_test_attr("\x00\x00\x80\x3f", 4, bin_float, f) &&
            f == 1.0f);
        BOOST_TEST(binary_test_attr("\x00\x00\x00\x00\x00\x00\xf0\x3f",
            8, bin_double, d) && f == 1.0);
#else
        BOOST_TEST(test_attr("\x01", byte_, uc) && uc == 0x01);
        BOOST_TEST(test_attr("\x01\x02", word, us) && us ==  0x0102);
        BOOST_TEST(test_attr("\x01\x02\x03\x04", dword, ui) && ui == 0x01020304);
#ifdef BOOST_HAS_LONG_LONG
        BOOST_TEST(test_attr("\x01\x02\x03\x04\x05\x06\x07\x08", qword, ul) &&
            ul == 0x0102030405060708LL);
#endif
        BOOST_TEST(binary_test_attr("\x3f\x80\x00\x00", 4, bin_float, f) &&
            f == 1.0f);
        BOOST_TEST(binary_test_attr("\x3f\xf0\x00\x00\x00\x00\x00\x00",
            8, bin_double, d) && f == 1.0);
#endif
    }

    {   // test native endian binaries
#ifdef BOOST_LITTLE_ENDIAN
        BOOST_TEST(test("\x01", byte_(0x01)));
        BOOST_TEST(test("\x01\x02", word(0x0201)));
        BOOST_TEST(test("\x01\x02\x03\x04", dword(0x04030201)));
#ifdef BOOST_HAS_LONG_LONG
        BOOST_TEST(test("\x01\x02\x03\x04\x05\x06\x07\x08",
            qword(0x0807060504030201LL)));
#endif
        BOOST_TEST(binary_test("\x00\x00\x80\x3f", 4, bin_float(1.0f)));
        BOOST_TEST(binary_test("\x00\x00\x00\x00\x00\x00\xf0\x3f", 8,
            bin_double(1.0)));
#else
        BOOST_TEST(test("\x01", byte_(0x01)));
        BOOST_TEST(test("\x01\x02", word(0x0102)));
        BOOST_TEST(test("\x01\x02\x03\x04", dword(0x01020304)));
#ifdef BOOST_HAS_LONG_LONG
        BOOST_TEST(test("\x01\x02\x03\x04\x05\x06\x07\x08",
            qword(0x0102030405060708LL)));
#endif
        BOOST_TEST(binary_test("\x3f\x80\x00\x00", 4, bin_float(1.0f)));
        BOOST_TEST(binary_test("\x3f\x80\x00\x00\x00\x00\x00\x00", 8,
            bin_double(1.0)));
#endif
    }

    {   // test big endian binaries
        BOOST_TEST(test_attr("\x01\x02", big_word, us) && us == 0x0102);
        BOOST_TEST(test_attr("\x01\x02\x03\x04", big_dword, ui) && ui == 0x01020304);
#ifdef BOOST_HAS_LONG_LONG
        BOOST_TEST(test_attr("\x01\x02\x03\x04\x05\x06\x07\x08", big_qword, ul)
            && ul == 0x0102030405060708LL);
#endif
        BOOST_TEST(binary_test_attr("\x3f\x80\x00\x00", 4, big_bin_float, f) &&
            f == 1.0f);
        BOOST_TEST(binary_test_attr("\x3f\xf0\x00\x00\x00\x00\x00\x00",
            8, big_bin_double, d) && f == 1.0);
    }

    {
        BOOST_TEST(test("\x01\x02", big_word(0x0102)));
        BOOST_TEST(test("\x01\x02\x03\x04", big_dword(0x01020304)));
#ifdef BOOST_HAS_LONG_LONG
        BOOST_TEST(test("\x01\x02\x03\x04\x05\x06\x07\x08",
            big_qword(0x0102030405060708LL)));
#endif
        BOOST_TEST(binary_test("\x3f\x80\x00\x00", 4, big_bin_float(1.0f)));
        BOOST_TEST(binary_test("\x3f\xf0\x00\x00\x00\x00\x00\x00", 8,
            big_bin_double(1.0)));
    }

    {   // test little endian binaries
        BOOST_TEST(test_attr("\x01\x02", little_word, us) && us == 0x0201);
        BOOST_TEST(test_attr("\x01\x02\x03\x04", little_dword, ui) && ui == 0x04030201);
#ifdef BOOST_HAS_LONG_LONG
        BOOST_TEST(test_attr("\x01\x02\x03\x04\x05\x06\x07\x08", little_qword, ul)
            && ul == 0x0807060504030201LL);
#endif
        BOOST_TEST(binary_test_attr("\x00\x00\x80\x3f", 4,
            little_bin_float, f) && f == 1.0f);
        BOOST_TEST(binary_test_attr("\x00\x00\x00\x00\x00\x00\xf0\x3f",
            8, little_bin_double, d) && f == 1.0);
    }

    {
        BOOST_TEST(test("\x01\x02", little_word(0x0201)));
        BOOST_TEST(test("\x01\x02\x03\x04", little_dword(0x04030201)));
#ifdef BOOST_HAS_LONG_LONG
        BOOST_TEST(test("\x01\x02\x03\x04\x05\x06\x07\x08",
            little_qword(0x0807060504030201LL)));
#endif
        BOOST_TEST(binary_test("\x00\x00\x80\x3f", 4, little_bin_float(1.0f)));
        BOOST_TEST(binary_test("\x00\x00\x00\x00\x00\x00\xf0\x3f", 8,
            little_bin_double(1.0)));
    }

    return boost::report_errors();
}
Example #24
0
int main()
{
    using spirit_test::test;
    using boost::spirit::karma::symbols;

    { // advanced
        using boost::spirit::karma::rule;
        using boost::spirit::karma::lit;

        typedef spirit_test::output_iterator<char>::type output_iterator_type;

        symbols<char, rule<output_iterator_type> > sym;

        rule<output_iterator_type> r1 = lit("Joel");
        rule<output_iterator_type> r2 = lit("Hartmut");
        rule<output_iterator_type> r3 = lit("Tom");
        rule<output_iterator_type> r4 = lit("Kim");

        sym.add
            ('j', r1.alias())
            ('h', r2.alias())
            ('t', r3.alias())
            ('k', r4.alias())
        ;

        boost::mpl::true_ f =
            boost::mpl::bool_<boost::spirit::traits::is_generator<
                symbols<char, rule<output_iterator_type> > >::value>();

        // silence stupid compiler warnings
        // i.e. MSVC warning C4189: 'f' : local variable is initialized but not referenced
        BOOST_TEST((f.value));

        BOOST_TEST((test("Joel", sym, 'j')));
        BOOST_TEST((test("Hartmut", sym, 'h')));
        BOOST_TEST((test("Tom", sym, 't')));
        BOOST_TEST((test("Kim", sym, 'k')));
        BOOST_TEST((!test("", sym, 'x')));

        // test copy
        symbols<char, rule<output_iterator_type> > sym2;
        sym2 = sym;
        BOOST_TEST((test("Joel", sym2, 'j')));
        BOOST_TEST((test("Hartmut", sym2, 'h')));
        BOOST_TEST((test("Tom", sym2, 't')));
        BOOST_TEST((test("Kim", sym2, 'k')));
        BOOST_TEST((!test("", sym2, 'x')));

        // make sure it plays well with other generators
        BOOST_TEST((test("Joelyo", sym << "yo", 'j')));

        sym.remove
            ('j')
            ('h')
        ;

        BOOST_TEST((!test("", sym, 'j')));
        BOOST_TEST((!test("", sym, 'h')));
    }

    { // more advanced
        using boost::spirit::karma::rule;
        using boost::spirit::karma::lit;
        using boost::spirit::karma::string;

        typedef spirit_test::output_iterator<char>::type output_iterator_type;

        symbols<char, rule<output_iterator_type, std::string()> > sym;
        rule<output_iterator_type, std::string()> r1 = string;

        sym.add
            ('j', r1.alias())
            ('h', r1.alias())
            ('t', r1.alias())
            ('k', r1.alias())
        ;

        boost::mpl::true_ f =
            boost::mpl::bool_<boost::spirit::traits::is_generator<
                symbols<char, std::string> >::value>();

        // silence stupid compiler warnings
        // i.e. MSVC warning C4189: 'f' : local variable is initialized but not referenced
        BOOST_TEST((f.value));

        BOOST_TEST((test("Joel", sym, fusion::make_vector('j', "Joel"))));
        BOOST_TEST((test("Hartmut", sym, fusion::make_vector('h', "Hartmut"))));
        BOOST_TEST((test("Tom", sym, fusion::make_vector('t', "Tom"))));
        BOOST_TEST((test("Kim", sym, fusion::make_vector('k', "Kim"))));
        BOOST_TEST((!test("", sym, 'x')));

        // test copy
        symbols<char, rule<output_iterator_type, std::string()> > sym2;
        sym2 = sym;
        BOOST_TEST((test("Joel", sym2, fusion::make_vector('j', "Joel"))));
        BOOST_TEST((test("Hartmut", sym2, fusion::make_vector('h', "Hartmut"))));
        BOOST_TEST((test("Tom", sym2, fusion::make_vector('t', "Tom"))));
        BOOST_TEST((test("Kim", sym2, fusion::make_vector('k', "Kim"))));
        BOOST_TEST((!test("", sym2, 'x')));

        // make sure it plays well with other generators
        BOOST_TEST((test("Joelyo", sym << "yo", fusion::make_vector('j', "Joel"))));

        sym.remove
            ('j')
            ('h')
        ;

        BOOST_TEST((!test("", sym, 'j')));
        BOOST_TEST((!test("", sym, 'h')));
    }

    { // test for proto problem with rvalue references (10-11-2011)
        symbols<char, std::string> sym;

        sym += std::make_pair('j', "Joel");
        sym += std::make_pair('h', "Hartmut");

        BOOST_TEST((test("Joel", sym, 'j')));
        BOOST_TEST((test("Hartmut", sym, 'h')));
    }

    return boost::report_errors();
}
int
main()
{
    using spirit_test::test;
    using spirit_test::test_attr;
    using namespace boost::spirit::ascii;
    using boost::spirit::qi::int_;
    using boost::spirit::qi::omit;
    using boost::spirit::qi::lit;
    using boost::spirit::qi::_1;
    using boost::spirit::qi::lexeme;

    {
        BOOST_TEST(test("aaaaaaaa", +char_));
        BOOST_TEST(test("a", +char_));
        BOOST_TEST(!test("", +char_));
        BOOST_TEST(test("aaaaaaaa", +alpha));
        BOOST_TEST(!test("aaaaaaaa", +upper));
    }

    {
        BOOST_TEST(test(" a a aaa aa", +char_, space));
        BOOST_TEST(test("12345 678 9 ", +digit, space));
    }

    {
        BOOST_TEST(test("aBcdeFGH", no_case[+char_]));
        BOOST_TEST(test("a B cde FGH  ", no_case[+char_], space));
    }

    {
        std::vector<int> v;
        BOOST_TEST(test_attr("123 456 789 10", +int_, v, space) && 4 == v.size() &&
            v[0] == 123 && v[1] == 456 && v[2] == 789 &&  v[3] == 10);
    }

    {
        std::vector<std::string> v;
        BOOST_TEST(test_attr("a b c d", +lexeme[+alpha], v, space) && 4 == v.size() &&
            v[0] == "a" && v[1] == "b" && v[2] == "c" &&  v[3] == "d");
    }
    
    {
        BOOST_TEST(test("Kim Kim Kim", +lit("Kim"), space));
    }

    {
        // The following 4 tests show that omit does not inhibit explicit attributes

        std::string s;
        BOOST_TEST(test_attr("bbbb", +omit[char_('b')], s) && s == "bbbb");

        s.clear();
        BOOST_TEST(test_attr("bbbb", omit[+char_('b')], s) && s == "bbbb");

        s.clear();
        BOOST_TEST(test_attr("b b b b ", +omit[char_('b')], s, space) && s == "bbbb");

        s.clear();
        BOOST_TEST(test_attr("b b b b ", omit[+char_('b')], s, space) && s == "bbbb");
    }

    { // actions
        namespace phx = boost::phoenix;

        std::vector<char> v;
        BOOST_TEST(test("bbbb", (+char_)[phx::ref(v) = _1]) && 4 == v.size() &&
            v[0] == 'b' && v[1] == 'b' && v[2] == 'b' &&  v[3] == 'b');
    }

    { // more actions
        namespace phx = boost::phoenix;

        std::vector<int> v;
        BOOST_TEST(test("1 2 3", (+int_)[phx::ref(v) = _1], space) && 3 == v.size() &&
            v[0] == 1 && v[1] == 2 && v[2] == 3);
    }
    
    { // attribute customization
        
        x_attr x;
        test_attr("abcde", +char_, x);
    }

    return boost::report_errors();
}
Example #26
0
int
main()
{
    using spirit_test::test;
    using spirit_test::test_attr;
    ///////////////////////////////////////////////////////////////////////////
    //  signed real number tests
    ///////////////////////////////////////////////////////////////////////////
    {
        using boost::spirit::x3::double_;
        using boost::spirit::x3::parse;
        double  d;

        BOOST_TEST(test("-1234", double_));
        BOOST_TEST(test_attr("-1234", double_, d) && compare(d, -1234));

        BOOST_TEST(test("-1.2e3", double_));
        BOOST_TEST(test_attr("-1.2e3", double_, d) && compare(d, -1.2e3));

        BOOST_TEST(test("+1.2e3", double_));
        BOOST_TEST(test_attr("+1.2e3", double_, d) && compare(d, 1.2e3));

        BOOST_TEST(test("-0.1", double_));
        BOOST_TEST(test_attr("-0.1", double_, d) && compare(d, -0.1));

        BOOST_TEST(test("-1.2e-3", double_));
        BOOST_TEST(test_attr("-1.2e-3", double_, d) && compare(d, -1.2e-3));

        BOOST_TEST(test("-1.e2", double_));
        BOOST_TEST(test_attr("-1.e2", double_, d) && compare(d, -1.e2));

        BOOST_TEST(test("-.2e3", double_));
        BOOST_TEST(test_attr("-.2e3", double_, d) && compare(d, -.2e3));

        BOOST_TEST(test("-2e3", double_));
        BOOST_TEST(test_attr("-2e3", double_, d) && compare(d, -2e3));

        BOOST_TEST(!test("-e3", double_));
        BOOST_TEST(!test_attr("-e3", double_, d));

        BOOST_TEST(!test("-1.2e", double_));
        BOOST_TEST(!test_attr("-1.2e", double_, d));

#if defined(BOOST_SPIRIT_TEST_REAL_PRECISION)
        BOOST_TEST(test_attr("-5.7222349715140557e+307", double_, d));
        BOOST_TEST(d == -5.7222349715140557e+307); // exact!

        BOOST_TEST(test_attr("2.0332938517515416e-308", double_, d));
        BOOST_TEST(d == 2.0332938517515416e-308); // exact!

        BOOST_TEST(test_attr("20332938517515416e291", double_, d));
        BOOST_TEST(d == 20332938517515416e291); // exact!

        BOOST_TEST(test_attr("2.0332938517515416e307", double_, d));
        BOOST_TEST(d == 2.0332938517515416e307); // exact!
#endif

        using boost::math::fpclassify;
        using boost::spirit::x3::signbit;   // Boost version is broken

        BOOST_TEST(test("-inf", double_));
        BOOST_TEST(test("-infinity", double_));
        BOOST_TEST(test_attr("-inf", double_, d) &&
            FP_INFINITE == fpclassify(d) && signbit(d));
        BOOST_TEST(test_attr("-infinity", double_, d) &&
            FP_INFINITE == fpclassify(d) && signbit(d));
        BOOST_TEST(test("-INF", double_));
        BOOST_TEST(test("-INFINITY", double_));
        BOOST_TEST(test_attr("-INF", double_, d) &&
            FP_INFINITE == fpclassify(d) && signbit(d));
        BOOST_TEST(test_attr("-INFINITY", double_, d) &&
            FP_INFINITE == fpclassify(d) && signbit(d));

        BOOST_TEST(test("-nan", double_));
        BOOST_TEST(test_attr("-nan", double_, d) &&
            FP_NAN == fpclassify(d) && signbit(d));
        BOOST_TEST(test("-NAN", double_));
        BOOST_TEST(test_attr("-NAN", double_, d) &&
            FP_NAN == fpclassify(d) && signbit(d));

        BOOST_TEST(test("-nan(...)", double_));
        BOOST_TEST(test_attr("-nan(...)", double_, d) &&
            FP_NAN == fpclassify(d) && signbit(d));
        BOOST_TEST(test("-NAN(...)", double_));
        BOOST_TEST(test_attr("-NAN(...)", double_, d) &&
            FP_NAN == fpclassify(d) && signbit(d));
    }

    return boost::report_errors();
}
Example #27
0
int
main()
{
    using spirit_test::test;
    using spirit_test::test_attr;

    ///////////////////////////////////////////////////////////////////////////
    //  parameterized signed integer tests
    ///////////////////////////////////////////////////////////////////////////
    {
        using boost::spirit::int_;
        int i;

        BOOST_TEST(test("123456", int_(123456)));
        BOOST_TEST(!test("123456", int_(654321)));
        BOOST_TEST(test_attr("123456", int_(123456), i));
        BOOST_TEST(i == 123456);
        BOOST_TEST(!test_attr("123456", int_(654321), i));

        BOOST_TEST(test("+123456", int_(123456)));
        BOOST_TEST(!test("+123456", int_(654321)));
        BOOST_TEST(test_attr("+123456", int_(123456), i));
        BOOST_TEST(i == 123456);
        BOOST_TEST(!test_attr("+123456", int_(654321), i));

        BOOST_TEST(test("-123456", int_(-123456)));
        BOOST_TEST(!test("-123456", int_(123456)));
        BOOST_TEST(test_attr("-123456", int_(-123456), i));
        BOOST_TEST(i == -123456);
        BOOST_TEST(!test_attr("-123456", int_(123456), i));

        BOOST_TEST(test(max_int, int_(INT_MAX)));
        BOOST_TEST(test_attr(max_int, int_(INT_MAX), i));
        BOOST_TEST(i == INT_MAX);

        BOOST_TEST(test(min_int, int_(INT_MIN)));
        BOOST_TEST(test_attr(min_int, int_(INT_MIN), i));
        BOOST_TEST(i == INT_MIN);

        // with leading zeros
        BOOST_TEST(test("0000000000123456", int_(123456)));
        BOOST_TEST(test_attr("0000000000123456", int_(123456), i));
        BOOST_TEST(i == 123456);
    }

    ///////////////////////////////////////////////////////////////////////////
    //  parameterized long long tests
    ///////////////////////////////////////////////////////////////////////////
#ifdef BOOST_HAS_LONG_LONG
    {
        using boost::spirit::long_long;
        boost::long_long_type ll;

        BOOST_TEST(test("1234567890123456789"
          , long_long(1234567890123456789LL)));
        BOOST_TEST(!test("1234567890123456789"
          , long_long(0)));
        BOOST_TEST(test_attr("1234567890123456789"
          , long_long(1234567890123456789LL), ll));
        BOOST_TEST(ll == 1234567890123456789LL);
        BOOST_TEST(!test_attr("1234567890123456789"
          , long_long(0), ll));

        BOOST_TEST(test("-1234567890123456789"
          , long_long(-1234567890123456789LL)));
        BOOST_TEST(!test("-1234567890123456789"
          , long_long(1234567890123456789LL)));
        BOOST_TEST(test_attr("-1234567890123456789"
          , long_long(-1234567890123456789LL), ll));
        BOOST_TEST(ll == -1234567890123456789LL);
        BOOST_TEST(!test_attr("-1234567890123456789"
          , long_long(1234567890123456789LL), ll));

        BOOST_TEST(test(max_long_long, long_long(LONG_LONG_MAX)));
        BOOST_TEST(test_attr(max_long_long, long_long(LONG_LONG_MAX), ll));
        BOOST_TEST(ll == LONG_LONG_MAX);

        BOOST_TEST(test(min_long_long, long_long(LONG_LONG_MIN)));
        BOOST_TEST(test_attr(min_long_long, long_long(LONG_LONG_MIN), ll));
        BOOST_TEST(ll == LONG_LONG_MIN);
    }
#endif

    ///////////////////////////////////////////////////////////////////////////
    //  parameterized short_ and long_ tests
    ///////////////////////////////////////////////////////////////////////////
    {
        using boost::spirit::short_;
        using boost::spirit::long_;
        int i;

        BOOST_TEST(test("12345", short_(12345)));
        BOOST_TEST(!test("12345", short_(54321)));
        BOOST_TEST(test_attr("12345", short_(12345), i));
        BOOST_TEST(i == 12345);
        BOOST_TEST(!test_attr("12345", short_(54321), i));

        BOOST_TEST(test("1234567890", long_(1234567890L)));
        BOOST_TEST(!test("1234567890", long_(987654321L)));
        BOOST_TEST(test_attr("1234567890", long_(1234567890L), i));
        BOOST_TEST(i == 1234567890);
        BOOST_TEST(!test_attr("1234567890", long_(987654321L), i));
    }

    ///////////////////////////////////////////////////////////////////////////
    //  parameterized action tests
    ///////////////////////////////////////////////////////////////////////////
    {
        using boost::phoenix::ref;
        using boost::spirit::ascii::space;
        using boost::spirit::qi::int_;
        using boost::spirit::qi::_1;
        int n, m;

        BOOST_TEST(test("123", int_(123)[ref(n) = _1]));
        BOOST_TEST(n == 123);
        BOOST_TEST(!test("123", int_(321)[ref(n) = _1]));

        BOOST_TEST(test_attr("789", int_(789)[ref(n) = _1], m));
        BOOST_TEST(n == 789 && m == 789);
        BOOST_TEST(!test_attr("789", int_(987)[ref(n) = _1], m));

        BOOST_TEST(test("   456", int_(456)[ref(n) = _1], space));
        BOOST_TEST(n == 456);
        BOOST_TEST(!test("   456", int_(654)[ref(n) = _1], space));
    }
    
    ///////////////////////////////////////////////////////////////////////////
    //  parameterized lazy tests
    ///////////////////////////////////////////////////////////////////////////
    {
        using boost::phoenix::ref;
        using boost::spirit::qi::int_;
        int n = 123, m = 321;

        BOOST_TEST(test("123", int_(ref(n))));
        BOOST_TEST(!test("123", int_(ref(m))));
    }

    ///////////////////////////////////////////////////////////////////////////
    //  parameterized custom int tests
    ///////////////////////////////////////////////////////////////////////////
    {
        using boost::spirit::qi::int_;
        using boost::spirit::qi::int_parser;
        custom_int i;

        BOOST_TEST(test_attr("-123456", int_(-123456), i));
        int_parser<custom_int, 10, 1, 2> int2;
        BOOST_TEST(test_attr("-12", int2(-12), i));
    }

    return boost::report_errors();
}
Example #28
0
int main()
{
    {
        BOOST_TEST((!traits::meta_create_exists<qi::domain, my_type>::value));
    }

    {
        // test primitive types
        bool b = false;
        BOOST_TEST(test_create_parser("true", b) && b == true);
        int i = 0;
        BOOST_TEST(test_create_parser("1", i) && i == 1);
        double d = 0;
        BOOST_TEST(test_create_parser("1.1", d) && d == 1.1);
        char c = '\0';
        BOOST_TEST(test_create_parser("a", c) && c == 'a');
        wchar_t wc = L'\0';
        BOOST_TEST(test_create_parser(L"a", wc) && wc == L'a');

        // test containers
        std::vector<int> v;
        BOOST_TEST(test_create_parser("0 1 2", v) && v.size() == 3 && 
            v[0] == 0 && v[1] == 1 && v[2] == 2);

        std::list<int> l;
        BOOST_TEST(test_create_parser("0 1 2", l) && l.size() == 3 &&
            *l.begin() == 0 && *(++l.begin()) == 1 && *(++ ++l.begin()) == 2);

        // test optional
        boost::optional<int> o;
        BOOST_TEST(test_create_parser("", o) && !o);
        BOOST_TEST(test_create_parser("1", o) && !!o && boost::get<int>(o) == 1);

        // test alternative
        boost::variant<double, bool, std::vector<char> > vv;
        BOOST_TEST(test_create_parser("true", vv) && vv.which() == 1 &&
            boost::get<bool>(vv) == true);
        BOOST_TEST(test_create_parser("1.0", vv) && vv.which() == 0 &&
            boost::get<double>(vv) == 1.0);
        BOOST_TEST(test_create_parser("some_string", vv) && vv.which() == 2 &&
            boost::equals(boost::get<std::vector<char> >(vv), "some_string"));

        // test fusion sequence
        std::pair<int, double> p;
        BOOST_TEST(test_create_parser("1 2.0", p) && 
            p.first == 1 && p.second == 2.0);
    }

    {
        // test containers
        std::vector<int> v;
        BOOST_TEST(test_create_parser_auto("0 1 2", v) && v.size() == 3 && 
            v[0] == 0 && v[1] == 1 && v[2] == 2);

        std::list<int> l;
        BOOST_TEST(test_create_parser_auto("0 1 2", l) && l.size() == 3 &&
            *l.begin() == 0 && *(++l.begin()) == 1 && *(++ ++l.begin()) == 2);

        // test optional
        boost::optional<int> o;
        BOOST_TEST(test_create_parser_auto("", o) && !o);
        BOOST_TEST(test_create_parser_auto("1", o) && !!o && boost::get<int>(o) == 1);

        // test alternative
        boost::variant<double, bool, std::vector<char> > vv;
        BOOST_TEST(test_create_parser_auto("true", vv) && vv.which() == 1 &&
            boost::get<bool>(vv) == true);
        BOOST_TEST(test_create_parser_auto("1.0", vv) && vv.which() == 0 &&
            boost::get<double>(vv) == 1.0);
        BOOST_TEST(test_create_parser_auto("some_string", vv) && vv.which() == 2 &&
            boost::equals(boost::get<std::vector<char> >(vv), "some_string"));

        // test fusion sequence
        std::pair<int, double> p;
        BOOST_TEST(test_create_parser_auto("1 2.0", p) && 
            p.first == 1 && p.second == 2.0);
    }

    {
        using qi::auto_;
        using qi::no_case;
        using spirit_test::test_attr;

        // test primitive types
        bool b = false;
        BOOST_TEST(test_attr("true", auto_, b) && b == true);
        int i = 0;
        BOOST_TEST(test_attr("1", auto_, i) && i == 1);
        double d = 0;
        BOOST_TEST(test_attr("1.1", auto_, d) && d == 1.1);
        char c = '\0';
        BOOST_TEST(test_attr("a", auto_, c) && c == 'a');
        wchar_t wc = L'\0';
        BOOST_TEST(test_attr(L"a", auto_, wc) && wc == L'a');

        b = false;
        BOOST_TEST(test_attr("TRUE", no_case[auto_], b) && b == true);

        // test containers
        std::vector<int> v;
        BOOST_TEST(test_attr("0 1 2", auto_, v, qi::space) && v.size() == 3 && 
            v[0] == 0 && v[1] == 1 && v[2] == 2);
        v.clear();
        BOOST_TEST(test_attr("0,1,2", auto_ % ',', v) && v.size() == 3 && 
            v[0] == 0 && v[1] == 1 && v[2] == 2);

        std::list<int> l;
        BOOST_TEST(test_attr("0 1 2", auto_, l, qi::space) && l.size() == 3 &&
            *l.begin() == 0 && *(++l.begin()) == 1 && *(++ ++l.begin()) == 2);
        l.clear();
        BOOST_TEST(test_attr("0,1,2", auto_ % ',', l) && l.size() == 3 &&
            *l.begin() == 0 && *(++l.begin()) == 1 && *(++ ++l.begin()) == 2);

        // test optional
        boost::optional<int> o;
        BOOST_TEST(test_attr("", auto_, o) && !o);
        BOOST_TEST(test_attr("1", auto_, o) && !!o && boost::get<int>(o) == 1);

        // test alternative
        boost::variant<double, bool, std::vector<char> > vv;
        BOOST_TEST(test_attr("true", auto_, vv) && vv.which() == 1 &&
            boost::get<bool>(vv) == true);
        BOOST_TEST(test_attr("1.0", auto_, vv) && vv.which() == 0 &&
            boost::get<double>(vv) == 1.0);
        BOOST_TEST(test_create_parser("some_string", vv) && vv.which() == 2 &&
            boost::equals(boost::get<std::vector<char> >(vv), "some_string"));

        // test fusion sequence
        std::pair<int, double> p;
        BOOST_TEST(test_attr("1 2.0", auto_, p, qi::space) && 
            p.first == 1 && p.second == 2.0);
    }

    {
        // test primitive types
        BOOST_TEST(test_rule("true", true));
        BOOST_TEST(test_rule("1", 1));
        BOOST_TEST(test_rule("1.1", 1.1));

        // test containers
        std::vector<int> v;
        v.push_back(0);
        v.push_back(1);
        v.push_back(2);
        BOOST_TEST(test_rule("0 1 2", v, qi::space));

        std::list<int> l;
        l.push_back(0);
        l.push_back(1);
        l.push_back(2);
        BOOST_TEST(test_rule("0 1 2", l, qi::space));

        // test optional
        boost::optional<int> o;
        BOOST_TEST(test_rule("", o));
        o = 1;
        BOOST_TEST(test_rule("1", o));

        // test alternative
//         boost::variant<int, double, float, std::string> vv;
//         vv = 1;
//         BOOST_TEST(test_rule("1", vv));
//         vv = 1.0;
//         BOOST_TEST(test_rule("1.0", vv));
//         vv = 1.0f;
//         BOOST_TEST(test_rule("1.0", vv));
//         vv = "some string";
//         BOOST_TEST(test_rule("some string", vv));

        // test fusion sequence
        std::pair<int, double> p (1, 2.0);
        BOOST_TEST(test_rule("1 2.0", p, qi::space));
    }

    return boost::report_errors();
}
Example #29
0
int
main()
{
    using spirit_test::test;
    using spirit_test::test_attr;
    using boost::spirit::x3::symbols;
    //~ using boost::spirit::x3::lazy;

    { // basics
        symbols<char, int> sym;

        sym.add
            ("Joel")
            ("Ruby")
            ("Tenji")
            ("Tutit")
            ("Kim")
            ("Joey")
            ("Joeyboy")
        ;

        // $$$ Hat is this??? $$$
        //~ boost::mpl::true_ f = boost::mpl::bool_<boost::spirit::x3::traits::is_parser<symbols<char, int> >::value>();

        // silence stupid compiler warnings
        // i.e. MSVC warning C4189: 'f' : local variable is initialized but not referenced
        //~ BOOST_TEST((f.value));

        BOOST_TEST((test("Joel", sym)));
        BOOST_TEST((test("Ruby", sym)));
        BOOST_TEST((test("Tenji", sym)));
        BOOST_TEST((test("Tutit", sym)));
        BOOST_TEST((test("Kim", sym)));
        BOOST_TEST((test("Joey", sym)));
        BOOST_TEST((test("Joeyboy", sym)));
        BOOST_TEST((!test("XXX", sym)));

        // test copy
        symbols<char, int> sym2;
        sym2 = sym;
        BOOST_TEST((test("Joel", sym2)));
        BOOST_TEST((test("Ruby", sym2)));
        BOOST_TEST((test("Tenji", sym2)));
        BOOST_TEST((test("Tutit", sym2)));
        BOOST_TEST((test("Kim", sym2)));
        BOOST_TEST((test("Joey", sym2)));
        BOOST_TEST((!test("XXX", sym2)));

        // make sure it plays well with other parsers
        BOOST_TEST((test("Joelyo", sym >> "yo")));

        sym.remove
            ("Joel")
            ("Ruby")
        ;

        BOOST_TEST((!test("Joel", sym)));
        BOOST_TEST((!test("Ruby", sym)));
    }

    { // comma syntax
        symbols<char, int> sym;
        sym += "Joel", "Ruby", "Tenji", "Tutit", "Kim", "Joey";

        BOOST_TEST((test("Joel", sym)));
        BOOST_TEST((test("Ruby", sym)));
        BOOST_TEST((test("Tenji", sym)));
        BOOST_TEST((test("Tutit", sym)));
        BOOST_TEST((test("Kim", sym)));
        BOOST_TEST((test("Joey", sym)));
        BOOST_TEST((!test("XXX", sym)));

        sym -= "Joel", "Ruby";

        BOOST_TEST((!test("Joel", sym)));
        BOOST_TEST((!test("Ruby", sym)));
    }

    /// $$$ Not yet implemented $$$
    //~ { // no-case handling
        //~ using namespace boost::spirit::x3::ascii;

        //~ symbols<char, int> sym;
        //~ // NOTE: make sure all entries are in lower-case!!!
        //~ sym = "joel", "ruby", "tenji", "tutit", "kim", "joey";

        //~ BOOST_TEST((test("joel", no_case[sym])));
        //~ BOOST_TEST((test("ruby", no_case[sym])));
        //~ BOOST_TEST((test("tenji", no_case[sym])));
        //~ BOOST_TEST((test("tutit", no_case[sym])));
        //~ BOOST_TEST((test("kim", no_case[sym])));
        //~ BOOST_TEST((test("joey", no_case[sym])));

        //~ BOOST_TEST((test("JOEL", no_case[sym])));
        //~ BOOST_TEST((test("RUBY", no_case[sym])));
        //~ BOOST_TEST((test("TENJI", no_case[sym])));
        //~ BOOST_TEST((test("TUTIT", no_case[sym])));
        //~ BOOST_TEST((test("KIM", no_case[sym])));
        //~ BOOST_TEST((test("JOEY", no_case[sym])));

        //~ // make sure it plays well with other parsers
        //~ BOOST_TEST((test("Joelyo", no_case[sym] >> "yo")));
    //~ }

    { // attributes
        symbols<char, int> sym;

        sym.add
            ("Joel", 1)
            ("Ruby", 2)
            ("Tenji", 3)
            ("Tutit", 4)
            ("Kim", 5)
            ("Joey", 6)
        ;

        int i;
        BOOST_TEST((test_attr("Joel", sym, i)));
        BOOST_TEST(i == 1);
        BOOST_TEST((test_attr("Ruby", sym, i)));
        BOOST_TEST(i == 2);
        BOOST_TEST((test_attr("Tenji", sym, i)));
        BOOST_TEST(i == 3);
        BOOST_TEST((test_attr("Tutit", sym, i)));
        BOOST_TEST(i == 4);
        BOOST_TEST((test_attr("Kim", sym, i)));
        BOOST_TEST(i == 5);
        BOOST_TEST((test_attr("Joey", sym, i)));
        BOOST_TEST(i == 6);
        BOOST_TEST((!test_attr("XXX", sym, i)));

        // double add:

        sym.add("Joel", 265);
        BOOST_TEST((test_attr("Joel", sym, i)));
        BOOST_TEST(i == 1);
    }

    //~ { // actions
        //~ namespace phx = boost::phoenix;
        //~ using boost::spirit::x3::_1;

        //~ symbols<char, int> sym;
        //~ sym.add
            //~ ("Joel", 1)
            //~ ("Ruby", 2)
            //~ ("Tenji", 3)
            //~ ("Tutit", 4)
            //~ ("Kim", 5)
            //~ ("Joey", 6)
        //~ ;

        //~ int i;
        //~ BOOST_TEST((test("Joel", sym[phx::ref(i) = _1])));
        //~ BOOST_TEST(i == 1);
        //~ BOOST_TEST((test("Ruby", sym[phx::ref(i) = _1])));
        //~ BOOST_TEST(i == 2);
        //~ BOOST_TEST((test("Tenji", sym[phx::ref(i) = _1])));
        //~ BOOST_TEST(i == 3);
        //~ BOOST_TEST((test("Tutit", sym[phx::ref(i) = _1])));
        //~ BOOST_TEST(i == 4);
        //~ BOOST_TEST((test("Kim", sym[phx::ref(i) = _1])));
        //~ BOOST_TEST(i == 5);
        //~ BOOST_TEST((test("Joey", sym[phx::ref(i) = _1])));
        //~ BOOST_TEST(i == 6);
        //~ BOOST_TEST((!test("XXX", sym[phx::ref(i) = _1])));
    //~ }

    return boost::report_errors();
}
Example #30
0
int
main()
{
    using spirit_test::test;
    using spirit_test::test_attr;
    ///////////////////////////////////////////////////////////////////////////
    //  unsigned tests
    ///////////////////////////////////////////////////////////////////////////
    {
        using boost::spirit::x3::uint_;
        unsigned u;

        BOOST_TEST(test("123456", uint_));
        BOOST_TEST(test_attr("123456", uint_, u));
        BOOST_TEST(u == 123456);

        BOOST_TEST(test(max_unsigned, uint_));
        BOOST_TEST(test_attr(max_unsigned, uint_, u));
        BOOST_TEST(u == UINT_MAX);

        BOOST_TEST(!test(unsigned_overflow, uint_));
        BOOST_TEST(!test_attr(unsigned_overflow, uint_, u));
    }

    ///////////////////////////////////////////////////////////////////////////
    //  binary tests
    ///////////////////////////////////////////////////////////////////////////
    {
        using boost::spirit::x3::bin;
        unsigned u;

        BOOST_TEST(test("11111110", bin));
        BOOST_TEST(test_attr("11111110", bin, u));
        BOOST_TEST(u == 0xFE);

        BOOST_TEST(test(max_binary, bin));
        BOOST_TEST(test_attr(max_binary, bin, u));
        BOOST_TEST(u == UINT_MAX);

        BOOST_TEST(!test(binary_overflow, bin));
        BOOST_TEST(!test_attr(binary_overflow, bin, u));
    }

    ///////////////////////////////////////////////////////////////////////////
    //  octal tests
    ///////////////////////////////////////////////////////////////////////////
    {
        using boost::spirit::x3::oct;
        unsigned u;

        BOOST_TEST(test("12545674515", oct));
        BOOST_TEST(test_attr("12545674515", oct, u));
        BOOST_TEST(u == 012545674515);

        BOOST_TEST(test(max_octal, oct));
        BOOST_TEST(test_attr(max_octal, oct, u));
        BOOST_TEST(u == UINT_MAX);

        BOOST_TEST(!test(octal_overflow, oct));
        BOOST_TEST(!test_attr(octal_overflow, oct, u));
    }

    ///////////////////////////////////////////////////////////////////////////
    //  hex tests
    ///////////////////////////////////////////////////////////////////////////
    {
        using boost::spirit::x3::hex;
        unsigned u;

        BOOST_TEST(test("95BC8DF", hex));
        BOOST_TEST(test_attr("95BC8DF", hex, u));
        BOOST_TEST(u == 0x95BC8DF);

        BOOST_TEST(test("abcdef12", hex));
        BOOST_TEST(test_attr("abcdef12", hex, u));
        BOOST_TEST(u == 0xabcdef12);

        BOOST_TEST(test(max_hex, hex));
        BOOST_TEST(test_attr(max_hex, hex, u));
        BOOST_TEST(u == UINT_MAX);

        BOOST_TEST(!test(hex_overflow, hex));
        BOOST_TEST(!test_attr(hex_overflow, hex, u));
    }

    ///////////////////////////////////////////////////////////////////////////
    //  limited fieldwidth
    ///////////////////////////////////////////////////////////////////////////
    {
        unsigned u;
        using boost::spirit::x3::uint_parser;

        uint_parser<unsigned, 10, 1, 3> uint3;
        BOOST_TEST(test("123456", uint3, false));
        BOOST_TEST(test_attr("123456", uint3, u, false));
        BOOST_TEST(u == 123);

        uint_parser<unsigned, 10, 2, 4> uint4;
        BOOST_TEST(test("123456", uint4, false));
        BOOST_TEST(test_attr("123456", uint4, u, false));
        BOOST_TEST(u == 1234);

        char const * first = "0000000";
        char const * last  = first + std::strlen(first);
        uint_parser<unsigned, 10, 4, 4> uint_exact4;
        BOOST_TEST(boost::spirit::x3::parse(first, last, uint_exact4, u)
            && first != last && (last-first == 3) && u == 0);

        first = "0001400";
        last  = first + std::strlen(first);
        BOOST_TEST(boost::spirit::x3::parse(first, last, uint_exact4, u)
            && first != last && (last-first == 3) && u == 1);

        BOOST_TEST(!test("1", uint4));
        BOOST_TEST(!test_attr("1", uint4, u));
        BOOST_TEST(test_attr("014567", uint4, u, false) && u == 145);
    }

    ///////////////////////////////////////////////////////////////////////////
    //  action tests
    ///////////////////////////////////////////////////////////////////////////
    {
        using boost::spirit::x3::_attr;
        using boost::spirit::x3::uint_;
        using boost::spirit::x3::ascii::space;
        int n;

        auto f = [&](auto& ctx){ n = _attr(ctx); };

        BOOST_TEST(test("123", uint_[f]));
        BOOST_TEST(n == 123);
        BOOST_TEST(test("   456", uint_[f], space));
        BOOST_TEST(n == 456);
    }

    ///////////////////////////////////////////////////////////////////////////
    // Check overflow is parse error
    ///////////////////////////////////////////////////////////////////////////
    {
        boost::spirit::x3::uint_parser<boost::uint8_t> uint8_;
        boost::uint8_t u8;

        BOOST_TEST(!test_attr("999", uint8_, u8));
        BOOST_TEST(!test_attr("256", uint8_, u8));
        BOOST_TEST(test_attr("255", uint8_, u8));

        boost::spirit::x3::uint_parser<boost::uint16_t> uint16_;
        boost::uint16_t u16;

        BOOST_TEST(!test_attr("99999", uint16_, u16));
        BOOST_TEST(!test_attr("65536", uint16_, u16));
        BOOST_TEST(test_attr("65535", uint16_, u16));

        boost::spirit::x3::uint_parser<boost::uint32_t> uint32_;
        boost::uint32_t u32;

        BOOST_TEST(!test_attr("9999999999", uint32_, u32));
        BOOST_TEST(!test_attr("4294967296", uint32_, u32));
        BOOST_TEST(test_attr("4294967295", uint32_, u32));
    }

    ///////////////////////////////////////////////////////////////////////////
    //  custom uint tests
    ///////////////////////////////////////////////////////////////////////////
    {
        using boost::spirit::x3::uint_;
        using boost::spirit::x3::uint_parser;
        custom_uint u;

        BOOST_TEST(test_attr("123456", uint_, u));
        uint_parser<custom_uint, 10, 1, 2> uint2;
        BOOST_TEST(test_attr("12", uint2, u));
    }

    return boost::report_errors();
}