sequence.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // Copyright Abel Sinkovics (abel@sinkovics.hu) 2010.
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. #include <boost/metaparse/sequence.hpp>
  6. #include <boost/metaparse/is_error.hpp>
  7. #include <boost/metaparse/start.hpp>
  8. #include <boost/metaparse/get_result.hpp>
  9. #include <boost/metaparse/always.hpp>
  10. #include <boost/metaparse/one_char.hpp>
  11. #include "common.hpp"
  12. #include <boost/mpl/equal_to.hpp>
  13. #include <boost/mpl/apply_wrap.hpp>
  14. #include <boost/mpl/list.hpp>
  15. #include <boost/mpl/at.hpp>
  16. #include <boost/mpl/equal.hpp>
  17. #include <boost/mpl/vector_c.hpp>
  18. #include <boost/mpl/vector.hpp>
  19. #include <boost/mpl/assert.hpp>
  20. #include "test_case.hpp"
  21. BOOST_METAPARSE_TEST_CASE(sequence)
  22. {
  23. using boost::metaparse::get_result;
  24. using boost::metaparse::sequence;
  25. using boost::metaparse::start;
  26. using boost::metaparse::is_error;
  27. using boost::metaparse::always;
  28. using boost::metaparse::one_char;
  29. using boost::mpl::equal;
  30. using boost::mpl::apply_wrap2;
  31. using boost::mpl::list;
  32. using boost::mpl::equal_to;
  33. using boost::mpl::at_c;
  34. using boost::mpl::vector_c;
  35. using boost::mpl::vector;
  36. typedef always<one_char, int> always_int;
  37. // test_no_parser
  38. BOOST_MPL_ASSERT((
  39. equal<get_result<apply_wrap2<sequence<>, str_hello, start> >::type, list<> >
  40. ));
  41. // test_one_parser
  42. BOOST_MPL_ASSERT((
  43. equal<
  44. get_result<apply_wrap2<sequence<lit_h>, str_hello, start> >::type,
  45. vector_c<char, 'h'>
  46. >
  47. ));
  48. // test_one_failing_parser
  49. BOOST_MPL_ASSERT((is_error<apply_wrap2<sequence<lit_e>, str_hello, start> >));
  50. // test_two_chars
  51. BOOST_MPL_ASSERT((
  52. equal<
  53. get_result<apply_wrap2<sequence<lit_h, lit_e>, str_hello, start> >::type,
  54. vector_c<char, 'h', 'e'>
  55. >
  56. ));
  57. // test_first_fails
  58. BOOST_MPL_ASSERT((
  59. is_error<apply_wrap2<sequence<lit_x, lit_e>, str_hello, start> >
  60. ));
  61. // test_second_fails
  62. BOOST_MPL_ASSERT((
  63. is_error<apply_wrap2<sequence<lit_h, lit_x>, str_hello, start> >
  64. ));
  65. // test_empty_input
  66. BOOST_MPL_ASSERT((is_error<apply_wrap2<sequence<lit_h,lit_e>, str_,start> >));
  67. // test_three_chars
  68. BOOST_MPL_ASSERT((
  69. equal<
  70. get_result<
  71. apply_wrap2<sequence<lit_h, lit_e, lit_l>, str_hello, start>
  72. >::type,
  73. vector_c<char, 'h', 'e', 'l'>
  74. >
  75. ));
  76. // test_indexing_in_result
  77. BOOST_MPL_ASSERT((
  78. equal_to<
  79. at_c<
  80. get_result<
  81. apply_wrap2<sequence<lit_h, lit_e, lit_l>, str_hello, start>
  82. >::type,
  83. 1
  84. >::type,
  85. char_e
  86. >
  87. ));
  88. // test_no_extra_evaluation
  89. BOOST_MPL_ASSERT((
  90. equal<
  91. get_result<
  92. apply_wrap2<sequence<always_int, always_int>, str_ca, start>
  93. >::type,
  94. vector<int, int>
  95. >
  96. ));
  97. }