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 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
|
// 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/mpl/print.hpp>
#include <boost/config/warning_disable.hpp>
#include <boost/spirit/include/karma.hpp>
#include <boost/spirit/include/karma_format.hpp>
#include <boost/spirit/include/karma_format_auto.hpp>
#include <boost/spirit/include/karma_stream.hpp>
#include <boost/spirit/include/phoenix_core.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <string>
#include <sstream>
#include <vector>
#include <list>
#include <boost/detail/lightweight_test.hpp>
#include <boost/assign/std/vector.hpp>
#include <boost/assign/std/list.hpp>
///////////////////////////////////////////////////////////////////////////////
template <typename Char, typename Expr>
bool test(Char const *expected, Expr const& xpr)
{
// Report invalid expression error as early as possible.
// If you got an error_invalid_expression error message here,
// then the expression (expr) is not a valid spirit karma expression.
BOOST_SPIRIT_ASSERT_MATCH(boost::spirit::karma::domain, Expr);
std::ostringstream ostrm;
ostrm << boost::spirit::compile<boost::spirit::karma::domain>(xpr);
return ostrm.good() && ostrm.str() == expected;
}
template <typename Char, typename Expr, typename CopyExpr, typename CopyAttr
, typename Delimiter, typename Attribute>
bool test(Char const *expected,
boost::spirit::karma::detail::format_manip<
Expr, CopyExpr, CopyAttr, Delimiter, Attribute> const& fm)
{
std::ostringstream ostrm;
ostrm << fm;
return ostrm.good() && ostrm.str() == expected;
}
///////////////////////////////////////////////////////////////////////////////
int
main()
{
using namespace boost::spirit;
using namespace boost::spirit::ascii;
namespace fusion = boost::fusion;
using namespace boost::phoenix;
{
BOOST_TEST(test( "a",
char_('a')
));
BOOST_TEST(test( "a",
char_[_1 = val('a')]
));
BOOST_TEST(test( "a",
karma::format(char_[_1 = val('a')])
));
BOOST_TEST(test( "a ",
karma::format_delimited(char_[_1 = val('a')], space)
));
BOOST_TEST(test( "a",
karma::format(char_, 'a')
));
BOOST_TEST(test( "a ",
karma::format_delimited(char_, space, 'a')
));
}
{
BOOST_TEST(test( "ab",
char_[_1 = val('a')] << char_[_1 = val('b')]
));
BOOST_TEST(test( "ab",
karma::format(char_[_1 = val('a')] << char_[_1 = val('b')])
));
BOOST_TEST(test( "a b ",
karma::format_delimited(char_[_1 = val('a')] << char_[_1 = val('b')], space)
));
fusion::vector<char, char> t('a', 'b');
BOOST_TEST(test( "ab",
karma::format(char_ << char_, t)
));
BOOST_TEST(test( "a b ",
karma::format_delimited(char_ << char_, space, t)
));
BOOST_TEST(test( "ab",
karma::format(t)
));
BOOST_TEST(test( "a b ",
karma::format_delimited(t, space)
));
}
{
BOOST_TEST(test( "abc",
char_[_1 = 'a'] << char_[_1 = 'b'] << char_[_1 = 'c']
));
BOOST_TEST(test( "abc",
karma::format(char_('a') << char_('b') << char_('c'))
));
BOOST_TEST(test( "a b c ",
karma::format_delimited(char_('a') << char_('b') << char_('c'), space)
));
fusion::vector<char, char, char> t('a', 'b', 'c');
BOOST_TEST(test( "abc",
karma::format(char_ << char_ << char_, t)
));
BOOST_TEST(test( "a b c ",
karma::format_delimited(char_ << char_ << char_, space, t)
));
}
{
BOOST_TEST(test( "a2",
(char_ << int_)[(_1 = 'a', _2 = 2)]
));
fusion::vector<char, int> t('a', 2);
BOOST_TEST(test( "a2",
karma::format(char_ << int_, t)
));
BOOST_TEST(test( "a 2 ",
karma::format_delimited(char_ << int_, space, t)
));
}
using namespace boost::assign;
{
// output all elements of a vector
std::vector<char> v;
v += 'a', 'b', 'c';
BOOST_TEST(test( "abc",
(*char_)[_1 = v]
));
BOOST_TEST(test( "abc",
karma::format(*char_, v)
));
BOOST_TEST(test( "a b c ",
karma::format_delimited(*char_, space, v)
));
BOOST_TEST(test( "abc",
karma::format(v)
));
BOOST_TEST(test( "a b c ",
karma::format_delimited(v, space)
));
// output a comma separated list of vector elements
BOOST_TEST(test( "a, b, c",
(char_ % lit(", "))[_0 = fusion::make_single_view(v)]
));
BOOST_TEST(test( "a, b, c",
karma::format((char_ % lit(", "))[_0 = fusion::make_single_view(v)])
));
BOOST_TEST(test( "a , b , c ",
karma::format_delimited((char_ % ',')[_0 = fusion::make_single_view(v)], space)
));
BOOST_TEST(test( "a,b,c",
karma::format(char_ % ',', v)
));
BOOST_TEST(test( "a , b , c ",
karma::format_delimited(char_ % ',', space, v)
));
// output all elements of a list
std::list<char> l;
l += 'a', 'b', 'c';
// BOOST_TEST(test( "abc",
// (*char_)[_1 = l]
// ));
// BOOST_TEST(test( "abc",
// karma::format((*char_)[_1 = l])
// ));
// BOOST_TEST(test( "a b c ",
// karma::format_delimited((*char_)[_1 = l], space)
// ));
BOOST_TEST(test( "abc",
karma::format(*char_, l)
));
BOOST_TEST(test( "a b c ",
karma::format_delimited(*char_, space, l)
));
BOOST_TEST(test( "abc",
karma::format(l)
));
BOOST_TEST(test( "a b c ",
karma::format_delimited(l, space)
));
}
return boost::report_errors();
}
|