actions.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*=============================================================================
  2. Copyright (c) 2001-2011 Hartmut Kaiser
  3. Copyright (c) 2001-2011 Joel de Guzman
  4. Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. =============================================================================*/
  7. #include <boost/config/warning_disable.hpp>
  8. #include <boost/detail/lightweight_test.hpp>
  9. #include <boost/spirit/include/karma_char.hpp>
  10. #include <boost/spirit/include/karma_numeric.hpp>
  11. #include <boost/spirit/include/karma_action.hpp>
  12. #include <boost/spirit/include/karma_generate.hpp>
  13. #include <boost/spirit/include/karma_operator.hpp>
  14. #include <boost/bind.hpp>
  15. #include <boost/lambda/lambda.hpp>
  16. #include <sstream>
  17. #include "test.hpp"
  18. using namespace spirit_test;
  19. using boost::spirit::unused_type;
  20. void read1(int& i)
  21. {
  22. i = 42;
  23. }
  24. void read2(int& i, unused_type)
  25. {
  26. i = 42;
  27. }
  28. void read3(int& i, unused_type, bool&)
  29. {
  30. i = 42;
  31. }
  32. void read3_fail(int& i, unused_type, bool& pass)
  33. {
  34. i = 42;
  35. pass = false;
  36. }
  37. struct read_action
  38. {
  39. void operator()(int& i, unused_type, unused_type) const
  40. {
  41. i = 42;
  42. }
  43. };
  44. ///////////////////////////////////////////////////////////////////////////////
  45. int main()
  46. {
  47. using boost::spirit::karma::int_;
  48. {
  49. BOOST_TEST(test("42", int_[&read1]));
  50. BOOST_TEST(test_delimited("42 ", int_[&read1], ' '));
  51. BOOST_TEST(test("42", int_[&read2]));
  52. BOOST_TEST(test_delimited("42 ", int_[&read2], ' '));
  53. BOOST_TEST(test("42", int_[&read3]));
  54. BOOST_TEST(test_delimited("42 ", int_[&read3], ' '));
  55. BOOST_TEST(!test("42", int_[&read3_fail]));
  56. BOOST_TEST(!test_delimited("42 ", int_[&read3_fail], ' '));
  57. }
  58. {
  59. BOOST_TEST(test("42", int_[read_action()]));
  60. BOOST_TEST(test_delimited("42 ", int_[read_action()], ' '));
  61. }
  62. {
  63. BOOST_TEST(test("42", int_[boost::bind(&read1, _1)]));
  64. BOOST_TEST(test_delimited("42 ", int_[boost::bind(&read1, _1)], ' '));
  65. BOOST_TEST(test("42", int_[boost::bind(&read2, _1, _2)]));
  66. BOOST_TEST(test_delimited("42 ", int_[boost::bind(&read2, _1, _2)], ' '));
  67. BOOST_TEST(test("42", int_[boost::bind(&read3, _1, _2, _3)]));
  68. BOOST_TEST(test_delimited("42 ", int_[boost::bind(&read3, _1, _2, _3)], ' '));
  69. }
  70. {
  71. namespace lambda = boost::lambda;
  72. {
  73. std::stringstream strm("42");
  74. BOOST_TEST(test("42", int_[strm >> lambda::_1]));
  75. }
  76. {
  77. std::stringstream strm("42");
  78. BOOST_TEST(test_delimited("42 ", int_[strm >> lambda::_1], ' '));
  79. }
  80. }
  81. {
  82. BOOST_TEST(test("{42}", '{' << int_[&read1] << '}'));
  83. BOOST_TEST(test_delimited("{ 42 } ", '{' << int_[&read1] << '}', ' '));
  84. BOOST_TEST(test("{42}", '{' << int_[&read2] << '}'));
  85. BOOST_TEST(test_delimited("{ 42 } ", '{' << int_[&read2] << '}', ' '));
  86. BOOST_TEST(test("{42}", '{' << int_[&read3] << '}'));
  87. BOOST_TEST(test_delimited("{ 42 } ", '{' << int_[&read3] << '}', ' '));
  88. }
  89. {
  90. BOOST_TEST(test("{42}", '{' << int_[read_action()] << '}'));
  91. BOOST_TEST(test_delimited("{ 42 } ", '{' << int_[read_action()] << '}', ' '));
  92. }
  93. {
  94. BOOST_TEST(test("{42}", '{' << int_[boost::bind(&read1, _1)] << '}'));
  95. BOOST_TEST(test_delimited("{ 42 } ",
  96. '{' << int_[boost::bind(&read1, _1)] << '}', ' '));
  97. BOOST_TEST(test("{42}", '{' << int_[boost::bind(&read2, _1, _2)] << '}'));
  98. BOOST_TEST(test_delimited("{ 42 } ",
  99. '{' << int_[boost::bind(&read2, _1, _2)] << '}', ' '));
  100. BOOST_TEST(test("{42}", '{' << int_[boost::bind(&read3, _1, _2, _3)] << '}'));
  101. BOOST_TEST(test_delimited("{ 42 } ",
  102. '{' << int_[boost::bind(&read3, _1, _2, _3)] << '}', ' '));
  103. }
  104. {
  105. namespace lambda = boost::lambda;
  106. {
  107. std::stringstream strm("42");
  108. BOOST_TEST(test("{42}", '{' << int_[strm >> lambda::_1] << '}'));
  109. }
  110. {
  111. std::stringstream strm("42");
  112. BOOST_TEST(test_delimited("{ 42 } ",
  113. '{' << int_[strm >> lambda::_1] << '}', ' '));
  114. }
  115. }
  116. return boost::report_errors();
  117. }