sequence1.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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/fusion/include/vector.hpp>
  19. #include "test.hpp"
  20. using namespace spirit_test;
  21. ///////////////////////////////////////////////////////////////////////////////
  22. int main()
  23. {
  24. using namespace boost::spirit;
  25. using namespace boost::spirit::ascii;
  26. namespace fusion = boost::fusion;
  27. {
  28. BOOST_TEST(test("xi", char_('x') << char_('i')));
  29. BOOST_TEST(!test("xi", char_('x') << char_('o')));
  30. }
  31. {
  32. BOOST_TEST(test_delimited("x i ", char_('x') << 'i', char(' ')));
  33. BOOST_TEST(!test_delimited("x i ",
  34. char_('x') << char_('o'), char(' ')));
  35. }
  36. {
  37. BOOST_TEST(test_delimited("Hello , World ",
  38. lit("Hello") << ',' << "World", char(' ')));
  39. }
  40. {
  41. // a single element
  42. char attr = 'a';
  43. BOOST_TEST((test("ab", char_ << 'b', attr)));
  44. }
  45. {
  46. // a single element fusion sequence
  47. fusion::vector<char> attr('a');
  48. BOOST_TEST((test("ab", char_ << 'b', attr)));
  49. }
  50. {
  51. fusion::vector<char, char, std::string> p ('a', 'b', "cdefg");
  52. BOOST_TEST(test("abcdefg", char_ << char_ << string, p));
  53. BOOST_TEST(test_delimited("a b cdefg ",
  54. char_ << char_ << string, p, char(' ')));
  55. }
  56. {
  57. fusion::vector<char, int, char> p ('a', 12, 'c');
  58. BOOST_TEST(test("a12c", char_ << int_ << char_, p));
  59. BOOST_TEST(test_delimited("a 12 c ",
  60. char_ << int_ << char_, p, char(' ')));
  61. }
  62. {
  63. // element sequence can be shorter and longer than the attribute
  64. // sequence
  65. using boost::spirit::karma::strict;
  66. using boost::spirit::karma::relaxed;
  67. fusion::vector<char, int, char> p ('a', 12, 'c');
  68. BOOST_TEST(test("a12", char_ << int_, p));
  69. BOOST_TEST(test_delimited("a 12 ", char_ << int_, p, char(' ')));
  70. BOOST_TEST(test("a12", relaxed[char_ << int_], p));
  71. BOOST_TEST(test_delimited("a 12 ", relaxed[char_ << int_], p, char(' ')));
  72. BOOST_TEST(!test("", strict[char_ << int_], p));
  73. BOOST_TEST(!test_delimited("", strict[char_ << int_], p, char(' ')));
  74. fusion::vector<char, int> p1 ('a', 12);
  75. BOOST_TEST(test("a12c", char_ << int_ << char_('c'), p1));
  76. BOOST_TEST(test_delimited("a 12 c ", char_ << int_ << char_('c'),
  77. p1, char(' ')));
  78. BOOST_TEST(test("a12c", relaxed[char_ << int_ << char_('c')], p1));
  79. BOOST_TEST(test_delimited("a 12 c ",
  80. relaxed[char_ << int_ << char_('c')], p1, char(' ')));
  81. BOOST_TEST(!test("", strict[char_ << int_ << char_('c')], p1));
  82. BOOST_TEST(!test_delimited("", strict[char_ << int_ << char_('c')],
  83. p1, char(' ')));
  84. BOOST_TEST(test("a12", strict[char_ << int_], p1));
  85. BOOST_TEST(test_delimited("a 12 ", strict[char_ << int_], p1, char(' ')));
  86. std::string value("foo ' bar");
  87. BOOST_TEST(test("\"foo ' bar\"", '"' << strict[*(~char_('*'))] << '"', value));
  88. BOOST_TEST(test("\"foo ' bar\"", strict['"' << *(~char_('*')) << '"'], value));
  89. }
  90. {
  91. // if all elements of a sequence have unused parameters, the whole
  92. // sequence has an unused parameter as well
  93. fusion::vector<char, char> p ('a', 'e');
  94. BOOST_TEST(test("abcde",
  95. char_ << (lit('b') << 'c' << 'd') << char_, p));
  96. BOOST_TEST(test_delimited("a b c d e ",
  97. char_ << (lit('b') << 'c' << 'd') << char_, p, char(' ')));
  98. }
  99. {
  100. // literal generators do not need an attribute
  101. fusion::vector<char, char> p('a', 'c');
  102. BOOST_TEST(test("abc", char_ << 'b' << char_, p));
  103. BOOST_TEST(test_delimited("a b c ",
  104. char_ << 'b' << char_, p, char(' ')));
  105. }
  106. {
  107. // literal generators do not need an attribute, not even at the end
  108. fusion::vector<char, char> p('a', 'c');
  109. BOOST_TEST(test("acb", char_ << char_ << 'b', p));
  110. BOOST_TEST(test_delimited("a c b ",
  111. char_ << char_ << 'b', p, char(' ')));
  112. }
  113. return boost::report_errors();
  114. }