test_assert.cpp 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // test_assert.cpp
  3. //
  4. // Copyright 2008 Eric Niebler. Distributed under the Boost
  5. // Software License, Version 1.0. (See accompanying file
  6. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. #include <iostream>
  8. #include <boost/xpressive/xpressive_static.hpp>
  9. #include <boost/xpressive/regex_actions.hpp>
  10. #include <boost/test/unit_test.hpp>
  11. using namespace boost::xpressive;
  12. bool three_or_six(ssub_match const &sub)
  13. {
  14. return sub.length() == 3 || sub.length() == 6;
  15. }
  16. ///////////////////////////////////////////////////////////////////////////////
  17. // test1
  18. // simple custom assert that checks the length of a matched sub-expression
  19. void test1()
  20. {
  21. std::string str("foo barbaz fink");
  22. // match words of 3 characters or 6 characters.
  23. sregex rx = (bow >> +_w >> eow)[ check(&three_or_six) ] ;
  24. sregex_iterator first(str.begin(), str.end(), rx), last;
  25. BOOST_CHECK_EQUAL(std::distance(first, last), 2);
  26. }
  27. ///////////////////////////////////////////////////////////////////////////////
  28. // test2
  29. // same as above, but using a lambda
  30. void test2()
  31. {
  32. std::string str("foo barbaz fink");
  33. // match words of 3 characters or 6 characters.
  34. sregex rx = (bow >> +_w >> eow)[ check(length(_)==3 || length(_)==6) ] ;
  35. sregex_iterator first(str.begin(), str.end(), rx), last;
  36. BOOST_CHECK_EQUAL(std::distance(first, last), 2);
  37. }
  38. ///////////////////////////////////////////////////////////////////////////////
  39. // test3
  40. // more complicated use of custom assertions to validate a date
  41. void test3()
  42. {
  43. int const days_per_month[] =
  44. {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 31, 31};
  45. mark_tag month(1), day(2);
  46. // find a valid date of the form month/day/year.
  47. sregex date =
  48. (
  49. // Month must be between 1 and 12 inclusive
  50. (month= _d >> !_d) [ check(as<int>(_) >= 1
  51. && as<int>(_) <= 12) ]
  52. >> '/'
  53. // Day must be between 1 and 31 inclusive
  54. >> (day= _d >> !_d) [ check(as<int>(_) >= 1
  55. && as<int>(_) <= 31) ]
  56. >> '/'
  57. // Only consider years between 1970 and 2038
  58. >> (_d >> _d >> _d >> _d) [ check(as<int>(_) >= 1970
  59. && as<int>(_) <= 2038) ]
  60. )
  61. // Ensure the month actually has that many days.
  62. [ check( ref(days_per_month)[as<int>(month)-1] >= as<int>(day) ) ]
  63. ;
  64. smatch what;
  65. std::string str("99/99/9999 2/30/2006 2/28/2006");
  66. BOOST_REQUIRE(regex_search(str, what, date));
  67. BOOST_CHECK_EQUAL(what[0], "2/28/2006");
  68. }
  69. using namespace boost::unit_test;
  70. ///////////////////////////////////////////////////////////////////////////////
  71. // init_unit_test_suite
  72. //
  73. test_suite* init_unit_test_suite( int argc, char* argv[] )
  74. {
  75. test_suite *test = BOOST_TEST_SUITE("test_assert");
  76. test->add(BOOST_TEST_CASE(&test1));
  77. test->add(BOOST_TEST_CASE(&test2));
  78. test->add(BOOST_TEST_CASE(&test3));
  79. return test;
  80. }