omit.cpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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.hpp>
  8. #include <boost/fusion/include/std_pair.hpp>
  9. #include <boost/spirit/include/phoenix_core.hpp>
  10. #include <boost/spirit/include/phoenix_operator.hpp>
  11. #include <iostream>
  12. #include "test.hpp"
  13. using namespace spirit_test;
  14. int main()
  15. {
  16. using boost::spirit::karma::_1;
  17. using boost::spirit::karma::_a;
  18. using boost::spirit::karma::double_;
  19. using boost::spirit::karma::int_;
  20. using boost::spirit::karma::omit;
  21. using boost::spirit::karma::skip;
  22. using boost::spirit::karma::rule;
  23. using boost::spirit::karma::locals;
  24. typedef spirit_test::output_iterator<char>::type outiter_type;
  25. typedef std::pair<std::vector<int>, int> attribute_type;
  26. rule<outiter_type, attribute_type(), locals<int> > r;
  27. attribute_type a;
  28. a.first.push_back(0x01);
  29. a.first.push_back(0x02);
  30. a.first.push_back(0x04);
  31. a.first.push_back(0x08);
  32. a.second = 0;
  33. // omit[] is supposed to execute the embedded generator
  34. {
  35. std::pair<double, double> p (1.0, 2.0);
  36. BOOST_TEST(test("2.0", omit[double_] << double_, p));
  37. r %= omit[ *(int_[_a = _a + _1]) ] << int_[_1 = _a];
  38. BOOST_TEST(test("15", r, a));
  39. }
  40. // even if omit[] never fails, it has to honor the result of the
  41. // embedded generator
  42. {
  43. typedef std::pair<double, double> attribute_type;
  44. rule<outiter_type, attribute_type()> r;
  45. r %= omit[double_(1.0) << double_] | "42";
  46. attribute_type p1 (1.0, 2.0);
  47. BOOST_TEST(test("", r, p1));
  48. attribute_type p2 (10.0, 2.0);
  49. BOOST_TEST(test("42", r, p2));
  50. }
  51. // skip[] is not supposed to execute the embedded generator
  52. {
  53. using boost::spirit::karma::double_;
  54. using boost::spirit::karma::skip;
  55. std::pair<double, double> p (1.0, 2.0);
  56. BOOST_TEST(test("2.0", skip[double_] << double_, p));
  57. r %= skip[ *(int_[_a = _a + _1]) ] << int_[_1 = _a];
  58. BOOST_TEST(test("0", r, a));
  59. }
  60. return boost::report_errors();
  61. }