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 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
|
/*
* Copyright (c) 2015, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @key randomness
* @bug 8135068
* @summary Tests CompilerCommand's method matcher
* @modules java.base/jdk.internal.misc
* @library /test/lib /
*
* @build jdk.test.whitebox.WhiteBox
* @run driver jdk.test.lib.helpers.ClassFileInstaller jdk.test.whitebox.WhiteBox
* @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions
* -XX:+WhiteBoxAPI compiler.compilercontrol.matcher.MethodMatcherTest
*/
package compiler.compilercontrol.matcher;
import compiler.compilercontrol.share.method.MethodDescriptor;
import compiler.compilercontrol.share.method.MethodGenerator;
import compiler.compilercontrol.share.pool.PoolHelper;
import jdk.test.lib.util.Pair;
import jdk.test.whitebox.WhiteBox;
import java.lang.reflect.Executable;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MethodMatcherTest {
private static final WhiteBox WB = WhiteBox.getWhiteBox();
private static final PoolHelper POOL = new PoolHelper();
private static final List<Pair<Executable, Callable<?>>> METHODS =
POOL.getAllMethods();
private static final int AMOUNT = Integer.parseInt(System
.getProperty("test.amount", "25"));
public static void main(String[] args) {
MethodGenerator gen = new MethodGenerator();
List<Pair<Executable, Callable<?>>> testMethods =
POOL.getAllMethods(PoolHelper.METHOD_FILTER);
for (Pair<Executable, Callable<?>> pair : testMethods) {
for (int i = 0; i < AMOUNT; i++) {
MethodDescriptor md = gen.generateRandomDescriptor(pair.first);
check(md);
}
}
}
/**
* Check method matcher with given test case
*
* @param methodDescriptor method descriptor to check matcher's pattern
*/
private static void check(MethodDescriptor methodDescriptor) {
System.out.println("Test case: " + methodDescriptor.getString());
System.out.println("Regex: " + methodDescriptor.getRegexp());
Pattern pattern = Pattern.compile(methodDescriptor.getRegexp());
boolean isValidDesc = methodDescriptor.isValid();
List<MethodDescriptor> failList = new ArrayList<>();
// walk through all methods in pool to check match with test pattern
for (Pair<Executable, Callable<?>> pair : METHODS) {
MethodDescriptor m = MethodGenerator.commandDescriptor(pair.first);
Matcher matcher = pattern.matcher(m.getCanonicalString());
// get expected result
MatcherResult expected;
if (isValidDesc) {
expected = matcher.matches() ?
MatcherResult.MATCH : MatcherResult.NO_MATCH;
} else {
expected = MatcherResult.PARSING_FAILURE;
}
// get MethodMatcher's result
MatcherResult matchResult = MatcherResult.fromCode(WB.matchesMethod(
pair.first, methodDescriptor.getString()));
// compare
if (matchResult != expected) {
System.out.printf("- Method: %s%n-- FAILED: result: %s, " +
"but expected: %s%n", m.getCanonicalString(),
matchResult, expected);
failList.add(m);
}
}
int size = failList.size();
if (size != 0) {
System.err.println("FAILED test case: " + methodDescriptor
.getString());
if (size == METHODS.size()) {
System.err.println("-- All methods failed to match");
} else {
for (MethodDescriptor md : failList) {
System.err.println("-- FAILED match: " + md.getString());
}
}
throw new AssertionError("FAIL: " + methodDescriptor.getString());
}
System.out.println("--PASSED");
}
/**
* Represents MethodMatcher's matching result
*/
public enum MatcherResult {
PARSING_FAILURE(-1, "Parsing failed"),
NO_MATCH(0, "No match"),
MATCH(1, "Match");
public final int code;
private final String message;
private MatcherResult(int code, String message) {
this.code = code;
this.message = message;
}
public static MatcherResult fromCode(int code) {
switch (code) {
case -1: return PARSING_FAILURE;
case 0: return NO_MATCH;
case 1: return MATCH;
default:
throw new IllegalArgumentException("MATCHER FAILURE:"
+ "Wrong code: " + code);
}
}
@Override
public String toString() {
return message;
}
}
}
|