test_regex_search.hpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  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_SEARCH_HPP
  18. #define BOOST_REGEX_REGRESS_REGEX_SEARCH_HPP
  19. #include "info.hpp"
  20. #ifdef TEST_ROPE
  21. #include <rope>
  22. #endif
  23. //
  24. // this file implements a test for a regular expression that should compile,
  25. // followed by a search for that expression:
  26. //
  27. struct test_regex_search_tag{};
  28. template <class BidirectionalIterator>
  29. void test_sub_match(const boost::sub_match<BidirectionalIterator>& sub, BidirectionalIterator base, const int* answer_table, int i, bool recurse = true)
  30. {
  31. #ifdef BOOST_MSVC
  32. #pragma warning(push)
  33. #pragma warning(disable:4244)
  34. #endif
  35. if(recurse)
  36. {
  37. boost::sub_match<BidirectionalIterator> copy(sub);
  38. test_sub_match(copy, base, answer_table, i, false);
  39. }
  40. typedef typename boost::sub_match<BidirectionalIterator>::value_type charT;
  41. if((sub.matched == 0)
  42. &&
  43. !((i == 0)
  44. && (test_info<charT>::match_options() & boost::match_partial)) )
  45. {
  46. if(answer_table[2*i] >= 0)
  47. {
  48. BOOST_REGEX_TEST_ERROR(
  49. "Sub-expression " << i
  50. << " was not matched when it should have been.", charT);
  51. }
  52. }
  53. else
  54. {
  55. if(boost::BOOST_REGEX_DETAIL_NS::distance(base, sub.first) != answer_table[2*i])
  56. {
  57. BOOST_REGEX_TEST_ERROR(
  58. "Error in start location of sub-expression "
  59. << i << ", found " << boost::BOOST_REGEX_DETAIL_NS::distance(base, sub.first)
  60. << ", expected " << answer_table[2*i] << ".", charT);
  61. }
  62. if(boost::BOOST_REGEX_DETAIL_NS::distance(base, sub.second) != answer_table[1+ 2*i])
  63. {
  64. BOOST_REGEX_TEST_ERROR(
  65. "Error in end location of sub-expression "
  66. << i << ", found " << boost::BOOST_REGEX_DETAIL_NS::distance(base, sub.second)
  67. << ", expected " << answer_table[1 + 2*i] << ".", charT);
  68. }
  69. }
  70. #ifdef BOOST_MSVC
  71. #pragma warning(pop)
  72. #endif
  73. }
  74. template <class BidirectionalIterator, class Allocator>
  75. void test_result(const boost::match_results<BidirectionalIterator, Allocator>& what, BidirectionalIterator base, const int* answer_table, bool recurse = true)
  76. {
  77. if(recurse)
  78. {
  79. boost::match_results<BidirectionalIterator, Allocator> copy(what);
  80. test_result(copy, base, answer_table, false);
  81. boost::match_results<BidirectionalIterator, Allocator> s;
  82. s.swap(copy);
  83. test_result(s, base, answer_table, false);
  84. boost::match_results<BidirectionalIterator, Allocator> s2;
  85. s2 = what;
  86. test_result(s2, base, answer_table, false);
  87. }
  88. for(unsigned i = 0; i < what.size(); ++i)
  89. {
  90. test_sub_match(what[i], base, answer_table, i);
  91. }
  92. }
  93. template<class charT, class traits>
  94. void test_simple_search(boost::basic_regex<charT, traits>& r)
  95. {
  96. typedef typename std::basic_string<charT>::const_iterator const_iterator;
  97. const std::basic_string<charT>& search_text = test_info<charT>::search_text();
  98. boost::regex_constants::match_flag_type opts = test_info<charT>::match_options();
  99. const int* answer_table = test_info<charT>::answer_table();
  100. boost::match_results<const_iterator> what;
  101. if(boost::regex_search(
  102. search_text.begin(),
  103. search_text.end(),
  104. what,
  105. r,
  106. opts))
  107. {
  108. test_result(what, search_text.begin(), answer_table);
  109. // setting match_any should have no effect on the result returned:
  110. if(!boost::regex_search(
  111. search_text.begin(),
  112. search_text.end(),
  113. r,
  114. opts|boost::regex_constants::match_any))
  115. {
  116. BOOST_REGEX_TEST_ERROR("Expected match was not found when using the match_any flag.", charT);
  117. }
  118. }
  119. else
  120. {
  121. if(answer_table[0] >= 0)
  122. {
  123. // we should have had a match but didn't:
  124. BOOST_REGEX_TEST_ERROR("Expected match was not found.", charT);
  125. }
  126. // setting match_any should have no effect on the result returned:
  127. else if(boost::regex_search(
  128. search_text.begin(),
  129. search_text.end(),
  130. r,
  131. opts|boost::regex_constants::match_any))
  132. {
  133. BOOST_REGEX_TEST_ERROR("Unexpected match was found when using the match_any flag.", charT);
  134. }
  135. }
  136. #ifdef TEST_ROPE
  137. std::rope<charT> rsearch_text;
  138. for(unsigned i = 0; i < search_text.size(); ++i)
  139. {
  140. std::rope<charT> c(search_text[i]);
  141. if(++i != search_text.size())
  142. {
  143. c.append(search_text[i]);
  144. if(++i != search_text.size())
  145. {
  146. c.append(search_text[i]);
  147. }
  148. }
  149. rsearch_text.append(c);
  150. }
  151. boost::match_results<std::rope<charT>::const_iterator> rwhat;
  152. if(boost::regex_search(
  153. rsearch_text.begin(),
  154. rsearch_text.end(),
  155. rwhat,
  156. r,
  157. opts))
  158. {
  159. test_result(rwhat, rsearch_text.begin(), answer_table);
  160. }
  161. else
  162. {
  163. if(answer_table[0] >= 0)
  164. {
  165. // we should have had a match but didn't:
  166. BOOST_REGEX_TEST_ERROR("Expected match was not found.", charT);
  167. }
  168. }
  169. #endif
  170. }
  171. template<class charT, class traits>
  172. void test_regex_iterator(boost::basic_regex<charT, traits>& r)
  173. {
  174. typedef typename std::basic_string<charT>::const_iterator const_iterator;
  175. typedef boost::regex_iterator<const_iterator, charT, traits> test_iterator;
  176. const std::basic_string<charT>& search_text = test_info<charT>::search_text();
  177. boost::regex_constants::match_flag_type opts = test_info<charT>::match_options();
  178. const int* answer_table = test_info<charT>::answer_table();
  179. test_iterator start(search_text.begin(), search_text.end(), r, opts), end;
  180. test_iterator copy(start);
  181. const_iterator last_end = search_text.begin();
  182. while(start != end)
  183. {
  184. if(start != copy)
  185. {
  186. BOOST_REGEX_TEST_ERROR("Failed iterator != comparison.", charT);
  187. }
  188. if(!(start == copy))
  189. {
  190. BOOST_REGEX_TEST_ERROR("Failed iterator == comparison.", charT);
  191. }
  192. test_result(*start, search_text.begin(), answer_table);
  193. // test $` and $' :
  194. if(start->prefix().first != last_end)
  195. {
  196. BOOST_REGEX_TEST_ERROR("Incorrect position for start of $`", charT);
  197. }
  198. if(start->prefix().second != (*start)[0].first)
  199. {
  200. BOOST_REGEX_TEST_ERROR("Incorrect position for end of $`", charT);
  201. }
  202. if(start->prefix().matched != (start->prefix().first != start->prefix().second))
  203. {
  204. BOOST_REGEX_TEST_ERROR("Incorrect position for matched member of $`", charT);
  205. }
  206. if(start->suffix().first != (*start)[0].second)
  207. {
  208. BOOST_REGEX_TEST_ERROR("Incorrect position for start of $'", charT);
  209. }
  210. if(start->suffix().second != search_text.end())
  211. {
  212. BOOST_REGEX_TEST_ERROR("Incorrect position for end of $'", charT);
  213. }
  214. if(start->suffix().matched != (start->suffix().first != start->suffix().second))
  215. {
  216. BOOST_REGEX_TEST_ERROR("Incorrect position for matched member of $'", charT);
  217. }
  218. last_end = (*start)[0].second;
  219. ++start;
  220. ++copy;
  221. // move on the answer table to next set of answers;
  222. if(*answer_table != -2)
  223. while(*answer_table++ != -2){}
  224. }
  225. if(answer_table[0] >= 0)
  226. {
  227. // we should have had a match but didn't:
  228. BOOST_REGEX_TEST_ERROR("Expected match was not found.", charT);
  229. }
  230. }
  231. template<class charT, class traits>
  232. void test_regex_token_iterator(boost::basic_regex<charT, traits>& r)
  233. {
  234. typedef typename std::basic_string<charT>::const_iterator const_iterator;
  235. typedef boost::regex_token_iterator<const_iterator, charT, traits> test_iterator;
  236. const std::basic_string<charT>& search_text = test_info<charT>::search_text();
  237. boost::regex_constants::match_flag_type opts = test_info<charT>::match_options();
  238. const int* answer_table = test_info<charT>::answer_table();
  239. //
  240. // we start by testing sub-expression 0:
  241. //
  242. test_iterator start(search_text.begin(), search_text.end(), r, 0, opts), end;
  243. test_iterator copy(start);
  244. while(start != end)
  245. {
  246. if(start != copy)
  247. {
  248. BOOST_REGEX_TEST_ERROR("Failed iterator != comparison.", charT);
  249. }
  250. if(!(start == copy))
  251. {
  252. BOOST_REGEX_TEST_ERROR("Failed iterator == comparison.", charT);
  253. }
  254. test_sub_match(*start, search_text.begin(), answer_table, 0);
  255. ++start;
  256. ++copy;
  257. // move on the answer table to next set of answers;
  258. if(*answer_table != -2)
  259. while(*answer_table++ != -2){}
  260. }
  261. if(answer_table[0] >= 0)
  262. {
  263. // we should have had a match but didn't:
  264. BOOST_REGEX_TEST_ERROR("Expected match was not found.", charT);
  265. }
  266. //
  267. // and now field spitting:
  268. //
  269. test_iterator start2(search_text.begin(), search_text.end(), r, -1, opts), end2;
  270. test_iterator copy2(start2);
  271. int last_end2 = 0;
  272. answer_table = test_info<charT>::answer_table();
  273. while(start2 != end2)
  274. {
  275. if(start2 != copy2)
  276. {
  277. BOOST_REGEX_TEST_ERROR("Failed iterator != comparison.", charT);
  278. }
  279. if(!(start2 == copy2))
  280. {
  281. BOOST_REGEX_TEST_ERROR("Failed iterator == comparison.", charT);
  282. }
  283. #ifdef BOOST_MSVC
  284. #pragma warning(push)
  285. #pragma warning(disable:4244)
  286. #endif
  287. if(boost::BOOST_REGEX_DETAIL_NS::distance(search_text.begin(), start2->first) != last_end2)
  288. {
  289. BOOST_REGEX_TEST_ERROR(
  290. "Error in location of start of field split, found: "
  291. << boost::BOOST_REGEX_DETAIL_NS::distance(search_text.begin(), start2->first)
  292. << ", expected: "
  293. << last_end2
  294. << ".", charT);
  295. }
  296. int expected_end = static_cast<int>(answer_table[0] < 0 ? search_text.size() : answer_table[0]);
  297. if(boost::BOOST_REGEX_DETAIL_NS::distance(search_text.begin(), start2->second) != expected_end)
  298. {
  299. BOOST_REGEX_TEST_ERROR(
  300. "Error in location of end2 of field split, found: "
  301. << boost::BOOST_REGEX_DETAIL_NS::distance(search_text.begin(), start2->second)
  302. << ", expected: "
  303. << expected_end
  304. << ".", charT);
  305. }
  306. #ifdef BOOST_MSVC
  307. #pragma warning(pop)
  308. #endif
  309. last_end2 = answer_table[1];
  310. ++start2;
  311. ++copy2;
  312. // move on the answer table to next set of answers;
  313. if(*answer_table != -2)
  314. while(*answer_table++ != -2){}
  315. }
  316. if(answer_table[0] >= 0)
  317. {
  318. // we should have had a match but didn't:
  319. BOOST_REGEX_TEST_ERROR("Expected match was not found.", charT);
  320. }
  321. #if !BOOST_WORKAROUND(BOOST_MSVC, < 1300)
  322. //
  323. // and now both field splitting and $0:
  324. //
  325. std::vector<int> subs;
  326. subs.push_back(-1);
  327. subs.push_back(0);
  328. start2 = test_iterator(search_text.begin(), search_text.end(), r, subs, opts);
  329. copy2 = start2;
  330. last_end2 = 0;
  331. answer_table = test_info<charT>::answer_table();
  332. while(start2 != end2)
  333. {
  334. if(start2 != copy2)
  335. {
  336. BOOST_REGEX_TEST_ERROR("Failed iterator != comparison.", charT);
  337. }
  338. if(!(start2 == copy2))
  339. {
  340. BOOST_REGEX_TEST_ERROR("Failed iterator == comparison.", charT);
  341. }
  342. #ifdef BOOST_MSVC
  343. #pragma warning(push)
  344. #pragma warning(disable:4244)
  345. #endif
  346. if(boost::BOOST_REGEX_DETAIL_NS::distance(search_text.begin(), start2->first) != last_end2)
  347. {
  348. BOOST_REGEX_TEST_ERROR(
  349. "Error in location of start of field split, found: "
  350. << boost::BOOST_REGEX_DETAIL_NS::distance(search_text.begin(), start2->first)
  351. << ", expected: "
  352. << last_end2
  353. << ".", charT);
  354. }
  355. int expected_end = static_cast<int>(answer_table[0] < 0 ? search_text.size() : answer_table[0]);
  356. if(boost::BOOST_REGEX_DETAIL_NS::distance(search_text.begin(), start2->second) != expected_end)
  357. {
  358. BOOST_REGEX_TEST_ERROR(
  359. "Error in location of end2 of field split, found: "
  360. << boost::BOOST_REGEX_DETAIL_NS::distance(search_text.begin(), start2->second)
  361. << ", expected: "
  362. << expected_end
  363. << ".", charT);
  364. }
  365. #ifdef BOOST_MSVC
  366. #pragma warning(pop)
  367. #endif
  368. last_end2 = answer_table[1];
  369. ++start2;
  370. ++copy2;
  371. if((start2 == end2) && (answer_table[0] >= 0))
  372. {
  373. BOOST_REGEX_TEST_ERROR(
  374. "Expected $0 match not found", charT);
  375. }
  376. if(start2 != end2)
  377. {
  378. test_sub_match(*start2, search_text.begin(), answer_table, 0);
  379. ++start2;
  380. ++copy2;
  381. }
  382. // move on the answer table to next set of answers;
  383. if(*answer_table != -2)
  384. while(*answer_table++ != -2){}
  385. }
  386. if(answer_table[0] >= 0)
  387. {
  388. // we should have had a match but didn't:
  389. BOOST_REGEX_TEST_ERROR("Expected match was not found.", charT);
  390. }
  391. #endif
  392. }
  393. template <class charT, class traits>
  394. struct grep_test_predicate
  395. {
  396. typedef typename std::basic_string<charT>::const_iterator test_iter;
  397. grep_test_predicate(test_iter b, const int* a)
  398. : m_base(b), m_table(a)
  399. {}
  400. bool operator()(const boost::match_results<test_iter>& what)
  401. {
  402. test_result(what, m_base, m_table);
  403. // move on the answer table to next set of answers;
  404. if(*m_table != -2)
  405. while(*m_table++ != -2){}
  406. return true;
  407. }
  408. private:
  409. test_iter m_base;
  410. const int* m_table;
  411. };
  412. template<class charT, class traits>
  413. void test_regex_grep(boost::basic_regex<charT, traits>& r)
  414. {
  415. //typedef typename std::basic_string<charT>::const_iterator const_iterator;
  416. const std::basic_string<charT>& search_text = test_info<charT>::search_text();
  417. boost::regex_constants::match_flag_type opts = test_info<charT>::match_options();
  418. const int* answer_table = test_info<charT>::answer_table();
  419. grep_test_predicate<charT, traits> pred(search_text.begin(), answer_table);
  420. boost::regex_grep(pred, search_text.begin(), search_text.end(), r, opts);
  421. }
  422. template<class charT, class traits>
  423. void test_regex_match(boost::basic_regex<charT, traits>& r)
  424. {
  425. typedef typename std::basic_string<charT>::const_iterator const_iterator;
  426. const std::basic_string<charT>& search_text = test_info<charT>::search_text();
  427. boost::regex_constants::match_flag_type opts = test_info<charT>::match_options();
  428. const int* answer_table = test_info<charT>::answer_table();
  429. boost::match_results<const_iterator> what;
  430. if(answer_table[0] < 0)
  431. {
  432. if(boost::regex_match(search_text, r, opts))
  433. {
  434. BOOST_REGEX_TEST_ERROR("boost::regex_match found a match when it should not have done so.", charT);
  435. }
  436. }
  437. else
  438. {
  439. if((answer_table[0] > 0) && boost::regex_match(search_text, r, opts))
  440. {
  441. BOOST_REGEX_TEST_ERROR("boost::regex_match found a match when it should not have done so.", charT);
  442. }
  443. else if((answer_table[0] == 0) && (answer_table[1] == static_cast<int>(search_text.size())))
  444. {
  445. if(boost::regex_match(
  446. search_text.begin(),
  447. search_text.end(),
  448. what,
  449. r,
  450. opts))
  451. {
  452. test_result(what, search_text.begin(), answer_table);
  453. }
  454. else if(answer_table[0] >= 0)
  455. {
  456. // we should have had a match but didn't:
  457. BOOST_REGEX_TEST_ERROR("Expected match was not found.", charT);
  458. }
  459. }
  460. }
  461. }
  462. template<class charT, class traits>
  463. void test(boost::basic_regex<charT, traits>& r, const test_regex_search_tag&)
  464. {
  465. const std::basic_string<charT>& expression = test_info<charT>::expression();
  466. boost::regex_constants::syntax_option_type syntax_options = test_info<charT>::syntax_options();
  467. #ifndef BOOST_NO_EXCEPTIONS
  468. try
  469. #endif
  470. {
  471. r.assign(expression, syntax_options);
  472. if(r.status())
  473. {
  474. BOOST_REGEX_TEST_ERROR("Expression did not compile when it should have done, error code = " << r.status(), charT);
  475. }
  476. if(expression != std::basic_string<charT>(r.begin(), r.end()))
  477. {
  478. BOOST_REGEX_TEST_ERROR("Stored expression string was incorrect", charT);
  479. }
  480. test_simple_search(r);
  481. test_regex_iterator(r);
  482. test_regex_token_iterator(r);
  483. test_regex_grep(r);
  484. test_regex_match(r);
  485. //
  486. // Verify sub-expression locations:
  487. //
  488. #ifndef BOOST_NO_EXCEPTIONS
  489. if((syntax_options & boost::regbase::save_subexpression_location) == 0)
  490. {
  491. bool have_except = false;
  492. try
  493. {
  494. r.subexpression(1);
  495. }
  496. catch(const std::out_of_range&)
  497. {
  498. have_except = true;
  499. }
  500. if(!have_except)
  501. {
  502. BOOST_REGEX_TEST_ERROR("Expected std::out_of_range error was not found.", charT);
  503. }
  504. }
  505. #endif
  506. r.assign(expression, syntax_options | boost::regbase::save_subexpression_location);
  507. for(std::size_t i = 0; i < r.mark_count(); ++i)
  508. {
  509. std::pair<const charT*, const charT*> p = r.subexpression(i);
  510. if(*p.first != '(')
  511. {
  512. BOOST_REGEX_TEST_ERROR("Starting location of sub-expression " << i << " iterator was invalid.", charT);
  513. }
  514. if(*p.second != ')')
  515. {
  516. BOOST_REGEX_TEST_ERROR("Ending location of sub-expression " << i << " iterator was invalid.", charT);
  517. }
  518. }
  519. }
  520. #ifndef BOOST_NO_EXCEPTIONS
  521. catch(const boost::bad_expression& e)
  522. {
  523. BOOST_REGEX_TEST_ERROR("Expression did not compile when it should have done: " << e.what(), charT);
  524. }
  525. catch(const std::runtime_error& e)
  526. {
  527. BOOST_REGEX_TEST_ERROR("Received an unexpected std::runtime_error: " << e.what(), charT);
  528. }
  529. catch(const std::exception& e)
  530. {
  531. BOOST_REGEX_TEST_ERROR("Received an unexpected std::exception: " << e.what(), charT);
  532. }
  533. catch(...)
  534. {
  535. BOOST_REGEX_TEST_ERROR("Received an unexpected exception of unknown type", charT);
  536. }
  537. #endif
  538. }
  539. #endif