Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_34_1/libs/xpressive/test/regress.ipp @ 44

Last change on this file since 44 was 29, checked in by landauf, 17 years ago

updated boost from 1_33_1 to 1_34_1

File size: 11.1 KB
Line 
1//////////////////////////////////////////////////////////////////////////////
2// regress.ipp
3//
4//  (C) Copyright Eric Niebler 2004.
5//  Use, modification and distribution are subject to the
6//  Boost Software License, Version 1.0. (See accompanying file
7//  LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
8
9/*
10 Revision history:
11   7 March 2004 : Initial version.
12*/
13
14#if defined(_MSC_VER) && defined(_DEBUG)
15# include <crtdbg.h>
16#endif
17
18#include <locale>
19#include <vector>
20#include <fstream>
21#include <boost/lexical_cast.hpp>
22#include <boost/xpressive/xpressive.hpp>
23#include "./test_minimal.hpp"
24
25#define BOOST_XPR_CHECK(pred)                                                   \
26    if(pred) {} else { BOOST_ERROR(format_msg(#pred).c_str()); }
27
28using namespace boost::xpressive;
29
30//////////////////////////////////////////////////////////////////////////////
31// test_case
32template<typename Char>
33struct test_case
34{
35    typedef std::basic_string<Char> string_type;
36    std::string section;
37    string_type str;
38    string_type pat;
39    string_type sub;
40    string_type res;
41    regex_constants::syntax_option_type syntax_flags;
42    regex_constants::match_flag_type match_flags;
43    std::vector<string_type> br;
44
45    test_case()
46    {
47        this->reset();
48    }
49
50    void reset()
51    {
52        this->section.clear();
53        this->str.clear();
54        this->pat.clear();
55        this->sub.clear();
56        this->res.clear();
57        this->br.clear();
58        this->syntax_flags = regex_constants::ECMAScript;
59        this->match_flags = regex_constants::match_default | regex_constants::format_first_only;
60    }
61};
62
63//////////////////////////////////////////////////////////////////////////////
64// globals
65std::ifstream in;
66unsigned int test_count = 0;
67
68// The global object that contains the current test case
69test_case<char> test;
70
71sregex const rx_sec = '[' >> (s1= +_) >> ']';
72sregex const rx_str = "str=" >> (s1= *_);
73sregex const rx_pat = "pat=" >> (s1= *_);
74sregex const rx_flg = "flg=" >> (s1= *_);
75sregex const rx_sub = "sub=" >> (s1= *_);
76sregex const rx_res = "res=" >> (s1= *_);
77sregex const rx_br = "br" >> (s1= +digit) >> '=' >> (s2= *_);
78
79///////////////////////////////////////////////////////////////////////////////
80// format_msg
81std::string format_msg(char const *msg)
82{
83    return test.section + " /" + test.pat + "/ : " + msg;
84}
85
86#ifndef BOOST_XPRESSIVE_NO_WREGEX
87///////////////////////////////////////////////////////////////////////////////
88// widen
89//  make a std::wstring from a std::string by widening according to the
90//  current ctype<char> facet
91std::wstring widen(std::string const &str)
92{
93    std::ctype<char> const &ct = BOOST_USE_FACET(std::ctype<char>, std::locale());
94    std::wstring res;
95    for(int i=0; i<str.size(); ++i)
96    {
97        res += ct.widen(str[i]);
98    }
99    return res;
100}
101
102///////////////////////////////////////////////////////////////////////////////
103// widen
104//  widens an entire test case
105test_case<wchar_t> widen(test_case<char> const &test)
106{
107    test_case<wchar_t> wtest;
108    wtest.section = test.section;
109    wtest.str = ::widen(test.str);
110    wtest.pat = ::widen(test.pat);
111    wtest.sub = ::widen(test.sub);
112    wtest.res = ::widen(test.res);
113    wtest.syntax_flags = test.syntax_flags;
114    wtest.match_flags = test.match_flags;
115    wtest.br.reserve(test.br.size());
116    for(std::size_t i = 0; i < test.br.size(); ++i)
117    {
118        wtest.br.push_back(::widen(test.br[i]));
119    }
120    return wtest;
121}
122#endif // BOOST_XPRESSIVE_NO_WREGEX
123
124std::string escape(std::string str)
125{
126    for(std::string::size_type pos = 0; std::string::npos != (pos = str.find('\\', pos)); ++pos)
127    {
128        if(pos + 1 == str.size())
129            break;
130
131        switch(str[pos + 1])
132        {
133        case '\\': str.replace(pos, 2, "\\"); break;
134        case 'n': str.replace(pos, 2, "\n"); break;
135        case 'r': str.replace(pos, 2, "\r"); break;
136        }
137    }
138    return str;
139}
140
141///////////////////////////////////////////////////////////////////////////////
142// get_test
143//   read the next section out of the input file, and fill out
144//   the global variables
145bool get_test()
146{
147    test.reset();
148    bool first = true;
149    std::string line;
150    smatch what;
151
152    while(in.good())
153    {
154        std::getline(in, line);
155
156        if(regex_match(line, what, rx_sec))
157        {
158            if(!first)
159            {
160                if(what[1] != "end")
161                {
162                    BOOST_FAIL(("invalid input : " + line).c_str());
163                }
164                break;
165            }
166
167            first = false;
168            test.section = what[1].str();
169        }
170        else if(regex_match(line, what, rx_str))
171        {
172            test.str = escape(what[1].str());
173        }
174        else if(regex_match(line, what, rx_pat))
175        {
176            test.pat = what[1].str();
177        }
178        else if(regex_match(line, what, rx_sub))
179        {
180            test.sub = what[1].str();
181        }
182        else if(regex_match(line, what, rx_res))
183        {
184            test.res = escape(what[1].str());
185        }
186        else if(regex_match(line, what, rx_flg))
187        {
188            std::string flg = what[1].str();
189
190            if(std::string::npos != flg.find('i'))
191            {
192                test.syntax_flags = test.syntax_flags | regex_constants::icase;
193            }
194            if(std::string::npos == flg.find('m'))
195            {
196                test.syntax_flags = test.syntax_flags | regex_constants::single_line;
197            }
198            if(std::string::npos == flg.find('s'))
199            {
200                test.syntax_flags = test.syntax_flags | regex_constants::not_dot_newline;
201            }
202            if(std::string::npos != flg.find('x'))
203            {
204                test.syntax_flags = test.syntax_flags | regex_constants::ignore_white_space;
205            }
206            if(std::string::npos != flg.find('g'))
207            {
208                test.match_flags = test.match_flags & ~regex_constants::format_first_only;
209            }
210        }
211        else if(regex_match(line, what, rx_br))
212        {
213            std::size_t nbr = boost::lexical_cast<std::size_t>(what[1].str());
214
215            if(nbr >= test.br.size())
216            {
217                test.br.resize(nbr + 1);
218            }
219
220            test.br[nbr] = escape(what[2].str());
221        }
222        else if(!line.empty() && ';' != line[0])
223        {
224            BOOST_FAIL((std::string("invalid input : ") + line).c_str());
225        }
226    }
227
228    return !first;
229}
230
231///////////////////////////////////////////////////////////////////////////////
232// run_test_impl
233//   run the test
234template<typename Char>
235void run_test_impl(test_case<Char> const &test)
236{
237    try
238    {
239        Char const empty[] = {0};
240        typedef typename std::basic_string<Char>::const_iterator iterator;
241        basic_regex<iterator> rx = basic_regex<iterator>::compile(test.pat, test.syntax_flags);
242
243        if(!test.res.empty())
244        {
245            // test regex_replace
246            std::basic_string<Char> res = regex_replace(test.str, rx, test.sub, test.match_flags);
247            BOOST_XPR_CHECK(res == test.res);
248        }
249
250        if(0 == (test.match_flags & regex_constants::format_first_only))
251        {
252            // global search, use regex_iterator
253            std::vector<sub_match<iterator> > br;
254            regex_iterator<iterator> begin(test.str.begin(), test.str.end(), rx, test.match_flags), end;
255            for(; begin != end; ++begin)
256            {
257                match_results<iterator> const &what = *begin;
258                br.insert(br.end(), what.begin(), what.end());
259            }
260
261            // match succeeded: was it expected to succeed?
262            BOOST_XPR_CHECK(br.size() == test.br.size());
263
264            for(std::size_t i = 0; i < br.size() && i < test.br.size(); ++i)
265            {
266                BOOST_XPR_CHECK(!br[i].matched && test.br[i] == empty || test.br[i] == br[i].str());
267            }
268        }
269        else
270        {
271            // test regex_search
272            match_results<iterator> what;
273            if(regex_search(test.str, what, rx, test.match_flags))
274            {
275                // match succeeded: was it expected to succeed?
276                BOOST_XPR_CHECK(what.size() == test.br.size());
277
278                for(std::size_t i = 0; i < what.size() && i < test.br.size(); ++i)
279                {
280                    BOOST_XPR_CHECK(!what[i].matched && test.br[i] == empty || test.br[i] == what[i].str());
281                }
282            }
283            else
284            {
285                // match failed: was it expected to fail?
286                BOOST_XPR_CHECK(0 == test.br.size());
287            }
288        }
289    }
290    catch(regex_error const &e)
291    {
292        BOOST_ERROR(format_msg(e.what()).c_str());
293    }
294}
295
296///////////////////////////////////////////////////////////////////////////////
297// run_test_impl
298//   run the current test
299void run_test_a()
300{
301    run_test_impl(test);
302}
303
304///////////////////////////////////////////////////////////////////////////////
305// run_test_u
306//   widen the current test and run it
307void run_test_u()
308{
309    #ifndef BOOST_XPRESSIVE_NO_WREGEX
310    test_case<wchar_t> wtest = ::widen(test);
311    run_test_impl(wtest);
312    #endif
313}
314
315///////////////////////////////////////////////////////////////////////////////
316// run_test
317void run_test()
318{
319    run_test_a();
320    run_test_u();
321}
322
323///////////////////////////////////////////////////////////////////////////////
324// open_test
325bool open_test()
326{
327    // This test-file is usually run from either $BOOST_ROOT/status, or
328    // $BOOST_ROOT/libs/xpressive/test. Therefore, the relative path
329    // to the data file this test depends on will be one of two
330    // possible paths.
331
332    // first assume we are being run from boost_root/status
333    in.open("../libs/xpressive/test/regress.txt");
334
335    if(!in.good())
336    {
337        // couldn't find the data file so try to find the data file from the test dir
338        in.clear();
339        in.open("./regress.txt");
340    }
341
342    return in.good();
343}
344
345///////////////////////////////////////////////////////////////////////////////
346// test_main
347//   read the tests from the input file and execute them
348int test_main(int argc, char* argv[])
349{
350    if(!open_test())
351    {
352        std::cout << "Error: unable to open input file." << std::endl;
353        return -1;
354    }
355
356    while(get_test())
357    {
358        run_test();
359        ++test_count;
360    }
361
362    std::cout << test_count << " tests completed." << std::endl;
363
364    return 0;
365}
366
367///////////////////////////////////////////////////////////////////////////////
368// debug_init
369static const struct debug_init
370{
371    debug_init()
372    {
373    #if defined(_MSC_VER) && defined(_DEBUG)
374        // Send warnings, errors and asserts to STDERR
375        _CrtSetReportMode(_CRT_WARN,   _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG);
376        _CrtSetReportFile(_CRT_WARN,   _CRTDBG_FILE_STDERR);
377        _CrtSetReportMode(_CRT_ERROR,  _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG);
378        _CrtSetReportFile(_CRT_ERROR,  _CRTDBG_FILE_STDERR);
379        _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG);
380        _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);
381
382        // Check for leaks at program termination
383        _CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF | _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG));
384
385        //_CrtSetBreakAlloc(221);
386    #endif
387    }
388} g_debug_init;
Note: See TracBrowser for help on using the repository browser.