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