1 /* 2 * Copyright (c) 2018, 2019, 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 package org.graalvm.compiler.replacements.test; 26 27 import static org.graalvm.compiler.core.common.GraalOptions.RemoveNeverExecutedCode; 28 29 import java.util.List; 30 31 import org.graalvm.compiler.graph.Node; 32 import org.graalvm.compiler.nodes.StructuredGraph; 33 import org.graalvm.compiler.options.OptionValues; 34 import org.graalvm.compiler.replacements.nodes.ArrayCompareToNode; 35 import org.graalvm.compiler.serviceprovider.GraalServices; 36 import org.graalvm.compiler.serviceprovider.JavaVersionUtil; 37 import org.junit.Assert; 38 import org.junit.Assume; 39 import org.junit.Test; 40 41 import jdk.vm.ci.aarch64.AArch64; 42 import jdk.vm.ci.amd64.AMD64; 43 import jdk.vm.ci.meta.ResolvedJavaMethod; 44 45 /** 46 * Tests compareTo method intrinsic. 47 */ 48 public class StringCompareToTest extends StringSubstitutionTestBase { 49 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 54 public StringCompareToTest() { 55 initSubstitution( 56 getResolvedJavaMethod(String.class, "compareTo", String.class), 57 getResolvedJavaMethod("stringCompareTo"), 58 ArrayCompareToNode.class); 59 } 60 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 86 StructuredGraph replacement = getReplacements().getSubstitution(realMethod, -1, false, null, graph.getOptions()); 87 if (replacement == null) { 88 assertInGraph(graph, expectedNode); 89 } 90 91 OptionValues options; 92 boolean needCheckNode = true; 93 94 if (JavaVersionUtil.JAVA_SPEC <= 8) { 95 needCheckNode = false; 96 } else { 97 List<String> vmArgs = GraalServices.getInputArguments(); 98 Assume.assumeTrue(vmArgs != null); 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 118 public static int stringCompareTo(String a, String b) { 119 return a.compareTo(b); 120 } 121 122 @Test 123 @Override 124 public void testEqualString() { 125 super.testEqualString(); 126 } 127 128 @Test 129 @Override 130 public void testDifferentString() { 131 super.testDifferentString(); 132 } 133 134 @Test 135 @Override 136 public void testAllStrings() { 137 super.testAllStrings(); 138 } 139 }