Imported Upstream version 1.72.0
[platform/upstream/boost.git] / libs / spirit / test / karma / symbols2.cpp
1 //  Copyright (c) 2001-2011 Hartmut Kaiser
2 //
3 //  Distributed under the Boost Software License, Version 1.0. (See accompanying
4 //  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5
6 #include <boost/spirit/include/karma_auxiliary.hpp>
7 #include <boost/spirit/include/karma_char.hpp>
8 #include <boost/spirit/include/karma_string.hpp>
9 #include <boost/spirit/include/karma_operator.hpp>
10 #include <boost/spirit/include/karma_directive.hpp>
11 #include <boost/spirit/include/karma_generate.hpp>
12 #include <boost/spirit/include/karma_nonterminal.hpp>
13
14 #include <boost/core/lightweight_test.hpp>
15 #include <boost/core/lightweight_test_trait.hpp>
16
17 #include "test.hpp"
18
19 namespace fusion = boost::fusion;
20
21 template <typename T>
22 inline std::vector<T>
23 make_vector(T const& t1, T const& t2)
24 {
25     std::vector<T> v;
26     v.push_back(t1);
27     v.push_back(t2);
28     return v;
29 }
30
31 int main()
32 {
33     using spirit_test::test;
34     using boost::spirit::karma::symbols;
35
36     { // advanced
37         using boost::spirit::karma::rule;
38         using boost::spirit::karma::lit;
39
40         typedef spirit_test::output_iterator<char>::type output_iterator_type;
41
42         symbols<char, rule<output_iterator_type> > sym;
43
44         rule<output_iterator_type> r1 = lit("Joel");
45         rule<output_iterator_type> r2 = lit("Hartmut");
46         rule<output_iterator_type> r3 = lit("Tom");
47         rule<output_iterator_type> r4 = lit("Kim");
48
49         sym.add
50             ('j', r1.alias())
51             ('h', r2.alias())
52             ('t', r3.alias())
53             ('k', r4.alias())
54         ;
55
56         BOOST_TEST_TRAIT_TRUE((
57             boost::spirit::traits::is_generator<
58                 symbols<char, rule<output_iterator_type> > >));
59
60         BOOST_TEST((test("Joel", sym, 'j')));
61         BOOST_TEST((test("Hartmut", sym, 'h')));
62         BOOST_TEST((test("Tom", sym, 't')));
63         BOOST_TEST((test("Kim", sym, 'k')));
64         BOOST_TEST((!test("", sym, 'x')));
65
66         // test copy
67         symbols<char, rule<output_iterator_type> > sym2;
68         sym2 = sym;
69         BOOST_TEST((test("Joel", sym2, 'j')));
70         BOOST_TEST((test("Hartmut", sym2, 'h')));
71         BOOST_TEST((test("Tom", sym2, 't')));
72         BOOST_TEST((test("Kim", sym2, 'k')));
73         BOOST_TEST((!test("", sym2, 'x')));
74
75         // make sure it plays well with other generators
76         BOOST_TEST((test("Joelyo", sym << "yo", 'j')));
77
78         sym.remove
79             ('j')
80             ('h')
81         ;
82
83         BOOST_TEST((!test("", sym, 'j')));
84         BOOST_TEST((!test("", sym, 'h')));
85     }
86
87     { // more advanced
88         using boost::spirit::karma::rule;
89         using boost::spirit::karma::lit;
90         using boost::spirit::karma::string;
91
92         typedef spirit_test::output_iterator<char>::type output_iterator_type;
93
94         symbols<char, rule<output_iterator_type, std::string()> > sym;
95         rule<output_iterator_type, std::string()> r1 = string;
96
97         sym.add
98             ('j', r1.alias())
99             ('h', r1.alias())
100             ('t', r1.alias())
101             ('k', r1.alias())
102         ;
103
104         BOOST_TEST_TRAIT_TRUE((
105             boost::spirit::traits::is_generator<
106                 symbols<char, std::string> >));
107
108         BOOST_TEST((test("Joel", sym, fusion::make_vector('j', "Joel"))));
109         BOOST_TEST((test("Hartmut", sym, fusion::make_vector('h', "Hartmut"))));
110         BOOST_TEST((test("Tom", sym, fusion::make_vector('t', "Tom"))));
111         BOOST_TEST((test("Kim", sym, fusion::make_vector('k', "Kim"))));
112         BOOST_TEST((!test("", sym, 'x')));
113
114         // test copy
115         symbols<char, rule<output_iterator_type, std::string()> > sym2;
116         sym2 = sym;
117         BOOST_TEST((test("Joel", sym2, fusion::make_vector('j', "Joel"))));
118         BOOST_TEST((test("Hartmut", sym2, fusion::make_vector('h', "Hartmut"))));
119         BOOST_TEST((test("Tom", sym2, fusion::make_vector('t', "Tom"))));
120         BOOST_TEST((test("Kim", sym2, fusion::make_vector('k', "Kim"))));
121         BOOST_TEST((!test("", sym2, 'x')));
122
123         // make sure it plays well with other generators
124         BOOST_TEST((test("Joelyo", sym << "yo", fusion::make_vector('j', "Joel"))));
125
126         sym.remove
127             ('j')
128             ('h')
129         ;
130
131         BOOST_TEST((!test("", sym, 'j')));
132         BOOST_TEST((!test("", sym, 'h')));
133     }
134
135     { // test for proto problem with rvalue references (10-11-2011)
136         symbols<char, std::string> sym;
137
138         sym += std::make_pair('j', "Joel");
139         sym += std::make_pair('h', "Hartmut");
140
141         BOOST_TEST((test("Joel", sym, 'j')));
142         BOOST_TEST((test("Hartmut", sym, 'h')));
143     }
144
145     return boost::report_errors();
146 }