captures_test.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. *
  3. * Copyright (c) 2004
  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. /*
  12. * LOCATION: see http://www.boost.org for most recent version.
  13. * FILE captures_test.cpp
  14. * VERSION see <boost/version.hpp>
  15. * DESCRIPTION: Basic tests for additional captures information.
  16. */
  17. #include <boost/regex.hpp>
  18. #include <boost/detail/lightweight_main.hpp>
  19. #include "../test_macros.hpp"
  20. #include <boost/array.hpp>
  21. #include <cstring>
  22. #ifdef BOOST_HAS_ICU
  23. #include <boost/regex/icu.hpp>
  24. #endif
  25. #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
  26. template <int N>
  27. size_t array_size(const char* (&p)[N])
  28. {
  29. for(size_t i = 0; i < N; ++i)
  30. if(p[i] == 0)
  31. return i;
  32. return N;
  33. }
  34. std::wstring make_wstring(const char* p)
  35. {
  36. return std::wstring(p, p + std::strlen(p));
  37. }
  38. #ifdef __sgi
  39. template <class T>
  40. void test_captures(const std::string& regx, const std::string& text, const T& expected)
  41. #else
  42. template <class T>
  43. void test_captures(const std::string& regx, const std::string& text, T& expected)
  44. #endif
  45. {
  46. boost::regex e(regx);
  47. boost::smatch what;
  48. if(boost::regex_match(text, what, e, boost::match_extra))
  49. {
  50. unsigned i, j;
  51. #ifndef __sgi
  52. // strange type deduction causes this test to fail on SGI:
  53. BOOST_CHECK(what.size() == ARRAY_SIZE(expected));
  54. #endif
  55. for(i = 0; i < what.size(); ++i)
  56. {
  57. BOOST_CHECK(what.captures(i).size() == array_size(expected[i]));
  58. for(j = 0; j < what.captures(i).size(); ++j)
  59. {
  60. BOOST_CHECK(what.captures(i)[j] == expected[i][j]);
  61. }
  62. }
  63. }
  64. std::wstring wre(regx.begin(), regx.end());
  65. std::wstring wtext(text.begin(), text.end());
  66. boost::wregex we(wre);
  67. boost::wsmatch wwhat;
  68. if(boost::regex_match(wtext, wwhat, we, boost::match_extra))
  69. {
  70. unsigned i, j;
  71. #ifndef __sgi
  72. // strange type deduction causes this test to fail on SGI:
  73. BOOST_CHECK(wwhat.size() == ARRAY_SIZE(expected));
  74. #endif
  75. for(i = 0; i < wwhat.size(); ++i)
  76. {
  77. BOOST_CHECK(wwhat.captures(i).size() == array_size(expected[i]));
  78. for(j = 0; j < wwhat.captures(i).size(); ++j)
  79. {
  80. BOOST_CHECK(wwhat.captures(i)[j] == make_wstring(expected[i][j]));
  81. }
  82. }
  83. }
  84. #ifdef BOOST_HAS_ICU
  85. boost::u32regex ure = boost::make_u32regex(regx);
  86. what = boost::smatch();
  87. if(boost::u32regex_match(text, what, ure, boost::match_extra))
  88. {
  89. unsigned i, j;
  90. #ifndef __sgi
  91. // strange type deduction causes this test to fail on SGI:
  92. BOOST_CHECK(what.size() == ARRAY_SIZE(expected));
  93. #endif
  94. for(i = 0; i < what.size(); ++i)
  95. {
  96. BOOST_CHECK(what.captures(i).size() == array_size(expected[i]));
  97. for(j = 0; j < what.captures(i).size(); ++j)
  98. {
  99. BOOST_CHECK(what.captures(i)[j] == expected[i][j]);
  100. }
  101. }
  102. }
  103. #endif
  104. }
  105. int cpp_main(int , char* [])
  106. {
  107. typedef const char* pchar;
  108. pchar e1[4][5] =
  109. {
  110. { "aBBcccDDDDDeeeeeeee", },
  111. { "a", "BB", "ccc", "DDDDD", "eeeeeeee", },
  112. { "a", "ccc", "eeeeeeee", },
  113. { "BB", "DDDDD", },
  114. };
  115. test_captures("(([[:lower:]]+)|([[:upper:]]+))+", "aBBcccDDDDDeeeeeeee", e1);
  116. pchar e2[4][2] =
  117. {
  118. { "abd" },
  119. { "b", "" },
  120. { "" },
  121. };
  122. test_captures("a(b+|((c)*))+d", "abd", e2);
  123. pchar e3[3][1] =
  124. {
  125. { "abcbar" },
  126. { "abc" },
  127. };
  128. test_captures("(.*)bar|(.*)bah", "abcbar", e3);
  129. pchar e4[3][1] =
  130. {
  131. { "abcbah" },
  132. { 0, },
  133. { "abc" },
  134. };
  135. test_captures("(.*)bar|(.*)bah", "abcbah", e4);
  136. pchar e5[2][16] =
  137. {
  138. { "now is the time for all good men to come to the aid of the party" },
  139. { "now", "is", "the", "time", "for", "all", "good", "men", "to", "come", "to", "the", "aid", "of", "the", "party" },
  140. };
  141. test_captures("^(?:(\\w+)|(?>\\W+))*$", "now is the time for all good men to come to the aid of the party", e5);
  142. pchar e6[2][16] =
  143. {
  144. { "now is the time for all good men to come to the aid of the party" },
  145. { "now", "is", "the", "time", "for", "all", "good", "men", "to", "come", "to", "the", "aid", "of", "the", "party" },
  146. };
  147. test_captures("^(?>(\\w+)\\W*)*$", "now is the time for all good men to come to the aid of the party", e6);
  148. pchar e7[4][14] =
  149. {
  150. { "now is the time for all good men to come to the aid of the party" },
  151. { "now" },
  152. { "is", "the", "time", "for", "all", "good", "men", "to", "come", "to", "the", "aid", "of", "the" },
  153. { "party" },
  154. };
  155. test_captures("^(\\w+)\\W+(?>(\\w+)\\W+)*(\\w+)$", "now is the time for all good men to come to the aid of the party", e7);
  156. pchar e8[5][9] =
  157. {
  158. { "now is the time for all good men to come to the aid of the party" } ,
  159. { "now" },
  160. { "is", "for", "men", "to", "of" },
  161. { "the", "time", "all", "good", "to", "come", "the", "aid", "the" },
  162. { "party" },
  163. };
  164. test_captures("^(\\w+)\\W+(?>(\\w+)\\W+(?:(\\w+)\\W+){0,2})*(\\w+)$", "now is the time for all good men to come to the aid of the party", e8);
  165. return 0;
  166. }