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