summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/spirit/test/qi/permutation.cpp
blob: d29174e3eefdc4427dcf1354bcdcd93916e10641 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*=============================================================================
    Copyright (c) 2001-2010 Joel de Guzman

    Distributed under 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)
=============================================================================*/
#include <boost/detail/lightweight_test.hpp>
#include <boost/spirit/include/qi_operator.hpp>
#include <boost/spirit/include/qi_char.hpp>
#include <boost/spirit/include/qi_string.hpp>
#include <boost/spirit/include/qi_numeric.hpp>
#include <boost/spirit/include/qi_action.hpp>
#include <boost/spirit/include/qi_nonterminal.hpp>
#include <boost/spirit/include/support_argument.hpp>
#include <boost/fusion/include/vector.hpp>
#include <boost/fusion/include/at.hpp>
#include <boost/spirit/include/phoenix_core.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <boost/optional.hpp>

#include <string>
#include <iostream>
#include "test.hpp"

using namespace spirit_test;

int
main()
{
    using boost::spirit::qi::int_;
    using boost::spirit::qi::_1;
    using boost::spirit::qi::_2;
    using boost::spirit::qi::rule;
    using boost::spirit::ascii::alpha;
    using boost::spirit::ascii::char_;

    using boost::fusion::vector;
    using boost::fusion::at_c;
    using boost::optional;

    {
        BOOST_TEST((test("a", char_('a') ^ char_('b') ^ char_('c'))));
        BOOST_TEST((test("b", char_('a') ^ char_('b') ^ char_('c'))));
        BOOST_TEST((test("ab", char_('a') ^ char_('b') ^ char_('c'))));
        BOOST_TEST((test("ba", char_('a') ^ char_('b') ^ char_('c'))));
        BOOST_TEST((test("abc", char_('a') ^ char_('b') ^ char_('c'))));
        BOOST_TEST((test("acb", char_('a') ^ char_('b') ^ char_('c'))));
        BOOST_TEST((test("bca", char_('a') ^ char_('b') ^ char_('c'))));
        BOOST_TEST((test("bac", char_('a') ^ char_('b') ^ char_('c'))));
        BOOST_TEST((test("cab", char_('a') ^ char_('b') ^ char_('c'))));
        BOOST_TEST((test("cba", char_('a') ^ char_('b') ^ char_('c'))));

        BOOST_TEST((!test("cca", char_('a') ^ char_('b') ^ char_('c'))));
    }

    {   // test optional must stay uninitialized
        optional<int> i;
        BOOST_TEST((test_attr("", -int_ ^ int_, i)));
        BOOST_TEST(!i);
    }

    {
        vector<optional<int>, optional<char> > attr;

        BOOST_TEST((test_attr("a", int_ ^ alpha, attr)));
        BOOST_TEST((!at_c<0>(attr)));
        BOOST_TEST((at_c<1>(attr).get() == 'a'));

        at_c<1>(attr) = optional<char>(); // clear the optional
        BOOST_TEST((test_attr("123", int_ ^ alpha, attr)));
        BOOST_TEST((at_c<0>(attr).get() == 123));
        BOOST_TEST((!at_c<1>(attr)));

        at_c<0>(attr) = optional<int>(); // clear the optional
        BOOST_TEST((test_attr("123a", int_ ^ alpha, attr)));
        BOOST_TEST((at_c<0>(attr).get() == 123));
        BOOST_TEST((at_c<1>(attr).get() == 'a'));

        at_c<0>(attr) = optional<int>(); // clear the optional
        at_c<1>(attr) = optional<char>(); // clear the optional
        BOOST_TEST((test_attr("a123", int_ ^ alpha, attr)));
        BOOST_TEST((at_c<0>(attr).get() == 123));
        BOOST_TEST((at_c<1>(attr).get() == 'a'));
    }

    {   // test action
        using namespace boost::phoenix;
        namespace phx = boost::phoenix;

        optional<int> i;
        optional<char> c;

        BOOST_TEST((test("123a", (int_ ^ alpha)[phx::ref(i) = _1, phx::ref(c) = _2])));
        BOOST_TEST((i.get() == 123));
        BOOST_TEST((c.get() == 'a'));
    }

    {   // test rule %=

        typedef vector<optional<int>, optional<char> > attr_type;
        attr_type attr;

        rule<char const*, attr_type()> r;
        r %= int_ ^ alpha;

        BOOST_TEST((test_attr("a", r, attr)));
        BOOST_TEST((!at_c<0>(attr)));
        BOOST_TEST((at_c<1>(attr).get() == 'a'));

        at_c<1>(attr) = optional<char>(); // clear the optional
        BOOST_TEST((test_attr("123", r, attr)));
        BOOST_TEST((at_c<0>(attr).get() == 123));
        BOOST_TEST((!at_c<1>(attr)));

        at_c<0>(attr) = optional<int>(); // clear the optional
        BOOST_TEST((test_attr("123a", r, attr)));
        BOOST_TEST((at_c<0>(attr).get() == 123));
        BOOST_TEST((at_c<1>(attr).get() == 'a'));

        at_c<0>(attr) = optional<int>(); // clear the optional
        at_c<1>(attr) = optional<char>(); // clear the optional
        BOOST_TEST((test_attr("a123", r, attr)));
        BOOST_TEST((at_c<0>(attr).get() == 123));
        BOOST_TEST((at_c<1>(attr).get() == 'a'));
    }

    return boost::report_errors();
}