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             Assume.assumeTrue(vmArgs != null);
  97             for (String vmArg : vmArgs) {
  98                 if (vmArg.equals(DISABLE_COMPACTSTRINGS_FLAG)) {
  99                     needCheckNode = false;
 100                 }
 101             }
 102         }
 103 
 104         if (needCheckNode) {
 105             options = new OptionValues(getInitialOptions(), RemoveNeverExecutedCode, false);
 106             Assert.assertEquals(EXPECT_NODE_COUNT, countNode(testMethod, expectedNode, options));
 107         } else {
 108             options = getInitialOptions();
 109         }
 110 
 111         // Force compilation.
 112         testCode = getCode(testMethod, options);
 113         Assert.assertNotNull(testCode);
 114     }
 115 
 116     public static int stringCompareTo(String a, String b) {
 117         return a.compareTo(b);
 118     }
 119 
 120     @Test
 121     @Override
 122     public void testEqualString() {
 123         super.testEqualString();
 124     }
 125 
 126     @Test
 127     @Override
 128     public void testDifferentString() {
 129         super.testDifferentString();
 130     }
 131 
 132     @Test
 133     @Override
 134     public void testAllStrings() {
 135         super.testAllStrings();
 136     }
 137 }