InlineMatcherTest.java revision 9191:a176d4737606
1198893Srdivacky/*
2198893Srdivacky * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
3198893Srdivacky * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4198893Srdivacky *
5198893Srdivacky * This code is free software; you can redistribute it and/or modify it
6198893Srdivacky * under the terms of the GNU General Public License version 2 only, as
7198893Srdivacky * published by the Free Software Foundation.
8198893Srdivacky *
9198893Srdivacky * This code is distributed in the hope that it will be useful, but WITHOUT
10198893Srdivacky * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11198893Srdivacky * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12198893Srdivacky * version 2 for more details (a copy is included in the LICENSE file that
13198893Srdivacky * accompanied this code).
14198893Srdivacky *
15198893Srdivacky * You should have received a copy of the GNU General Public License version
16218893Sdim * 2 along with this work; if not, write to the Free Software Foundation,
17198893Srdivacky * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18199990Srdivacky *
19198893Srdivacky * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20218893Sdim * or visit www.oracle.com if you need additional information or have any
21218893Sdim * questions.
22198893Srdivacky */
23208600Srdivacky
24218893Sdim/*
25234353Sdim * @test InlineMatcherTest
26249423Sdim * @bug 8074095
27218893Sdim * @library /testlibrary /../../test/lib
28198893Srdivacky * @run main ClassFileInstaller sun.hotspot.WhiteBox
29198893Srdivacky *                              sun.hotspot.WhiteBox$WhiteBoxPermission
30198893Srdivacky * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI InlineMatcherTest
31218893Sdim * @summary Testing of compiler/InlineMatcher
32218893Sdim */
33218893Sdim
34218893Sdimimport java.lang.reflect.Method;
35218893Sdimimport java.util.ArrayList;
36218893Sdimimport sun.hotspot.WhiteBox;
37226633Sdim
38218893Sdimpublic class InlineMatcherTest {
39239462Sdim
40218893Sdim    /** Instance of WhiteBox */
41218893Sdim    protected static final WhiteBox WHITE_BOX = WhiteBox.getWhiteBox();
42239462Sdim
43218893Sdim    Method helper;
44239462Sdim    Method getDate;
45219077Sdim    Method inner;
46234353Sdim    Method toString;
47219077Sdim
48218893Sdim    final public static int FORCE_INLINE = 2;
49239462Sdim    final public static int DONT_INLINE = 1;
50218893Sdim    final public static int NO_MATCH = 0;
51218893Sdim    final public static int PARSING_FAILURE = -1;
52218893Sdim
53198893Srdivacky    public InlineMatcherTest() {
54198893Srdivacky
55198893Srdivacky    }
56198893Srdivacky
57239462Sdim    public void test() throws Exception {
58263508Sdim        // instantiate before calling getMethod on innerHelper
59263508Sdim        TestCases testCases = new TestCases();
60239462Sdim
61239462Sdim        helper = getMethod(InlineMatcherTest.class, "helper");
62239462Sdim
63239462Sdim        testCases.add(helper, "*.*", PARSING_FAILURE);
64239462Sdim        testCases.add(helper, "+*.*", FORCE_INLINE);
65239462Sdim        testCases.add(helper, "++*.*", NO_MATCH); // + is a valid part of the
66239462Sdim                                                  // class name
67239462Sdim        testCases.add(helper, "-*.*", DONT_INLINE);
68239462Sdim        testCases.add(helper, "--*.*", NO_MATCH); // - is a valid part of the
69239462Sdim                                                  // class name
70239462Sdim
71239462Sdim        testCases.add(helper, "+InlineMatcherTest.*", FORCE_INLINE);
72239462Sdim        testCases.add(helper, "+InlineMatcherTest.helper", FORCE_INLINE);
73239462Sdim        testCases.add(helper, "+InlineMatcherTest.helper()", FORCE_INLINE);
74239462Sdim        testCases.add(helper, "+InlineMatcherTest.helper()V", FORCE_INLINE);
75239462Sdim        testCases.add(helper, "+InlineMatcherTest.helper(", FORCE_INLINE);
76218893Sdim
77218893Sdim        testCases.add(helper, "-InlineMatcherTest.*", DONT_INLINE);
78218893Sdim        testCases.add(helper, "-InlineMatcherTest.helper", DONT_INLINE);
79218893Sdim        testCases.add(helper, "-InlineMatcherTest.helper()", DONT_INLINE);
80243830Sdim        testCases.add(helper, "-InlineMatcherTest.helper()V", DONT_INLINE);
81218893Sdim        testCases.add(helper, "-InlineMatcherTest.helper(", DONT_INLINE);
82218893Sdim
83218893Sdim        testCases.add(helper, "+abc.*", NO_MATCH);
84218893Sdim        testCases.add(helper, "+*.abc", NO_MATCH);
85218893Sdim        testCases.add(helper, "-abc.*", NO_MATCH);
86198893Srdivacky        testCases.add(helper, "-*.abcls ", NO_MATCH);
87218893Sdim
88218893Sdim        int failures = 0;
89218893Sdim
90226633Sdim        for (TestCase t : testCases) {
91218893Sdim            System.out.println("Test case: " + t.pattern);
92218893Sdim            if (!t.test()) {
93218893Sdim                failures++;
94218893Sdim                System.out.println(" * FAILED");
95218893Sdim            }
96218893Sdim        }
97218893Sdim        if (failures != 0) {
98218893Sdim            throw new Exception("There where " + failures + " failures in this test");
99218893Sdim        }
100218893Sdim    }
101218893Sdim
102243830Sdim    public static void main(String... args) throws Exception {
103243830Sdim        InlineMatcherTest test = new InlineMatcherTest();
104243830Sdim        test.test();
105243830Sdim    }
106243830Sdim
107234353Sdim    public void helper() {
108218893Sdim
109218893Sdim    }
110218893Sdim
111218893Sdim    private static Method getMethod(Class klass, String name, Class<?>... parameterTypes) {
112218893Sdim        try {
113218893Sdim            return klass.getDeclaredMethod(name, parameterTypes);
114218893Sdim        } catch (NoSuchMethodException | SecurityException e) {
115218893Sdim            throw new RuntimeException("exception on getting method Helper." + name, e);
116218893Sdim        }
117218893Sdim    }
118218893Sdim
119218893Sdim    class TestCase {
120218893Sdim        String pattern;
121218893Sdim        Method testTarget;
122218893Sdim        int expectedResult;
123198893Srdivacky
124198893Srdivacky        public TestCase(Method testTarget, String pattern, int expectedResult) {
125234353Sdim            this.testTarget = testTarget;
126198893Srdivacky            this.pattern = pattern;
127198893Srdivacky            this.expectedResult = expectedResult;
128224145Sdim        }
129224145Sdim
130224145Sdim        public String resultAsStr(int errorCode) {
131226633Sdim            switch (errorCode) {
132224145Sdim            case PARSING_FAILURE:
133224145Sdim                return "Parsing failed";
134224145Sdim            case NO_MATCH:
135224145Sdim                return "No match";
136224145Sdim            case DONT_INLINE:
137224145Sdim                return "Dont Inline";
138224145Sdim            case FORCE_INLINE:
139224145Sdim                return "Force Inline";
140224145Sdim            default:
141224145Sdim                return "Unknown error";
142224145Sdim            }
143243830Sdim        }
144243830Sdim
145243830Sdim        boolean test() {
146243830Sdim            int result = WHITE_BOX.matchesInline(testTarget, pattern);
147243830Sdim            if (result != expectedResult) {
148234353Sdim                System.out
149234353Sdim                        .println("FAIL Wrong result, Got: " + resultAsStr(result) + "\n TestCase: " + this.toString());
150224145Sdim                return false;
151224145Sdim            }
152224145Sdim            return true;
153224145Sdim        }
154224145Sdim
155224145Sdim        @Override
156224145Sdim        public String toString() {
157224145Sdim            return "Method: '" + testTarget.toString() + "' Pattern: '" + pattern + "' Expected: "
158224145Sdim                    + resultAsStr(expectedResult);
159224145Sdim        }
160224145Sdim
161224145Sdim        public void innerHelper() {
162224145Sdim
163224145Sdim        }
164224145Sdim    }
165234353Sdim
166234353Sdim    class TestCases extends ArrayList<TestCase> {
167224145Sdim        private static final long serialVersionUID = 1L;
168224145Sdim
169218893Sdim        public boolean add(Method testTarget, String pattern, int expectedResult) {
170218893Sdim            return super.add(new TestCase(testTarget, pattern, expectedResult));
171218893Sdim        }
172218893Sdim    }
173218893Sdim}
174218893Sdim