📄 tests.cpp
字号:
/*
*
* Copyright (c) 1998-2002
* 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)
*
*/
/*
*
* FILE tests.cpp
* VERSION see <boost/version.hpp>
*
* the actual tests conducted by regress.
*
*/
#include <cassert>
#include <boost/regex.hpp>
#include "regress.h"
#ifndef BOOST_REGEX_NO_TEST
# ifdef BOOST_MSVC
# pragma warning(disable: 4244 4267)
#endif
using namespace boost;
template <class M1, class M2>
bool compare_result(const M1& sm, const M2& m)
{
if(sm.size() != m.size())
return false;
#ifdef BOOST_REGEX_V3
if(sm.line() != m.line())
return false;
#endif
for(unsigned int i = 0; i < sm.size(); ++i)
{
if(sm.position(i) != m.position(i))
return false;
if(sm.length(i) != m.length(i))
return false;
}
return true;
}
template <class M1>
bool compare_result(const M1& sm, const M1& m)
{
return sm == m;
}
template <class C, class T, class A>
void cpp_eh_tests(const basic_regex<C, T, A>& )
{
#ifndef __GNUC__
bool thrown = false;
// try set_expression form first:
#ifndef BOOST_NO_EXCEPTIONS
try
{
#endif
A a;
basic_regex<C, T, A> e(a);
e.set_expression(expression.c_str(), flags[2] | regex::use_except);
#ifndef BOOST_NO_EXCEPTIONS
}
catch(const boost::bad_expression&)
{
thrown = true;
}
catch(...){}
if(!thrown)
{
begin_error();
cout << "Error: expected exception not thrown" << endl;
}
#endif
// now try constructor form:
thrown = false;
#ifndef BOOST_NO_EXCEPTIONS
try
#endif
{
A a;
basic_regex<C, T, A> e(expression.c_str(), flags[2] | regex::use_except, a);
}
#ifndef BOOST_NO_EXCEPTIONS
catch(const boost::bad_expression&)
{
thrown = true;
}
catch(...){}
if(!thrown)
{
begin_error();
cout << "Error: expected exception not thrown" << endl;
}
#endif
#endif
}
template <class iterator>
iterator find_last_line(iterator start, iterator end)
{
iterator result = start;
while(start != end)
{
if(*start == '\n')
{
++start;
result = start;
}
else
++start;
}
return result;
}
template <class iterator>
unsigned int count_lines(iterator start, iterator end)
{
unsigned int result = 0;
while(start != end)
{
if(*start == '\n')
++result;
++start;
}
return result;
}
template <class iterator, class Alloc>
class grep_test_predicate
{
int match_id;
iterator base, term;
// rebind allocator to correct type:
#ifdef BOOST_REGEX_V3
typedef Alloc alloc_type;
#else
typedef typename detail::rebind_allocator<boost::sub_match<iterator>, Alloc>::type alloc_type;
#endif
public:
grep_test_predicate(iterator i, iterator j) : base(i), term(j) { match_id = 0; }
~grep_test_predicate(){}
bool operator()(const boost::match_results< iterator, alloc_type >& m);
};
template <class iterator, class Alloc>
bool grep_test_predicate<iterator, Alloc>::operator()(const boost::match_results< iterator, alloc_type >& m)
{
std::ptrdiff_t start, end;
start = m[0].first - base;
end = m[0].second - base;
if((matches[match_id] != start) || (matches[match_id + 1] != end))
{
begin_error();
cout << "regex++ grep match error: found [" << start << "," << end << "] expected [" << matches[match_id] << "," << matches[match_id+1] << "]" << endl;
}
//
// check $`:
start = m[-1].first - base;
end = m[-1].second - base;
if(match_id &&
( (end != matches[match_id]) || (start != matches[match_id - 1]) )
)
{
begin_error();
cout << "regex++ grep error in $`: found [" << start << "," << end << "] expected [" << matches[match_id-1] << "," << matches[match_id] << "]" << endl;
}
else if((!match_id) && ((start != 0) || (end != matches[0])))
{
begin_error();
cout << "regex++ grep error in $`: found [" << start << "," << end << "] expected [" << 0 << "," << matches[0] << "]" << endl;
}
//
// check $':
start = m[-2].first - base;
end = m[-2].second - base;
if((start != matches[match_id + 1]) || (end != (term-base)))
{
begin_error();
cout << "regex++ grep error in $': found [" << start << "," << end << "] expected [" << matches[match_id + 1] << "," << (term-base) << "]" << endl;
}
//
// now check line()
/* don't check this, it's not supported in the new algorithm....
start = m.line();
end = count_lines(base, iterator(m[0].first)) + 1;
if(start != end)
{
begin_error();
cout << "regex++ grep error in line(): found " << start << " expected " << end << endl;
}
*/
//
// now check line_start()
/* don't check this, it's not supported in the new algorithm....
start = m.line_start() - base;
end = find_last_line(base, iterator(m[0].first)) - base;
if(start != end)
{
begin_error();
cout << "regex++ grep error in line_start(): found " << start << " expected " << end << endl;
}
*/
match_id += 2;
return true;
}
template <class C, class T, class A>
void cpp_tests(const basic_regex<C, T, A>& e, bool recurse = true)
{
typedef typename basic_regex<C, T, A>::allocator_type base_allocator_type;
#ifdef BOOST_REGEX_V3
typedef base_allocator_type allocator_type;
#else
typedef typename detail::rebind_allocator<boost::sub_match<debug_iterator<string_type::iterator> >, base_allocator_type>::type allocator_type;
#endif
if(flags[4] & REG_MERGE)
{
//
// test merge code:
//
string_type s;
s = regex_merge(search_text, e, format_string.c_str(), static_cast<boost::match_flag_type>(flags[3]));
if(s != merge_string)
{
begin_error();
cout << "merge result mismatch: found \"" << make_narrow(s).c_str() << "\" expected \"" << make_narrow(merge_string.c_str()) << "\"" << endl;
}
return;
}
if(recurse)
{
// copy and assign test:
basic_regex<C, T, A> e2(e);
cpp_tests(e2, false);
e2 = e;
cpp_tests(e2, false);
basic_regex<C, T, A> e3;
#ifndef BOOST_REGEX_V3
e3.swap(e2);
cpp_tests(e3, false);
#endif
}
if(e.error_code())
{
// verify that we don't expect expression to compile
if(search_text != BOOST_RE_STR("!"))
{
begin_error();
cout << "Expression did not compile using regex++ API" << endl;
}
else if((recurse) && ((flags[3] & match_partial) == 0))
cpp_eh_tests(e);
}
else if(flags[4] & REG_GREP)
{
// try to do grep:
debug_iterator<string_type::iterator> x(search_text.begin(), search_text.begin(), search_text.end());
debug_iterator<string_type::iterator> y(search_text.end(), search_text.begin(), search_text.end());
grep_test_predicate<debug_iterator<string_type::iterator>, allocator_type> oi(x, y);
regex_grep(oi, x, y, e, static_cast<boost::match_flag_type>(flags[3]));
#if !defined(BOOST_NO_FUNCTION_TEMPLATE_ORDERING)
if(!recurse)
{
unsigned len = search_text.size();
const std::basic_string<char_t>& s = search_text;
grep_test_predicate<std::basic_string<char_t>::const_iterator, base_allocator_type> oi2(s.begin(), s.end());
regex_grep(oi2, s, e, static_cast<boost::match_flag_type>(flags[3]));
grep_test_predicate<const char_t*, base_allocator_type> oi3(s.c_str(), s.c_str()+s.size());
regex_grep(oi3, s.c_str(), e, static_cast<boost::match_flag_type>(flags[3]));
assert(s.size() == len);
assert(s.end() - s.begin() == (std::basic_string<char_t>::difference_type)len);
}
#endif
}
else
{
// try to find match
match_results< debug_iterator<string_type::iterator>, allocator_type> m;
debug_iterator<string_type::iterator> x(search_text.begin(), search_text.begin(), search_text.end());
debug_iterator<string_type::iterator> y(search_text.end(), search_text.begin(), search_text.end());
if(regex_search(x, y, m, e, static_cast<boost::match_flag_type>(flags[3])))
{
// special case for partial matches:
if(flags[4] & REG_PARTIAL_MATCH)
{
if(m[0].matched)
{
begin_error();
cout << "regex++ API result mismatch, found full match when partial match was expected: found ("
<< (m[0].first - x) << "," <<
(m[0].second - x) << ")" << endl;
}
else if(((m[0].first - x) != matches[0]) || ((m[0].second - x) != matches[1]))
{
begin_error();
cout << "regex++ API result mismatch in sub-expression " << 0 <<
", found (" << (m[0].first - x) << "," <<
(m[0].second - x) << ") expected (" <<
matches[0] << "," << matches[1] << ")" << endl;
}
return; // don't bother testing anything else for partial matches
}
// match found compare what matched with what we expect:
int j = 0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -