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)
13 * LOCATION: see http://www.boost.org for most recent version.
15 * VERSION see <boost/version.hpp>
16 * DESCRIPTION: Test code for MFC/ATL strings with Boost.Regex.
20 // We can only build this if we have ATL support:
22 #include <boost/config.hpp>
26 #include <boost/regex/mfc.hpp>
30 #pragma warning(disable:4267)
32 void test_mfc(const char&, const test_regex_search_tag
&)
34 const std::string
& ss
= test_info
<char>::search_text();
35 const std::string
& ss2
= test_info
<char>::expression();
36 CAtlStringA
s(ss
.c_str(), ss
.size());
37 CAtlStringA
s2(ss2
.c_str(), ss2
.size());
38 boost::regex_constants::match_flag_type opts
= test_info
<char>::match_options();
39 const int* answer_table
= test_info
<char>::answer_table();
40 boost::regex r
= boost::make_regex(s2
, test_info
<char>::syntax_options());
42 if(boost::regex_search(
48 test_result(what
, s
.GetString(), answer_table
);
50 else if(answer_table
[0] >= 0)
52 // we should have had a match but didn't:
53 BOOST_REGEX_TEST_ERROR("Expected match was not found.", char);
58 if(answer_table
[0] < 0)
60 if(boost::regex_match(s
, r
, opts
))
62 BOOST_REGEX_TEST_ERROR("boost::regex_match found a match when it should not have done so.", char);
67 if((answer_table
[0] > 0) && boost::regex_match(s
, r
, opts
))
69 BOOST_REGEX_TEST_ERROR("boost::regex_match found a match when it should not have done so.", char);
71 else if((answer_table
[0] == 0) && (answer_table
[1] == static_cast<int>(ss
.size())))
73 if(boost::regex_match(
79 test_result(what
, s
.GetString(), answer_table
);
80 if(!boost::regex_match(s
, r
, opts
))
82 // we should have had a match but didn't:
83 BOOST_REGEX_TEST_ERROR("Expected match was not found.", char);
86 else if(answer_table
[0] >= 0)
88 // we should have had a match but didn't:
89 BOOST_REGEX_TEST_ERROR("Expected match was not found.", char);
94 // test regex_iterator:
96 boost::cregex_iterator
start(boost::make_regex_iterator(s
, r
, opts
)), end
;
97 boost::cregex_iterator
copy(start
);
102 BOOST_REGEX_TEST_ERROR("Failed iterator != comparison.", char);
106 BOOST_REGEX_TEST_ERROR("Failed iterator == comparison.", char);
108 test_result(*start
, s
.GetString(), answer_table
);
111 // move on the answer table to next set of answers;
112 if(*answer_table
!= -2)
113 while(*answer_table
++ != -2){}
115 if(answer_table
[0] >= 0)
117 // we should have had a match but didn't:
118 BOOST_REGEX_TEST_ERROR("Expected match was not found.", char);
121 // test regex_token_iterator:
123 typedef boost::regex_token_iterator
<const char*> token_iterator
;
124 answer_table
= test_info
<char>::answer_table();
126 // we start by testing sub-expression 0:
128 token_iterator
tstart(boost::make_regex_token_iterator(s
, r
, 0, opts
)), tend
;
129 token_iterator
tcopy(tstart
);
130 while(tstart
!= tend
)
134 BOOST_REGEX_TEST_ERROR("Failed iterator != comparison.", char);
136 if(!(tstart
== tcopy
))
138 BOOST_REGEX_TEST_ERROR("Failed iterator == comparison.", char);
140 test_sub_match(*tstart
, s
.GetString(), answer_table
, 0);
143 // move on the answer table to next set of answers;
144 if(*answer_table
!= -2)
145 while(*answer_table
++ != -2){}
147 if(answer_table
[0] >= 0)
149 // we should have had a match but didn't:
150 BOOST_REGEX_TEST_ERROR("Expected match was not found.", char);
153 // and now field spitting:
155 token_iterator
tstart2(boost::make_regex_token_iterator(s
, r
, -1, opts
)), tend2
;
156 token_iterator
tcopy2(tstart2
);
158 answer_table
= test_info
<char>::answer_table();
159 while(tstart2
!= tend2
)
161 if(tstart2
!= tcopy2
)
163 BOOST_REGEX_TEST_ERROR("Failed iterator != comparison.", char);
165 if(!(tstart2
== tcopy2
))
167 BOOST_REGEX_TEST_ERROR("Failed iterator == comparison.", char);
170 #pragma warning(push)
171 #pragma warning(disable:4244)
173 if(std::distance(s
.GetString(), tstart2
->first
) != last_end2
)
175 BOOST_REGEX_TEST_ERROR(
176 "Error in location of start of field split, found: "
177 << std::distance(s
.GetString(), tstart2
->first
)
182 int expected_end
= static_cast<int>(answer_table
[0] < 0 ? s
.GetLength() : answer_table
[0]);
183 if(std::distance(s
.GetString(), tstart2
->second
) != expected_end
)
185 BOOST_REGEX_TEST_ERROR(
186 "Error in location of end2 of field split, found: "
187 << std::distance(s
.GetString(), tstart2
->second
)
195 last_end2
= answer_table
[1];
198 // move on the answer table to next set of answers;
199 if(*answer_table
!= -2)
200 while(*answer_table
++ != -2){}
202 if(answer_table
[0] >= 0)
204 // we should have had a match but didn't:
205 BOOST_REGEX_TEST_ERROR("Expected match was not found.", char);
210 void test_mfc(const wchar_t&, const test_regex_search_tag
&)
212 const std::wstring
& ss
= test_info
<wchar_t>::search_text();
213 const std::wstring
& ss2
= test_info
<wchar_t>::expression();
214 CAtlStringW
s(ss
.c_str(), ss
.size());
215 CAtlStringW
s2(ss2
.c_str(), ss2
.size());
216 boost::regex_constants::match_flag_type opts
= test_info
<wchar_t>::match_options();
217 const int* answer_table
= test_info
<wchar_t>::answer_table();
218 boost::wregex r
= boost::make_regex(s2
, test_info
<wchar_t>::syntax_options());
220 if(boost::regex_search(
226 test_result(what
, s
.GetString(), answer_table
);
228 else if(answer_table
[0] >= 0)
230 // we should have had a match but didn't:
231 BOOST_REGEX_TEST_ERROR("Expected match was not found.", wchar_t);
234 // regex_match tests:
236 if(answer_table
[0] < 0)
238 if(boost::regex_match(s
, r
, opts
))
240 BOOST_REGEX_TEST_ERROR("boost::regex_match found a match when it should not have done so.", wchar_t);
245 if((answer_table
[0] > 0) && boost::regex_match(s
, r
, opts
))
247 BOOST_REGEX_TEST_ERROR("boost::regex_match found a match when it should not have done so.", wchar_t);
249 else if((answer_table
[0] == 0) && (answer_table
[1] == static_cast<int>(ss
.size())))
251 if(boost::regex_match(
257 test_result(what
, s
.GetString(), answer_table
);
258 if(!boost::regex_match(s
, r
, opts
))
260 // we should have had a match but didn't:
261 BOOST_REGEX_TEST_ERROR("Expected match was not found.", wchar_t);
264 else if(answer_table
[0] >= 0)
266 // we should have had a match but didn't:
267 BOOST_REGEX_TEST_ERROR("Expected match was not found.", wchar_t);
272 // test regex_iterator:
274 boost::wcregex_iterator
start(boost::make_regex_iterator(s
, r
, opts
)), end
;
275 boost::wcregex_iterator
copy(start
);
280 BOOST_REGEX_TEST_ERROR("Failed iterator != comparison.", wchar_t);
284 BOOST_REGEX_TEST_ERROR("Failed iterator == comparison.", wchar_t);
286 test_result(*start
, s
.GetString(), answer_table
);
289 // move on the answer table to next set of answers;
290 if(*answer_table
!= -2)
291 while(*answer_table
++ != -2){}
293 if(answer_table
[0] >= 0)
295 // we should have had a match but didn't:
296 BOOST_REGEX_TEST_ERROR("Expected match was not found.", wchar_t);
299 // test regex_token_iterator:
301 typedef boost::regex_token_iterator
<const wchar_t*> token_iterator
;
302 answer_table
= test_info
<wchar_t>::answer_table();
304 // we start by testing sub-expression 0:
306 token_iterator
tstart(boost::make_regex_token_iterator(s
, r
, 0, opts
)), tend
;
307 token_iterator
tcopy(tstart
);
308 while(tstart
!= tend
)
312 BOOST_REGEX_TEST_ERROR("Failed iterator != comparison.", wchar_t);
314 if(!(tstart
== tcopy
))
316 BOOST_REGEX_TEST_ERROR("Failed iterator == comparison.", wchar_t);
318 test_sub_match(*tstart
, s
.GetString(), answer_table
, 0);
321 // move on the answer table to next set of answers;
322 if(*answer_table
!= -2)
323 while(*answer_table
++ != -2){}
325 if(answer_table
[0] >= 0)
327 // we should have had a match but didn't:
328 BOOST_REGEX_TEST_ERROR("Expected match was not found.", wchar_t);
331 // and now field spitting:
333 token_iterator
tstart2(boost::make_regex_token_iterator(s
, r
, -1, opts
)), tend2
;
334 token_iterator
tcopy2(tstart2
);
336 answer_table
= test_info
<wchar_t>::answer_table();
337 while(tstart2
!= tend2
)
339 if(tstart2
!= tcopy2
)
341 BOOST_REGEX_TEST_ERROR("Failed iterator != comparison.", wchar_t);
343 if(!(tstart2
== tcopy2
))
345 BOOST_REGEX_TEST_ERROR("Failed iterator == comparison.", wchar_t);
348 #pragma warning(push)
349 #pragma warning(disable:4244)
351 if(std::distance(s
.GetString(), tstart2
->first
) != last_end2
)
353 BOOST_REGEX_TEST_ERROR(
354 "Error in location of start of field split, found: "
355 << std::distance(s
.GetString(), tstart2
->first
)
360 int expected_end
= static_cast<int>(answer_table
[0] < 0 ? s
.GetLength() : answer_table
[0]);
361 if(std::distance(s
.GetString(), tstart2
->second
) != expected_end
)
363 BOOST_REGEX_TEST_ERROR(
364 "Error in location of end2 of field split, found: "
365 << std::distance(s
.GetString(), tstart2
->second
)
373 last_end2
= answer_table
[1];
376 // move on the answer table to next set of answers;
377 if(*answer_table
!= -2)
378 while(*answer_table
++ != -2){}
380 if(answer_table
[0] >= 0)
382 // we should have had a match but didn't:
383 BOOST_REGEX_TEST_ERROR("Expected match was not found.", wchar_t);
388 void test_mfc(const char&, const test_invalid_regex_tag
&)
390 std::string ss
= test_info
<char>::expression();
391 CAtlStringA
s(ss
.c_str(), ss
.size());
392 bool have_catch
= false;
394 boost::regex e
= boost::make_regex(s
, test_info
<char>::syntax_options());
398 catch(const boost::bad_expression
&)
402 catch(const std::runtime_error
& r
)
405 BOOST_REGEX_TEST_ERROR("Expected a bad_expression exception, but a std::runtime_error instead: " << r
.what(), char);
407 catch(const std::exception
& r
)
410 BOOST_REGEX_TEST_ERROR("Expected a bad_expression exception, but a std::exception instead: " << r
.what(), char);
415 BOOST_REGEX_TEST_ERROR("Expected a bad_expression exception, but got an exception of unknown type instead", char);
419 // oops expected exception was not thrown:
420 BOOST_REGEX_TEST_ERROR("Expected an exception, but didn't find one.", char);
425 void test_mfc(const wchar_t&, const test_invalid_regex_tag
&)
427 std::wstring ss
= test_info
<wchar_t>::expression();
428 CAtlStringW
s(ss
.c_str(), ss
.size());
429 bool have_catch
= false;
431 boost::wregex e
= boost::make_regex(s
, test_info
<wchar_t>::syntax_options());
435 catch(const boost::bad_expression
&)
439 catch(const std::runtime_error
& r
)
442 BOOST_REGEX_TEST_ERROR("Expected a bad_expression exception, but a std::runtime_error instead: " << r
.what(), wchar_t);
444 catch(const std::exception
& r
)
447 BOOST_REGEX_TEST_ERROR("Expected a bad_expression exception, but a std::exception instead: " << r
.what(), wchar_t);
452 BOOST_REGEX_TEST_ERROR("Expected a bad_expression exception, but got an exception of unknown type instead", wchar_t);
456 // oops expected exception was not thrown:
457 BOOST_REGEX_TEST_ERROR("Expected an exception, but didn't find one.", wchar_t);
461 void test_mfc(const char&, const test_regex_replace_tag
&)
463 const CStringA
expression(test_info
<char>::expression().c_str(), test_info
<char>::expression().size());
464 boost::regex_constants::syntax_option_type syntax_options
= test_info
<char>::syntax_options();
466 boost::regex r
= boost::make_regex(expression
, syntax_options
);
469 BOOST_REGEX_TEST_ERROR("Expression did not compile when it should have done, error code = " << r
.status(), char);
471 const CStringA
search_text(test_info
<char>::search_text().c_str(), test_info
<char>::search_text().size());
472 boost::regex_constants::match_flag_type opts
= test_info
<char>::match_options();
473 const CStringA
format_string(test_info
<char>::format_string().c_str(), test_info
<char>::format_string().size());
474 const CStringA
result_string(test_info
<char>::result_string().c_str(), test_info
<char>::result_string().size());
476 CStringA result
= boost::regex_replace(search_text
, r
, format_string
, opts
);
477 if(result
!= result_string
)
479 BOOST_REGEX_TEST_ERROR("regex_replace generated an incorrect string result", char);
482 catch(const boost::bad_expression
& e
)
484 BOOST_REGEX_TEST_ERROR("Expression did not compile when it should have done: " << e
.what(), char);
486 catch(const std::runtime_error
& r
)
488 BOOST_REGEX_TEST_ERROR("Received an unexpected std::runtime_error: " << r
.what(), char);
490 catch(const std::exception
& r
)
492 BOOST_REGEX_TEST_ERROR("Received an unexpected std::exception: " << r
.what(), char);
496 BOOST_REGEX_TEST_ERROR("Received an unexpected exception of unknown type", char);
500 void test_mfc(const wchar_t&, const test_regex_replace_tag
&)
502 const CStringW
expression(test_info
<wchar_t>::expression().c_str(), test_info
<wchar_t>::expression().size());
503 boost::regex_constants::syntax_option_type syntax_options
= test_info
<wchar_t>::syntax_options();
505 boost::wregex r
= boost::make_regex(expression
, syntax_options
);
508 BOOST_REGEX_TEST_ERROR("Expression did not compile when it should have done, error code = " << r
.status(), wchar_t);
510 const CStringW
search_text(test_info
<wchar_t>::search_text().c_str(), test_info
<wchar_t>::search_text().size());
511 boost::regex_constants::match_flag_type opts
= test_info
<wchar_t>::match_options();
512 const CStringW
format_string(test_info
<wchar_t>::format_string().c_str(), test_info
<wchar_t>::format_string().size());
513 const CStringW
result_string(test_info
<wchar_t>::result_string().c_str(), test_info
<wchar_t>::result_string().size());
515 CStringW result
= boost::regex_replace(search_text
, r
, format_string
, opts
);
516 if(result
!= result_string
)
518 BOOST_REGEX_TEST_ERROR("regex_replace generated an incorrect string result", wchar_t);
521 catch(const boost::bad_expression
& e
)
523 BOOST_REGEX_TEST_ERROR("Expression did not compile when it should have done: " << e
.what(), wchar_t);
525 catch(const std::runtime_error
& r
)
527 BOOST_REGEX_TEST_ERROR("Received an unexpected std::runtime_error: " << r
.what(), wchar_t);
529 catch(const std::exception
& r
)
531 BOOST_REGEX_TEST_ERROR("Received an unexpected std::exception: " << r
.what(), wchar_t);
535 BOOST_REGEX_TEST_ERROR("Received an unexpected exception of unknown type", wchar_t);
543 void test_mfc(const char&, const test_regex_search_tag
&){}
544 void test_mfc(const wchar_t&, const test_regex_search_tag
&){}
545 void test_mfc(const char&, const test_invalid_regex_tag
&){}
546 void test_mfc(const wchar_t&, const test_invalid_regex_tag
&){}
547 void test_mfc(const char&, const test_regex_replace_tag
&){}
548 void test_mfc(const wchar_t&, const test_regex_replace_tag
&){}