epsilon_tests.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*=============================================================================
  2. Copyright (c) 2002-2003 Martin Wille
  3. http://spirit.sourceforge.net/
  4. Use, modification and distribution is subject to the Boost Software
  5. License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  6. http://www.boost.org/LICENSE_1_0.txt)
  7. =============================================================================*/
  8. #include <iostream>
  9. #include <cstring>
  10. #include <boost/detail/lightweight_test.hpp>
  11. // This test program only includes the epsilon.hpp header from Spirit
  12. #include <boost/spirit/include/classic_epsilon.hpp>
  13. #include <boost/detail/lightweight_test.hpp>
  14. #include "impl/var.hpp"
  15. #include "impl/string_length.hpp"
  16. using namespace test;
  17. static BOOST_SPIRIT_CLASSIC_NS::parse_info<char const *> pi;
  18. ////////////////////////////////////////////////
  19. // These macros are used with BOOST_TEST
  20. #define matches (pi.hit)
  21. #define full_match (pi.hit && pi.full)
  22. #define partial_match (pi.hit && !pi.full)
  23. #define no_match (!pi.hit && !pi.full)
  24. #define zero_length_match (pi.length == 0)
  25. #define stop_equals_start (pi.stop == s)
  26. template<typename ParserT>
  27. static void
  28. parse(char const *s, ParserT const &p, bool match)
  29. {
  30. pi = BOOST_SPIRIT_CLASSIC_NS::parse(s, s + test_impl::string_length(s), p);
  31. if (match)
  32. {
  33. BOOST_TEST(matches);
  34. BOOST_TEST(zero_length_match);
  35. BOOST_TEST(stop_equals_start);
  36. }
  37. else
  38. {
  39. BOOST_TEST(no_match);
  40. }
  41. }
  42. static char const empty[] = "";
  43. static char const not_empty[] = "asdfgh";
  44. ////////////////////////////////////////////////
  45. // Test wether epsilon_p/eps_p work as
  46. // primitive parsers
  47. static void
  48. epsilon_as_primitive()
  49. {
  50. // This test case also is a compile time check wether
  51. // both eps_p and epsilon_p are present.
  52. parse(empty, BOOST_SPIRIT_CLASSIC_NS::epsilon_p, true);
  53. BOOST_TEST(full_match);
  54. parse(not_empty, BOOST_SPIRIT_CLASSIC_NS::epsilon_p, true);
  55. BOOST_TEST(partial_match);
  56. parse(empty, BOOST_SPIRIT_CLASSIC_NS::eps_p, true);
  57. BOOST_TEST(full_match);
  58. parse(not_empty, BOOST_SPIRIT_CLASSIC_NS::eps_p, true);
  59. BOOST_TEST(partial_match);
  60. }
  61. ////////////////////////////////////////////////
  62. // Test wether epsilon_p/eps_p work correctly as
  63. // a parser generator for creating parsers from
  64. // functors
  65. static void
  66. epsilon_as_parser_generator_for_functors()
  67. {
  68. bool flag = false;
  69. parse(empty, BOOST_SPIRIT_CLASSIC_NS::epsilon_p(var(flag)), flag);
  70. BOOST_TEST(no_match);
  71. flag = true;
  72. parse(empty, BOOST_SPIRIT_CLASSIC_NS::epsilon_p(var(flag)), flag);
  73. BOOST_TEST(full_match);
  74. }
  75. ////////////////////////////////////////////////
  76. // Test wether epsilon_p/eps_p work correctly as
  77. // a parser generator for creating parsers from
  78. // other parsers
  79. static void
  80. epsilon_as_parser_generator_for_parsers()
  81. {
  82. // This test case uses a parser created by epsilon_p
  83. // as body-parser for another invokation of epsilon_p
  84. bool flag = false;
  85. parse(empty, BOOST_SPIRIT_CLASSIC_NS::epsilon_p(
  86. BOOST_SPIRIT_CLASSIC_NS::epsilon_p(var(flag))), flag);
  87. BOOST_TEST(no_match);
  88. flag = true;
  89. parse(empty, BOOST_SPIRIT_CLASSIC_NS::epsilon_p(
  90. BOOST_SPIRIT_CLASSIC_NS::epsilon_p(var(flag))), flag);
  91. BOOST_TEST(full_match);
  92. }
  93. ////////////////////////////////////////////////
  94. // Test wether epsilon_p/eps_p support negation
  95. static void
  96. negation_operator_for_epsilon()
  97. {
  98. bool flag = false;
  99. parse(empty, ~BOOST_SPIRIT_CLASSIC_NS::epsilon_p(var(flag)), !flag);
  100. BOOST_TEST(full_match);
  101. parse(empty, ~~BOOST_SPIRIT_CLASSIC_NS::epsilon_p(var(flag)), flag);
  102. BOOST_TEST(no_match);
  103. flag = true;
  104. parse(empty, ~BOOST_SPIRIT_CLASSIC_NS::epsilon_p(var(flag)), !flag);
  105. BOOST_TEST(no_match);
  106. parse(empty, ~~BOOST_SPIRIT_CLASSIC_NS::epsilon_p(var(flag)), flag);
  107. BOOST_TEST(full_match);
  108. }
  109. int
  110. main()
  111. {
  112. epsilon_as_primitive();
  113. epsilon_as_parser_generator_for_functors();
  114. epsilon_as_parser_generator_for_parsers();
  115. negation_operator_for_epsilon();
  116. return boost::report_errors();
  117. }