summaryrefslogtreecommitdiff
path: root/src/gtest-port.cc
diff options
context:
space:
mode:
authorvladlosev <vladlosev@861a406c-534a-0410-8894-cb66d6ee9925>2008-11-20 01:40:35 +0000
committervladlosev <vladlosev@861a406c-534a-0410-8894-cb66d6ee9925>2008-11-20 01:40:35 +0000
commitf904a612d9444ab36c07a8e619c113432e046f49 (patch)
treeec4a9020570acc6d09366e5b305b9d162c1a6026 /src/gtest-port.cc
parent6c84bcf915f498278cd28c9180b699595e7b6470 (diff)
downloadgtest-f904a612d9444ab36c07a8e619c113432e046f49.tar.gz
gtest-f904a612d9444ab36c07a8e619c113432e046f49.tar.bz2
gtest-f904a612d9444ab36c07a8e619c113432e046f49.tar.xz
Value-parameterized tests and many bugfixes
git-svn-id: http://googletest.googlecode.com/svn/trunk@120 861a406c-534a-0410-8894-cb66d6ee9925
Diffstat (limited to 'src/gtest-port.cc')
-rw-r--r--src/gtest-port.cc32
1 files changed, 28 insertions, 4 deletions
diff --git a/src/gtest-port.cc b/src/gtest-port.cc
index 000a4ab..9878cae 100644
--- a/src/gtest-port.cc
+++ b/src/gtest-port.cc
@@ -56,25 +56,49 @@ namespace internal {
// Implements RE. Currently only needed for death tests.
RE::~RE() {
- regfree(&regex_);
+ regfree(&partial_regex_);
+ regfree(&full_regex_);
free(const_cast<char*>(pattern_));
}
-// Returns true iff str contains regular expression re.
+// Returns true iff regular expression re matches the entire str.
+bool RE::FullMatch(const char* str, const RE& re) {
+ if (!re.is_valid_) return false;
+
+ regmatch_t match;
+ return regexec(&re.full_regex_, str, 1, &match, 0) == 0;
+}
+
+// Returns true iff regular expression re matches a substring of str
+// (including str itself).
bool RE::PartialMatch(const char* str, const RE& re) {
if (!re.is_valid_) return false;
regmatch_t match;
- return regexec(&re.regex_, str, 1, &match, 0) == 0;
+ return regexec(&re.partial_regex_, str, 1, &match, 0) == 0;
}
// Initializes an RE from its string representation.
void RE::Init(const char* regex) {
pattern_ = strdup(regex);
- is_valid_ = regcomp(&regex_, regex, REG_EXTENDED) == 0;
+
+ // Reserves enough bytes to hold the regular expression used for a
+ // full match.
+ const size_t full_regex_len = strlen(regex) + 10;
+ char* const full_pattern = new char[full_regex_len];
+
+ snprintf(full_pattern, full_regex_len, "^(%s)$", regex);
+ is_valid_ = regcomp(&full_regex_, full_pattern, REG_EXTENDED) == 0;
+ // We want to call regcomp(&partial_regex_, ...) even if the
+ // previous expression returns false. Otherwise partial_regex_ may
+ // not be properly initialized can may cause trouble when it's
+ // freed.
+ is_valid_ = (regcomp(&partial_regex_, regex, REG_EXTENDED) == 0) && is_valid_;
EXPECT_TRUE(is_valid_)
<< "Regular expression \"" << regex
<< "\" is not a valid POSIX Extended regular expression.";
+
+ delete[] full_pattern;
}
#endif // GTEST_HAS_DEATH_TEST