Path: blob/master/test/hotspot/jtreg/compiler/compilercontrol/matcher/MethodMatcherTest.java
41153 views
/*1* Copyright (c) 2015, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @key randomness26* @bug 813506827* @summary Tests CompilerCommand's method matcher28* @modules java.base/jdk.internal.misc29* @library /test/lib /30*31* @build sun.hotspot.WhiteBox32* @run driver jdk.test.lib.helpers.ClassFileInstaller sun.hotspot.WhiteBox33* @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions34* -XX:+WhiteBoxAPI compiler.compilercontrol.matcher.MethodMatcherTest35*/3637package compiler.compilercontrol.matcher;3839import compiler.compilercontrol.share.method.MethodDescriptor;40import compiler.compilercontrol.share.method.MethodGenerator;41import compiler.compilercontrol.share.pool.PoolHelper;42import jdk.test.lib.util.Pair;43import sun.hotspot.WhiteBox;4445import java.lang.reflect.Executable;46import java.util.ArrayList;47import java.util.List;48import java.util.concurrent.Callable;49import java.util.regex.Matcher;50import java.util.regex.Pattern;5152public class MethodMatcherTest {53private static final WhiteBox WB = WhiteBox.getWhiteBox();54private static final PoolHelper POOL = new PoolHelper();55private static final List<Pair<Executable, Callable<?>>> METHODS =56POOL.getAllMethods();57private static final int AMOUNT = Integer.parseInt(System58.getProperty("test.amount", "25"));5960public static void main(String[] args) {61MethodGenerator gen = new MethodGenerator();62List<Pair<Executable, Callable<?>>> testMethods =63POOL.getAllMethods(PoolHelper.METHOD_FILTER);64for (Pair<Executable, Callable<?>> pair : testMethods) {65for (int i = 0; i < AMOUNT; i++) {66MethodDescriptor md = gen.generateRandomDescriptor(pair.first);67check(md);68}69}70}7172/**73* Check method matcher with given test case74*75* @param methodDescriptor method descriptor to check matcher's pattern76*/77private static void check(MethodDescriptor methodDescriptor) {78System.out.println("Test case: " + methodDescriptor.getString());79System.out.println("Regex: " + methodDescriptor.getRegexp());80Pattern pattern = Pattern.compile(methodDescriptor.getRegexp());81boolean isValidDesc = methodDescriptor.isValid();82List<MethodDescriptor> failList = new ArrayList<>();83// walk through all methods in pool to check match with test pattern84for (Pair<Executable, Callable<?>> pair : METHODS) {85MethodDescriptor m = MethodGenerator.commandDescriptor(pair.first);86Matcher matcher = pattern.matcher(m.getCanonicalString());87// get expected result88MatcherResult expected;89if (isValidDesc) {90expected = matcher.matches() ?91MatcherResult.MATCH : MatcherResult.NO_MATCH;92} else {93expected = MatcherResult.PARSING_FAILURE;94}95// get MethodMatcher's result96MatcherResult matchResult = MatcherResult.fromCode(WB.matchesMethod(97pair.first, methodDescriptor.getString()));98// compare99if (matchResult != expected) {100System.out.printf("- Method: %s%n-- FAILED: result: %s, " +101"but expected: %s%n", m.getCanonicalString(),102matchResult, expected);103failList.add(m);104}105}106int size = failList.size();107if (size != 0) {108System.err.println("FAILED test case: " + methodDescriptor109.getString());110if (size == METHODS.size()) {111System.err.println("-- All methods failed to match");112} else {113for (MethodDescriptor md : failList) {114System.err.println("-- FAILED match: " + md.getString());115}116}117throw new AssertionError("FAIL: " + methodDescriptor.getString());118}119System.out.println("--PASSED");120}121122/**123* Represents MethodMatcher's matching result124*/125public enum MatcherResult {126PARSING_FAILURE(-1, "Parsing failed"),127NO_MATCH(0, "No match"),128MATCH(1, "Match");129130public final int code;131private final String message;132133private MatcherResult(int code, String message) {134this.code = code;135this.message = message;136}137138public static MatcherResult fromCode(int code) {139switch (code) {140case -1: return PARSING_FAILURE;141case 0: return NO_MATCH;142case 1: return MATCH;143default:144throw new IllegalArgumentException("MATCHER FAILURE:"145+ "Wrong code: " + code);146}147}148149@Override150public String toString() {151return message;152}153}154}155156157