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_deprecated.cpp |
---|
15 | * VERSION see <boost/version.hpp> |
---|
16 | * DESCRIPTION: Tests for deprecated interfaces. |
---|
17 | */ |
---|
18 | |
---|
19 | #include "test.hpp" |
---|
20 | #include <boost/cregex.hpp> |
---|
21 | |
---|
22 | #ifdef BOOST_MSVC |
---|
23 | #pragma warning(disable:4267) |
---|
24 | #endif |
---|
25 | |
---|
26 | #ifdef BOOST_NO_STDC_NAMESPACE |
---|
27 | namespace std{ |
---|
28 | using ::atoi; |
---|
29 | using ::wcstol; |
---|
30 | } |
---|
31 | #endif |
---|
32 | |
---|
33 | int get_posix_compile_options(boost::regex_constants::syntax_option_type opts) |
---|
34 | { |
---|
35 | using namespace boost; |
---|
36 | int result = 0; |
---|
37 | switch(opts & regbase::main_option_type) |
---|
38 | { |
---|
39 | case regbase::perl: |
---|
40 | result = (opts & regbase::no_perl_ex) ? REG_EXTENDED : REG_PERL; |
---|
41 | if(opts & (regbase::no_bk_refs|regbase::no_mod_m|regbase::mod_x|regbase::mod_s|regbase::no_mod_s|regbase::no_escape_in_lists)) |
---|
42 | return -1; |
---|
43 | break; |
---|
44 | case regbase::basic: |
---|
45 | result = REG_BASIC; |
---|
46 | if(opts & (regbase::no_char_classes|regbase::no_intervals|regbase::bk_plus_qm|regbase::bk_vbar)) |
---|
47 | return -1; |
---|
48 | if((opts & regbase::no_escape_in_lists) == 0) |
---|
49 | return -1; |
---|
50 | break; |
---|
51 | default: |
---|
52 | return -1; |
---|
53 | } |
---|
54 | |
---|
55 | if(opts & regbase::icase) |
---|
56 | result |= REG_ICASE; |
---|
57 | if(opts & regbase::nosubs) |
---|
58 | result |= REG_NOSUB; |
---|
59 | if(opts & regbase::newline_alt) |
---|
60 | result |= REG_NEWLINE; |
---|
61 | if((opts & regbase::collate) == 0) |
---|
62 | result |= REG_NOCOLLATE; |
---|
63 | |
---|
64 | return result; |
---|
65 | } |
---|
66 | |
---|
67 | int get_posix_match_flags(boost::regex_constants::match_flag_type f) |
---|
68 | { |
---|
69 | int result = 0; |
---|
70 | if(f & boost::regex_constants::match_not_bol) |
---|
71 | result |= boost::REG_NOTBOL; |
---|
72 | if(f & boost::regex_constants::match_not_eol) |
---|
73 | result |= boost::REG_NOTEOL; |
---|
74 | if(f & ~(boost::regex_constants::match_not_bol|boost::regex_constants::match_not_eol)) |
---|
75 | return -1; |
---|
76 | return result; |
---|
77 | } |
---|
78 | |
---|
79 | void test_deprecated(const char&, const test_regex_search_tag&) |
---|
80 | { |
---|
81 | const std::string& expression = test_info<char>::expression(); |
---|
82 | if(expression.find('\0') != std::string::npos) |
---|
83 | return; |
---|
84 | const std::string& search_text = test_info<char>::search_text(); |
---|
85 | if(search_text.find('\0') != std::string::npos) |
---|
86 | return; |
---|
87 | int posix_options = get_posix_compile_options(test_info<char>::syntax_options()); |
---|
88 | if(posix_options < 0) |
---|
89 | return; |
---|
90 | int posix_match_options = get_posix_match_flags(test_info<char>::match_options()); |
---|
91 | if(posix_match_options < 0) |
---|
92 | return; |
---|
93 | const int* results = test_info<char>::answer_table(); |
---|
94 | |
---|
95 | // OK try and compile the expression: |
---|
96 | boost::regex_tA re; |
---|
97 | if(boost::regcompA(&re, expression.c_str(), posix_options) != 0) |
---|
98 | { |
---|
99 | BOOST_REGEX_TEST_ERROR("Expression : \"" << expression.c_str() << "\" did not compile with the POSIX C API.", char); |
---|
100 | return; |
---|
101 | } |
---|
102 | // try and find the first occurance: |
---|
103 | static const unsigned max_subs = 100; |
---|
104 | boost::regmatch_t matches[max_subs]; |
---|
105 | if(boost::regexecA(&re, search_text.c_str(), max_subs, matches, posix_match_options) == 0) |
---|
106 | { |
---|
107 | int i = 0; |
---|
108 | while(results[2*i] != -2) |
---|
109 | { |
---|
110 | if(max_subs > i) |
---|
111 | { |
---|
112 | if(results[2*i] != matches[i].rm_so) |
---|
113 | { |
---|
114 | BOOST_REGEX_TEST_ERROR("Mismatch in start of subexpression " << i << " found with the POSIX C API.", char); |
---|
115 | } |
---|
116 | if(results[2*i+1] != matches[i].rm_eo) |
---|
117 | { |
---|
118 | BOOST_REGEX_TEST_ERROR("Mismatch in end of subexpression " << i << " found with the POSIX C API.", char); |
---|
119 | } |
---|
120 | } |
---|
121 | ++i; |
---|
122 | } |
---|
123 | } |
---|
124 | else |
---|
125 | { |
---|
126 | if(results[0] >= 0) |
---|
127 | { |
---|
128 | BOOST_REGEX_TEST_ERROR("Expression : \"" << expression.c_str() << "\" was not found with the POSIX C API.", char); |
---|
129 | } |
---|
130 | } |
---|
131 | // clean up whatever: |
---|
132 | boost::regfreeA(&re); |
---|
133 | |
---|
134 | // |
---|
135 | // now try the RegEx class: |
---|
136 | // |
---|
137 | if(test_info<char>::syntax_options() & ~boost::regex::icase) |
---|
138 | return; |
---|
139 | try{ |
---|
140 | boost::RegEx e(expression, test_info<char>::syntax_options() & boost::regex::icase); |
---|
141 | if(e.error_code()) |
---|
142 | { |
---|
143 | BOOST_REGEX_TEST_ERROR("Expression did not compile when it should have done, error code = " << e.error_code(), char); |
---|
144 | } |
---|
145 | if(e.Search(search_text, test_info<char>::match_options())) |
---|
146 | { |
---|
147 | int i = 0; |
---|
148 | while(results[i*2] != -2) |
---|
149 | { |
---|
150 | if(e.Matched(i)) |
---|
151 | { |
---|
152 | if(results[2*i] != static_cast<int>(e.Position(i))) |
---|
153 | { |
---|
154 | BOOST_REGEX_TEST_ERROR("Mismatch in start of subexpression " << i << " found with the RegEx class (found " << e.Position(i) << " expected " << results[2*i] << ").", char); |
---|
155 | } |
---|
156 | if(results[2*i+1] != static_cast<int>(e.Position(i) + e.Length(i))) |
---|
157 | { |
---|
158 | BOOST_REGEX_TEST_ERROR("Mismatch in end of subexpression " << i << " found with the RegEx class (found " << e.Position(i) + e.Length(i) << " expected " << results[2*i+1] << ").", char); |
---|
159 | } |
---|
160 | } |
---|
161 | else |
---|
162 | { |
---|
163 | if(results[2*i] >= 0) |
---|
164 | { |
---|
165 | BOOST_REGEX_TEST_ERROR("Mismatch in start of subexpression " << i << " found with the RegEx class (found " << e.Position(i) << " expected " << results[2*i] << ").", char); |
---|
166 | } |
---|
167 | if(results[2*i+1] >= 0) |
---|
168 | { |
---|
169 | BOOST_REGEX_TEST_ERROR("Mismatch in end of subexpression " << i << " found with the RegEx class (found " << e.Position(i) + e.Length(i) << " expected " << results[2*i+1] << ").", char); |
---|
170 | } |
---|
171 | } |
---|
172 | ++i; |
---|
173 | } |
---|
174 | } |
---|
175 | else |
---|
176 | { |
---|
177 | if(results[0] >= 0) |
---|
178 | { |
---|
179 | BOOST_REGEX_TEST_ERROR("Expression : \"" << expression.c_str() << "\" was not found with class RegEx.", char); |
---|
180 | } |
---|
181 | } |
---|
182 | } |
---|
183 | catch(const boost::bad_expression& r) |
---|
184 | { |
---|
185 | BOOST_REGEX_TEST_ERROR("Expression did not compile with RegEx class: " << r.what(), char); |
---|
186 | } |
---|
187 | catch(const std::runtime_error& r) |
---|
188 | { |
---|
189 | BOOST_REGEX_TEST_ERROR("Unexpected std::runtime_error : " << r.what(), char); |
---|
190 | } |
---|
191 | catch(const std::exception& r) |
---|
192 | { |
---|
193 | BOOST_REGEX_TEST_ERROR("Unexpected std::exception: " << r.what(), char); |
---|
194 | } |
---|
195 | catch(...) |
---|
196 | { |
---|
197 | BOOST_REGEX_TEST_ERROR("Unexpected exception of unknown type", char); |
---|
198 | } |
---|
199 | |
---|
200 | } |
---|
201 | |
---|
202 | void test_deprecated(const wchar_t&, const test_regex_search_tag&) |
---|
203 | { |
---|
204 | #ifndef BOOST_NO_WREGEX |
---|
205 | const std::wstring& expression = test_info<wchar_t>::expression(); |
---|
206 | if(expression.find(L'\0') != std::wstring::npos) |
---|
207 | return; |
---|
208 | const std::wstring& search_text = test_info<wchar_t>::search_text(); |
---|
209 | if(search_text.find(L'\0') != std::wstring::npos) |
---|
210 | return; |
---|
211 | int posix_options = get_posix_compile_options(test_info<wchar_t>::syntax_options()); |
---|
212 | if(posix_options < 0) |
---|
213 | return; |
---|
214 | int posix_match_options = get_posix_match_flags(test_info<wchar_t>::match_options()); |
---|
215 | if(posix_match_options < 0) |
---|
216 | return; |
---|
217 | const int* results = test_info<wchar_t>::answer_table(); |
---|
218 | |
---|
219 | // OK try and compile the expression: |
---|
220 | boost::regex_tW re; |
---|
221 | if(boost::regcompW(&re, expression.c_str(), posix_options) != 0) |
---|
222 | { |
---|
223 | BOOST_REGEX_TEST_ERROR("Expression : \"" << expression.c_str() << "\" did not compile with the POSIX C API.", wchar_t); |
---|
224 | return; |
---|
225 | } |
---|
226 | // try and find the first occurance: |
---|
227 | static const unsigned max_subs = 100; |
---|
228 | boost::regmatch_t matches[max_subs]; |
---|
229 | if(boost::regexecW(&re, search_text.c_str(), max_subs, matches, posix_match_options) == 0) |
---|
230 | { |
---|
231 | int i = 0; |
---|
232 | while(results[2*i] != -2) |
---|
233 | { |
---|
234 | if(max_subs > i) |
---|
235 | { |
---|
236 | if(results[2*i] != matches[i].rm_so) |
---|
237 | { |
---|
238 | BOOST_REGEX_TEST_ERROR("Mismatch in start of subexpression " << i << " found with the POSIX C API.", wchar_t); |
---|
239 | } |
---|
240 | if(results[2*i+1] != matches[i].rm_eo) |
---|
241 | { |
---|
242 | BOOST_REGEX_TEST_ERROR("Mismatch in end of subexpression " << i << " found with the POSIX C API.", wchar_t); |
---|
243 | } |
---|
244 | } |
---|
245 | ++i; |
---|
246 | } |
---|
247 | } |
---|
248 | else |
---|
249 | { |
---|
250 | if(results[0] >= 0) |
---|
251 | { |
---|
252 | BOOST_REGEX_TEST_ERROR("Expression : \"" << expression.c_str() << "\" was not found with the POSIX C API.", wchar_t); |
---|
253 | } |
---|
254 | } |
---|
255 | // clean up whatever: |
---|
256 | boost::regfreeW(&re); |
---|
257 | #endif |
---|
258 | } |
---|
259 | |
---|
260 | void test_deprecated(const char&, const test_invalid_regex_tag&) |
---|
261 | { |
---|
262 | const std::string& expression = test_info<char>::expression(); |
---|
263 | if(expression.find('\0') != std::string::npos) |
---|
264 | return; |
---|
265 | int posix_options = get_posix_compile_options(test_info<char>::syntax_options()); |
---|
266 | if(posix_options < 0) |
---|
267 | return; |
---|
268 | |
---|
269 | // OK try and compile the expression: |
---|
270 | boost::regex_tA re; |
---|
271 | int code = boost::regcompA(&re, expression.c_str(), posix_options); |
---|
272 | if(code == 0) |
---|
273 | { |
---|
274 | boost::regfreeA(&re); |
---|
275 | BOOST_REGEX_TEST_ERROR("Expression : \"" << expression.c_str() << "\" unexpectedly compiled with the POSIX C API.", char); |
---|
276 | } |
---|
277 | else |
---|
278 | { |
---|
279 | char buf[100]; |
---|
280 | int s = boost::regerrorA(code, &re, 0, 0); |
---|
281 | if(s < 100) |
---|
282 | s = boost::regerrorA(code, &re, buf, 100); |
---|
283 | s = boost::regerrorA(code | boost::REG_ITOA, &re, 0, 0); |
---|
284 | if(s < 100) |
---|
285 | { |
---|
286 | s = boost::regerrorA(code | boost::REG_ITOA, &re, buf, 100); |
---|
287 | re.re_endp = buf; |
---|
288 | s = boost::regerrorA(code | boost::REG_ATOI, &re, buf, 100); |
---|
289 | if(s) |
---|
290 | { |
---|
291 | int code2 = std::atoi(buf); |
---|
292 | if(code2 != code) |
---|
293 | { |
---|
294 | BOOST_REGEX_TEST_ERROR("Got a bad error code from regerrA with REG_ATOI set: ", char); |
---|
295 | } |
---|
296 | } |
---|
297 | } |
---|
298 | } |
---|
299 | // |
---|
300 | // now try the RegEx class: |
---|
301 | // |
---|
302 | if(test_info<char>::syntax_options() & ~boost::regex::icase) |
---|
303 | return; |
---|
304 | bool have_catch = false; |
---|
305 | try{ |
---|
306 | boost::RegEx e(expression, test_info<char>::syntax_options() & boost::regex::icase); |
---|
307 | if(e.error_code()) |
---|
308 | have_catch = true; |
---|
309 | } |
---|
310 | catch(const boost::bad_expression&) |
---|
311 | { |
---|
312 | have_catch = true; |
---|
313 | } |
---|
314 | catch(const std::runtime_error& r) |
---|
315 | { |
---|
316 | have_catch = true; |
---|
317 | BOOST_REGEX_TEST_ERROR("Expected a bad_expression exception, but a std::runtime_error instead: " << r.what(), char); |
---|
318 | } |
---|
319 | catch(const std::exception& r) |
---|
320 | { |
---|
321 | have_catch = true; |
---|
322 | BOOST_REGEX_TEST_ERROR("Expected a bad_expression exception, but a std::exception instead: " << r.what(), char); |
---|
323 | } |
---|
324 | catch(...) |
---|
325 | { |
---|
326 | have_catch = true; |
---|
327 | BOOST_REGEX_TEST_ERROR("Expected a bad_expression exception, but got an exception of unknown type instead", char); |
---|
328 | } |
---|
329 | if(!have_catch) |
---|
330 | { |
---|
331 | // oops expected exception was not thrown: |
---|
332 | BOOST_REGEX_TEST_ERROR("Expected an exception, but didn't find one.", char); |
---|
333 | } |
---|
334 | } |
---|
335 | |
---|
336 | void test_deprecated(const wchar_t&, const test_invalid_regex_tag&) |
---|
337 | { |
---|
338 | #ifndef BOOST_NO_WREGEX |
---|
339 | const std::wstring& expression = test_info<wchar_t>::expression(); |
---|
340 | if(expression.find(L'\0') != std::string::npos) |
---|
341 | return; |
---|
342 | int posix_options = get_posix_compile_options(test_info<wchar_t>::syntax_options()); |
---|
343 | if(posix_options < 0) |
---|
344 | return; |
---|
345 | |
---|
346 | // OK try and compile the expression: |
---|
347 | boost::regex_tW re; |
---|
348 | int code = boost::regcompW(&re, expression.c_str(), posix_options); |
---|
349 | if(code == 0) |
---|
350 | { |
---|
351 | boost::regfreeW(&re); |
---|
352 | BOOST_REGEX_TEST_ERROR("Expression : \"" << expression.c_str() << "\" unexpectedly compiled with the POSIX C API.", wchar_t); |
---|
353 | } |
---|
354 | else |
---|
355 | { |
---|
356 | wchar_t buf[100]; |
---|
357 | int s = boost::regerrorW(code, &re, 0, 0); |
---|
358 | if(s < 100) |
---|
359 | s = boost::regerrorW(code, &re, buf, 100); |
---|
360 | s = boost::regerrorW(code | boost::REG_ITOA, &re, 0, 0); |
---|
361 | if(s < 100) |
---|
362 | { |
---|
363 | s = boost::regerrorW(code | boost::REG_ITOA, &re, buf, 100); |
---|
364 | re.re_endp = buf; |
---|
365 | s = boost::regerrorW(code | boost::REG_ATOI, &re, buf, 100); |
---|
366 | if(s) |
---|
367 | { |
---|
368 | long code2 = std::wcstol(buf, 0, 10); |
---|
369 | if(code2 != code) |
---|
370 | { |
---|
371 | BOOST_REGEX_TEST_ERROR("Got a bad error code from regerrW with REG_ATOI set: ", char); |
---|
372 | } |
---|
373 | } |
---|
374 | } |
---|
375 | } |
---|
376 | #endif |
---|
377 | } |
---|