list.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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/mpl/print.hpp>
  8. #include <boost/spirit/include/karma_operator.hpp>
  9. #include <boost/spirit/include/karma_char.hpp>
  10. #include <boost/spirit/include/karma_string.hpp>
  11. #include <boost/spirit/include/karma_numeric.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/phoenix_core.hpp>
  17. #include <boost/spirit/include/phoenix_operator.hpp>
  18. #include <boost/fusion/include/std_pair.hpp>
  19. #include <boost/assign/std/vector.hpp>
  20. #include <string>
  21. #include <vector>
  22. #include <iostream>
  23. #include "test.hpp"
  24. using namespace spirit_test;
  25. using boost::spirit::unused_type;
  26. ///////////////////////////////////////////////////////////////////////////////
  27. struct action
  28. {
  29. action (std::vector<char>& vec)
  30. : vec(vec), it(vec.begin())
  31. {}
  32. void operator()(unsigned& value, unused_type const&, bool& pass) const
  33. {
  34. pass = (it != vec.end());
  35. if (pass)
  36. value = *it++;
  37. }
  38. std::vector<char>& vec;
  39. mutable std::vector<char>::iterator it;
  40. };
  41. ///////////////////////////////////////////////////////////////////////////////
  42. int main()
  43. {
  44. using namespace boost::spirit;
  45. using namespace boost::spirit::ascii;
  46. using namespace boost::assign;
  47. std::vector<char> v;
  48. v += 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h';
  49. {
  50. BOOST_TEST(test("a,b,c,d,e,f,g,h", char_ % ',', v));
  51. BOOST_TEST(test_delimited("a , b , c , d , e , f , g , h ",
  52. char_ % ',', v, space));
  53. }
  54. {
  55. std::string s ("abcdefgh");
  56. BOOST_TEST(test("a,b,c,d,e,f,g,h", char_ % ',', s));
  57. BOOST_TEST(test_delimited("a , b , c , d , e , f , g , h ",
  58. char_ % ',', s, space));
  59. }
  60. {
  61. std::string s ("abcdefg");
  62. BOOST_TEST(test("abc,de,fg", char_ << ((char_ << char_) % ','), s));
  63. BOOST_TEST(test_delimited("a b c , d e , f g ",
  64. char_ << ((char_ << char_) % ','), s, space));
  65. }
  66. { // actions
  67. namespace phx = boost::phoenix;
  68. BOOST_TEST(test("a,b,c,d,e,f,g,h", (char_ % ',')[_1 = phx::ref(v)]));
  69. BOOST_TEST(test_delimited("a , b , c , d , e , f , g , h ",
  70. (char_ % ',')[_1 = phx::ref(v)], space));
  71. }
  72. // failing sub-generators
  73. {
  74. using boost::spirit::karma::strict;
  75. using boost::spirit::karma::relaxed;
  76. typedef std::pair<char, char> data;
  77. std::vector<data> v2;
  78. v2 += std::make_pair('a', 'a'),
  79. std::make_pair('b', 'b'),
  80. std::make_pair('c', 'c'),
  81. std::make_pair('d', 'd'),
  82. std::make_pair('e', 'e'),
  83. std::make_pair('f', 'f'),
  84. std::make_pair('g', 'g');
  85. karma::rule<spirit_test::output_iterator<char>::type, data()> r;
  86. r = &char_('d') << char_;
  87. BOOST_TEST(test("d", r % ',', v2));
  88. BOOST_TEST(test("d", relaxed[r % ','], v2));
  89. BOOST_TEST(!test("", strict[r % ','], v2));
  90. r = &char_('a') << char_;
  91. BOOST_TEST(test("a", r % ',', v2));
  92. BOOST_TEST(test("a", relaxed[r % ','], v2));
  93. BOOST_TEST(test("a", strict[r % ','], v2));
  94. r = &char_('g') << char_;
  95. BOOST_TEST(test("g", r % ',', v2));
  96. BOOST_TEST(test("g", relaxed[r % ','], v2));
  97. BOOST_TEST(!test("", strict[r % ','], v2));
  98. r = !char_('d') << char_;
  99. BOOST_TEST(test("a,b,c,e,f,g", r % ',', v2));
  100. BOOST_TEST(test("a,b,c,e,f,g", relaxed[r % ','], v2));
  101. BOOST_TEST(test("a,b,c", strict[r % ','], v2));
  102. r = !char_('a') << char_;
  103. BOOST_TEST(test("b,c,d,e,f,g", r % ',', v2));
  104. BOOST_TEST(test("b,c,d,e,f,g", relaxed[r % ','], v2));
  105. BOOST_TEST(!test("", strict[r % ','], v2));
  106. r = !char_('g') << char_;
  107. BOOST_TEST(test("a,b,c,d,e,f", r % ',', v2));
  108. BOOST_TEST(test("a,b,c,d,e,f", relaxed[r % ','], v2));
  109. BOOST_TEST(test("a,b,c,d,e,f", strict[r % ','], v2));
  110. r = &char_('A') << char_;
  111. BOOST_TEST(!test("", r % ',', v2));
  112. }
  113. {
  114. // make sure user defined end condition is applied if no attribute
  115. // is passed in
  116. BOOST_TEST(test("[61,62,63,64,65,66,67,68]",
  117. '[' << hex[action(v)] % ',' << ']'));
  118. }
  119. return boost::report_errors();
  120. }