regress.ipp 14 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451
//////////////////////////////////////////////////////////////////////////////
// regress.ipp
//
//  (C) Copyright Eric Niebler 2004.
//  Use, modification and distribution are subject to the
//  Boost Software License, Version 1.0. (See accompanying file
//  LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

/*
 Revision history:
   7 March 2004 : Initial version.
*/

#include <locale>
#include <vector>
#include <string>
#include <fstream>
#include <iostream>
#include <boost/lexical_cast.hpp>
#include <boost/xpressive/xpressive.hpp>
#include <boost/test/unit_test.hpp>

#if defined(_MSC_VER) && defined(_DEBUG)
# define _CRTDBG_MAP_ALLOC
# include <crtdbg.h>
#endif

#if defined(BOOST_XPRESSIVE_TEST_WREGEX) && !defined(BOOST_XPRESSIVE_NO_WREGEX)
namespace std
{
    inline std::ostream &operator <<(std::ostream &sout, std::wstring const &wstr)
    {
        for(std::size_t n = 0; n < wstr.size(); ++n)
            sout.put(BOOST_USE_FACET(std::ctype<wchar_t>, std::locale()).narrow(wstr[n], '?'));
        return sout;
    }
}
#endif

#define BOOST_XPR_CHECK(pred)                                                   \
    if(pred) {} else { BOOST_ERROR(case_ << #pred); }

using namespace boost::unit_test;
using namespace boost::xpressive;

//////////////////////////////////////////////////////////////////////////////
// xpr_test_case
template<typename Char>
struct xpr_test_case
{
    typedef std::basic_string<Char> string_type;
    std::string section;
    string_type str;
    string_type pat;
    string_type sub;
    string_type res;
    regex_constants::syntax_option_type syntax_flags;
    regex_constants::match_flag_type match_flags;
    std::vector<string_type> br;

    xpr_test_case()
    {
        this->reset();
    }

    void reset()
    {
        this->section.clear();
        this->str.clear();
        this->pat.clear();
        this->sub.clear();
        this->res.clear();
        this->br.clear();
        this->syntax_flags = regex_constants::ECMAScript;
        this->match_flags = regex_constants::match_default | regex_constants::format_first_only;
    }
};

//////////////////////////////////////////////////////////////////////////////
// globals
std::ifstream in;
unsigned int test_count = 0;

// The global object that contains the current test case
xpr_test_case<char> test;

struct test_case_formatter
{
    friend std::ostream &operator <<(std::ostream &sout, test_case_formatter)
    {
        sout << test.section << " /" << test.pat << "/ : ";
        return sout;
    }
};

test_case_formatter const case_ = {};

#if defined(BOOST_XPRESSIVE_TEST_WREGEX) && !defined(BOOST_XPRESSIVE_NO_WREGEX)
///////////////////////////////////////////////////////////////////////////////
// widen
//  make a std::wstring from a std::string by widening according to the
//  current ctype<char> facet
inline std::wstring widen(std::string const &str)
{
    std::ctype<char> const &ct = BOOST_USE_FACET(std::ctype<char>, std::locale());
    std::wstring res;
    for(size_t i=0; i<str.size(); ++i)
    {
        res += ct.widen(str[i]);
    }
    return res;
}

///////////////////////////////////////////////////////////////////////////////
// widen
//  widens an entire test case
xpr_test_case<wchar_t> widen(xpr_test_case<char> const &test)
{
    xpr_test_case<wchar_t> wtest;
    wtest.section = test.section;
    wtest.str = ::widen(test.str);
    wtest.pat = ::widen(test.pat);
    wtest.sub = ::widen(test.sub);
    wtest.res = ::widen(test.res);
    wtest.syntax_flags = test.syntax_flags;
    wtest.match_flags = test.match_flags;
    wtest.br.reserve(test.br.size());
    for(std::size_t i = 0; i < test.br.size(); ++i)
    {
        wtest.br.push_back(::widen(test.br[i]));
    }
    return wtest;
}
#endif // BOOST_XPRESSIVE_NO_WREGEX

std::string escape(std::string str)
{
    for(std::string::size_type pos = 0; std::string::npos != (pos = str.find('\\', pos)); ++pos)
    {
        if(pos + 1 == str.size())
            break;

        switch(str[pos + 1])
        {
        case '\\': str.replace(pos, 2, "\\"); break;
        case 'n': str.replace(pos, 2, "\n"); break;
        case 'r': str.replace(pos, 2, "\r"); break;
        }
    }
    return str;
}

///////////////////////////////////////////////////////////////////////////////
// get_test
//   read the next section out of the input file, and fill out
//   the global variables
bool get_test()
{
    test.reset();
    bool first = true;
    std::string line;
    smatch what;

    sregex const rx_sec = '[' >> (s1= +_) >> ']';
    sregex const rx_str = "str=" >> (s1= *_);
    sregex const rx_pat = "pat=" >> (s1= *_);
    sregex const rx_flg = "flg=" >> (s1= *_);
    sregex const rx_sub = "sub=" >> (s1= *_);
    sregex const rx_res = "res=" >> (s1= *_);
    sregex const rx_br = "br" >> (s1= +digit) >> '=' >> (s2= *_);

    while(in.good())
    {
        std::getline(in, line);

        if(!line.empty() && '\r' == line[line.size()-1])
        {
            line.erase(line.size()-1);
        }

        if(regex_match(line, what, rx_sec))
        {
            if(!first)
            {
                if(what[1] != "end")
                {
                    BOOST_FAIL(("invalid input : " + line).c_str());
                }
                break;
            }

            first = false;
            test.section = what[1].str();
        }
        else if(regex_match(line, what, rx_str))
        {
            test.str = escape(what[1].str());
        }
        else if(regex_match(line, what, rx_pat))
        {
            test.pat = what[1].str();
        }
        else if(regex_match(line, what, rx_sub))
        {
            test.sub = what[1].str();
        }
        else if(regex_match(line, what, rx_res))
        {
            test.res = escape(what[1].str());
        }
        else if(regex_match(line, what, rx_flg))
        {
            std::string flg = what[1].str();

            if(std::string::npos != flg.find('i'))
            {
                test.syntax_flags = test.syntax_flags | regex_constants::icase;
            }
            if(std::string::npos == flg.find('m'))
            {
                test.syntax_flags = test.syntax_flags | regex_constants::single_line;
            }
            if(std::string::npos == flg.find('s'))
            {
                test.syntax_flags = test.syntax_flags | regex_constants::not_dot_newline;
            }
            if(std::string::npos != flg.find('x'))
            {
                test.syntax_flags = test.syntax_flags | regex_constants::ignore_white_space;
            }
            if(std::string::npos != flg.find('g'))
            {
                test.match_flags = test.match_flags & ~regex_constants::format_first_only;
            }
            if(std::string::npos != flg.find('a'))
            {
                test.match_flags = test.match_flags | regex_constants::format_all;
            }
            if(std::string::npos != flg.find('p'))
            {
                test.match_flags = test.match_flags | regex_constants::format_perl;
            }
            if(std::string::npos != flg.find('d'))
            {
                test.match_flags = test.match_flags | regex_constants::format_sed;
            }
        }
        else if(regex_match(line, what, rx_br))
        {
            std::size_t nbr = boost::lexical_cast<std::size_t>(what[1].str());

            if(nbr >= test.br.size())
            {
                test.br.resize(nbr + 1);
            }

            test.br[nbr] = escape(what[2].str());
        }
        else if(!line.empty() && ';' != line[0])
        {
            BOOST_FAIL((std::string("invalid input : ") + line).c_str());
        }
    }

    return !first;
}

///////////////////////////////////////////////////////////////////////////////
// run_test_impl
//   run the test
template<typename Char>
void run_test_impl(xpr_test_case<Char> const &test)
{
    try
    {
        Char const empty[] = {0};
        typedef typename std::basic_string<Char>::const_iterator iterator;
        basic_regex<iterator> rx = basic_regex<iterator>::compile(test.pat, test.syntax_flags);

        // Build the same regex for use with C strings
        basic_regex<Char const *> c_rx = basic_regex<Char const *>::compile(test.pat, test.syntax_flags);

        if(!test.res.empty())
        {
            // test regex_replace
            std::basic_string<Char> res = regex_replace(test.str, rx, test.sub, test.match_flags);
            BOOST_CHECK_MESSAGE(res == test.res, case_ << res << " != " << test.res );

            // test regex_replace with NTBS format string
            std::basic_string<Char> res2 = regex_replace(test.str, rx, test.sub.c_str(), test.match_flags);
            BOOST_CHECK_MESSAGE(res2 == test.res, case_ << res2 << " != " << test.res );

            // test regex_replace with NTBS input string
            std::basic_string<Char> res3 = regex_replace(test.str.c_str(), c_rx, test.sub, test.match_flags);
            BOOST_CHECK_MESSAGE(res3 == test.res, case_ << res3 << " != " << test.res );

            // test regex_replace with NTBS input string and NTBS format string
            std::basic_string<Char> res4 = regex_replace(test.str.c_str(), c_rx, test.sub.c_str(), test.match_flags);
            BOOST_CHECK_MESSAGE(res4 == test.res, case_ << res4 << " != " << test.res );
        }

        if(0 == (test.match_flags & regex_constants::format_first_only))
        {
            {
                // global search, use regex_iterator
                std::vector<sub_match<iterator> > br;
                regex_iterator<iterator> begin(test.str.begin(), test.str.end(), rx, test.match_flags), end;
                for(; begin != end; ++begin)
                {
                    match_results<iterator> const &what = *begin;
                    br.insert(br.end(), what.begin(), what.end());
                }

                // match succeeded: was it expected to succeed?
                BOOST_XPR_CHECK(br.size() == test.br.size());

                for(std::size_t i = 0; i < br.size() && i < test.br.size(); ++i)
                {
                    BOOST_XPR_CHECK((!br[i].matched && test.br[i] == empty) || test.br[i] == br[i].str());
                }
            }

            {
                // global search, use regex_token_iterator
                std::vector<typename sub_match<iterator>::string_type> br2;
                std::vector<int> subs(rx.mark_count() + 1, 0);
                // regex_token_iterator will extract all sub_matches, in order:
                for(std::size_t i = 0; i < subs.size(); ++i)
                {
                    subs[i] = static_cast<int>(i);
                }
                regex_token_iterator<iterator> begin2(test.str.begin(), test.str.end(), rx, subs, test.match_flags), end2;
                for(; begin2 != end2; ++begin2)
                {
                    br2.push_back(*begin2);
                }

                // match succeeded: was it expected to succeed?
                BOOST_XPR_CHECK(br2.size() == test.br.size());

                for(std::size_t i = 0; i < br2.size() && i < test.br.size(); ++i)
                {
                    BOOST_XPR_CHECK(test.br[i] == br2[i]);
                }
            }
        }
        else
        {
            // test regex_search
            match_results<iterator> what;
            if(regex_search(test.str, what, rx, test.match_flags))
            {
                // match succeeded: was it expected to succeed?
                BOOST_XPR_CHECK(what.size() == test.br.size());

                for(std::size_t i = 0; i < what.size() && i < test.br.size(); ++i)
                {
                    BOOST_XPR_CHECK((!what[i].matched && test.br[i] == empty) || test.br[i] == what[i].str());
                }
            }
            else
            {
                // match failed: was it expected to fail?
                BOOST_XPR_CHECK(0 == test.br.size());
            }
        }
    }
    catch(regex_error const &e)
    {
        BOOST_ERROR(case_ << e.what());
    }
}

///////////////////////////////////////////////////////////////////////////////
// run_test_impl
//   run the current test
void run_test()
{
    #if defined(BOOST_XPRESSIVE_TEST_WREGEX) && !defined(BOOST_XPRESSIVE_NO_WREGEX)
    xpr_test_case<wchar_t> wtest = ::widen(test);
    run_test_impl(wtest);
    #else
    run_test_impl(test);
    #endif
}

static char const * s_argv1;

///////////////////////////////////////////////////////////////////////////////
// open_test
bool open_test()
{
    in.open( s_argv1? s_argv1: "regress.txt" );
    return in.good();
}

///////////////////////////////////////////////////////////////////////////////
// test_main
//   read the tests from the input file and execute them
void test_main()
{
    #if !defined(BOOST_XPRESSIVE_TEST_WREGEX) || !defined(BOOST_XPRESSIVE_NO_WREGEX)
    if(!open_test())
    {
        BOOST_ERROR("Error: unable to open input file.");
    }

    while(get_test())
    {
        run_test();
        ++test_count;
    }
    #endif

    std::cout << test_count << " tests completed." << std::endl;
}

///////////////////////////////////////////////////////////////////////////////
// init_unit_test_suite
//
test_suite* init_unit_test_suite( int argc, char* argv[] )
{
    s_argv1 = argv[1];

    test_suite *test = BOOST_TEST_SUITE("basic regression test");
    test->add(BOOST_TEST_CASE(&test_main));
    return test;
}

///////////////////////////////////////////////////////////////////////////////
// debug_init
static const struct debug_init
{
    debug_init()
    {
    #if defined(_MSC_VER) && defined(_DEBUG)
        // Send warnings, errors and asserts to STDERR
        _CrtSetReportMode(_CRT_WARN,   _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG);
        _CrtSetReportFile(_CRT_WARN,   _CRTDBG_FILE_STDERR);
        _CrtSetReportMode(_CRT_ERROR,  _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG);
        _CrtSetReportFile(_CRT_ERROR,  _CRTDBG_FILE_STDERR);
        _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG);
        _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);

        // Check for leaks at program termination
        _CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF | _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG));

        //_CrtSetBreakAlloc(221);
    #endif
    }
} g_debug_init;