30
30
31
31
import java .util .ArrayList ;
32
32
import java .util .List ;
33
+ import java .util .regex .MatchResult ;
33
34
import java .util .regex .Matcher ;
34
35
import java .util .regex .Pattern ;
36
+ import java .util .stream .Collectors ;
35
37
36
38
/**
37
39
* Class representing a counts attribute of an IR rule.
@@ -53,7 +55,7 @@ public static Counts create(List<String> nodesWithCountConstraint, IRRule irRule
53
55
TestFormat .check (i + 1 < nodesWithCountConstraint .size (),
54
56
"Missing count " + getPostfixErrorMsg (irRule , node ));
55
57
String countConstraint = nodesWithCountConstraint .get (i + 1 );
56
- Comparison <Long > comparison = parseComparison (irRule , node , countConstraint );
58
+ Comparison <Integer > comparison = parseComparison (irRule , node , countConstraint );
57
59
constraints .add (new Constraint (node , comparison , nodeId ));
58
60
}
59
61
return new Counts (constraints );
@@ -63,9 +65,9 @@ private static String getPostfixErrorMsg(IRRule irRule, String node) {
63
65
return "for IR rule " + irRule .getRuleId () + ", node \" " + node + "\" at " + irRule .getMethod ();
64
66
}
65
67
66
- private static Comparison <Long > parseComparison (IRRule irRule , String node , String constraint ) {
68
+ private static Comparison <Integer > parseComparison (IRRule irRule , String node , String constraint ) {
67
69
String postfixErrorMsg = "in count constraint " + getPostfixErrorMsg (irRule , node );
68
- return ComparisonConstraintParser .parse (constraint , Long :: parseLong , postfixErrorMsg );
70
+ return ComparisonConstraintParser .parse (constraint , Integer :: parseInt , postfixErrorMsg );
69
71
}
70
72
71
73
@ Override
@@ -82,37 +84,30 @@ private void checkConstraints(CountsMatchResult result, String compilation) {
82
84
}
83
85
84
86
private void checkConstraint (CountsMatchResult result , String compilation , Constraint constraint ) {
85
- long foundCount = getFoundCount (compilation , constraint );
86
- Comparison <Long > comparison = constraint .comparison ;
87
- if (!comparison .compare (foundCount )) {
88
- result .addFailure (createRegexFailure (compilation , constraint , foundCount ));
87
+ List < String > countsMatches = getCountsMatches (compilation , constraint );
88
+ Comparison <Integer > comparison = constraint .comparison ;
89
+ if (!comparison .compare (countsMatches . size () )) {
90
+ result .addFailure (createRegexFailure (countsMatches , constraint ));
89
91
}
90
92
}
91
93
92
- private long getFoundCount (String compilation , Constraint constraint ) {
94
+ private List < String > getCountsMatches (String compilation , Constraint constraint ) {
93
95
Pattern pattern = Pattern .compile (constraint .nodeRegex );
94
96
Matcher matcher = pattern .matcher (compilation );
95
- return matcher .results ().count ( );
97
+ return matcher .results ().map ( MatchResult :: group ). collect ( Collectors . toList () );
96
98
}
97
99
98
- private CountsRegexFailure createRegexFailure (String compilation , Constraint constraint , long foundCount ) {
99
- Pattern p = Pattern .compile (constraint .nodeRegex );
100
- Matcher m = p .matcher (compilation );
101
- List <String > matches ;
102
- if (m .find ()) {
103
- matches = getMatchedNodes (m );
104
- } else {
105
- matches = new ArrayList <>();
106
- }
107
- return new CountsRegexFailure (constraint .nodeRegex , constraint .nodeId , foundCount , constraint .comparison , matches );
100
+ private CountsRegexFailure createRegexFailure (List <String > countsMatches , Constraint constraint ) {
101
+ return new CountsRegexFailure (constraint .nodeRegex , constraint .nodeId , countsMatches .size (), constraint .comparison ,
102
+ countsMatches );
108
103
}
109
104
110
105
static class Constraint {
111
106
final String nodeRegex ;
112
- final Comparison <Long > comparison ;
107
+ final Comparison <Integer > comparison ;
113
108
private final int nodeId ;
114
109
115
- Constraint (String nodeRegex , Comparison <Long > comparison , int nodeId ) {
110
+ Constraint (String nodeRegex , Comparison <Integer > comparison , int nodeId ) {
116
111
this .nodeRegex = nodeRegex ;
117
112
this .comparison = comparison ;
118
113
this .nodeId = nodeId ;
1 commit comments
openjdk-notifier[bot] commentedon Aug 15, 2022
Review
Issues