• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/mpl/print.hpp>
7 #include <boost/config/warning_disable.hpp>
8 #include <boost/spirit/include/karma.hpp>
9 #include <boost/spirit/include/karma_format.hpp>
10 #include <boost/spirit/include/karma_format_auto.hpp>
11 #include <boost/spirit/include/karma_stream.hpp>
12 #include <boost/spirit/include/phoenix_core.hpp>
13 #include <boost/spirit/include/phoenix_operator.hpp>
14 
15 #include <string>
16 #include <sstream>
17 #include <vector>
18 #include <list>
19 
20 #include <boost/detail/lightweight_test.hpp>
21 #include <boost/assign/std/vector.hpp>
22 #include <boost/assign/std/list.hpp>
23 
24 ///////////////////////////////////////////////////////////////////////////////
25 template <typename Char, typename Expr>
test(Char const * expected,Expr const & xpr)26 bool test(Char const *expected, Expr const& xpr)
27 {
28     // Report invalid expression error as early as possible.
29     // If you got an error_invalid_expression error message here,
30     // then the expression (expr) is not a valid spirit karma expression.
31     BOOST_SPIRIT_ASSERT_MATCH(boost::spirit::karma::domain, Expr);
32 
33     std::ostringstream ostrm;
34     ostrm << boost::spirit::compile<boost::spirit::karma::domain>(xpr);
35     return ostrm.good() && ostrm.str() == expected;
36 }
37 
38 template <typename Char, typename Expr, typename CopyExpr, typename CopyAttr
39   , typename Delimiter, typename Attribute>
test(Char const * expected,boost::spirit::karma::detail::format_manip<Expr,CopyExpr,CopyAttr,Delimiter,Attribute> const & fm)40 bool test(Char const *expected,
41     boost::spirit::karma::detail::format_manip<
42         Expr, CopyExpr, CopyAttr, Delimiter, Attribute> const& fm)
43 {
44     std::ostringstream ostrm;
45     ostrm << fm;
46     return ostrm.good() && ostrm.str() == expected;
47 }
48 
49 ///////////////////////////////////////////////////////////////////////////////
50 int
main()51 main()
52 {
53     using namespace boost::spirit;
54     using namespace boost::spirit::ascii;
55 
56     namespace fusion = boost::fusion;
57     using namespace boost::phoenix;
58 
59     {
60         BOOST_TEST(test( "a",
61             char_('a')
62         ));
63         BOOST_TEST(test( "a",
64             char_[_1 = val('a')]
65         ));
66         BOOST_TEST(test( "a",
67             karma::format(char_[_1 = val('a')])
68         ));
69         BOOST_TEST(test( "a ",
70             karma::format_delimited(char_[_1 = val('a')], space)
71         ));
72         BOOST_TEST(test( "a",
73             karma::format(char_, 'a')
74         ));
75         BOOST_TEST(test( "a ",
76             karma::format_delimited(char_, space, 'a')
77         ));
78     }
79 
80     {
81         BOOST_TEST(test( "ab",
82             char_[_1 = val('a')] << char_[_1 = val('b')]
83         ));
84         BOOST_TEST(test( "ab",
85             karma::format(char_[_1 = val('a')] << char_[_1 = val('b')])
86         ));
87         BOOST_TEST(test( "a b ",
88             karma::format_delimited(char_[_1 = val('a')] << char_[_1 = val('b')], space)
89         ));
90 
91         fusion::vector<char, char> t('a', 'b');
92 
93         BOOST_TEST(test( "ab",
94             karma::format(char_ << char_, t)
95         ));
96         BOOST_TEST(test( "a b ",
97             karma::format_delimited(char_ << char_, space, t)
98         ));
99 
100         BOOST_TEST(test( "ab",
101             karma::format(t)
102         ));
103         BOOST_TEST(test( "a b ",
104             karma::format_delimited(t, space)
105         ));
106     }
107 
108     {
109         BOOST_TEST(test( "abc",
110             char_[_1 = 'a'] << char_[_1 = 'b'] << char_[_1 = 'c']
111         ));
112         BOOST_TEST(test( "abc",
113             karma::format(char_('a') << char_('b') << char_('c'))
114         ));
115         BOOST_TEST(test( "a b c ",
116             karma::format_delimited(char_('a') << char_('b') << char_('c'), space)
117         ));
118 
119         fusion::vector<char, char, char> t('a', 'b', 'c');
120 
121         BOOST_TEST(test( "abc",
122             karma::format(char_ << char_ << char_, t)
123         ));
124         BOOST_TEST(test( "a b c ",
125             karma::format_delimited(char_ << char_ << char_, space, t)
126         ));
127     }
128 
129     {
130         BOOST_TEST(test( "a2",
131             (char_ << int_)[(_1 = 'a', _2 = 2)]
132         ));
133 
134         fusion::vector<char, int> t('a', 2);
135 
136         BOOST_TEST(test( "a2",
137             karma::format(char_ << int_, t)
138         ));
139         BOOST_TEST(test( "a 2 ",
140             karma::format_delimited(char_ << int_, space, t)
141         ));
142     }
143 
144     using namespace boost::assign;
145 
146     {
147         // output all elements of a vector
148         std::vector<char> v;
149         v += 'a', 'b', 'c';
150 
151         BOOST_TEST(test( "abc",
152             (*char_)[_1 = v]
153         ));
154         BOOST_TEST(test( "abc",
155             karma::format(*char_, v)
156         ));
157         BOOST_TEST(test( "a b c ",
158             karma::format_delimited(*char_, space, v)
159         ));
160 
161         BOOST_TEST(test( "abc",
162             karma::format(v)
163         ));
164         BOOST_TEST(test( "a b c ",
165             karma::format_delimited(v, space)
166         ));
167 
168         // output a comma separated list of vector elements
169         BOOST_TEST(test( "a, b, c",
170             (char_ % lit(", "))[_0 = fusion::make_single_view(v)]
171         ));
172         BOOST_TEST(test( "a, b, c",
173             karma::format((char_ % lit(", "))[_0 = fusion::make_single_view(v)])
174         ));
175         BOOST_TEST(test( "a , b , c ",
176             karma::format_delimited((char_ % ',')[_0 = fusion::make_single_view(v)], space)
177         ));
178         BOOST_TEST(test( "a,b,c",
179             karma::format(char_ % ',', v)
180         ));
181         BOOST_TEST(test( "a , b , c ",
182             karma::format_delimited(char_ % ',', space, v)
183         ));
184 
185         // output all elements of a list
186         std::list<char> l;
187         l += 'a', 'b', 'c';
188 
189 //         BOOST_TEST(test( "abc",
190 //             (*char_)[_1 = l]
191 //         ));
192 //         BOOST_TEST(test( "abc",
193 //             karma::format((*char_)[_1 = l])
194 //         ));
195 //         BOOST_TEST(test( "a b c ",
196 //             karma::format_delimited((*char_)[_1 = l], space)
197 //         ));
198         BOOST_TEST(test( "abc",
199             karma::format(*char_, l)
200         ));
201         BOOST_TEST(test( "a b c ",
202             karma::format_delimited(*char_, space, l)
203         ));
204 
205         BOOST_TEST(test( "abc",
206             karma::format(l)
207         ));
208         BOOST_TEST(test( "a b c ",
209             karma::format_delimited(l, space)
210         ));
211     }
212 
213     return boost::report_errors();
214 }
215 
216