📄 tests.cpp
字号:
{ if(compare_result(sm, m) == false) { begin_error(); cout << "regex++ API result mismatch in regex_match(const std::string&, match_results&, const reg_expression&, int)" << endl; } } else { begin_error(); cout << "regex++ API result mismatch in regex_match(const std::string&, match_results&, const reg_expression&, int)" << endl; } if(s.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, flags[3])) { if(compare_result(ssm, m) == false) { begin_error(); cout << "regex++ API result mismatch in regex_match(const char_t*, match_results&, const reg_expression&, int)" << endl; } } else { begin_error(); cout << "regex++ API result mismatch in regex_match(const char_t*, match_results&, const reg_expression&, 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)__cdecl#endifhl_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; } hl_match_id += 2; return true;}void cpp_hl_tests(RegEx& e, bool recurse = true){ if(flags[4] & REG_MERGE) return; if(e.error_code()) { if(search_text != BOOST_RE_STR("!")) { begin_error(); cout << "Expression did not compile with class RegEx" << endl; } return; } if(recurse) { // copy and assign test: RegEx e2(e); cpp_hl_tests(e2, false); e2 = e; cpp_hl_tests(e2, false); } if(flags[4] & REG_GREP) { // try to do grep: hl_match_id = 0; GrepCallback cb = hl_grep_test_proc; e.Grep(cb, search_text.c_str(), flags[3]); } else { if(e.Search(search_text.c_str(), flags[3])) { unsigned int i = 0; unsigned int j = 0; while(matches[j] != -2) { if( (matches[j] != (int)e.Position(i)) || ((matches[j] != -1) && ((matches[j+1] - matches[j] != (int)e.Length(i)))) ) { begin_error(); cout << "RegEx::Search error in subexpression " << i << ": found [" << e.Position(i) << "," << (e.Position(i) + e.Length(i)) << "] expected [" << matches[j] << "," << matches[j+1] << "]" << endl; } ++i; j += 2; } } else { if(matches[0] != -1) { begin_error(); cout << "match expected but not found with RexEx::Search" << endl; } } // // test RegEx::Match only if we expect to match all of the input: // if((matches[0] == 0) && (matches[1] == search_text.size())) { if(e.Match(search_text.c_str(), flags[3])) { unsigned int i = 0; unsigned int j = 0; while(matches[j] != -2) { if( (matches[j] != (int)e.Position(i)) || ((matches[j] != -1) && ((matches[j+1] - matches[j] != (int)e.Length(i)))) ) { begin_error(); cout << "RegEx::Match error in subexpression " << i << ": found [" << e.Position(i) << "," << (e.Position(i) + e.Length(i)) << "] expected [" << matches[j] << "," << matches[j+1] << "]" << endl; } ++i; j += 2; } } else { begin_error(); cout << "Match expected but not found with RegEx::Match" << endl; } } }}#endiftemplate <class iterator>bool has_nulls(iterator i, iterator j){ while(i != j) { if(*i == 0) return true; ++i; } return false;}#ifdef TEST_UNICODE#undef regcomp#undef regerror#undef regexec#undef regfree#undef regex_t#define regcomp regcompW#define regerror regerrorW#define regexec regexecW#define regfree regfreeW#define regex_t regex_tW#endifvoid run_tests(){#ifndef TEST_UNICODE if(flags[4] & REG_UNICODE_ONLY) return;#endif#ifndef NO_CPP_TEST#ifndef BOOST_NO_EXCEPTIONS try {#endif unsigned int f = flags[2] & ~regbase::use_except; if(flags[0] & REG_ICASE) f |= regbase::icase; re_type e(expression.c_str(), f); cpp_tests(e, true);#ifndef BOOST_NO_EXCEPTIONS } catch(const std::exception& e) { // // this is only an error if the expression is expected to be valid: if(search_text != BOOST_RE_STR("!")) { begin_error(); cout << "Unexpected exception thrown from C++ library: " << e.what() << endl; } } catch(...) { begin_error(); cout << "Unexpected exception thrown from C++ library" << endl; }#endif // BOOST_NO_EXCEPTIONS#endif#if !defined(TEST_UNICODE)#ifndef BOOST_NO_EXCEPTIONS try {#endif if(((flags[3] & match_partial) == 0) && (flags[2] == regbase::normal) && (has_nulls(search_text.begin(), search_text.end()) == false)) { RegEx e; e.SetExpression(expression.c_str(), flags[0] & REG_ICASE); cpp_hl_tests(e, true); }#ifndef BOOST_NO_EXCEPTIONS } catch(const std::exception& ) { if(search_text != BOOST_RE_STR("!")) { begin_error(); cout << "Expression did not compile with class RegEx" << endl; } } catch(...) { begin_error(); cout << "Unexpected exception thrown from RegEx::SetExpression" << endl; }#endif // BOOST_NO_EXCEPTIONS#endif if(flags[4] & (REG_NO_POSIX_TEST | REG_GREP | REG_MERGE | REG_MERGE_COPY)) return; regex_t posix_expression; regmatch_t m[MAX_MATCHES]; if(regcomp(&posix_expression, expression.c_str(), flags[0]) == 0) { if(flags[1] & REG_STARTEND) { m[0].rm_so = 0; m[0].rm_eo = search_text.size(); } if(regexec(&posix_expression, search_text.c_str(), MAX_MATCHES, m, flags[1])) { // match not found if(matches[0] != -1) { begin_error(); cout << "Match expected but not found using POSIX API" << endl; } } else { // match found compare what matched with what we expect: int j = 0; for(unsigned int i = 0; i <= posix_expression.re_nsub; ++i, j += 2) { if((m[i].rm_so != matches[j]) || (m[i].rm_eo != matches[j+1])) { begin_error(); cout << "POSIX API result mismatch in sub-expression " << i << ", found (" << m[i].rm_so << "," << m[i].rm_eo << ") expected (" << matches[j] << "," << matches[j+1] << ")" << endl; } } } regfree(&posix_expression); } else { // expression did not compile if(search_text != BOOST_RE_STR("!")) { begin_error(); cout << "Expression did not compile using POSIX API" << endl; } }}//// error reporting:int last_line = 0;void begin_error(){ if(line != last_line) { cout << "Error in line " << line << " of file " << file << endl; cout << "Expression: " << make_narrow(expression).c_str() << endl; cout << "Search text: " << make_narrow(search_text).c_str() << endl; cout << "Flags: "; bool started = false; unsigned int id = 0; while(flag_data[id].len != 0) { if(flags[flag_data[id].id] & flag_data[id].value) { if(started) cout << " | "; cout << make_narrow(flag_data[id].name).c_str(); started = true; } ++id; } cout << endl; last_line = line; ++error_count; }}void reset_error(){ last_line = 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -