1 /* 2 * Copyright (c) 2016, 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 org.graalvm.compiler.core.test.GraalCompilerTest; 28 import org.graalvm.compiler.nodes.ConstantNode; 29 import org.graalvm.compiler.nodes.FixedNode; 30 import org.graalvm.compiler.nodes.ReturnNode; 31 import org.graalvm.compiler.nodes.StructuredGraph; 32 import org.graalvm.compiler.nodes.ValueNode; 33 import jdk.vm.ci.meta.JavaConstant; 34 import org.junit.Test; 35 36 import jdk.vm.ci.meta.ResolvedJavaMethod; 37 import static org.hamcrest.CoreMatchers.instanceOf; 38 import org.junit.Assert; 39 40 /** 41 * Tests constant folding of string equality. 42 */ 43 public class StringEqualsConstantTest extends GraalCompilerTest { 44 asConstant(StructuredGraph graph, String str)45 private ValueNode asConstant(StructuredGraph graph, String str) { 46 return ConstantNode.forConstant(getSnippetReflection().forObject(str), getMetaAccess(), graph); 47 } 48 testStringEquals(String s0, String s1)49 private void testStringEquals(String s0, String s1) { 50 ResolvedJavaMethod method = getResolvedJavaMethod("stringEquals"); 51 StructuredGraph graph = parseForCompile(method); 52 53 graph.getParameter(0).replaceAndDelete(asConstant(graph, s0)); 54 graph.getParameter(1).replaceAndDelete(asConstant(graph, s1)); 55 56 compile(method, graph); 57 58 FixedNode firstFixed = graph.start().next(); 59 Assert.assertThat(firstFixed, instanceOf(ReturnNode.class)); 60 61 ReturnNode ret = (ReturnNode) firstFixed; 62 JavaConstant result = ret.result().asJavaConstant(); 63 if (result == null) { 64 Assert.fail("result not constant: " + ret.result()); 65 } else { 66 int expected = s0.equals(s1) ? 1 : 0; 67 Assert.assertEquals("result", expected, result.asInt()); 68 } 69 } 70 71 @Test testSameString()72 public void testSameString() { 73 String s = "equal-string"; 74 testStringEquals(s, s); 75 } 76 77 @Test testEqualString()78 public void testEqualString() { 79 String s = "equal-string"; 80 testStringEquals(s, new String(s.toCharArray())); 81 } 82 83 @Test testDifferentString()84 public void testDifferentString() { 85 testStringEquals("some-string", "different-string"); 86 } 87 88 @Test testSameLengthString()89 public void testSameLengthString() { 90 testStringEquals("string-1", "string-2"); 91 } 92 stringEquals(String a, String b)93 public static boolean stringEquals(String a, String b) { 94 return a.equals(b); 95 } 96 } 97