001/* 002 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. 003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 004 * 005 * This code is free software; you can redistribute it and/or modify it 006 * under the terms of the GNU General Public License version 2 only, as 007 * published by the Free Software Foundation. 008 * 009 * This code is distributed in the hope that it will be useful, but WITHOUT 010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 011 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 012 * version 2 for more details (a copy is included in the LICENSE file that 013 * accompanied this code). 014 * 015 * You should have received a copy of the GNU General Public License version 016 * 2 along with this work; if not, write to the Free Software Foundation, 017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 018 * 019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 020 * or visit www.oracle.com if you need additional information or have any 021 * questions. 022 */ 023package com.oracle.graal.replacements.test; 024 025import jdk.internal.jvmci.code.*; 026import jdk.internal.jvmci.meta.*; 027 028import org.junit.*; 029 030import com.oracle.graal.nodes.*; 031import com.oracle.graal.replacements.*; 032import com.oracle.graal.replacements.nodes.*; 033 034/** 035 * Tests {@link StringSubstitutions}. 036 */ 037public class StringSubstitutionsTest extends MethodSubstitutionTest { 038 039 public void testSubstitution(String testMethodName, Class<?> intrinsicClass, Class<?> holder, String methodName, boolean optional, Object[] args1, Object[] args2) { 040 ResolvedJavaMethod realMethod = getResolvedJavaMethod(holder, methodName); 041 ResolvedJavaMethod testMethod = getResolvedJavaMethod(testMethodName); 042 StructuredGraph graph = testGraph(testMethodName); 043 044 // Check to see if the resulting graph contains the expected node 045 StructuredGraph replacement = getReplacements().getSubstitution(realMethod, -1); 046 if (replacement == null && !optional) { 047 assertInGraph(graph, intrinsicClass); 048 } 049 050 // Force compilation 051 InstalledCode code = getCode(testMethod); 052 assert optional || code != null; 053 054 for (int i = 0; i < args1.length; i++) { 055 Object arg1 = args1[i]; 056 Object arg2 = args2[i]; 057 Object expected = invokeSafe(realMethod, arg1, arg2); 058 // Verify that the original method and the substitution produce the same value 059 assertDeepEquals(expected, invokeSafe(testMethod, null, arg1, arg2)); 060 // Verify that the generated code and the original produce the same value 061 assertDeepEquals(expected, executeVarargsSafe(code, arg1, arg2)); 062 } 063 } 064 065 @Test 066 public void testEquals() { 067 final int n = 1000; 068 Object[] args1 = new Object[n]; 069 Object[] args2 = new Object[n]; 070 071 // equal strings 072 String s1 = ""; 073 String s2 = ""; 074 for (int i = 0; i < n / 2; i++) { 075 args1[i] = s1; 076 args2[i] = s2; 077 s1 = s1 + "0"; 078 s2 = s2 + "0"; 079 } 080 081 // non-equal strings 082 s1 = ""; 083 s2 = ""; 084 for (int i = n / 2; i < n; i++) { 085 args1[i] = s1; 086 args2[i] = s2; 087 s2 = s1 + "1"; 088 s1 = s1 + "0"; 089 } 090 091 testSubstitution("stringEquals", ArrayEqualsNode.class, String.class, "equals", false, args1, args2); 092 } 093 094 @SuppressWarnings("all") 095 public static boolean stringEquals(String a, String b) { 096 return a.equals(b); 097 } 098 099}