sequence2.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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/config/warning_disable.hpp>
  6. #include <boost/detail/lightweight_test.hpp>
  7. #include <boost/spirit/include/karma_char.hpp>
  8. #include <boost/spirit/include/karma_string.hpp>
  9. #include <boost/spirit/include/karma_numeric.hpp>
  10. #include <boost/spirit/include/karma_generate.hpp>
  11. #include <boost/spirit/include/karma_operator.hpp>
  12. #include <boost/spirit/include/karma_directive.hpp>
  13. #include <boost/spirit/include/karma_action.hpp>
  14. #include <boost/spirit/include/karma_nonterminal.hpp>
  15. #include <boost/spirit/include/karma_auxiliary.hpp>
  16. #include <boost/spirit/include/karma_directive.hpp>
  17. #include <boost/spirit/include/support_unused.hpp>
  18. #include <boost/spirit/include/phoenix_core.hpp>
  19. #include <boost/spirit/include/phoenix_operator.hpp>
  20. #include <boost/spirit/include/phoenix_function.hpp>
  21. #include <boost/fusion/include/vector.hpp>
  22. #include "test.hpp"
  23. using namespace spirit_test;
  24. ///////////////////////////////////////////////////////////////////////////////
  25. // lazy version of fusion::size
  26. struct seqsize_impl
  27. {
  28. template <typename Sequence>
  29. struct result
  30. : boost::fusion::result_of::size<Sequence>
  31. {};
  32. template <typename This, typename Sequence>
  33. struct result<This(Sequence)>
  34. : result<typename boost::proto::detail::uncvref<Sequence>::type>
  35. {};
  36. template <typename Sequence>
  37. typename result<Sequence>::type
  38. operator()(Sequence const& seq) const
  39. {
  40. return boost::fusion::size(seq);
  41. }
  42. };
  43. boost::phoenix::function<seqsize_impl> const seqsize = seqsize_impl();
  44. ///////////////////////////////////////////////////////////////////////////////
  45. int main()
  46. {
  47. using namespace boost::spirit;
  48. using namespace boost::spirit::ascii;
  49. namespace fusion = boost::fusion;
  50. {
  51. std::list<int> v;
  52. v.push_back(1);
  53. v.push_back(2);
  54. v.push_back(3);
  55. BOOST_TEST(test("123", int_ << int_ << int_, v));
  56. BOOST_TEST(test_delimited("1 2 3 ", int_ << int_ << int_, v, ' '));
  57. BOOST_TEST(test("1,2,3", int_ << ',' << int_ << ',' << int_, v));
  58. BOOST_TEST(test_delimited("1 , 2 , 3 ", int_ << ',' << int_ << ',' << int_, v, ' '));
  59. }
  60. {
  61. BOOST_TEST(test("aa", lower[char_('A') << 'a']));
  62. BOOST_TEST(test_delimited("BEGIN END ",
  63. upper[lit("begin") << "end"], char(' ')));
  64. BOOST_TEST(!test_delimited("BEGIN END ",
  65. upper[lit("begin") << "nend"], char(' ')));
  66. BOOST_TEST(test("Aa ", left_align[char_('A') << 'a']));
  67. BOOST_TEST(test(" Aa ", center[char_('A') << 'a']));
  68. BOOST_TEST(test(" Aa", right_align[char_('A') << 'a']));
  69. }
  70. {
  71. // make sure single element tuples get passed through if the rhs
  72. // has a single element tuple as its attribute
  73. typedef spirit_test::output_iterator<char>::type iterator_type;
  74. fusion::vector<double, int> fv(2.0, 1);
  75. karma::rule<iterator_type, fusion::vector<double, int>()> r;
  76. r %= double_ << ',' << int_;
  77. BOOST_TEST(test("test:2.0,1", "test:" << r, fv));
  78. }
  79. // action tests
  80. {
  81. using namespace boost::phoenix;
  82. BOOST_TEST(test("abcdefg",
  83. (char_ << char_ << string)[_1 = 'a', _2 = 'b', _3 = "cdefg"]));
  84. BOOST_TEST(test_delimited("a b cdefg ",
  85. (char_ << char_ << string)[_1 = 'a', _2 = 'b', _3 = "cdefg"],
  86. char(' ')));
  87. BOOST_TEST(test_delimited("a 12 c ",
  88. (char_ << lit(12) << char_)[_1 = 'a', _2 = 'c'], char(' ')));
  89. char c = 'c';
  90. BOOST_TEST(test("abc",
  91. (char_[_1 = 'a'] << 'b' << char_)[_1 = 'x', _2 = ref(c)]));
  92. BOOST_TEST(test_delimited("a b c ",
  93. (char_[_1 = 'a'] << 'b' << char_)[_2 = ref(c)], char(' ')));
  94. BOOST_TEST(test("aa", lower[char_ << 'A'][_1 = 'A']));
  95. BOOST_TEST(test("AA", upper[char_ << 'a'][_1 = 'a']));
  96. BOOST_TEST(test("Aa ", left_align[char_ << 'a'][_1 = 'A']));
  97. BOOST_TEST(test(" Aa ", center[char_ << 'a'][_1 = 'A']));
  98. BOOST_TEST(test(" Aa", right_align[char_ << 'a'][_1 = 'A']));
  99. }
  100. // test special case where sequence has a one element vector attribute
  101. // sequence and this element is a rule (attribute has to be passed through
  102. // without change)
  103. {
  104. typedef spirit_test::output_iterator<char>::type outiter_type;
  105. namespace karma = boost::spirit::karma;
  106. karma::rule<outiter_type, std::vector<int>()> r = -(int_ % ',');
  107. std::vector<int> v;
  108. BOOST_TEST(test(">", '>' << r, v));
  109. v.push_back(1);
  110. v.push_back(2);
  111. v.push_back(3);
  112. v.push_back(4);
  113. BOOST_TEST(test(">1,2,3,4", '>' << r, v));
  114. }
  115. {
  116. namespace karma = boost::spirit::karma;
  117. typedef spirit_test::output_iterator<char>::type outiter_type;
  118. karma::rule<outiter_type, std::string()> e = karma::string;
  119. karma::rule<outiter_type, std::vector<std::string>()> l = e << *(',' << e);
  120. std::vector<std::string> v;
  121. v.push_back("abc1");
  122. v.push_back("abc2");
  123. v.push_back("abc3");
  124. BOOST_TEST(test("abc1,abc2,abc3", l, v));
  125. }
  126. {
  127. namespace karma = boost::spirit::karma;
  128. namespace phoenix = boost::phoenix;
  129. typedef spirit_test::output_iterator<char>::type outiter_type;
  130. typedef fusion::vector<char, char, char> vector_type;
  131. vector_type p ('a', 'b', 'c');
  132. BOOST_TEST(test("ab", char_ << char_, p));
  133. karma::rule<outiter_type, vector_type()> r;
  134. r %= char_ << char_ << &karma::eps[seqsize(_val) == 3];
  135. BOOST_TEST(!test("", r, p));
  136. r %= char_ << char_ << char_ << &karma::eps[seqsize(_val) == 3];
  137. BOOST_TEST(test("abc", r, p));
  138. }
  139. {
  140. std::list<int> v;
  141. v.push_back(1);
  142. v.push_back(2);
  143. v.push_back(3);
  144. v.push_back(4);
  145. BOOST_TEST(test("1234", repeat(2)[int_] << *int_, v));
  146. BOOST_TEST(test_delimited("1 2 3 4 ", repeat(2)[int_] << *int_, v, char(' ')));
  147. }
  148. return boost::report_errors();
  149. }