1/*
2 * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/*
25 * @test InlineMatcherTest
26 * @bug 8074095
27 * @summary Testing of compiler/InlineMatcher
28 * @modules java.base/jdk.internal.misc
29 * @library /test/lib
30 *
31 * @build sun.hotspot.WhiteBox
32 * @run driver ClassFileInstaller sun.hotspot.WhiteBox
33 *                                sun.hotspot.WhiteBox$WhiteBoxPermission
34 * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
35 *      compiler.compilercontrol.InlineMatcherTest
36 */
37
38package compiler.compilercontrol;
39
40import sun.hotspot.WhiteBox;
41
42import java.lang.reflect.Method;
43import java.util.ArrayList;
44
45public class InlineMatcherTest {
46
47    /** Instance of WhiteBox */
48    protected static final WhiteBox WHITE_BOX = WhiteBox.getWhiteBox();
49
50    Method helper;
51    Method getDate;
52    Method inner;
53    Method toString;
54
55    final public static int FORCE_INLINE = 2;
56    final public static int DONT_INLINE = 1;
57    final public static int NO_MATCH = 0;
58    final public static int PARSING_FAILURE = -1;
59
60    public InlineMatcherTest() {
61
62    }
63
64    public void test() throws Exception {
65        // instantiate before calling getMethod on innerHelper
66        TestCases testCases = new TestCases();
67
68        helper = getMethod(InlineMatcherTest.class, "helper");
69
70        testCases.add(helper, "*.*", PARSING_FAILURE);
71        testCases.add(helper, "+*.*", FORCE_INLINE);
72        testCases.add(helper, "++*.*", NO_MATCH); // + is a valid part of the
73                                                  // class name
74        testCases.add(helper, "-*.*", DONT_INLINE);
75        testCases.add(helper, "--*.*", NO_MATCH); // - is a valid part of the
76                                                  // class name
77
78        String className = this.getClass().getName().replace('.', '/');
79        testCases.add(helper, "+" + className + ".*", FORCE_INLINE);
80        testCases.add(helper, "+" + className + ".helper", FORCE_INLINE);
81        testCases.add(helper, "+" + className + ".helper()", FORCE_INLINE);
82        testCases.add(helper, "+" + className + ".helper()V", FORCE_INLINE);
83        testCases.add(helper, "+" + className + ".helper(", FORCE_INLINE);
84
85        testCases.add(helper, "-" + className + ".*", DONT_INLINE);
86        testCases.add(helper, "-" + className + ".helper", DONT_INLINE);
87        testCases.add(helper, "-" + className + ".helper()", DONT_INLINE);
88        testCases.add(helper, "-" + className + ".helper()V", DONT_INLINE);
89        testCases.add(helper, "-" + className + ".helper(", DONT_INLINE);
90
91        testCases.add(helper, "+abc.*", NO_MATCH);
92        testCases.add(helper, "+*.abc", NO_MATCH);
93        testCases.add(helper, "-abc.*", NO_MATCH);
94        testCases.add(helper, "-*.abcls ", NO_MATCH);
95
96        int failures = 0;
97
98        for (TestCase t : testCases) {
99            System.out.println("Test case: " + t.pattern);
100            if (!t.test()) {
101                failures++;
102                System.out.println(" * FAILED");
103            }
104        }
105        if (failures != 0) {
106            throw new Exception("There where " + failures + " failures in this test");
107        }
108    }
109
110    public static void main(String... args) throws Exception {
111        InlineMatcherTest test = new InlineMatcherTest();
112        test.test();
113    }
114
115    public void helper() {
116
117    }
118
119    private static Method getMethod(Class klass, String name, Class<?>... parameterTypes) {
120        try {
121            return klass.getDeclaredMethod(name, parameterTypes);
122        } catch (NoSuchMethodException | SecurityException e) {
123            throw new RuntimeException("exception on getting method Helper." + name, e);
124        }
125    }
126
127    class TestCase {
128        String pattern;
129        Method testTarget;
130        int expectedResult;
131
132        public TestCase(Method testTarget, String pattern, int expectedResult) {
133            this.testTarget = testTarget;
134            this.pattern = pattern;
135            this.expectedResult = expectedResult;
136        }
137
138        public String resultAsStr(int errorCode) {
139            switch (errorCode) {
140            case PARSING_FAILURE:
141                return "Parsing failed";
142            case NO_MATCH:
143                return "No match";
144            case DONT_INLINE:
145                return "Dont Inline";
146            case FORCE_INLINE:
147                return "Force Inline";
148            default:
149                return "Unknown error";
150            }
151        }
152
153        boolean test() {
154            int result = WHITE_BOX.matchesInline(testTarget, pattern);
155            if (result != expectedResult) {
156                System.out
157                        .println("FAIL Wrong result, Got: " + resultAsStr(result) + "\n TestCase: " + this.toString());
158                return false;
159            }
160            return true;
161        }
162
163        @Override
164        public String toString() {
165            return "Method: '" + testTarget.toString() + "' Pattern: '" + pattern + "' Expected: "
166                    + resultAsStr(expectedResult);
167        }
168
169        public void innerHelper() {
170
171        }
172    }
173
174    class TestCases extends ArrayList<TestCase> {
175        private static final long serialVersionUID = 1L;
176
177        public boolean add(Method testTarget, String pattern, int expectedResult) {
178            return super.add(new TestCase(testTarget, pattern, expectedResult));
179        }
180    }
181}
182