[vm] Fix EXPECT_STREQ output when the common prefix and suffix overlap.
Currently, the common prefix and suffix can end up being larger than one of the two strings, if the other string has extra content that matches the portion in the overlap. For example, in JSON output, there might be a missing } in a run of consecutive }s when closing JSON outputs. (Or, more likely thanks to our infrastructure for generating JSON outputs, that the expected string has an extra }.) In this case, the returned mismatches are empty, making it hard to discern what went wrong. This CL fixes that by limiting the common suffix to be no larger than the portion of the smaller string after the common prefix. Also add handling in the case where the two strings are equal (here, returning the escaped string in the prefix and leaving the other outputs empty), though this shouldn't happen since this function is only called when the null-terminated strings are not equal. TEST=ci (manual testing on while working on CL 450381) Change-Id: I52140f32bd44d7e31cf6ba97d862a89ac0568f02 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/499221 Reviewed-by: Alexander Markov <alexmarkov@google.com> Commit-Queue: Tess Strickland <sstrickl@google.com>
This commit is contained in:
committed by
dart-scoped@luci-project-accounts.iam.gserviceaccount.com
parent
4ee8852d77
commit
73a2c6c9a7
@@ -175,6 +175,16 @@ static void Escape(std::string& dst, const char* src, int len) {
|
||||
}
|
||||
}
|
||||
|
||||
// Splits two null-terminated strings into a common prefix and suffix
|
||||
// and the mismatched parts of the two strings, escaping the outputs.
|
||||
//
|
||||
// If the two strings are the same, then an escaped version of
|
||||
// the string is in the prefix and the other outputs are empty.
|
||||
//
|
||||
// If there is no common prefix and suffix or the size of the common
|
||||
// prefix and suffix combined is small in comparison to the size
|
||||
// of the two strings, then the prefix and suffix are empty to limit
|
||||
// the noise in the output of Expect::StringEquals.
|
||||
static void FindCommonPrefixAndSuffix(const char* expected,
|
||||
const char* actual,
|
||||
std::string& prefix,
|
||||
@@ -185,12 +195,29 @@ static void FindCommonPrefixAndSuffix(const char* expected,
|
||||
const int actual_len = strlen(actual);
|
||||
int prefix_length = 0;
|
||||
while (expected[prefix_length] == actual[prefix_length]) {
|
||||
// Either the prefix or actual string is a prefix of the other.
|
||||
if (expected[prefix_length] == '\0' || actual[prefix_length] == '\0') {
|
||||
break;
|
||||
}
|
||||
prefix_length += 1;
|
||||
}
|
||||
|
||||
if (prefix_length == expected_len && prefix_length == actual_len) {
|
||||
// The two are equal, so just escape the whole string into the prefix.
|
||||
Escape(prefix, expected, prefix_length);
|
||||
return;
|
||||
}
|
||||
|
||||
int suffix_length = 0;
|
||||
while (expected[(expected_len - 1) - suffix_length] ==
|
||||
actual[(actual_len - 1) - suffix_length]) {
|
||||
if (prefix_length + suffix_length == expected_len ||
|
||||
prefix_length + suffix_length == actual_len) {
|
||||
// The prefix and suffix cover the entirety of one of the
|
||||
// two strings, meaning the actual string either has missing
|
||||
// or extra contents compared to the expected string.
|
||||
break;
|
||||
}
|
||||
suffix_length += 1;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user