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 {@link String#hashCode()}.
  40  */
  41 public class StringHashConstantTest extends GraalCompilerTest {
  42 
  43     private static final String A_CONSTANT_STRING = "a constant string";
  44 
  45     private ValueNode asConstant(StructuredGraph graph, String str) {
  46         return ConstantNode.forConstant(getSnippetReflection().forObject(str), getMetaAccess(), graph);
  47     }
  48 
  49     @Test
  50     public void test1() {
  51         ResolvedJavaMethod method = getResolvedJavaMethod("parameterizedHashCode");
  52         StructuredGraph graph = parseForCompile(method);
  53 
  54         String s = "some string";
  55         int expected = s.hashCode();
  56         graph.getParameter(0).replaceAndDelete(asConstant(graph, s));
  57 
  58         compile(method, graph);
  59 
  60         FixedNode firstFixed = graph.start().next();
  61         Assert.assertThat(firstFixed, instanceOf(ReturnNode.class));
  62 
  63         ReturnNode ret = (ReturnNode) firstFixed;
  64         JavaConstant result = ret.result().asJavaConstant();
  65         if (result == null) {
  66             Assert.fail("result not constant: " + ret.result());
  67         } else {
  68             Assert.assertEquals("result", expected, result.asInt());
  69         }
  70     }
  71 
  72     public static int parameterizedHashCode(String value) {
  73         return value.hashCode();
  74     }
  75 
  76     @Test
  77     public void test2() {
  78         ResolvedJavaMethod method = getResolvedJavaMethod("constantHashCode");
  79         StructuredGraph graph = parseForCompile(method);
  80 
  81         FixedNode firstFixed = graph.start().next();
  82         Assert.assertThat(firstFixed, instanceOf(ReturnNode.class));
  83 
  84         ReturnNode ret = (ReturnNode) firstFixed;
  85         JavaConstant result = ret.result().asJavaConstant();
  86         if (result == null) {
  87             Assert.fail("result not constant: " + ret.result());
  88         } else {
  89             int expected = A_CONSTANT_STRING.hashCode();
  90             Assert.assertEquals("result", expected, result.asInt());
  91         }
  92     }
  93 
  94     public static int constantHashCode() {
  95         return A_CONSTANT_STRING.hashCode();
  96     }
  97 }