File: sequence2.cpp

package info (click to toggle)
boost1.74 1.74.0-9
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 464,084 kB
  • sloc: cpp: 3,338,324; xml: 131,293; python: 33,088; ansic: 14,336; asm: 4,034; sh: 3,351; makefile: 1,193; perl: 1,036; yacc: 478; php: 212; ruby: 102; lisp: 24; sql: 13; csh: 6
file content (183 lines) | stat: -rw-r--r-- 6,345 bytes parent folder | download | duplicates (2)
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
//  Copyright (c) 2001-2011 Hartmut Kaiser
// 
//  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/config/warning_disable.hpp>
#include <boost/detail/lightweight_test.hpp>

#include <boost/spirit/include/karma_char.hpp>
#include <boost/spirit/include/karma_string.hpp>
#include <boost/spirit/include/karma_numeric.hpp>
#include <boost/spirit/include/karma_generate.hpp>
#include <boost/spirit/include/karma_operator.hpp>
#include <boost/spirit/include/karma_directive.hpp>
#include <boost/spirit/include/karma_action.hpp>
#include <boost/spirit/include/karma_nonterminal.hpp>
#include <boost/spirit/include/karma_auxiliary.hpp>
#include <boost/spirit/include/karma_directive.hpp>
#include <boost/spirit/include/support_unused.hpp>
#include <boost/spirit/include/phoenix_core.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <boost/spirit/include/phoenix_function.hpp>
#include <boost/fusion/include/vector.hpp>

#include "test.hpp"

using namespace spirit_test;

///////////////////////////////////////////////////////////////////////////////
// lazy version of fusion::size
struct seqsize_impl
{
    template <typename Sequence>
    struct result
      : boost::fusion::result_of::size<Sequence>
    {};

    template <typename This, typename Sequence>
    struct result<This(Sequence)>
        : result<typename boost::proto::detail::uncvref<Sequence>::type>
    {};

    template <typename Sequence>
    typename result<Sequence>::type
    operator()(Sequence const& seq) const
    {
        return boost::fusion::size(seq);
    }
};

boost::phoenix::function<seqsize_impl> const seqsize = seqsize_impl();

///////////////////////////////////////////////////////////////////////////////
int main()
{
    using namespace boost::spirit;
    using namespace boost::spirit::ascii;
    namespace fusion = boost::fusion;

    {
        std::list<int> v;
        v.push_back(1);
        v.push_back(2);
        v.push_back(3);
        BOOST_TEST(test("123", int_ << int_ << int_, v));
        BOOST_TEST(test_delimited("1 2 3 ", int_ << int_ << int_, v, ' '));
        BOOST_TEST(test("1,2,3", int_ << ',' << int_ << ',' << int_, v));
        BOOST_TEST(test_delimited("1 , 2 , 3 ", int_ << ',' << int_ << ',' << int_, v, ' '));
    }

    {
        BOOST_TEST(test("aa", lower[char_('A') << 'a']));
        BOOST_TEST(test_delimited("BEGIN END ", 
            upper[lit("begin") << "end"], char(' ')));
        BOOST_TEST(!test_delimited("BEGIN END ", 
            upper[lit("begin") << "nend"], char(' ')));

        BOOST_TEST(test("Aa        ", left_align[char_('A') << 'a']));
        BOOST_TEST(test("    Aa    ", center[char_('A') << 'a']));
        BOOST_TEST(test("        Aa", right_align[char_('A') << 'a']));
    }

    {
        // make sure single element tuples get passed through if the rhs 
        // has a single element tuple as its attribute
        typedef spirit_test::output_iterator<char>::type iterator_type;
        fusion::vector<double, int> fv(2.0, 1);
        karma::rule<iterator_type, fusion::vector<double, int>()> r;
        r %= double_ << ',' << int_;
        BOOST_TEST(test("test:2.0,1", "test:" << r, fv));
    }

    // action tests
    {
        using namespace boost::phoenix;

        BOOST_TEST(test("abcdefg", 
            (char_ << char_ << string)[(_1 = 'a', _2 = 'b', _3 = "cdefg")]));
        BOOST_TEST(test_delimited("a b cdefg ", 
            (char_ << char_ << string)[(_1 = 'a', _2 = 'b', _3 = "cdefg")], 
            char(' ')));

        BOOST_TEST(test_delimited("a 12 c ", 
            (char_ << lit(12) << char_)[(_1 = 'a', _2 = 'c')], char(' ')));

        char c = 'c';
        BOOST_TEST(test("abc", 
            (char_[_1 = 'a'] << 'b' << char_)[(_1 = 'x', _2 = ref(c))]));
        BOOST_TEST(test_delimited("a b c ", 
            (char_[_1 = 'a'] << 'b' << char_)[_2 = ref(c)], char(' ')));

        BOOST_TEST(test("aa", lower[char_ << 'A'][_1 = 'A']));
        BOOST_TEST(test("AA", upper[char_ << 'a'][_1 = 'a']));

        BOOST_TEST(test("Aa        ", left_align[char_ << 'a'][_1 = 'A']));
        BOOST_TEST(test("    Aa    ", center[char_ << 'a'][_1 = 'A']));
        BOOST_TEST(test("        Aa", right_align[char_ << 'a'][_1 = 'A']));
    }

    // test special case where sequence has a one element vector attribute 
    // sequence and this element is a rule (attribute has to be passed through 
    // without change)
    {
        typedef spirit_test::output_iterator<char>::type outiter_type;
        namespace karma = boost::spirit::karma;

        karma::rule<outiter_type, std::vector<int>()> r = -(int_ % ',');
        std::vector<int> v;
        BOOST_TEST(test(">", '>' << r, v));

        v.push_back(1);
        v.push_back(2);
        v.push_back(3);
        v.push_back(4);
        BOOST_TEST(test(">1,2,3,4", '>' << r, v));
    }

    {
        namespace karma = boost::spirit::karma;
        typedef spirit_test::output_iterator<char>::type outiter_type;

        karma::rule<outiter_type, std::string()> e = karma::string;
        karma::rule<outiter_type, std::vector<std::string>()> l = e << *(',' << e);

        std::vector<std::string> v;
        v.push_back("abc1");
        v.push_back("abc2");
        v.push_back("abc3");
        BOOST_TEST(test("abc1,abc2,abc3", l, v));
    }

    {
        namespace karma = boost::spirit::karma;
        namespace phoenix = boost::phoenix;

        typedef spirit_test::output_iterator<char>::type outiter_type;
        typedef fusion::vector<char, char, char> vector_type;
        
        vector_type p ('a', 'b', 'c');
        BOOST_TEST(test("ab", char_ << char_, p));

        karma::rule<outiter_type, vector_type()> r;
        r %= char_ << char_ << &karma::eps[seqsize(_val) == 3];
        BOOST_TEST(!test("", r, p));

        r %= char_ << char_ << char_ << &karma::eps[seqsize(_val) == 3];
        BOOST_TEST(test("abc", r, p));
    }

    {
        std::list<int> v;
        v.push_back(1);
        v.push_back(2);
        v.push_back(3);
        v.push_back(4);

        BOOST_TEST(test("1234", repeat(2)[int_] << *int_, v));
        BOOST_TEST(test_delimited("1 2 3 4 ", repeat(2)[int_] << *int_, v, char(' ')));
    }

    return boost::report_errors();
}