📄 tests.cpp
字号:
for(unsigned int i = 0; i < m.size(); ++i, j += 2)
{
if(m[i].matched == false)
{
if(matches[j] != -1)
{
begin_error();
cout << "regex++ API result mismatch in sub-expression " << i <<
", found (" << (m[i].first - x) << "," <<
(m[i].second - x) << ") expected (" <<
matches[j] << "," << matches[j+1] << ")" << endl;
}
}
else if(((m[i].first - x) != matches[j]) || ((m[i].second - x) != matches[j+1]))
{
begin_error();
cout << "regex++ API result mismatch in sub-expression " << i <<
", found (" << (m[i].first - x) << "," <<
(m[i].second - x) << ") expected (" <<
matches[j] << "," << matches[j+1] << ")" << endl;
}
}
//
// now check $` and $':
//
if((m[-1].first != x) || (m[-1].second != m[0].first))
{
begin_error();
cout << "regex++ API result mismatch in $` (match -1), found (" <<
(m[-1].first - x) << "," << (m[-1].second - x) << ") expected (0" <<
"," << matches[0] << ")" << endl;
}
if(((flags[3] & match_partial) == 0) && ((m[-2].first != m[0].second) || (m[-2].second != y)))
{
begin_error();
cout << "regex++ API result mismatch in $' (match -2), found (" <<
(m[-2].first - x) << "," << (m[-2].second - x) << ") expected (" <<
matches[1] << "," << (y-x) << ")" << endl;
}
#if !(defined(BOOST_MSVC) && (BOOST_MSVC <= 1300)) && !defined(BOOST_REGEX_V3)
//
// now try comparison operators:
string_type s(m[0]);
if((s != m[0]) || (m[0] != s)
|| !(s == m[0]) || !(m[0] == s)
|| (s < m[0]) || (m[0] < s)
|| (s > m[0]) || (m[0] > s)
|| !(s <= m[0]) || !(m[0] <= s)
|| !(s >= m[0]) || !(m[0] >= s))
{
begin_error();
cout << "string comparison failed for result" << std::endl;
}
if(s.find_first_of((string_type::value_type)0) == string_type::npos)
{
if((m[0] != s.c_str()) || (s.c_str() != m[0])
|| !(m[0] == s.c_str()) || !(s.c_str() == m[0])
|| (m[0] > s.c_str()) || (s.c_str() > m[0])
|| (m[0] < s.c_str()) || (s.c_str() < m[0])
|| !(m[0] >= s.c_str()) || !(s.c_str() >= m[0])
|| !(m[0] <= s.c_str()) || !(s.c_str() <= m[0]))
{
begin_error();
cout << "string comparison failed for result" << std::endl;
}
}
//
// now try addition operators:
string_type sa(1, (string_type::value_type)'a');
if(
((m[0] + sa) != (s + sa))
|| (sa + m[0]) != (sa + s)
|| ((m[0] + m[0]) != (s + s))
|| ((m[0] + m[0]) != (s + s))
|| ((m[0] + sa[0]) != (s + sa[0]))
|| ((sa[0] + m[0]) != (sa[0] + s))
)
{
begin_error();
cout << "string addition failed for result" << std::endl;
}
if(s.find_first_of((string_type::value_type)0) == string_type::npos)
{
if(
((m[0] + sa.c_str()) != (s + sa))
|| ((sa.c_str() + m[0]) != (sa + s))
)
{
begin_error();
cout << "string addition failed for result" << std::endl;
}
}
#endif
//
// now try alternative forms of regex_search if available:
#if !defined(BOOST_NO_FUNCTION_TEMPLATE_ORDERING)
if(!recurse)
{
std::basic_string<char_t> s2(search_text.begin(), search_text.end());
match_results<std::basic_string<char_t>::const_iterator> sm;
if(regex_search(s2, sm, e, static_cast<boost::match_flag_type>(flags[3])))
{
if(compare_result(sm, m) == false)
{
begin_error();
cout << "regex++ API result mismatch in regex_search(const std::string&, match_results&, const basic_regex&, int)" << endl;
}
}
else
{
begin_error();
cout << "regex++ API result mismatch in regex_search(const std::string&, match_results&, const basic_regex&, int)" << endl;
}
#ifndef BOOST_REGEX_V3
if(!regex_search(s2, e, static_cast<boost::match_flag_type>(flags[3]))
|| !regex_search(s2.begin(), s2.end(), e, static_cast<boost::match_flag_type>(flags[3])))
{
begin_error();
cout << "regex++ API result mismatch in regex_search(const std::string&, const basic_regex&, int)" << endl;
}
#endif
//
// partial match should give same result as full match
// provided a full match is expected:
//
if(matches[0] > 0)
{
if(regex_search(x, y, m, e, static_cast<boost::match_flag_type>(flags[3]) | boost::match_partial))
{
if(compare_result(sm, m) == false)
{
begin_error();
cout << "regex++ API result mismatch in regex_search when enabling match_partial" << endl;
}
}
else
{
begin_error();
cout << "regex++ API result: match not found when match_partial specified" << endl;
}
}
if(s2.find(char_t(0)) == std::basic_string<char_t>::npos)
{
match_results<const char_t*> ssm;
if(regex_search(search_text.c_str(), ssm, e, static_cast<boost::match_flag_type>(flags[3])))
{
if(compare_result(ssm, m) == false)
{
begin_error();
cout << "regex++ API result mismatch in regex_search(const char_t*, match_results&, const basic_regex&, int)" << endl;
}
}
else
{
begin_error();
cout << "regex++ API result mismatch in regex_search(const char_t*, match_results&, const basic_regex&, int)" << endl;
}
#ifndef BOOST_REGEX_V3
if(!regex_search(search_text.c_str(), e, static_cast<boost::match_flag_type>(flags[3])))
{
begin_error();
cout << "regex++ API result mismatch in regex_search(const char_t*, const basic_regex&, int)" << endl;
}
#endif
}
}
if((false == recurse) && (matches[0] == 0) && (matches[1] == static_cast<int>(search_text.size())))
{
//
// match expected on whole string, so all versions
// of regex_match should also succeed:
//
match_results< debug_iterator<string_type::iterator>, allocator_type> m1;
debug_iterator<string_type::iterator> x1(search_text.begin(), search_text.begin(), search_text.end());
debug_iterator<string_type::iterator> y1(search_text.end(), search_text.begin(), search_text.end());
if(regex_match(x1, y1, m1, e, static_cast<boost::match_flag_type>(flags[3])))
{
if(compare_result(m1, m) == false)
{
begin_error();
cout << "regex++ API result mismatch in regex_match(iterator, iterator, match_results&, const basic_regex&, int)" << endl;
}
}
else
{
begin_error();
cout << "regex++ API result mismatch in regex_match(iterator, iterator, match_results&, const basic_regex&, int)" << endl;
}
std::basic_string<char_t> s3(search_text.begin(), search_text.end());
match_results<std::basic_string<char_t>::const_iterator> sm;
if(regex_match(s3, sm, e, static_cast<boost::match_flag_type>(flags[3])))
{
if(compare_result(sm, m) == false)
{
begin_error();
cout << "regex++ API result mismatch in regex_match(const std::string&, match_results&, const basic_regex&, int)" << endl;
}
}
else
{
begin_error();
cout << "regex++ API result mismatch in regex_match(const std::string&, match_results&, const basic_regex&, int)" << endl;
}
if(s3.find(char_t(0)) == std::basic_string<char_t>::npos)
{
match_results<const char_t*> ssm;
if(regex_match(search_text.c_str(), ssm, e, static_cast<boost::match_flag_type>(flags[3])))
{
if(compare_result(ssm, m) == false)
{
begin_error();
cout << "regex++ API result mismatch in regex_match(const char_t*, match_results&, const basic_regex&, int)" << endl;
}
}
else
{
begin_error();
cout << "regex++ API result mismatch in regex_match(const char_t*, match_results&, const basic_regex&, int)" << endl;
}
}
}
#endif
}
else
{
// match not found
if(matches[0] != -1)
{
begin_error();
cout << "Match expected but not found using regex++ API" << endl;
}
}
}
}
#if !defined(TEST_UNICODE)
unsigned int hl_match_id;
bool
#if (defined(__BORLANDC__) || defined(BOOST_MSVC)) && !defined(BOOST_DISABLE_WIN32)
__cdecl
#endif
hl_grep_test_proc(const RegEx& e)
{
std::ptrdiff_t start, end;
start = e.Position(0);
end = start + e.Length();
if((matches[hl_match_id] != start) || (matches[hl_match_id + 1] != end))
{
begin_error();
cout << "class RegEx grep match error: found [" << start << "," << end << "] expected [" << matches[hl_match_id] << "," << matches[hl_match_id+1] << "]" << endl;
}
if(0 == (flags[4] & REG_GREP))
{
for(unsigned int sub = 1; sub < e.Marks(); ++sub)
{
start = e.Position(sub);
end = start + e.Length(sub);
if((matches[2*sub] != start) || (matches[2*sub + 1] != end))
{
begin_error();
cout << "class RegEx grep match error: found in sub " << sub << " [" << start << "," << end << "] expected [" << matches[2*sub] << "," << matches[2*sub+1] << "]" << endl;
}
}
}
//
// check $`:
start = e.Position(-1);
end = start + e.Length(-1);
if(start == -1)
{
if(hl_match_id &&
( matches[hl_match_id] != matches[hl_match_id - 1] )
)
{
begin_error();
cout << "class RegEx grep error in $`: found [" << start << "," << end << "] expected [" << matches[hl_match_id-1] << "," << matches[hl_match_id] << "]" << endl;
}
else if((!hl_match_id) && (0 != matches[0]))
{
begin_error();
cout << "class RegEx grep error in $`: found [" << start << "," << end << "] expected [" << 0 << "," << matches[0] << "]" << endl;
}
}
else
{
if(hl_match_id &&
( (end != matches[hl_match_id]) || (start != matches[hl_match_id - 1]) )
)
{
begin_error();
cout << "class RegEx grep error in $`: found [" << start << "," << end << "] expected [" << matches[hl_match_id-1] << "," << matches[hl_match_id] << "]" << endl;
}
else if((!hl_match_id) && ((start != 0) || (end != matches[0])))
{
begin_error();
cout << "class RegEx grep error in $`: found [" << start << "," << end << "] expected [" << 0 << "," << matches[0] << "]" << endl;
}
}
//
// check $':
start = e.Position(-2);
end = start + e.Length(-2);
if(start == -1)
{
if(matches[hl_match_id + 1] != (int)search_text.size())
{
begin_error();
cout << "class RegEx grep error in $': found [" << start << "," << end << "] expected [" << matches[hl_match_id + 1] << "," << (search_text.size()) << "]" << endl;
}
}
else if((start != matches[hl_match_id + 1]) || (end != (int)search_text.size()))
{
begin_error();
cout << "class RegEx grep error in $': found [" << start << "," << end << "] expected [" << matches[hl_match_id + 1] << "," << (search_text.size()) << "]" << endl;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -