49451
|
1 |
/*
|
58299
|
2 |
* Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
|
49451
|
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 |
*/
|
50858
|
23 |
|
|
24 |
|
49451
|
25 |
package org.graalvm.compiler.replacements.test;
|
|
26 |
|
54084
|
27 |
import static org.graalvm.compiler.core.common.GraalOptions.RemoveNeverExecutedCode;
|
|
28 |
|
|
29 |
import java.util.List;
|
|
30 |
|
52910
|
31 |
import org.graalvm.compiler.graph.Node;
|
|
32 |
import org.graalvm.compiler.nodes.StructuredGraph;
|
|
33 |
import org.graalvm.compiler.options.OptionValues;
|
49451
|
34 |
import org.graalvm.compiler.replacements.nodes.ArrayCompareToNode;
|
52910
|
35 |
import org.graalvm.compiler.serviceprovider.GraalServices;
|
54084
|
36 |
import org.graalvm.compiler.serviceprovider.JavaVersionUtil;
|
52910
|
37 |
import org.junit.Assert;
|
|
38 |
import org.junit.Assume;
|
49451
|
39 |
import org.junit.Test;
|
|
40 |
|
54084
|
41 |
import jdk.vm.ci.aarch64.AArch64;
|
|
42 |
import jdk.vm.ci.amd64.AMD64;
|
|
43 |
import jdk.vm.ci.meta.ResolvedJavaMethod;
|
52910
|
44 |
|
49451
|
45 |
/**
|
|
46 |
* Tests compareTo method intrinsic.
|
|
47 |
*/
|
51436
|
48 |
public class StringCompareToTest extends StringSubstitutionTestBase {
|
49451
|
49 |
|
52910
|
50 |
// The compareTo() implementation in java.lang.String has 4 calls to compareTo implementation.
|
|
51 |
private static final int EXPECT_NODE_COUNT = 4;
|
|
52 |
private static final String DISABLE_COMPACTSTRINGS_FLAG = "-XX:-CompactStrings";
|
|
53 |
|
49451
|
54 |
public StringCompareToTest() {
|
51436
|
55 |
initSubstitution(
|
|
56 |
getResolvedJavaMethod(String.class, "compareTo", String.class),
|
|
57 |
getResolvedJavaMethod("stringCompareTo"),
|
|
58 |
ArrayCompareToNode.class);
|
49451
|
59 |
}
|
|
60 |
|
52910
|
61 |
private int countNode(ResolvedJavaMethod method, Class<?> expectedNode, OptionValues options) {
|
|
62 |
StructuredGraph graph = parseForCompile(method, options);
|
|
63 |
applyFrontEnd(graph);
|
|
64 |
|
|
65 |
int c = 0;
|
|
66 |
for (Node node : graph.getNodes()) {
|
|
67 |
if (expectedNode.isInstance(node)) {
|
|
68 |
c += 1;
|
|
69 |
}
|
|
70 |
}
|
|
71 |
|
|
72 |
return c;
|
|
73 |
}
|
|
74 |
|
|
75 |
@Override
|
|
76 |
protected void initSubstitution(ResolvedJavaMethod theRealMethod,
|
|
77 |
ResolvedJavaMethod theTestMethod, Class<?> expectedNode) {
|
|
78 |
Assume.assumeTrue((getTarget().arch instanceof AMD64) || (getTarget().arch instanceof AArch64));
|
|
79 |
|
|
80 |
realMethod = theRealMethod;
|
|
81 |
testMethod = theTestMethod;
|
|
82 |
|
|
83 |
StructuredGraph graph = testGraph(testMethod.getName());
|
|
84 |
|
|
85 |
// Check to see if the resulting graph contains the expected node
|
54601
|
86 |
StructuredGraph replacement = getReplacements().getSubstitution(realMethod, -1, false, null, graph.getOptions());
|
52910
|
87 |
if (replacement == null) {
|
|
88 |
assertInGraph(graph, expectedNode);
|
|
89 |
}
|
|
90 |
|
|
91 |
OptionValues options;
|
|
92 |
boolean needCheckNode = true;
|
|
93 |
|
55509
|
94 |
if (JavaVersionUtil.JAVA_SPEC <= 8) {
|
52910
|
95 |
needCheckNode = false;
|
|
96 |
} else {
|
|
97 |
List<String> vmArgs = GraalServices.getInputArguments();
|
52956
|
98 |
Assume.assumeTrue(vmArgs != null);
|
52910
|
99 |
for (String vmArg : vmArgs) {
|
|
100 |
if (vmArg.equals(DISABLE_COMPACTSTRINGS_FLAG)) {
|
|
101 |
needCheckNode = false;
|
|
102 |
}
|
|
103 |
}
|
|
104 |
}
|
|
105 |
|
|
106 |
if (needCheckNode) {
|
|
107 |
options = new OptionValues(getInitialOptions(), RemoveNeverExecutedCode, false);
|
|
108 |
Assert.assertEquals(EXPECT_NODE_COUNT, countNode(testMethod, expectedNode, options));
|
|
109 |
} else {
|
|
110 |
options = getInitialOptions();
|
|
111 |
}
|
|
112 |
|
|
113 |
// Force compilation.
|
|
114 |
testCode = getCode(testMethod, options);
|
|
115 |
Assert.assertNotNull(testCode);
|
|
116 |
}
|
|
117 |
|
49451
|
118 |
public static int stringCompareTo(String a, String b) {
|
|
119 |
return a.compareTo(b);
|
|
120 |
}
|
|
121 |
|
|
122 |
@Test
|
51436
|
123 |
@Override
|
49451
|
124 |
public void testEqualString() {
|
51436
|
125 |
super.testEqualString();
|
49451
|
126 |
}
|
|
127 |
|
|
128 |
@Test
|
51436
|
129 |
@Override
|
49451
|
130 |
public void testDifferentString() {
|
51436
|
131 |
super.testDifferentString();
|
49451
|
132 |
}
|
|
133 |
|
|
134 |
@Test
|
51436
|
135 |
@Override
|
49451
|
136 |
public void testAllStrings() {
|
51436
|
137 |
super.testAllStrings();
|
49451
|
138 |
}
|
|
139 |
}
|