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