1 //////////////////////////////////////////////////////////////////////////////
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)
11 7 March 2004 : Initial version.
19 #include <boost/lexical_cast.hpp>
20 #include <boost/xpressive/xpressive.hpp>
21 #include <boost/test/unit_test.hpp>
23 #if defined(_MSC_VER) && defined(_DEBUG)
24 # define _CRTDBG_MAP_ALLOC
28 #if defined(BOOST_XPRESSIVE_TEST_WREGEX) && !defined(BOOST_XPRESSIVE_NO_WREGEX)
31 inline std::ostream &operator <<(std::ostream &sout, std::wstring const &wstr)
33 for(std::size_t n = 0; n < wstr.size(); ++n)
34 sout.put(BOOST_USE_FACET(std::ctype<wchar_t>, std::locale()).narrow(wstr[n], '?'));
40 #define BOOST_XPR_CHECK(pred) \
41 if(pred) {} else { BOOST_ERROR(case_ << #pred); }
43 using namespace boost::unit_test;
44 using namespace boost::xpressive;
46 //////////////////////////////////////////////////////////////////////////////
48 template<typename Char>
51 typedef std::basic_string<Char> string_type;
57 regex_constants::syntax_option_type syntax_flags;
58 regex_constants::match_flag_type match_flags;
59 std::vector<string_type> br;
68 this->section.clear();
74 this->syntax_flags = regex_constants::ECMAScript;
75 this->match_flags = regex_constants::match_default | regex_constants::format_first_only;
79 //////////////////////////////////////////////////////////////////////////////
82 unsigned int test_count = 0;
84 // The global object that contains the current test case
85 xpr_test_case<char> test;
87 struct test_case_formatter
89 friend std::ostream &operator <<(std::ostream &sout, test_case_formatter)
91 sout << test.section << " /" << test.pat << "/ : ";
96 test_case_formatter const case_ = {};
98 #if defined(BOOST_XPRESSIVE_TEST_WREGEX) && !defined(BOOST_XPRESSIVE_NO_WREGEX)
99 ///////////////////////////////////////////////////////////////////////////////
101 // make a std::wstring from a std::string by widening according to the
102 // current ctype<char> facet
103 inline std::wstring widen(std::string const &str)
105 std::ctype<char> const &ct = BOOST_USE_FACET(std::ctype<char>, std::locale());
107 for(size_t i=0; i<str.size(); ++i)
109 res += ct.widen(str[i]);
114 ///////////////////////////////////////////////////////////////////////////////
116 // widens an entire test case
117 xpr_test_case<wchar_t> widen(xpr_test_case<char> const &test)
119 xpr_test_case<wchar_t> wtest;
120 wtest.section = test.section;
121 wtest.str = ::widen(test.str);
122 wtest.pat = ::widen(test.pat);
123 wtest.sub = ::widen(test.sub);
124 wtest.res = ::widen(test.res);
125 wtest.syntax_flags = test.syntax_flags;
126 wtest.match_flags = test.match_flags;
127 wtest.br.reserve(test.br.size());
128 for(std::size_t i = 0; i < test.br.size(); ++i)
130 wtest.br.push_back(::widen(test.br[i]));
134 #endif // BOOST_XPRESSIVE_NO_WREGEX
136 std::string escape(std::string str)
138 for(std::string::size_type pos = 0; std::string::npos != (pos = str.find('\\', pos)); ++pos)
140 if(pos + 1 == str.size())
145 case '\\': str.replace(pos, 2, "\\"); break;
146 case 'n': str.replace(pos, 2, "\n"); break;
147 case 'r': str.replace(pos, 2, "\r"); break;
153 ///////////////////////////////////////////////////////////////////////////////
155 // read the next section out of the input file, and fill out
156 // the global variables
164 sregex const rx_sec = '[' >> (s1= +_) >> ']';
165 sregex const rx_str = "str=" >> (s1= *_);
166 sregex const rx_pat = "pat=" >> (s1= *_);
167 sregex const rx_flg = "flg=" >> (s1= *_);
168 sregex const rx_sub = "sub=" >> (s1= *_);
169 sregex const rx_res = "res=" >> (s1= *_);
170 sregex const rx_br = "br" >> (s1= +digit) >> '=' >> (s2= *_);
174 std::getline(in, line);
176 if(!line.empty() && '\r' == line[line.size()-1])
178 line.erase(line.size()-1);
181 if(regex_match(line, what, rx_sec))
187 BOOST_FAIL(("invalid input : " + line).c_str());
193 test.section = what[1].str();
195 else if(regex_match(line, what, rx_str))
197 test.str = escape(what[1].str());
199 else if(regex_match(line, what, rx_pat))
201 test.pat = what[1].str();
203 else if(regex_match(line, what, rx_sub))
205 test.sub = what[1].str();
207 else if(regex_match(line, what, rx_res))
209 test.res = escape(what[1].str());
211 else if(regex_match(line, what, rx_flg))
213 std::string flg = what[1].str();
215 if(std::string::npos != flg.find('i'))
217 test.syntax_flags = test.syntax_flags | regex_constants::icase;
219 if(std::string::npos == flg.find('m'))
221 test.syntax_flags = test.syntax_flags | regex_constants::single_line;
223 if(std::string::npos == flg.find('s'))
225 test.syntax_flags = test.syntax_flags | regex_constants::not_dot_newline;
227 if(std::string::npos != flg.find('x'))
229 test.syntax_flags = test.syntax_flags | regex_constants::ignore_white_space;
231 if(std::string::npos != flg.find('g'))
233 test.match_flags = test.match_flags & ~regex_constants::format_first_only;
235 if(std::string::npos != flg.find('a'))
237 test.match_flags = test.match_flags | regex_constants::format_all;
239 if(std::string::npos != flg.find('p'))
241 test.match_flags = test.match_flags | regex_constants::format_perl;
243 if(std::string::npos != flg.find('d'))
245 test.match_flags = test.match_flags | regex_constants::format_sed;
248 else if(regex_match(line, what, rx_br))
250 std::size_t nbr = boost::lexical_cast<std::size_t>(what[1].str());
252 if(nbr >= test.br.size())
254 test.br.resize(nbr + 1);
257 test.br[nbr] = escape(what[2].str());
259 else if(!line.empty() && ';' != line[0])
261 BOOST_FAIL((std::string("invalid input : ") + line).c_str());
268 ///////////////////////////////////////////////////////////////////////////////
271 template<typename Char>
272 void run_test_impl(xpr_test_case<Char> const &test)
276 Char const empty[] = {0};
277 typedef typename std::basic_string<Char>::const_iterator iterator;
278 basic_regex<iterator> rx = basic_regex<iterator>::compile(test.pat, test.syntax_flags);
280 // Build the same regex for use with C strings
281 basic_regex<Char const *> c_rx = basic_regex<Char const *>::compile(test.pat, test.syntax_flags);
283 if(!test.res.empty())
285 // test regex_replace
286 std::basic_string<Char> res = regex_replace(test.str, rx, test.sub, test.match_flags);
287 BOOST_CHECK_MESSAGE(res == test.res, case_ << res << " != " << test.res );
289 // test regex_replace with NTBS format string
290 std::basic_string<Char> res2 = regex_replace(test.str, rx, test.sub.c_str(), test.match_flags);
291 BOOST_CHECK_MESSAGE(res2 == test.res, case_ << res2 << " != " << test.res );
293 // test regex_replace with NTBS input string
294 std::basic_string<Char> res3 = regex_replace(test.str.c_str(), c_rx, test.sub, test.match_flags);
295 BOOST_CHECK_MESSAGE(res3 == test.res, case_ << res3 << " != " << test.res );
297 // test regex_replace with NTBS input string and NTBS format string
298 std::basic_string<Char> res4 = regex_replace(test.str.c_str(), c_rx, test.sub.c_str(), test.match_flags);
299 BOOST_CHECK_MESSAGE(res4 == test.res, case_ << res4 << " != " << test.res );
302 if(0 == (test.match_flags & regex_constants::format_first_only))
305 // global search, use regex_iterator
306 std::vector<sub_match<iterator> > br;
307 regex_iterator<iterator> begin(test.str.begin(), test.str.end(), rx, test.match_flags), end;
308 for(; begin != end; ++begin)
310 match_results<iterator> const &what = *begin;
311 br.insert(br.end(), what.begin(), what.end());
314 // match succeeded: was it expected to succeed?
315 BOOST_XPR_CHECK(br.size() == test.br.size());
317 for(std::size_t i = 0; i < br.size() && i < test.br.size(); ++i)
319 BOOST_XPR_CHECK((!br[i].matched && test.br[i] == empty) || test.br[i] == br[i].str());
324 // global search, use regex_token_iterator
325 std::vector<typename sub_match<iterator>::string_type> br2;
326 std::vector<int> subs(rx.mark_count() + 1, 0);
327 // regex_token_iterator will extract all sub_matches, in order:
328 for(std::size_t i = 0; i < subs.size(); ++i)
330 subs[i] = static_cast<int>(i);
332 regex_token_iterator<iterator> begin2(test.str.begin(), test.str.end(), rx, subs, test.match_flags), end2;
333 for(; begin2 != end2; ++begin2)
335 br2.push_back(*begin2);
338 // match succeeded: was it expected to succeed?
339 BOOST_XPR_CHECK(br2.size() == test.br.size());
341 for(std::size_t i = 0; i < br2.size() && i < test.br.size(); ++i)
343 BOOST_XPR_CHECK(test.br[i] == br2[i]);
350 match_results<iterator> what;
351 if(regex_search(test.str, what, rx, test.match_flags))
353 // match succeeded: was it expected to succeed?
354 BOOST_XPR_CHECK(what.size() == test.br.size());
356 for(std::size_t i = 0; i < what.size() && i < test.br.size(); ++i)
358 BOOST_XPR_CHECK((!what[i].matched && test.br[i] == empty) || test.br[i] == what[i].str());
363 // match failed: was it expected to fail?
364 BOOST_XPR_CHECK(0 == test.br.size());
368 catch(regex_error const &e)
370 BOOST_ERROR(case_ << e.what());
374 ///////////////////////////////////////////////////////////////////////////////
376 // run the current test
379 #if defined(BOOST_XPRESSIVE_TEST_WREGEX) && !defined(BOOST_XPRESSIVE_NO_WREGEX)
380 xpr_test_case<wchar_t> wtest = ::widen(test);
381 run_test_impl(wtest);
387 static char const * s_argv1;
389 ///////////////////////////////////////////////////////////////////////////////
393 in.open( s_argv1? s_argv1: "regress.txt" );
397 ///////////////////////////////////////////////////////////////////////////////
399 // read the tests from the input file and execute them
402 #if !defined(BOOST_XPRESSIVE_TEST_WREGEX) || !defined(BOOST_XPRESSIVE_NO_WREGEX)
405 BOOST_ERROR("Error: unable to open input file.");
415 std::cout << test_count << " tests completed." << std::endl;
418 ///////////////////////////////////////////////////////////////////////////////
419 // init_unit_test_suite
421 test_suite* init_unit_test_suite( int argc, char* argv[] )
425 test_suite *test = BOOST_TEST_SUITE("basic regression test");
426 test->add(BOOST_TEST_CASE(&test_main));
430 ///////////////////////////////////////////////////////////////////////////////
432 static const struct debug_init
436 #if defined(_MSC_VER) && defined(_DEBUG)
437 // Send warnings, errors and asserts to STDERR
438 _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG);
439 _CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDERR);
440 _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG);
441 _CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDERR);
442 _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG);
443 _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);
445 // Check for leaks at program termination
446 _CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF | _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG));
448 //_CrtSetBreakAlloc(221);