test_partial_match.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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 test_regex_search.hpp
  14. * VERSION see <boost/version.hpp>
  15. * DESCRIPTION: Declares tests for regex search and iteration.
  16. */
  17. #ifndef BOOST_REGEX_REGRESS_REGEX_PARTIAL_MATCH_HPP
  18. #define BOOST_REGEX_REGRESS_REGEX_PARTIAL_MATCH_HPP
  19. #include "info.hpp"
  20. //
  21. // this file implements a test for a regular expression that should compile,
  22. // followed by a search for that expression:
  23. //
  24. struct test_regex_search_tag{};
  25. template <class BidirectionalIterator>
  26. void test_sub_match(const boost::sub_match<BidirectionalIterator>& sub, BidirectionalIterator base, const int* answer_table, int i)
  27. {
  28. #ifdef BOOST_MSVC
  29. #pragma warning(push)
  30. #pragma warning(disable:4244)
  31. #endif
  32. typedef typename boost::sub_match<BidirectionalIterator>::value_type charT;
  33. if((sub.matched == 0)
  34. &&
  35. !((i == 0)
  36. && (test_info<charT>::match_options() & boost::match_partial)) )
  37. {
  38. if(answer_table[2*i] >= 0)
  39. {
  40. BOOST_REGEX_TEST_ERROR(
  41. "Sub-expression " << i
  42. << " was not matched when it should have been.", charT);
  43. }
  44. }
  45. else
  46. {
  47. if(boost::BOOST_REGEX_DETAIL_NS::distance(base, sub.first) != answer_table[2*i])
  48. {
  49. BOOST_REGEX_TEST_ERROR(
  50. "Error in start location of sub-expression "
  51. << i << ", found " << boost::BOOST_REGEX_DETAIL_NS::distance(base, sub.first)
  52. << ", expected " << answer_table[2*i] << ".", charT);
  53. }
  54. if(boost::BOOST_REGEX_DETAIL_NS::distance(base, sub.second) != answer_table[1+ 2*i])
  55. {
  56. BOOST_REGEX_TEST_ERROR(
  57. "Error in end location of sub-expression "
  58. << i << ", found " << boost::BOOST_REGEX_DETAIL_NS::distance(base, sub.second)
  59. << ", expected " << answer_table[1 + 2*i] << ".", charT);
  60. }
  61. }
  62. #ifdef BOOST_MSVC
  63. #pragma warning(pop)
  64. #endif
  65. }
  66. template <class BidirectionalIterator, class Allocator>
  67. void test_result(const boost::match_results<BidirectionalIterator, Allocator>& what, BidirectionalIterator base, const int* answer_table)
  68. {
  69. for(unsigned i = 0; i < what.size(); ++i)
  70. {
  71. test_sub_match(what[i], base, answer_table, i);
  72. }
  73. }
  74. template<class charT, class traits>
  75. void test_simple_search(boost::basic_regex<charT, traits>& r)
  76. {
  77. typedef typename std::basic_string<charT>::const_iterator const_iterator;
  78. const std::basic_string<charT>& search_text = test_info<charT>::search_text();
  79. boost::regex_constants::match_flag_type opts = test_info<charT>::match_options();
  80. const int* answer_table = test_info<charT>::answer_table();
  81. boost::match_results<const_iterator> what;
  82. if(boost::regex_search(
  83. search_text.begin(),
  84. search_text.end(),
  85. what,
  86. r,
  87. opts))
  88. {
  89. test_result(what, search_text.begin(), answer_table);
  90. // setting match_any should have no effect on the result returned:
  91. if(!boost::regex_search(
  92. search_text.begin(),
  93. search_text.end(),
  94. r,
  95. opts|boost::regex_constants::match_any))
  96. {
  97. BOOST_REGEX_TEST_ERROR("Expected match was not found when using the match_any flag.", charT);
  98. }
  99. }
  100. else
  101. {
  102. if(answer_table[0] >= 0)
  103. {
  104. // we should have had a match but didn't:
  105. BOOST_REGEX_TEST_ERROR("Expected match was not found.", charT);
  106. }
  107. // setting match_any should have no effect on the result returned:
  108. else if(boost::regex_search(
  109. search_text.begin(),
  110. search_text.end(),
  111. r,
  112. opts|boost::regex_constants::match_any))
  113. {
  114. BOOST_REGEX_TEST_ERROR("Unexpected match was found when using the match_any flag.", charT);
  115. }
  116. }
  117. }
  118. template<class charT, class traits>
  119. void test_regex_iterator(boost::basic_regex<charT, traits>& r)
  120. {
  121. typedef typename std::basic_string<charT>::const_iterator const_iterator;
  122. typedef boost::regex_iterator<const_iterator, charT, traits> test_iterator;
  123. const std::basic_string<charT>& search_text = test_info<charT>::search_text();
  124. boost::regex_constants::match_flag_type opts = test_info<charT>::match_options();
  125. const int* answer_table = test_info<charT>::answer_table();
  126. test_iterator start(search_text.begin(), search_text.end(), r, opts), end;
  127. test_iterator copy(start);
  128. const_iterator last_end = search_text.begin();
  129. while(start != end)
  130. {
  131. if(start != copy)
  132. {
  133. BOOST_REGEX_TEST_ERROR("Failed iterator != comparison.", charT);
  134. }
  135. if(!(start == copy))
  136. {
  137. BOOST_REGEX_TEST_ERROR("Failed iterator == comparison.", charT);
  138. }
  139. test_result(*start, search_text.begin(), answer_table);
  140. // test $` and $' :
  141. if(start->prefix().first != last_end)
  142. {
  143. BOOST_REGEX_TEST_ERROR("Incorrect position for start of $`", charT);
  144. }
  145. if(start->prefix().second != (*start)[0].first)
  146. {
  147. BOOST_REGEX_TEST_ERROR("Incorrect position for end of $`", charT);
  148. }
  149. if(start->prefix().matched != (start->prefix().first != start->prefix().second))
  150. {
  151. BOOST_REGEX_TEST_ERROR("Incorrect position for matched member of $`", charT);
  152. }
  153. if(start->suffix().first != (*start)[0].second)
  154. {
  155. BOOST_REGEX_TEST_ERROR("Incorrect position for start of $'", charT);
  156. }
  157. if(start->suffix().second != search_text.end())
  158. {
  159. BOOST_REGEX_TEST_ERROR("Incorrect position for end of $'", charT);
  160. }
  161. if(start->suffix().matched != (start->suffix().first != start->suffix().second))
  162. {
  163. BOOST_REGEX_TEST_ERROR("Incorrect position for matched member of $'", charT);
  164. }
  165. last_end = (*start)[0].second;
  166. ++start;
  167. ++copy;
  168. // move on the answer table to next set of answers;
  169. if(*answer_table != -2)
  170. while(*answer_table++ != -2){}
  171. }
  172. if(answer_table[0] >= 0)
  173. {
  174. // we should have had a match but didn't:
  175. BOOST_REGEX_TEST_ERROR("Expected match was not found.", charT);
  176. }
  177. }
  178. template<class charT, class traits>
  179. void test_regex_token_iterator(boost::basic_regex<charT, traits>& r)
  180. {
  181. typedef typename std::basic_string<charT>::const_iterator const_iterator;
  182. typedef boost::regex_token_iterator<const_iterator, charT, traits> test_iterator;
  183. const std::basic_string<charT>& search_text = test_info<charT>::search_text();
  184. boost::regex_constants::match_flag_type opts = test_info<charT>::match_options();
  185. const int* answer_table = test_info<charT>::answer_table();
  186. //
  187. // we start by testing sub-expression 0:
  188. //
  189. test_iterator start(search_text.begin(), search_text.end(), r, 0, opts), end;
  190. test_iterator copy(start);
  191. while(start != end)
  192. {
  193. if(start != copy)
  194. {
  195. BOOST_REGEX_TEST_ERROR("Failed iterator != comparison.", charT);
  196. }
  197. if(!(start == copy))
  198. {
  199. BOOST_REGEX_TEST_ERROR("Failed iterator == comparison.", charT);
  200. }
  201. test_sub_match(*start, search_text.begin(), answer_table, 0);
  202. ++start;
  203. ++copy;
  204. // move on the answer table to next set of answers;
  205. if(*answer_table != -2)
  206. while(*answer_table++ != -2){}
  207. }
  208. if(answer_table[0] >= 0)
  209. {
  210. // we should have had a match but didn't:
  211. BOOST_REGEX_TEST_ERROR("Expected match was not found.", charT);
  212. }
  213. //
  214. // and now field spitting:
  215. //
  216. test_iterator start2(search_text.begin(), search_text.end(), r, -1, opts), end2;
  217. test_iterator copy2(start2);
  218. int last_end2 = 0;
  219. answer_table = test_info<charT>::answer_table();
  220. while(start2 != end2)
  221. {
  222. if(start2 != copy2)
  223. {
  224. BOOST_REGEX_TEST_ERROR("Failed iterator != comparison.", charT);
  225. }
  226. if(!(start2 == copy2))
  227. {
  228. BOOST_REGEX_TEST_ERROR("Failed iterator == comparison.", charT);
  229. }
  230. #ifdef BOOST_MSVC
  231. #pragma warning(push)
  232. #pragma warning(disable:4244)
  233. #endif
  234. if(boost::BOOST_REGEX_DETAIL_NS::distance(search_text.begin(), start2->first) != last_end2)
  235. {
  236. BOOST_REGEX_TEST_ERROR(
  237. "Error in location of start of field split, found: "
  238. << boost::BOOST_REGEX_DETAIL_NS::distance(search_text.begin(), start2->first)
  239. << ", expected: "
  240. << last_end2
  241. << ".", charT);
  242. }
  243. int expected_end = static_cast<int>(answer_table[0] < 0 ? search_text.size() : answer_table[0]);
  244. if(boost::BOOST_REGEX_DETAIL_NS::distance(search_text.begin(), start2->second) != expected_end)
  245. {
  246. BOOST_REGEX_TEST_ERROR(
  247. "Error in location of end2 of field split, found: "
  248. << boost::BOOST_REGEX_DETAIL_NS::distance(search_text.begin(), start2->second)
  249. << ", expected: "
  250. << expected_end
  251. << ".", charT);
  252. }
  253. #ifdef BOOST_MSVC
  254. #pragma warning(pop)
  255. #endif
  256. last_end2 = answer_table[1];
  257. ++start2;
  258. ++copy2;
  259. // move on the answer table to next set of answers;
  260. if(*answer_table != -2)
  261. while(*answer_table++ != -2){}
  262. }
  263. if(answer_table[0] >= 0)
  264. {
  265. // we should have had a match but didn't:
  266. BOOST_REGEX_TEST_ERROR("Expected match was not found.", charT);
  267. }
  268. }
  269. template <class charT, class traits>
  270. struct grep_test_predicate
  271. {
  272. typedef typename std::basic_string<charT>::const_iterator test_iter;
  273. grep_test_predicate(test_iter b, const int* a)
  274. : m_base(b), m_table(a)
  275. {}
  276. bool operator()(const boost::match_results<test_iter>& what)
  277. {
  278. test_result(what, m_base, m_table);
  279. // move on the answer table to next set of answers;
  280. if(*m_table != -2)
  281. while(*m_table++ != -2){}
  282. return true;
  283. }
  284. private:
  285. test_iter m_base;
  286. const int* m_table;
  287. };
  288. template<class charT, class traits>
  289. void test_regex_grep(boost::basic_regex<charT, traits>& r)
  290. {
  291. typedef typename std::basic_string<charT>::const_iterator const_iterator;
  292. const std::basic_string<charT>& search_text = test_info<charT>::search_text();
  293. boost::regex_constants::match_flag_type opts = test_info<charT>::match_options();
  294. const int* answer_table = test_info<charT>::answer_table();
  295. grep_test_predicate<charT, traits> pred(search_text.begin(), answer_table);
  296. boost::regex_grep(pred, search_text.begin(), search_text.end(), r, opts);
  297. }
  298. template<class charT, class traits>
  299. void test_regex_match(boost::basic_regex<charT, traits>& r)
  300. {
  301. typedef typename std::basic_string<charT>::const_iterator const_iterator;
  302. const std::basic_string<charT>& search_text = test_info<charT>::search_text();
  303. boost::regex_constants::match_flag_type opts = test_info<charT>::match_options();
  304. const int* answer_table = test_info<charT>::answer_table();
  305. boost::match_results<const_iterator> what;
  306. if(answer_table[0] < 0)
  307. {
  308. if(boost::regex_match(search_text, r, opts))
  309. {
  310. BOOST_REGEX_TEST_ERROR("boost::regex_match found a match when it should not have done so.", charT);
  311. }
  312. }
  313. else
  314. {
  315. if((answer_table[0] > 0) && boost::regex_match(search_text, r, opts))
  316. {
  317. BOOST_REGEX_TEST_ERROR("boost::regex_match found a match when it should not have done so.", charT);
  318. }
  319. else if((answer_table[0] == 0) && (answer_table[1] == static_cast<int>(search_text.size())))
  320. {
  321. if(boost::regex_match(
  322. search_text.begin(),
  323. search_text.end(),
  324. what,
  325. r,
  326. opts))
  327. {
  328. test_result(what, search_text.begin(), answer_table);
  329. }
  330. else if(answer_table[0] >= 0)
  331. {
  332. // we should have had a match but didn't:
  333. BOOST_REGEX_TEST_ERROR("Expected match was not found.", charT);
  334. }
  335. }
  336. }
  337. }
  338. template<class charT, class traits>
  339. void test(boost::basic_regex<charT, traits>& r, const test_regex_search_tag&)
  340. {
  341. const std::basic_string<charT>& expression = test_info<charT>::expression();
  342. boost::regex_constants::syntax_option_type syntax_options = test_info<charT>::syntax_options();
  343. try{
  344. r.assign(expression, syntax_options);
  345. if(r.status())
  346. {
  347. BOOST_REGEX_TEST_ERROR("Expression did not compile when it should have done, error code = " << r.status(), charT);
  348. }
  349. test_simple_search(r);
  350. test_regex_iterator(r);
  351. test_regex_token_iterator(r);
  352. test_regex_grep(r);
  353. test_regex_match(r);
  354. }
  355. catch(const boost::bad_expression& e)
  356. {
  357. BOOST_REGEX_TEST_ERROR("Expression did not compile when it should have done: " << e.what(), charT);
  358. }
  359. catch(const std::runtime_error& r)
  360. {
  361. BOOST_REGEX_TEST_ERROR("Received an unexpected std::runtime_error: " << r.what(), charT);
  362. }
  363. catch(const std::exception& r)
  364. {
  365. BOOST_REGEX_TEST_ERROR("Received an unexpected std::exception: " << r.what(), charT);
  366. }
  367. catch(...)
  368. {
  369. BOOST_REGEX_TEST_ERROR("Received an unexpected exception of unknown type", charT);
  370. }
  371. }
  372. #endif