named_subexpressions_test.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. *
  3. * Copyright (c) 2009
  4. * John Maddock
  5. *
  6. * Use, modification and distribution are subject to the
  7. * Boost Software License, Version 1.0. (See accompanying file
  8. * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. *
  10. */
  11. #include <boost/regex.hpp>
  12. #include <boost/detail/lightweight_main.hpp>
  13. #include "../test_macros.hpp"
  14. #ifdef BOOST_INTEL
  15. #pragma warning(disable:1418 981 983 383)
  16. #endif
  17. template <class charT>
  18. void test_named_subexpressions(charT)
  19. {
  20. //
  21. // Really this is just a test that the overloaded access functions work correctly:
  22. //
  23. static const charT e[] =
  24. {
  25. '(', '?', '\'', 'o', 'n', 'e', '\'', 'a', '+', ')', '(', '?', '<', 't', 'w', 'o', '>', 'b', '+', ')', '\0'
  26. };
  27. static const charT t[] =
  28. {
  29. 'm', 'm', 'a', 'a', 'a', 'b', 'b', 'n', 'n', '\0'
  30. };
  31. static const charT one[] =
  32. {
  33. 'o', 'n', 'e', '\0'
  34. };
  35. static const charT two[] =
  36. {
  37. 't', 'w', 'o', '\0'
  38. };
  39. static const std::basic_string<charT> s_one(one);
  40. static const std::basic_string<charT> s_two(two);
  41. static const charT result1[] = { 'a', 'a', 'a', '\0' };
  42. static const charT result2[] = { 'b', 'b', '\0' };
  43. static const std::basic_string<charT> s_result1(result1);
  44. static const std::basic_string<charT> s_result2(result2);
  45. static const char* c_one = "one";
  46. static const char* c_two = "two";
  47. static const std::string cs_one(c_one);
  48. static const std::string cs_two(c_two);
  49. boost::basic_regex<charT> expression(e);
  50. boost::match_results<const charT*> what;
  51. if(regex_search(t, what, expression))
  52. {
  53. BOOST_CHECK(what.length(1) == 3);
  54. BOOST_CHECK(what.length(one) == 3);
  55. BOOST_CHECK(what.length(s_one) == 3);
  56. BOOST_CHECK(what.length(c_one) == 3);
  57. BOOST_CHECK(what.length(cs_one) == 3);
  58. BOOST_CHECK(what.position(1) == 2);
  59. BOOST_CHECK(what.position(one) == 2);
  60. BOOST_CHECK(what.position(s_one) == 2);
  61. BOOST_CHECK(what.position(c_one) == 2);
  62. BOOST_CHECK(what.position(cs_one) == 2);
  63. BOOST_CHECK(what.str(1) == s_result1);
  64. BOOST_CHECK(what.str(one) == s_result1);
  65. BOOST_CHECK(what.str(s_one) == s_result1);
  66. BOOST_CHECK(what.str(c_one) == s_result1);
  67. BOOST_CHECK(what.str(cs_one) == s_result1);
  68. BOOST_CHECK(what[1] == s_result1);
  69. BOOST_CHECK(what[one] == s_result1);
  70. BOOST_CHECK(what[s_one] == s_result1);
  71. BOOST_CHECK(what[c_one] == s_result1);
  72. BOOST_CHECK(what[cs_one] == s_result1);
  73. BOOST_CHECK(what.length(2) == 2);
  74. BOOST_CHECK(what.length(two) == 2);
  75. BOOST_CHECK(what.length(s_two) == 2);
  76. BOOST_CHECK(what.length(c_two) == 2);
  77. BOOST_CHECK(what.length(cs_two) == 2);
  78. BOOST_CHECK(what.position(2) == 5);
  79. BOOST_CHECK(what.position(two) == 5);
  80. BOOST_CHECK(what.position(s_two) == 5);
  81. BOOST_CHECK(what.position(c_two) == 5);
  82. BOOST_CHECK(what.position(cs_two) == 5);
  83. BOOST_CHECK(what.str(2) == s_result2);
  84. BOOST_CHECK(what.str(two) == s_result2);
  85. BOOST_CHECK(what.str(s_two) == s_result2);
  86. BOOST_CHECK(what.str(c_two) == s_result2);
  87. BOOST_CHECK(what.str(cs_two) == s_result2);
  88. BOOST_CHECK(what[2] == s_result2);
  89. BOOST_CHECK(what[two] == s_result2);
  90. BOOST_CHECK(what[s_two] == s_result2);
  91. BOOST_CHECK(what[c_two] == s_result2);
  92. BOOST_CHECK(what[cs_two] == s_result2);
  93. }
  94. else
  95. {
  96. BOOST_ERROR("Expected match not found");
  97. }
  98. }
  99. int cpp_main( int , char* [] )
  100. {
  101. test_named_subexpressions(char(0));
  102. test_named_subexpressions(wchar_t(0));
  103. return 0;
  104. }