1 /* 2 * Copyright (c) 2014, 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 package org.graalvm.compiler.replacements.test; 24 25 import org.junit.Test; 26 27 import org.graalvm.compiler.nodes.StructuredGraph; 28 import org.graalvm.compiler.replacements.StringSubstitutions; 29 import org.graalvm.compiler.replacements.nodes.ArrayEqualsNode; 30 31 import jdk.vm.ci.code.InstalledCode; 32 import jdk.vm.ci.meta.ResolvedJavaMethod; 33 34 /** 35 * Tests {@link StringSubstitutions}. 36 */ 37 public class StringSubstitutionsTest extends MethodSubstitutionTest { 38 39 public void testSubstitution(String testMethodName, Class<?> intrinsicClass, Class<?> holder, String methodName, boolean optional, Object[] args1, Object[] args2) { 40 ResolvedJavaMethod realMethod = getResolvedJavaMethod(holder, methodName); 41 ResolvedJavaMethod testMethod = getResolvedJavaMethod(testMethodName); 42 StructuredGraph graph = testGraph(testMethodName); 43 44 // Check to see if the resulting graph contains the expected node 45 StructuredGraph replacement = getReplacements().getSubstitution(realMethod, -1); 46 if (replacement == null && !optional) { 47 assertInGraph(graph, intrinsicClass); 48 } 49 50 // Force compilation 51 InstalledCode code = getCode(testMethod); 52 assert optional || code != null; 53 54 for (int i = 0; i < args1.length; i++) { 55 Object arg1 = args1[i]; 56 Object arg2 = args2[i]; 57 Object expected = invokeSafe(realMethod, arg1, arg2); 58 // Verify that the original method and the substitution produce the same value 59 assertDeepEquals(expected, invokeSafe(testMethod, null, arg1, arg2)); 60 // Verify that the generated code and the original produce the same value 61 assertDeepEquals(expected, executeVarargsSafe(code, arg1, arg2)); 62 } 63 } 64 65 @Test 66 public void testEquals() { 67 if (!Java8OrEarlier) { 68 // StringSubstitutions are disabled in 1.9 69 return; 70 } 71 72 final int n = 1000; 73 Object[] args1 = new Object[n]; 74 Object[] args2 = new Object[n]; 75 76 // equal strings 77 String s1 = ""; 78 String s2 = ""; 79 for (int i = 0; i < n / 2; i++) { 80 args1[i] = s1; 81 args2[i] = s2; 82 s1 = s1 + "0"; 83 s2 = s2 + "0"; 84 } 85 86 // non-equal strings 87 s1 = ""; 88 s2 = ""; 89 for (int i = n / 2; i < n; i++) { 90 args1[i] = s1; 91 args2[i] = s2; 92 s2 = s1 + "1"; 93 s1 = s1 + "0"; 94 } 95 96 testSubstitution("stringEquals", ArrayEqualsNode.class, String.class, "equals", false, args1, args2); 97 } 98 99 @SuppressWarnings("all") 100 public static boolean stringEquals(String a, String b) { 101 return a.equals(b); 102 } 103 104 }