format_manip.cpp 6.2 KB

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