Files
boost_regex/test/regress/test_icu.cpp
T
John Maddock c05af49444 Added object cache - internal state to traits classes is now cached.
Added first start to Unicode support.
Enabled external template instance support.
Fixed VC6 support.


[SVN r25914]
2004-10-28 11:17:26 +00:00

204 lines
6.7 KiB
C++

/*
*
* Copyright (c) 2004
* Dr John Maddock
*
* 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)
*
*/
/*
* LOCATION: see http://www.boost.org for most recent version.
* FILE test_icu.cpp
* VERSION see <boost/version.hpp>
* DESCRIPTION: Test code for Unicode regexes with ICU support.
*/
//
// We can only build this if we have ICU support:
//
#ifdef TEST_ICU
#include <boost/regex/icu.hpp>
#include "test.hpp"
void test_icu(const wchar_t&, const test_regex_search_tag& )
{
typedef boost::u16_to_u32_iterator<std::wstring::const_iterator, ::UChar32> conv_iterator;
boost::u32regex r;
if(*test_locale::c_str())
{
::Locale l(test_locale::c_str());
if(l.isBogus())
return;
r.imbue(l);
}
const std::wstring& expression = test_info<wchar_t>::expression();
boost::regex_constants::syntax_option_type syntax_options = test_info<UChar32>::syntax_options();
try{
r.assign(conv_iterator(expression.begin()), conv_iterator(expression.end()), syntax_options);
if(r.status())
{
BOOST_REGEX_TEST_ERROR("Expression did not compile when it should have done, error code = " << r.status(), UChar32);
}
const std::wstring& search_text = test_info<wchar_t>::search_text();
boost::regex_constants::match_flag_type opts = test_info<wchar_t>::match_options();
const int* answer_table = test_info<wchar_t>::answer_table();
boost::match_results<conv_iterator> what;
if(boost::regex_search(
conv_iterator(search_text.begin()),
conv_iterator(search_text.end()),
what,
r,
opts))
{
test_result(what, conv_iterator(search_text.begin()), answer_table);
}
else if(answer_table[0] >= 0)
{
// we should have had a match but didn't:
BOOST_REGEX_TEST_ERROR("Expected match was not found.", UChar32);
}
}
catch(const boost::bad_expression& e)
{
BOOST_REGEX_TEST_ERROR("Expression did not compile when it should have done: " << e.what(), UChar32);
}
catch(const std::runtime_error& r)
{
BOOST_REGEX_TEST_ERROR("Received an unexpected std::runtime_error: " << r.what(), UChar32);
}
catch(const std::exception& r)
{
BOOST_REGEX_TEST_ERROR("Received an unexpected std::exception: " << r.what(), UChar32);
}
catch(...)
{
BOOST_REGEX_TEST_ERROR("Received an unexpected exception of unknown type", UChar32);
}
}
void test_icu(const wchar_t&, const test_invalid_regex_tag&)
{
typedef boost::u16_to_u32_iterator<std::wstring::const_iterator, ::UChar32> conv_iterator;
const std::wstring& expression = test_info<wchar_t>::expression();
boost::regex_constants::syntax_option_type syntax_options = test_info<wchar_t>::syntax_options();
boost::u32regex r;
if(*test_locale::c_str())
{
::Locale l(test_locale::c_str());
if(l.isBogus())
return;
r.imbue(l);
}
//
// try it with exceptions disabled first:
//
try
{
if(0 == r.assign(conv_iterator(expression.begin()), conv_iterator(expression.end()), syntax_options | boost::regex_constants::no_except).status())
{
BOOST_REGEX_TEST_ERROR("Expression compiled when it should not have done so.", wchar_t);
}
}
catch(...)
{
BOOST_REGEX_TEST_ERROR("Unexpected exception thrown.", wchar_t);
}
//
// now try again with exceptions:
//
bool have_catch = false;
try{
r.assign(conv_iterator(expression.begin()), conv_iterator(expression.end()), syntax_options);
#ifdef BOOST_NO_EXCEPTIONS
if(r.status())
have_catch = true;
#endif
}
catch(const boost::bad_expression&)
{
have_catch = true;
}
catch(const std::runtime_error& r)
{
have_catch = true;
BOOST_REGEX_TEST_ERROR("Expected a bad_expression exception, but a std::runtime_error instead: " << r.what(), wchar_t);
}
catch(const std::exception& r)
{
have_catch = true;
BOOST_REGEX_TEST_ERROR("Expected a bad_expression exception, but a std::exception instead: " << r.what(), wchar_t);
}
catch(...)
{
have_catch = true;
BOOST_REGEX_TEST_ERROR("Expected a bad_expression exception, but got an exception of unknown type instead", wchar_t);
}
if(!have_catch)
{
// oops expected exception was not thrown:
BOOST_REGEX_TEST_ERROR("Expected an exception, but didn't find one.", wchar_t);
}
}
void test_icu(const wchar_t&, const test_regex_replace_tag&)
{
const std::wstring& expression = test_info<wchar_t>::expression();
boost::regex_constants::syntax_option_type syntax_options = test_info<UChar32>::syntax_options();
boost::u32regex r;
try{
r.assign(expression.begin(), expression.end(), syntax_options);
if(r.status())
{
BOOST_REGEX_TEST_ERROR("Expression did not compile when it should have done, error code = " << r.status(), UChar32);
}
typedef std::vector<UChar32> string_type;
string_type search_text;
search_text.assign(test_info<UChar32>::search_text().begin(), test_info<UChar32>::search_text().end());
boost::regex_constants::match_flag_type opts = test_info<UChar32>::match_options();
string_type format_string;
format_string.assign(test_info<UChar32>::format_string().begin(), test_info<UChar32>::format_string().end());
format_string.push_back(0);
string_type result_string;
result_string.assign(test_info<UChar32>::result_string().begin(), test_info<UChar32>::result_string().end());
string_type result;
boost::regex_replace(std::back_inserter(result), search_text.begin(), search_text.end(), r, &*format_string.begin(), opts);
if(result != result_string)
{
BOOST_REGEX_TEST_ERROR("regex_replace generated an incorrect string result", UChar32);
}
}
catch(const boost::bad_expression& e)
{
BOOST_REGEX_TEST_ERROR("Expression did not compile when it should have done: " << e.what(), UChar32);
}
catch(const std::runtime_error& r)
{
BOOST_REGEX_TEST_ERROR("Received an unexpected std::runtime_error: " << r.what(), UChar32);
}
catch(const std::exception& r)
{
BOOST_REGEX_TEST_ERROR("Received an unexpected std::exception: " << r.what(), UChar32);
}
catch(...)
{
BOOST_REGEX_TEST_ERROR("Received an unexpected exception of unknown type", UChar32);
}
}
#else
#include "test.hpp"
void test_icu(const wchar_t&, const test_regex_search_tag&){}
void test_icu(const wchar_t&, const test_invalid_regex_tag&){}
void test_icu(const wchar_t&, const test_regex_replace_tag&){}
#endif