test.hpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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.hpp
  14. * VERSION see <boost/version.hpp>
  15. * DESCRIPTION: Macros for test cases.
  16. */
  17. #ifndef BOOST_REGEX_REGRESS_TEST_HPP
  18. #define BOOST_REGEX_REGRESS_TEST_HPP
  19. #include <boost/regex.hpp>
  20. #ifdef BOOST_INTEL
  21. // disable Intel's "remarks":
  22. #pragma warning(disable:1418 981 383 1419 7)
  23. #endif
  24. #include <typeinfo>
  25. #include "test_not_regex.hpp"
  26. #include "test_regex_search.hpp"
  27. #include "test_regex_replace.hpp"
  28. #include "test_deprecated.hpp"
  29. #include "test_mfc.hpp"
  30. #include "test_icu.hpp"
  31. #include "test_locale.hpp"
  32. #ifdef TEST_THREADS
  33. #include <boost/thread/once.hpp>
  34. #endif
  35. //
  36. // define test entry proc, this forwards on to the appropriate
  37. // real test:
  38. //
  39. template <class charT, class tagT>
  40. void do_test(const charT& c, const tagT& tag);
  41. template <class charT, class tagT>
  42. void test(const charT& c, const tagT& tag)
  43. {
  44. do_test(c, tag);
  45. }
  46. //
  47. // make these non-templates to speed up compilation times:
  48. //
  49. void test(const char&, const test_regex_replace_tag&);
  50. void test(const char&, const test_regex_search_tag&);
  51. void test(const char&, const test_invalid_regex_tag&);
  52. #ifndef BOOST_NO_WREGEX
  53. void test(const wchar_t&, const test_regex_replace_tag&);
  54. void test(const wchar_t&, const test_regex_search_tag&);
  55. void test(const wchar_t&, const test_invalid_regex_tag&);
  56. #endif
  57. template <class Regex>
  58. struct call_once_func
  59. {
  60. Regex* pregex;
  61. void operator()()const
  62. {
  63. return test_empty(*pregex);
  64. }
  65. };
  66. template <class charT, class tagT>
  67. void do_test(const charT& c, const tagT& tag)
  68. {
  69. #ifndef BOOST_NO_STD_LOCALE
  70. #if BOOST_WORKAROUND(BOOST_MSVC, <= 1200) && defined(TEST_THREADS)
  71. // typeid appears to fail in multithreaded environments:
  72. test_info<charT>::set_typename("");
  73. #else
  74. test_info<charT>::set_typename(typeid(boost::basic_regex<charT, boost::cpp_regex_traits<charT> >).name());
  75. #endif
  76. boost::basic_regex<charT, boost::cpp_regex_traits<charT> > e1;
  77. #ifndef TEST_THREADS
  78. static bool done_empty_test = false;
  79. if(done_empty_test == false)
  80. {
  81. test_empty(e1);
  82. done_empty_test = true;
  83. }
  84. #else
  85. boost::once_flag f = BOOST_ONCE_INIT;
  86. call_once_func<boost::basic_regex<charT, boost::cpp_regex_traits<charT> > > proc = { &e1 };
  87. boost::call_once(f, proc);
  88. #endif
  89. if(test_locale::cpp_locale_state() == test_locale::test_with_locale)
  90. e1.imbue(test_locale::cpp_locale());
  91. if(test_locale::cpp_locale_state() != test_locale::no_test)
  92. test(e1, tag);
  93. #endif
  94. #if !BOOST_WORKAROUND(__BORLANDC__, < 0x560)
  95. #if BOOST_WORKAROUND(BOOST_MSVC, <= 1200) && defined(TEST_THREADS)
  96. // typeid appears to fail in multithreaded environments:
  97. test_info<charT>::set_typename("");
  98. #else
  99. test_info<charT>::set_typename(typeid(boost::basic_regex<charT, boost::c_regex_traits<charT> >).name());
  100. #endif
  101. boost::basic_regex<charT, boost::c_regex_traits<charT> > e2;
  102. if(test_locale::c_locale_state() != test_locale::no_test)
  103. test(e2, tag);
  104. #endif
  105. #if defined(_WIN32) && !defined(BOOST_REGEX_NO_W32)
  106. #if BOOST_WORKAROUND(BOOST_MSVC, <= 1200) && defined(TEST_THREADS)
  107. // typeid appears to fail in multithreaded environments:
  108. test_info<charT>::set_typename("");
  109. #else
  110. test_info<charT>::set_typename(typeid(boost::basic_regex<charT, boost::w32_regex_traits<charT> >).name());
  111. #endif
  112. boost::basic_regex<charT, boost::w32_regex_traits<charT> > e3;
  113. if(test_locale::win_locale_state() == test_locale::test_with_locale)
  114. e3.imbue(test_locale::win_locale());
  115. if(test_locale::win_locale_state() != test_locale::no_test)
  116. test(e3, tag);
  117. #endif
  118. // test old depecated code:
  119. test_info<charT>::set_typename("Deprecated interfaces");
  120. if((test_locale::win_locale_state() == test_locale::test_no_locale)
  121. && (test_locale::c_locale_state() == test_locale::test_no_locale)
  122. &&(test_locale::cpp_locale_state() == test_locale::test_no_locale))
  123. test_deprecated(c, tag);
  124. // test MFC/ATL wrappers:
  125. test_info<charT>::set_typename("MFC/ATL interfaces");
  126. if((test_locale::win_locale_state() == test_locale::test_no_locale)
  127. && (test_locale::c_locale_state() == test_locale::test_no_locale)
  128. &&(test_locale::cpp_locale_state() == test_locale::test_no_locale))
  129. test_mfc(c, tag);
  130. // test ICU code:
  131. test_info<charT>::set_typename("ICU interfaces");
  132. test_icu(c, tag);
  133. }
  134. //
  135. // define function to pack args into an array:
  136. //
  137. const int* make_array(int first, ...);
  138. //
  139. // define macros for testing invalid regexes:
  140. //
  141. #define TEST_INVALID_REGEX_N(s, f)\
  142. do{\
  143. const char e[] = { s };\
  144. std::string se(e, sizeof(e) - 1);\
  145. test_info<char>::set_info(__FILE__, __LINE__, se, f);\
  146. test(char(0), test_invalid_regex_tag());\
  147. }while(0)
  148. #ifndef BOOST_NO_WREGEX
  149. #define TEST_INVALID_REGEX_W(s, f)\
  150. do{\
  151. const wchar_t e[] = { s };\
  152. std::wstring se(e, (sizeof(e) / sizeof(wchar_t)) - 1);\
  153. test_info<wchar_t>::set_info(__FILE__, __LINE__, se, f);\
  154. test(wchar_t(0), test_invalid_regex_tag());\
  155. }while(0)
  156. #else
  157. #define TEST_INVALID_REGEX_W(s, f)
  158. #endif
  159. #define TEST_INVALID_REGEX(s, f)\
  160. TEST_INVALID_REGEX_N(s, f);\
  161. TEST_INVALID_REGEX_W(BOOST_JOIN(L, s), f)
  162. //
  163. // define macros for testing regex searches:
  164. //
  165. #define TEST_REGEX_SEARCH_N(s, f, t, m, a)\
  166. do{\
  167. const char e[] = { s };\
  168. std::string se(e, sizeof(e) - 1);\
  169. const char st[] = { t };\
  170. std::string sst(st, sizeof(st) - 1);\
  171. test_info<char>::set_info(__FILE__, __LINE__, se, f, sst, m, a);\
  172. test(char(0), test_regex_search_tag());\
  173. }while(0)
  174. #ifndef BOOST_NO_WREGEX
  175. #define TEST_REGEX_SEARCH_W(s, f, t, m, a)\
  176. do{\
  177. const wchar_t e[] = { s };\
  178. std::wstring se(e, (sizeof(e) / sizeof(wchar_t)) - 1);\
  179. const wchar_t st[] = { t };\
  180. std::wstring sst(st, (sizeof(st) / sizeof(wchar_t)) - 1);\
  181. test_info<wchar_t>::set_info(__FILE__, __LINE__, se, f, sst, m, a);\
  182. test(wchar_t(0), test_regex_search_tag());\
  183. }while(0)
  184. #else
  185. #define TEST_REGEX_SEARCH_W(s, f, t, m, a)
  186. #endif
  187. #define TEST_REGEX_SEARCH(s, f, t, m, a)\
  188. TEST_REGEX_SEARCH_N(s, f, t, m, a);\
  189. TEST_REGEX_SEARCH_W(BOOST_JOIN(L, s), f, BOOST_JOIN(L, t), m, a)
  190. #if (defined(__GNUC__) && (__GNUC__ == 3) && (__GNUC_MINOR__ >= 4))
  191. #define TEST_REGEX_SEARCH_L(s, f, t, m, a) TEST_REGEX_SEARCH_W(BOOST_JOIN(L, s), f, BOOST_JOIN(L, t), m, a)
  192. #else
  193. #define TEST_REGEX_SEARCH_L(s, f, t, m, a) TEST_REGEX_SEARCH(s, f, t, m, a)
  194. #endif
  195. //
  196. // define macros for testing regex replaces:
  197. //
  198. #define TEST_REGEX_REPLACE_N(s, f, t, m, fs, r)\
  199. do{\
  200. const char e[] = { s };\
  201. std::string se(e, sizeof(e) - 1);\
  202. const char st[] = { t };\
  203. std::string sst(st, sizeof(st) - 1);\
  204. const char ft[] = { fs };\
  205. std::string sft(ft, sizeof(ft) - 1);\
  206. const char rt[] = { r };\
  207. std::string srt(rt, sizeof(rt) - 1);\
  208. test_info<char>::set_info(__FILE__, __LINE__, se, f, sst, m, 0, sft, srt);\
  209. test(char(0), test_regex_replace_tag());\
  210. }while(0)
  211. #ifndef BOOST_NO_WREGEX
  212. #define TEST_REGEX_REPLACE_W(s, f, t, m, fs, r)\
  213. do{\
  214. const wchar_t e[] = { s };\
  215. std::wstring se(e, (sizeof(e) / sizeof(wchar_t)) - 1);\
  216. const wchar_t st[] = { t };\
  217. std::wstring sst(st, (sizeof(st) / sizeof(wchar_t)) - 1);\
  218. const wchar_t ft[] = { fs };\
  219. std::wstring sft(ft, (sizeof(ft) / sizeof(wchar_t)) - 1);\
  220. const wchar_t rt[] = { r };\
  221. std::wstring srt(rt, (sizeof(rt) / sizeof(wchar_t)) - 1);\
  222. test_info<wchar_t>::set_info(__FILE__, __LINE__, se, f, sst, m, 0, sft, srt);\
  223. test(wchar_t(0), test_regex_replace_tag());\
  224. }while(0)
  225. #else
  226. #define TEST_REGEX_REPLACE_W(s, f, t, m, fs, r)
  227. #endif
  228. #define TEST_REGEX_REPLACE(s, f, t, m, fs, r)\
  229. TEST_REGEX_REPLACE_N(s, f, t, m, fs, r);\
  230. TEST_REGEX_REPLACE_W(BOOST_JOIN(L, s), f, BOOST_JOIN(L, t), m, BOOST_JOIN(L, fs), BOOST_JOIN(L, r))
  231. //
  232. // define the test group proceedures:
  233. //
  234. void basic_tests();
  235. void test_simple_repeats();
  236. void test_alt();
  237. void test_sets();
  238. void test_sets2();
  239. void test_anchors();
  240. void test_backrefs();
  241. void test_character_escapes();
  242. void test_assertion_escapes();
  243. void test_tricky_cases();
  244. void test_grep();
  245. void test_replace();
  246. void test_non_greedy_repeats();
  247. void test_non_marking_paren();
  248. void test_partial_match();
  249. void test_forward_lookahead_asserts();
  250. void test_fast_repeats();
  251. void test_fast_repeats2();
  252. void test_tricky_cases2();
  253. void test_independent_subs();
  254. void test_nosubs();
  255. void test_conditionals();
  256. void test_options();
  257. void test_options2();
  258. void test_en_locale();
  259. void test_emacs();
  260. void test_operators();
  261. void test_overloads();
  262. void test_unicode();
  263. void test_pocessive_repeats();
  264. void test_mark_resets();
  265. void test_recursion();
  266. void test_verbs();
  267. #endif