forked from chipsalliance/verible
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsyntax_tree_search_test_utils.cc
126 lines (111 loc) · 4.53 KB
/
syntax_tree_search_test_utils.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Copyright 2017-2020 The Verible Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "common/analysis/syntax_tree_search_test_utils.h"
#include <iostream>
#include <iterator>
#include <set>
#include <vector>
#include "absl/strings/string_view.h"
#include "common/analysis/syntax_tree_search.h"
#include "common/text/token_info.h"
#include "common/text/tree_utils.h"
#include "common/util/algorithm.h"
#include "common/util/logging.h"
namespace verible {
// Returns 0 if strings' ranges are equal,
// negative if left is 'less' than right,
// positive if left is 'greater' than right.
// Compares lower bounds, and then upper bounds.
static int CompareStringRanges(absl::string_view left,
absl::string_view right) {
// tuple-compare the bounds of the string_view ranges (lexicographical)
{
const int delta = std::distance(right.begin(), left.begin());
if (delta != 0) {
return delta;
}
}
{
const int delta = std::distance(right.end(), left.end());
if (delta != 0) {
return delta;
}
}
// string_views point into same buffer, and thus should have equal contents
CHECK_EQ(left, right);
return 0;
}
struct LessStringRanges {
bool operator()(absl::string_view left, absl::string_view right) const {
return CompareStringRanges(left, right) < 0;
}
};
typedef std::set<absl::string_view, LessStringRanges> StringRangeSet;
// This function helps find symmetric differences between two sets
// of findings (actual vs. expected) based on locations.
static int CompareFindingLocation(absl::string_view lhs, const TokenInfo& rhs) {
const int delta = CompareStringRanges(lhs, rhs.text());
// Then compare enums, where we only care about equality.
return delta;
}
// TODO(b/151371397): refactor this for re-use for multi-findings style tests.
bool SyntaxTreeSearchTestCase::ExactMatchFindings(
const std::vector<TreeSearchMatch>& actual_findings, absl::string_view base,
std::ostream* diffstream) const {
// Convert actual_findings into string ranges. Ignore matches' context.
StringRangeSet actual_findings_ranges;
for (const auto& finding : actual_findings) {
if (finding.match == nullptr) continue;
const auto& match_symbol(*finding.match);
const absl::string_view spanned_text = StringSpanOfSymbol(match_symbol);
// Spanned text can be empty when a subtree is devoid of leaves.
if (spanned_text.empty()) continue;
actual_findings_ranges.insert(spanned_text);
}
// Due to the order in which findings are visited, we can assert that
// the reported findings are thus ordered.
// TODO(fangism): It wouldn't be too expensive to verify this assertion.
// The expected findings are also ordered by construction.
const auto expected_findings = FindImportantTokens(base);
// Thus, we can use an algorithm like std::set_symmetric_difference().
// These containers will catch unmatched differences found.
std::vector<absl::string_view> unmatched_actual_findings;
std::vector<TokenInfo> unmatched_expected_findings;
set_symmetric_difference_split(
actual_findings_ranges.begin(), actual_findings_ranges.end(),
expected_findings.begin(), expected_findings.end(),
std::back_inserter(unmatched_actual_findings),
std::back_inserter(unmatched_expected_findings), &CompareFindingLocation);
bool all_match = true;
const TokenInfo::Context context(base);
if (!unmatched_actual_findings.empty()) {
all_match = false;
*diffstream
<< "The following actual findings did not match the expected ones:\n";
for (const auto& finding : unmatched_actual_findings) {
constexpr int kIgnored = -1;
TokenInfo(kIgnored, finding).ToStream(*diffstream, context) << std::endl;
}
}
if (!unmatched_expected_findings.empty()) {
all_match = false;
*diffstream
<< "The following expected findings did not match the ones found:\n";
for (const auto& finding : unmatched_expected_findings) {
finding.ToStream(*diffstream, context) << std::endl;
}
}
return all_match;
}
} // namespace verible