int3.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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/lexical_cast.hpp>
  8. #include <boost/mpl/vector.hpp>
  9. #include <boost/mpl/for_each.hpp>
  10. #include <boost/mpl/if.hpp>
  11. #include <boost/mpl/bool.hpp>
  12. #include <boost/spirit/include/phoenix_core.hpp>
  13. #include <boost/spirit/include/phoenix_operator.hpp>
  14. #include <boost/spirit/include/karma_char.hpp>
  15. #include <boost/spirit/include/karma_numeric.hpp>
  16. #include <boost/spirit/include/karma_directive.hpp>
  17. #include <boost/spirit/include/karma_action.hpp>
  18. #include <boost/spirit/include/karma_phoenix_attributes.hpp>
  19. #include <boost/limits.hpp>
  20. #include "test.hpp"
  21. using namespace spirit_test;
  22. ///////////////////////////////////////////////////////////////////////////////
  23. struct test_minmax
  24. {
  25. template <typename T>
  26. void operator()(T) const
  27. {
  28. using namespace boost::spirit;
  29. using namespace boost::phoenix;
  30. T minval = (std::numeric_limits<T>::min)();
  31. T maxval = (std::numeric_limits<T>::max)();
  32. std::string expected_minval = boost::lexical_cast<std::string>(minval);
  33. std::string expected_maxval = boost::lexical_cast<std::string>(maxval);
  34. // create a correct generator type from the given integer type
  35. typedef typename
  36. boost::mpl::if_<
  37. boost::mpl::bool_<std::numeric_limits<T>::is_signed>,
  38. karma::int_generator<T>,
  39. karma::uint_generator<T>
  40. >::type
  41. int_generator_type;
  42. int_generator_type const gen = int_generator_type();
  43. BOOST_TEST(test(expected_maxval, gen, maxval));
  44. BOOST_TEST(test(expected_minval, gen, minval));
  45. BOOST_TEST(test(expected_maxval, gen(maxval)));
  46. BOOST_TEST(test(expected_minval, gen(minval)));
  47. BOOST_TEST(test(expected_maxval, gen(maxval), maxval));
  48. BOOST_TEST(test(expected_minval, gen(minval), minval));
  49. BOOST_TEST(!test("", gen(maxval), maxval-1));
  50. BOOST_TEST(!test("", gen(minval), minval+1));
  51. BOOST_TEST(test(expected_maxval, lit(maxval)));
  52. BOOST_TEST(test(expected_minval, lit(minval)));
  53. BOOST_TEST(test_delimited(expected_maxval + " ", gen, maxval, char(' ')));
  54. BOOST_TEST(test_delimited(expected_minval + " ", gen, minval, char(' ')));
  55. BOOST_TEST(test_delimited(expected_maxval + " ", gen(maxval), char(' ')));
  56. BOOST_TEST(test_delimited(expected_minval + " ", gen(minval), char(' ')));
  57. BOOST_TEST(test_delimited(expected_maxval + " ", gen(maxval), maxval, char(' ')));
  58. BOOST_TEST(test_delimited(expected_minval + " ", gen(minval), minval, char(' ')));
  59. BOOST_TEST(!test_delimited("", gen(maxval), maxval-1, char(' ')));
  60. BOOST_TEST(!test_delimited("", gen(minval), minval+1, char(' ')));
  61. BOOST_TEST(test_delimited(expected_maxval + " ", lit(maxval), char(' ')));
  62. BOOST_TEST(test_delimited(expected_minval + " ", lit(minval), char(' ')));
  63. // action tests
  64. BOOST_TEST(test(expected_maxval, gen[_1 = val(maxval)]));
  65. BOOST_TEST(test(expected_minval, gen[_1 = val(minval)]));
  66. // optional tests
  67. boost::optional<T> optmin, optmax(maxval);
  68. BOOST_TEST(!test("", gen, optmin));
  69. BOOST_TEST(!test("", gen(minval), optmin));
  70. optmin = minval;
  71. BOOST_TEST(test(expected_minval, gen, optmin));
  72. BOOST_TEST(test(expected_maxval, gen, optmax));
  73. BOOST_TEST(test(expected_minval, gen(minval), optmin));
  74. BOOST_TEST(test(expected_maxval, gen(maxval), optmax));
  75. // we support Phoenix attributes only starting with V2.2
  76. #if SPIRIT_VERSION >= 0x2020
  77. // Phoenix expression tests (only supported while including
  78. // karma_phoenix_attributes.hpp
  79. namespace phoenix = boost::phoenix;
  80. BOOST_TEST(test("1", gen, phoenix::val(1)));
  81. T val = 1;
  82. BOOST_TEST(test("1", gen, phoenix::ref(val)));
  83. BOOST_TEST(test("2", gen, ++phoenix::ref(val)));
  84. #endif
  85. }
  86. };
  87. ///////////////////////////////////////////////////////////////////////////////
  88. int
  89. main()
  90. {
  91. using namespace boost::spirit;
  92. // test boundary values
  93. typedef boost::mpl::vector<
  94. #ifdef BOOST_HAS_LONG_LONG
  95. boost::long_long_type, boost::ulong_long_type,
  96. #endif
  97. short, unsigned short,
  98. int, unsigned int,
  99. long, unsigned long
  100. > integer_types;
  101. boost::mpl::for_each<integer_types>(test_minmax());
  102. return boost::report_errors();
  103. }