test_mfc.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  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_mfc.cpp
  14. * VERSION see <boost/version.hpp>
  15. * DESCRIPTION: Test code for MFC/ATL strings with Boost.Regex.
  16. */
  17. //
  18. // We can only build this if we have ATL support:
  19. //
  20. #include <boost/config.hpp>
  21. #ifdef TEST_MFC
  22. #include <boost/regex/mfc.hpp>
  23. #include "test.hpp"
  24. #include "atlstr.h"
  25. #pragma warning(disable:4267)
  26. void test_mfc(const char&, const test_regex_search_tag&)
  27. {
  28. const std::string& ss = test_info<char>::search_text();
  29. const std::string& ss2 = test_info<char>::expression();
  30. CAtlStringA s(ss.c_str(), ss.size());
  31. CAtlStringA s2(ss2.c_str(), ss2.size());
  32. boost::regex_constants::match_flag_type opts = test_info<char>::match_options();
  33. const int* answer_table = test_info<char>::answer_table();
  34. boost::regex r = boost::make_regex(s2, test_info<char>::syntax_options());
  35. boost::cmatch what;
  36. if(boost::regex_search(
  37. s,
  38. what,
  39. r,
  40. opts))
  41. {
  42. test_result(what, s.GetString(), answer_table);
  43. }
  44. else if(answer_table[0] >= 0)
  45. {
  46. // we should have had a match but didn't:
  47. BOOST_REGEX_TEST_ERROR("Expected match was not found.", char);
  48. }
  49. //
  50. // regex_match tests:
  51. //
  52. if(answer_table[0] < 0)
  53. {
  54. if(boost::regex_match(s, r, opts))
  55. {
  56. BOOST_REGEX_TEST_ERROR("boost::regex_match found a match when it should not have done so.", char);
  57. }
  58. }
  59. else
  60. {
  61. if((answer_table[0] > 0) && boost::regex_match(s, r, opts))
  62. {
  63. BOOST_REGEX_TEST_ERROR("boost::regex_match found a match when it should not have done so.", char);
  64. }
  65. else if((answer_table[0] == 0) && (answer_table[1] == static_cast<int>(ss.size())))
  66. {
  67. if(boost::regex_match(
  68. s,
  69. what,
  70. r,
  71. opts))
  72. {
  73. test_result(what, s.GetString(), answer_table);
  74. if(!boost::regex_match(s, r, opts))
  75. {
  76. // we should have had a match but didn't:
  77. BOOST_REGEX_TEST_ERROR("Expected match was not found.", char);
  78. }
  79. }
  80. else if(answer_table[0] >= 0)
  81. {
  82. // we should have had a match but didn't:
  83. BOOST_REGEX_TEST_ERROR("Expected match was not found.", char);
  84. }
  85. }
  86. }
  87. //
  88. // test regex_iterator:
  89. //
  90. boost::cregex_iterator start(boost::make_regex_iterator(s, r, opts)), end;
  91. boost::cregex_iterator copy(start);
  92. while(start != end)
  93. {
  94. if(start != copy)
  95. {
  96. BOOST_REGEX_TEST_ERROR("Failed iterator != comparison.", char);
  97. }
  98. if(!(start == copy))
  99. {
  100. BOOST_REGEX_TEST_ERROR("Failed iterator == comparison.", char);
  101. }
  102. test_result(*start, s.GetString(), answer_table);
  103. ++start;
  104. ++copy;
  105. // move on the answer table to next set of answers;
  106. if(*answer_table != -2)
  107. while(*answer_table++ != -2){}
  108. }
  109. if(answer_table[0] >= 0)
  110. {
  111. // we should have had a match but didn't:
  112. BOOST_REGEX_TEST_ERROR("Expected match was not found.", char);
  113. }
  114. //
  115. // test regex_token_iterator:
  116. //
  117. typedef boost::regex_token_iterator<const char*> token_iterator;
  118. answer_table = test_info<char>::answer_table();
  119. //
  120. // we start by testing sub-expression 0:
  121. //
  122. token_iterator tstart(boost::make_regex_token_iterator(s, r, 0, opts)), tend;
  123. token_iterator tcopy(tstart);
  124. while(tstart != tend)
  125. {
  126. if(tstart != tcopy)
  127. {
  128. BOOST_REGEX_TEST_ERROR("Failed iterator != comparison.", char);
  129. }
  130. if(!(tstart == tcopy))
  131. {
  132. BOOST_REGEX_TEST_ERROR("Failed iterator == comparison.", char);
  133. }
  134. test_sub_match(*tstart, s.GetString(), answer_table, 0);
  135. ++tstart;
  136. ++tcopy;
  137. // move on the answer table to next set of answers;
  138. if(*answer_table != -2)
  139. while(*answer_table++ != -2){}
  140. }
  141. if(answer_table[0] >= 0)
  142. {
  143. // we should have had a match but didn't:
  144. BOOST_REGEX_TEST_ERROR("Expected match was not found.", char);
  145. }
  146. //
  147. // and now field spitting:
  148. //
  149. token_iterator tstart2(boost::make_regex_token_iterator(s, r, -1, opts)), tend2;
  150. token_iterator tcopy2(tstart2);
  151. int last_end2 = 0;
  152. answer_table = test_info<char>::answer_table();
  153. while(tstart2 != tend2)
  154. {
  155. if(tstart2 != tcopy2)
  156. {
  157. BOOST_REGEX_TEST_ERROR("Failed iterator != comparison.", char);
  158. }
  159. if(!(tstart2 == tcopy2))
  160. {
  161. BOOST_REGEX_TEST_ERROR("Failed iterator == comparison.", char);
  162. }
  163. #ifdef BOOST_MSVC
  164. #pragma warning(push)
  165. #pragma warning(disable:4244)
  166. #endif
  167. if(boost::BOOST_REGEX_DETAIL_NS::distance(s.GetString(), tstart2->first) != last_end2)
  168. {
  169. BOOST_REGEX_TEST_ERROR(
  170. "Error in location of start of field split, found: "
  171. << boost::BOOST_REGEX_DETAIL_NS::distance(s.GetString(), tstart2->first)
  172. << ", expected: "
  173. << last_end2
  174. << ".", char);
  175. }
  176. int expected_end = static_cast<int>(answer_table[0] < 0 ? s.GetLength() : answer_table[0]);
  177. if(boost::BOOST_REGEX_DETAIL_NS::distance(s.GetString(), tstart2->second) != expected_end)
  178. {
  179. BOOST_REGEX_TEST_ERROR(
  180. "Error in location of end2 of field split, found: "
  181. << boost::BOOST_REGEX_DETAIL_NS::distance(s.GetString(), tstart2->second)
  182. << ", expected: "
  183. << expected_end
  184. << ".", char);
  185. }
  186. #ifdef BOOST_MSVC
  187. #pragma warning(pop)
  188. #endif
  189. last_end2 = answer_table[1];
  190. ++tstart2;
  191. ++tcopy2;
  192. // move on the answer table to next set of answers;
  193. if(*answer_table != -2)
  194. while(*answer_table++ != -2){}
  195. }
  196. if(answer_table[0] >= 0)
  197. {
  198. // we should have had a match but didn't:
  199. BOOST_REGEX_TEST_ERROR("Expected match was not found.", char);
  200. }
  201. }
  202. void test_mfc(const wchar_t&, const test_regex_search_tag&)
  203. {
  204. const std::wstring& ss = test_info<wchar_t>::search_text();
  205. const std::wstring& ss2 = test_info<wchar_t>::expression();
  206. CAtlStringW s(ss.c_str(), ss.size());
  207. CAtlStringW s2(ss2.c_str(), ss2.size());
  208. boost::regex_constants::match_flag_type opts = test_info<wchar_t>::match_options();
  209. const int* answer_table = test_info<wchar_t>::answer_table();
  210. boost::wregex r = boost::make_regex(s2, test_info<wchar_t>::syntax_options());
  211. boost::wcmatch what;
  212. if(boost::regex_search(
  213. s,
  214. what,
  215. r,
  216. opts))
  217. {
  218. test_result(what, s.GetString(), answer_table);
  219. }
  220. else if(answer_table[0] >= 0)
  221. {
  222. // we should have had a match but didn't:
  223. BOOST_REGEX_TEST_ERROR("Expected match was not found.", wchar_t);
  224. }
  225. //
  226. // regex_match tests:
  227. //
  228. if(answer_table[0] < 0)
  229. {
  230. if(boost::regex_match(s, r, opts))
  231. {
  232. BOOST_REGEX_TEST_ERROR("boost::regex_match found a match when it should not have done so.", wchar_t);
  233. }
  234. }
  235. else
  236. {
  237. if((answer_table[0] > 0) && boost::regex_match(s, r, opts))
  238. {
  239. BOOST_REGEX_TEST_ERROR("boost::regex_match found a match when it should not have done so.", wchar_t);
  240. }
  241. else if((answer_table[0] == 0) && (answer_table[1] == static_cast<int>(ss.size())))
  242. {
  243. if(boost::regex_match(
  244. s,
  245. what,
  246. r,
  247. opts))
  248. {
  249. test_result(what, s.GetString(), answer_table);
  250. if(!boost::regex_match(s, r, opts))
  251. {
  252. // we should have had a match but didn't:
  253. BOOST_REGEX_TEST_ERROR("Expected match was not found.", wchar_t);
  254. }
  255. }
  256. else if(answer_table[0] >= 0)
  257. {
  258. // we should have had a match but didn't:
  259. BOOST_REGEX_TEST_ERROR("Expected match was not found.", wchar_t);
  260. }
  261. }
  262. }
  263. //
  264. // test regex_iterator:
  265. //
  266. boost::wcregex_iterator start(boost::make_regex_iterator(s, r, opts)), end;
  267. boost::wcregex_iterator copy(start);
  268. while(start != end)
  269. {
  270. if(start != copy)
  271. {
  272. BOOST_REGEX_TEST_ERROR("Failed iterator != comparison.", wchar_t);
  273. }
  274. if(!(start == copy))
  275. {
  276. BOOST_REGEX_TEST_ERROR("Failed iterator == comparison.", wchar_t);
  277. }
  278. test_result(*start, s.GetString(), answer_table);
  279. ++start;
  280. ++copy;
  281. // move on the answer table to next set of answers;
  282. if(*answer_table != -2)
  283. while(*answer_table++ != -2){}
  284. }
  285. if(answer_table[0] >= 0)
  286. {
  287. // we should have had a match but didn't:
  288. BOOST_REGEX_TEST_ERROR("Expected match was not found.", wchar_t);
  289. }
  290. //
  291. // test regex_token_iterator:
  292. //
  293. typedef boost::regex_token_iterator<const wchar_t*> token_iterator;
  294. answer_table = test_info<wchar_t>::answer_table();
  295. //
  296. // we start by testing sub-expression 0:
  297. //
  298. token_iterator tstart(boost::make_regex_token_iterator(s, r, 0, opts)), tend;
  299. token_iterator tcopy(tstart);
  300. while(tstart != tend)
  301. {
  302. if(tstart != tcopy)
  303. {
  304. BOOST_REGEX_TEST_ERROR("Failed iterator != comparison.", wchar_t);
  305. }
  306. if(!(tstart == tcopy))
  307. {
  308. BOOST_REGEX_TEST_ERROR("Failed iterator == comparison.", wchar_t);
  309. }
  310. test_sub_match(*tstart, s.GetString(), answer_table, 0);
  311. ++tstart;
  312. ++tcopy;
  313. // move on the answer table to next set of answers;
  314. if(*answer_table != -2)
  315. while(*answer_table++ != -2){}
  316. }
  317. if(answer_table[0] >= 0)
  318. {
  319. // we should have had a match but didn't:
  320. BOOST_REGEX_TEST_ERROR("Expected match was not found.", wchar_t);
  321. }
  322. //
  323. // and now field spitting:
  324. //
  325. token_iterator tstart2(boost::make_regex_token_iterator(s, r, -1, opts)), tend2;
  326. token_iterator tcopy2(tstart2);
  327. int last_end2 = 0;
  328. answer_table = test_info<wchar_t>::answer_table();
  329. while(tstart2 != tend2)
  330. {
  331. if(tstart2 != tcopy2)
  332. {
  333. BOOST_REGEX_TEST_ERROR("Failed iterator != comparison.", wchar_t);
  334. }
  335. if(!(tstart2 == tcopy2))
  336. {
  337. BOOST_REGEX_TEST_ERROR("Failed iterator == comparison.", wchar_t);
  338. }
  339. #ifdef BOOST_MSVC
  340. #pragma warning(push)
  341. #pragma warning(disable:4244)
  342. #endif
  343. if(boost::BOOST_REGEX_DETAIL_NS::distance(s.GetString(), tstart2->first) != last_end2)
  344. {
  345. BOOST_REGEX_TEST_ERROR(
  346. "Error in location of start of field split, found: "
  347. << boost::BOOST_REGEX_DETAIL_NS::distance(s.GetString(), tstart2->first)
  348. << ", expected: "
  349. << last_end2
  350. << ".", wchar_t);
  351. }
  352. int expected_end = static_cast<int>(answer_table[0] < 0 ? s.GetLength() : answer_table[0]);
  353. if(boost::BOOST_REGEX_DETAIL_NS::distance(s.GetString(), tstart2->second) != expected_end)
  354. {
  355. BOOST_REGEX_TEST_ERROR(
  356. "Error in location of end2 of field split, found: "
  357. << boost::BOOST_REGEX_DETAIL_NS::distance(s.GetString(), tstart2->second)
  358. << ", expected: "
  359. << expected_end
  360. << ".", wchar_t);
  361. }
  362. #ifdef BOOST_MSVC
  363. #pragma warning(pop)
  364. #endif
  365. last_end2 = answer_table[1];
  366. ++tstart2;
  367. ++tcopy2;
  368. // move on the answer table to next set of answers;
  369. if(*answer_table != -2)
  370. while(*answer_table++ != -2){}
  371. }
  372. if(answer_table[0] >= 0)
  373. {
  374. // we should have had a match but didn't:
  375. BOOST_REGEX_TEST_ERROR("Expected match was not found.", wchar_t);
  376. }
  377. }
  378. void test_mfc(const char&, const test_invalid_regex_tag&)
  379. {
  380. std::string ss = test_info<char>::expression();
  381. CAtlStringA s(ss.c_str(), ss.size());
  382. bool have_catch = false;
  383. try{
  384. boost::regex e = boost::make_regex(s, test_info<char>::syntax_options());
  385. if(e.error_code())
  386. have_catch = true;
  387. }
  388. catch(const boost::bad_expression&)
  389. {
  390. have_catch = true;
  391. }
  392. catch(const std::runtime_error& r)
  393. {
  394. have_catch = true;
  395. BOOST_REGEX_TEST_ERROR("Expected a bad_expression exception, but a std::runtime_error instead: " << r.what(), char);
  396. }
  397. catch(const std::exception& r)
  398. {
  399. have_catch = true;
  400. BOOST_REGEX_TEST_ERROR("Expected a bad_expression exception, but a std::exception instead: " << r.what(), char);
  401. }
  402. catch(...)
  403. {
  404. have_catch = true;
  405. BOOST_REGEX_TEST_ERROR("Expected a bad_expression exception, but got an exception of unknown type instead", char);
  406. }
  407. if(!have_catch)
  408. {
  409. // oops expected exception was not thrown:
  410. BOOST_REGEX_TEST_ERROR("Expected an exception, but didn't find one.", char);
  411. }
  412. }
  413. void test_mfc(const wchar_t&, const test_invalid_regex_tag&)
  414. {
  415. std::wstring ss = test_info<wchar_t>::expression();
  416. CAtlStringW s(ss.c_str(), ss.size());
  417. bool have_catch = false;
  418. try{
  419. boost::wregex e = boost::make_regex(s, test_info<wchar_t>::syntax_options());
  420. if(e.error_code())
  421. have_catch = true;
  422. }
  423. catch(const boost::bad_expression&)
  424. {
  425. have_catch = true;
  426. }
  427. catch(const std::runtime_error& r)
  428. {
  429. have_catch = true;
  430. BOOST_REGEX_TEST_ERROR("Expected a bad_expression exception, but a std::runtime_error instead: " << r.what(), wchar_t);
  431. }
  432. catch(const std::exception& r)
  433. {
  434. have_catch = true;
  435. BOOST_REGEX_TEST_ERROR("Expected a bad_expression exception, but a std::exception instead: " << r.what(), wchar_t);
  436. }
  437. catch(...)
  438. {
  439. have_catch = true;
  440. BOOST_REGEX_TEST_ERROR("Expected a bad_expression exception, but got an exception of unknown type instead", wchar_t);
  441. }
  442. if(!have_catch)
  443. {
  444. // oops expected exception was not thrown:
  445. BOOST_REGEX_TEST_ERROR("Expected an exception, but didn't find one.", wchar_t);
  446. }
  447. }
  448. void test_mfc(const char&, const test_regex_replace_tag&)
  449. {
  450. const CStringA expression(test_info<char>::expression().c_str(), test_info<char>::expression().size());
  451. boost::regex_constants::syntax_option_type syntax_options = test_info<char>::syntax_options();
  452. try{
  453. boost::regex r = boost::make_regex(expression, syntax_options);
  454. if(r.status())
  455. {
  456. BOOST_REGEX_TEST_ERROR("Expression did not compile when it should have done, error code = " << r.status(), char);
  457. }
  458. const CStringA search_text(test_info<char>::search_text().c_str(), test_info<char>::search_text().size());
  459. boost::regex_constants::match_flag_type opts = test_info<char>::match_options();
  460. const CStringA format_string(test_info<char>::format_string().c_str(), test_info<char>::format_string().size());
  461. const CStringA result_string(test_info<char>::result_string().c_str(), test_info<char>::result_string().size());
  462. CStringA result = boost::regex_replace(search_text, r, format_string, opts);
  463. if(result != result_string)
  464. {
  465. BOOST_REGEX_TEST_ERROR("regex_replace generated an incorrect string result", char);
  466. }
  467. }
  468. catch(const boost::bad_expression& e)
  469. {
  470. BOOST_REGEX_TEST_ERROR("Expression did not compile when it should have done: " << e.what(), char);
  471. }
  472. catch(const std::runtime_error& r)
  473. {
  474. BOOST_REGEX_TEST_ERROR("Received an unexpected std::runtime_error: " << r.what(), char);
  475. }
  476. catch(const std::exception& r)
  477. {
  478. BOOST_REGEX_TEST_ERROR("Received an unexpected std::exception: " << r.what(), char);
  479. }
  480. catch(...)
  481. {
  482. BOOST_REGEX_TEST_ERROR("Received an unexpected exception of unknown type", char);
  483. }
  484. }
  485. void test_mfc(const wchar_t&, const test_regex_replace_tag&)
  486. {
  487. const CStringW expression(test_info<wchar_t>::expression().c_str(), test_info<wchar_t>::expression().size());
  488. boost::regex_constants::syntax_option_type syntax_options = test_info<wchar_t>::syntax_options();
  489. try{
  490. boost::wregex r = boost::make_regex(expression, syntax_options);
  491. if(r.status())
  492. {
  493. BOOST_REGEX_TEST_ERROR("Expression did not compile when it should have done, error code = " << r.status(), wchar_t);
  494. }
  495. const CStringW search_text(test_info<wchar_t>::search_text().c_str(), test_info<wchar_t>::search_text().size());
  496. boost::regex_constants::match_flag_type opts = test_info<wchar_t>::match_options();
  497. const CStringW format_string(test_info<wchar_t>::format_string().c_str(), test_info<wchar_t>::format_string().size());
  498. const CStringW result_string(test_info<wchar_t>::result_string().c_str(), test_info<wchar_t>::result_string().size());
  499. CStringW result = boost::regex_replace(search_text, r, format_string, opts);
  500. if(result != result_string)
  501. {
  502. BOOST_REGEX_TEST_ERROR("regex_replace generated an incorrect string result", wchar_t);
  503. }
  504. }
  505. catch(const boost::bad_expression& e)
  506. {
  507. BOOST_REGEX_TEST_ERROR("Expression did not compile when it should have done: " << e.what(), wchar_t);
  508. }
  509. catch(const std::runtime_error& r)
  510. {
  511. BOOST_REGEX_TEST_ERROR("Received an unexpected std::runtime_error: " << r.what(), wchar_t);
  512. }
  513. catch(const std::exception& r)
  514. {
  515. BOOST_REGEX_TEST_ERROR("Received an unexpected std::exception: " << r.what(), wchar_t);
  516. }
  517. catch(...)
  518. {
  519. BOOST_REGEX_TEST_ERROR("Received an unexpected exception of unknown type", wchar_t);
  520. }
  521. }
  522. #else
  523. #include "test.hpp"
  524. void test_mfc(const char&, const test_regex_search_tag&){}
  525. void test_mfc(const wchar_t&, const test_regex_search_tag&){}
  526. void test_mfc(const char&, const test_invalid_regex_tag&){}
  527. void test_mfc(const wchar_t&, const test_invalid_regex_tag&){}
  528. void test_mfc(const char&, const test_regex_replace_tag&){}
  529. void test_mfc(const wchar_t&, const test_regex_replace_tag&){}
  530. #endif