nth_of.cpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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/nth_of.hpp>
  6. #include <boost/metaparse/nth_of_c.hpp>
  7. #include <boost/metaparse/is_error.hpp>
  8. #include <boost/metaparse/get_result.hpp>
  9. #include <boost/metaparse/start.hpp>
  10. #include "common.hpp"
  11. #include <boost/mpl/equal_to.hpp>
  12. #include <boost/mpl/apply_wrap.hpp>
  13. #include <boost/mpl/assert.hpp>
  14. #include "test_case.hpp"
  15. BOOST_METAPARSE_TEST_CASE(nth_of)
  16. {
  17. using boost::metaparse::get_result;
  18. using boost::metaparse::nth_of_c;
  19. using boost::metaparse::start;
  20. using boost::metaparse::nth_of;
  21. using boost::metaparse::is_error;
  22. using boost::mpl::equal_to;
  23. using boost::mpl::apply_wrap2;
  24. namespace mpl = boost::mpl;
  25. namespace mp = boost::metaparse;
  26. // test_first_of_one
  27. BOOST_MPL_ASSERT((
  28. equal_to<
  29. get_result<apply_wrap2<nth_of_c<0, lit_h>, str_hello, start> >::type,
  30. char_h
  31. >
  32. ));
  33. // test_first_of_two
  34. BOOST_MPL_ASSERT((
  35. equal_to<
  36. get_result<
  37. apply_wrap2<nth_of_c<0, lit_h, lit_e>, str_hello, start>
  38. >::type,
  39. char_h
  40. >
  41. ));
  42. // test_second_of_two
  43. BOOST_MPL_ASSERT((
  44. equal_to<
  45. get_result<
  46. apply_wrap2<nth_of<int1, lit_h, lit_e>, str_hello, start>
  47. >::type,
  48. char_e
  49. >
  50. ));
  51. // test_nothing
  52. BOOST_MPL_ASSERT((
  53. is_error<apply_wrap2<nth_of_c<1, lit_x, lit_e>, str_hello, start> >
  54. ));
  55. // test_first_of_none
  56. BOOST_MPL_ASSERT((
  57. is_error<apply_wrap2<nth_of_c<0>, str_hello, start> >
  58. ));
  59. // test_n_is_less_than_zero
  60. BOOST_MPL_ASSERT((
  61. is_error<apply_wrap2<nth_of_c<-1, lit_h, lit_e>, str_hello, start> >
  62. ));
  63. // test_n_is_greater_than_the_number_of_parsers
  64. BOOST_MPL_ASSERT((
  65. is_error<apply_wrap2<nth_of_c<2, lit_h, lit_e>, str_hello, start> >
  66. ));
  67. // test_error_before_the_nth
  68. BOOST_MPL_ASSERT((
  69. is_error<apply_wrap2<nth_of_c<1, lit_x, lit_e, lit_l>, str_hello, start> >
  70. ));
  71. // test_error_at_the_nth
  72. BOOST_MPL_ASSERT((
  73. is_error<apply_wrap2<nth_of_c<1, lit_h, lit_x, lit_l>, str_hello, start> >
  74. ));
  75. // test_error_after_the_nth
  76. BOOST_MPL_ASSERT((
  77. is_error<apply_wrap2<nth_of_c<1, lit_h, lit_e, lit_x>, str_hello, start> >
  78. ));
  79. }