regress.ipp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. //////////////////////////////////////////////////////////////////////////////
  2. // regress.ipp
  3. //
  4. // (C) Copyright Eric Niebler 2004.
  5. // Use, modification and distribution are subject to the
  6. // Boost Software License, Version 1.0. (See accompanying file
  7. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  8. /*
  9. Revision history:
  10. 7 March 2004 : Initial version.
  11. */
  12. #include <locale>
  13. #include <vector>
  14. #include <string>
  15. #include <fstream>
  16. #include <iostream>
  17. #include <boost/lexical_cast.hpp>
  18. #include <boost/xpressive/xpressive.hpp>
  19. #include <boost/test/unit_test.hpp>
  20. #if defined(_MSC_VER) && defined(_DEBUG)
  21. # define _CRTDBG_MAP_ALLOC
  22. # include <crtdbg.h>
  23. #endif
  24. #if defined(BOOST_XPRESSIVE_TEST_WREGEX) && !defined(BOOST_XPRESSIVE_NO_WREGEX)
  25. namespace std
  26. {
  27. inline std::ostream &operator <<(std::ostream &sout, std::wstring const &wstr)
  28. {
  29. for(std::size_t n = 0; n < wstr.size(); ++n)
  30. sout.put(BOOST_USE_FACET(std::ctype<wchar_t>, std::locale()).narrow(wstr[n], '?'));
  31. return sout;
  32. }
  33. }
  34. #endif
  35. #define BOOST_XPR_CHECK(pred) \
  36. if(pred) {} else { BOOST_ERROR(case_ << #pred); }
  37. using namespace boost::unit_test;
  38. using namespace boost::xpressive;
  39. //////////////////////////////////////////////////////////////////////////////
  40. // xpr_test_case
  41. template<typename Char>
  42. struct xpr_test_case
  43. {
  44. typedef std::basic_string<Char> string_type;
  45. std::string section;
  46. string_type str;
  47. string_type pat;
  48. string_type sub;
  49. string_type res;
  50. regex_constants::syntax_option_type syntax_flags;
  51. regex_constants::match_flag_type match_flags;
  52. std::vector<string_type> br;
  53. xpr_test_case()
  54. {
  55. this->reset();
  56. }
  57. void reset()
  58. {
  59. this->section.clear();
  60. this->str.clear();
  61. this->pat.clear();
  62. this->sub.clear();
  63. this->res.clear();
  64. this->br.clear();
  65. this->syntax_flags = regex_constants::ECMAScript;
  66. this->match_flags = regex_constants::match_default | regex_constants::format_first_only;
  67. }
  68. };
  69. //////////////////////////////////////////////////////////////////////////////
  70. // globals
  71. std::ifstream in;
  72. unsigned int test_count = 0;
  73. // The global object that contains the current test case
  74. xpr_test_case<char> test;
  75. struct test_case_formatter
  76. {
  77. friend std::ostream &operator <<(std::ostream &sout, test_case_formatter)
  78. {
  79. sout << test.section << " /" << test.pat << "/ : ";
  80. return sout;
  81. }
  82. };
  83. test_case_formatter const case_ = {};
  84. #if defined(BOOST_XPRESSIVE_TEST_WREGEX) && !defined(BOOST_XPRESSIVE_NO_WREGEX)
  85. ///////////////////////////////////////////////////////////////////////////////
  86. // widen
  87. // make a std::wstring from a std::string by widening according to the
  88. // current ctype<char> facet
  89. inline std::wstring widen(std::string const &str)
  90. {
  91. std::ctype<char> const &ct = BOOST_USE_FACET(std::ctype<char>, std::locale());
  92. std::wstring res;
  93. for(size_t i=0; i<str.size(); ++i)
  94. {
  95. res += ct.widen(str[i]);
  96. }
  97. return res;
  98. }
  99. ///////////////////////////////////////////////////////////////////////////////
  100. // widen
  101. // widens an entire test case
  102. xpr_test_case<wchar_t> widen(xpr_test_case<char> const &test)
  103. {
  104. xpr_test_case<wchar_t> wtest;
  105. wtest.section = test.section;
  106. wtest.str = ::widen(test.str);
  107. wtest.pat = ::widen(test.pat);
  108. wtest.sub = ::widen(test.sub);
  109. wtest.res = ::widen(test.res);
  110. wtest.syntax_flags = test.syntax_flags;
  111. wtest.match_flags = test.match_flags;
  112. wtest.br.reserve(test.br.size());
  113. for(std::size_t i = 0; i < test.br.size(); ++i)
  114. {
  115. wtest.br.push_back(::widen(test.br[i]));
  116. }
  117. return wtest;
  118. }
  119. #endif // BOOST_XPRESSIVE_NO_WREGEX
  120. std::string escape(std::string str)
  121. {
  122. for(std::string::size_type pos = 0; std::string::npos != (pos = str.find('\\', pos)); ++pos)
  123. {
  124. if(pos + 1 == str.size())
  125. break;
  126. switch(str[pos + 1])
  127. {
  128. case '\\': str.replace(pos, 2, "\\"); break;
  129. case 'n': str.replace(pos, 2, "\n"); break;
  130. case 'r': str.replace(pos, 2, "\r"); break;
  131. }
  132. }
  133. return str;
  134. }
  135. ///////////////////////////////////////////////////////////////////////////////
  136. // get_test
  137. // read the next section out of the input file, and fill out
  138. // the global variables
  139. bool get_test()
  140. {
  141. test.reset();
  142. bool first = true;
  143. std::string line;
  144. smatch what;
  145. sregex const rx_sec = '[' >> (s1= +_) >> ']';
  146. sregex const rx_str = "str=" >> (s1= *_);
  147. sregex const rx_pat = "pat=" >> (s1= *_);
  148. sregex const rx_flg = "flg=" >> (s1= *_);
  149. sregex const rx_sub = "sub=" >> (s1= *_);
  150. sregex const rx_res = "res=" >> (s1= *_);
  151. sregex const rx_br = "br" >> (s1= +digit) >> '=' >> (s2= *_);
  152. while(in.good())
  153. {
  154. std::getline(in, line);
  155. if(!line.empty() && '\r' == line[line.size()-1])
  156. {
  157. line.erase(line.size()-1);
  158. }
  159. if(regex_match(line, what, rx_sec))
  160. {
  161. if(!first)
  162. {
  163. if(what[1] != "end")
  164. {
  165. BOOST_FAIL(("invalid input : " + line).c_str());
  166. }
  167. break;
  168. }
  169. first = false;
  170. test.section = what[1].str();
  171. }
  172. else if(regex_match(line, what, rx_str))
  173. {
  174. test.str = escape(what[1].str());
  175. }
  176. else if(regex_match(line, what, rx_pat))
  177. {
  178. test.pat = what[1].str();
  179. }
  180. else if(regex_match(line, what, rx_sub))
  181. {
  182. test.sub = what[1].str();
  183. }
  184. else if(regex_match(line, what, rx_res))
  185. {
  186. test.res = escape(what[1].str());
  187. }
  188. else if(regex_match(line, what, rx_flg))
  189. {
  190. std::string flg = what[1].str();
  191. if(std::string::npos != flg.find('i'))
  192. {
  193. test.syntax_flags = test.syntax_flags | regex_constants::icase;
  194. }
  195. if(std::string::npos == flg.find('m'))
  196. {
  197. test.syntax_flags = test.syntax_flags | regex_constants::single_line;
  198. }
  199. if(std::string::npos == flg.find('s'))
  200. {
  201. test.syntax_flags = test.syntax_flags | regex_constants::not_dot_newline;
  202. }
  203. if(std::string::npos != flg.find('x'))
  204. {
  205. test.syntax_flags = test.syntax_flags | regex_constants::ignore_white_space;
  206. }
  207. if(std::string::npos != flg.find('g'))
  208. {
  209. test.match_flags = test.match_flags & ~regex_constants::format_first_only;
  210. }
  211. if(std::string::npos != flg.find('a'))
  212. {
  213. test.match_flags = test.match_flags | regex_constants::format_all;
  214. }
  215. if(std::string::npos != flg.find('p'))
  216. {
  217. test.match_flags = test.match_flags | regex_constants::format_perl;
  218. }
  219. if(std::string::npos != flg.find('d'))
  220. {
  221. test.match_flags = test.match_flags | regex_constants::format_sed;
  222. }
  223. }
  224. else if(regex_match(line, what, rx_br))
  225. {
  226. std::size_t nbr = boost::lexical_cast<std::size_t>(what[1].str());
  227. if(nbr >= test.br.size())
  228. {
  229. test.br.resize(nbr + 1);
  230. }
  231. test.br[nbr] = escape(what[2].str());
  232. }
  233. else if(!line.empty() && ';' != line[0])
  234. {
  235. BOOST_FAIL((std::string("invalid input : ") + line).c_str());
  236. }
  237. }
  238. return !first;
  239. }
  240. ///////////////////////////////////////////////////////////////////////////////
  241. // run_test_impl
  242. // run the test
  243. template<typename Char>
  244. void run_test_impl(xpr_test_case<Char> const &test)
  245. {
  246. try
  247. {
  248. Char const empty[] = {0};
  249. typedef typename std::basic_string<Char>::const_iterator iterator;
  250. basic_regex<iterator> rx = basic_regex<iterator>::compile(test.pat, test.syntax_flags);
  251. // Build the same regex for use with C strings
  252. basic_regex<Char const *> c_rx = basic_regex<Char const *>::compile(test.pat, test.syntax_flags);
  253. if(!test.res.empty())
  254. {
  255. // test regex_replace
  256. std::basic_string<Char> res = regex_replace(test.str, rx, test.sub, test.match_flags);
  257. BOOST_CHECK_MESSAGE(res == test.res, case_ << res << " != " << test.res );
  258. // test regex_replace with NTBS format string
  259. std::basic_string<Char> res2 = regex_replace(test.str, rx, test.sub.c_str(), test.match_flags);
  260. BOOST_CHECK_MESSAGE(res2 == test.res, case_ << res2 << " != " << test.res );
  261. // test regex_replace with NTBS input string
  262. std::basic_string<Char> res3 = regex_replace(test.str.c_str(), c_rx, test.sub, test.match_flags);
  263. BOOST_CHECK_MESSAGE(res3 == test.res, case_ << res3 << " != " << test.res );
  264. // test regex_replace with NTBS input string and NTBS format string
  265. std::basic_string<Char> res4 = regex_replace(test.str.c_str(), c_rx, test.sub.c_str(), test.match_flags);
  266. BOOST_CHECK_MESSAGE(res4 == test.res, case_ << res4 << " != " << test.res );
  267. }
  268. if(0 == (test.match_flags & regex_constants::format_first_only))
  269. {
  270. {
  271. // global search, use regex_iterator
  272. std::vector<sub_match<iterator> > br;
  273. regex_iterator<iterator> begin(test.str.begin(), test.str.end(), rx, test.match_flags), end;
  274. for(; begin != end; ++begin)
  275. {
  276. match_results<iterator> const &what = *begin;
  277. br.insert(br.end(), what.begin(), what.end());
  278. }
  279. // match succeeded: was it expected to succeed?
  280. BOOST_XPR_CHECK(br.size() == test.br.size());
  281. for(std::size_t i = 0; i < br.size() && i < test.br.size(); ++i)
  282. {
  283. BOOST_XPR_CHECK((!br[i].matched && test.br[i] == empty) || test.br[i] == br[i].str());
  284. }
  285. }
  286. {
  287. // global search, use regex_token_iterator
  288. std::vector<typename sub_match<iterator>::string_type> br2;
  289. std::vector<int> subs(rx.mark_count() + 1, 0);
  290. // regex_token_iterator will extract all sub_matches, in order:
  291. for(std::size_t i = 0; i < subs.size(); ++i)
  292. {
  293. subs[i] = static_cast<int>(i);
  294. }
  295. regex_token_iterator<iterator> begin2(test.str.begin(), test.str.end(), rx, subs, test.match_flags), end2;
  296. for(; begin2 != end2; ++begin2)
  297. {
  298. br2.push_back(*begin2);
  299. }
  300. // match succeeded: was it expected to succeed?
  301. BOOST_XPR_CHECK(br2.size() == test.br.size());
  302. for(std::size_t i = 0; i < br2.size() && i < test.br.size(); ++i)
  303. {
  304. BOOST_XPR_CHECK(test.br[i] == br2[i]);
  305. }
  306. }
  307. }
  308. else
  309. {
  310. // test regex_search
  311. match_results<iterator> what;
  312. if(regex_search(test.str, what, rx, test.match_flags))
  313. {
  314. // match succeeded: was it expected to succeed?
  315. BOOST_XPR_CHECK(what.size() == test.br.size());
  316. for(std::size_t i = 0; i < what.size() && i < test.br.size(); ++i)
  317. {
  318. BOOST_XPR_CHECK((!what[i].matched && test.br[i] == empty) || test.br[i] == what[i].str());
  319. }
  320. }
  321. else
  322. {
  323. // match failed: was it expected to fail?
  324. BOOST_XPR_CHECK(0 == test.br.size());
  325. }
  326. }
  327. }
  328. catch(regex_error const &e)
  329. {
  330. BOOST_ERROR(case_ << e.what());
  331. }
  332. }
  333. ///////////////////////////////////////////////////////////////////////////////
  334. // run_test_impl
  335. // run the current test
  336. void run_test()
  337. {
  338. #ifdef BOOST_XPRESSIVE_TEST_WREGEX
  339. xpr_test_case<wchar_t> wtest = ::widen(test);
  340. run_test_impl(wtest);
  341. #else
  342. run_test_impl(test);
  343. #endif
  344. }
  345. static char const * s_argv1;
  346. ///////////////////////////////////////////////////////////////////////////////
  347. // open_test
  348. bool open_test()
  349. {
  350. in.open( s_argv1? s_argv1: "regress.txt" );
  351. return in.good();
  352. }
  353. ///////////////////////////////////////////////////////////////////////////////
  354. // test_main
  355. // read the tests from the input file and execute them
  356. void test_main()
  357. {
  358. #if !defined(BOOST_XPRESSIVE_TEST_WREGEX) || !defined(BOOST_XPRESSIVE_NO_WREGEX)
  359. if(!open_test())
  360. {
  361. BOOST_ERROR("Error: unable to open input file.");
  362. }
  363. while(get_test())
  364. {
  365. run_test();
  366. ++test_count;
  367. }
  368. #endif
  369. std::cout << test_count << " tests completed." << std::endl;
  370. }
  371. ///////////////////////////////////////////////////////////////////////////////
  372. // init_unit_test_suite
  373. //
  374. test_suite* init_unit_test_suite( int argc, char* argv[] )
  375. {
  376. s_argv1 = argv[1];
  377. test_suite *test = BOOST_TEST_SUITE("basic regression test");
  378. test->add(BOOST_TEST_CASE(&test_main));
  379. return test;
  380. }
  381. ///////////////////////////////////////////////////////////////////////////////
  382. // debug_init
  383. static const struct debug_init
  384. {
  385. debug_init()
  386. {
  387. #if defined(_MSC_VER) && defined(_DEBUG)
  388. // Send warnings, errors and asserts to STDERR
  389. _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG);
  390. _CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDERR);
  391. _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG);
  392. _CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDERR);
  393. _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG);
  394. _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);
  395. // Check for leaks at program termination
  396. _CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF | _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG));
  397. //_CrtSetBreakAlloc(221);
  398. #endif
  399. }
  400. } g_debug_init;