optional.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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_operator.hpp>
  9. #include <boost/spirit/include/karma_numeric.hpp>
  10. #include <boost/spirit/include/karma_action.hpp>
  11. #include <boost/spirit/include/phoenix_core.hpp>
  12. #include <boost/spirit/include/phoenix_operator.hpp>
  13. #include <iostream>
  14. #include "test.hpp"
  15. int main()
  16. {
  17. using namespace spirit_test;
  18. using namespace boost::spirit;
  19. using namespace boost::spirit::ascii;
  20. {
  21. boost::optional<int> opt;
  22. BOOST_TEST(test("", -int_, opt));
  23. opt = 10;
  24. BOOST_TEST(test("10", -int_, opt));
  25. }
  26. {
  27. int opt = 10;
  28. BOOST_TEST(test("10", -int_, opt));
  29. }
  30. {
  31. boost::optional<int> opt;
  32. BOOST_TEST(test_delimited("", -int_, opt, space));
  33. opt = 10;
  34. BOOST_TEST(test_delimited("10 ", -int_, opt, space));
  35. }
  36. {
  37. int opt = 10;
  38. BOOST_TEST(test_delimited("10 ", -int_, opt, space));
  39. }
  40. { // test action
  41. using namespace boost::phoenix;
  42. namespace phoenix = boost::phoenix;
  43. boost::optional<int> n ;
  44. BOOST_TEST(test("", (-int_)[_1 = phoenix::ref(n)]));
  45. n = 1234;
  46. BOOST_TEST(test("1234", (-int_)[_1 = phoenix::ref(n)]));
  47. }
  48. { // test action
  49. using namespace boost::phoenix;
  50. namespace phoenix = boost::phoenix;
  51. int n = 1234;
  52. BOOST_TEST(test("1234", (-int_)[_1 = phoenix::ref(n)]));
  53. }
  54. { // test action
  55. using namespace boost::phoenix;
  56. namespace phoenix = boost::phoenix;
  57. boost::optional<int> n;
  58. BOOST_TEST(test_delimited("", (-int_)[_1 = phoenix::ref(n)], space));
  59. n = 1234;
  60. BOOST_TEST(test_delimited("1234 ", (-int_)[_1 = phoenix::ref(n)], space));
  61. }
  62. { // test action
  63. using namespace boost::phoenix;
  64. namespace phoenix = boost::phoenix;
  65. int n = 1234;
  66. BOOST_TEST(test_delimited("1234 ", (-int_)[_1 = phoenix::ref(n)], space));
  67. }
  68. return boost::report_errors();
  69. }