1 /*
   2  * Copyright (c) 2018, 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.nodes.graphbuilderconf.GraphBuilderConfiguration;
  28 import org.graalvm.compiler.replacements.ConstantBindingParameterPlugin;
  29 import org.graalvm.compiler.replacements.nodes.ArrayEqualsNode;
  30 import org.junit.Test;
  31 
  32 import java.util.Arrays;
  33 
  34 public class ArrayEqualsConstantLengthTest extends ArraysSubstitutionsTestBase {
  35 
  36     private static final int[] LENGTHS = {0, 1, 2, 3, 4, 5, 7, 8, 9, 15, 16, 17, 24, 31, 32, 33, 48, 63, 64, 65, 127, 128, 129, 255, 256, 257};
  37 
  38     private void testEquals(String methodName, Class<?>[] parameterTypes, ArrayBuilder builder) {
  39         for (int length : LENGTHS) {
  40             testSubstitution(methodName, ArrayEqualsNode.class, Arrays.class, "equals", parameterTypes, false,
  41                             true, new Object[]{
  42                                             builder.newArray(length, 0, 1),
  43                                             builder.newArray(length, 0, 1),
  44                                             builder.newArray(length, 0, 1)},
  45                             new Object[]{
  46                                             builder.newArray(length, 0, 1),
  47                                             builder.newArray(length, 1, 1),
  48                                             builder.newArray(length, 0, 2)});
  49         }
  50     }
  51 
  52     @Test
  53     public void testEqualsBoolean() {
  54         testEquals("arraysEqualsBoolean", new Class<?>[]{boolean[].class, boolean[].class}, ArraysSubstitutionsTestBase::booleanArray);
  55     }
  56 
  57     @Test
  58     public void testEqualsByte() {
  59         testEquals("arraysEqualsByte", new Class<?>[]{byte[].class, byte[].class}, ArraysSubstitutionsTestBase::byteArray);
  60     }
  61 
  62     @Test
  63     public void testEqualsChar() {
  64         testEquals("arraysEqualsChar", new Class<?>[]{char[].class, char[].class}, ArraysSubstitutionsTestBase::charArray);
  65     }
  66 
  67     @Test
  68     public void testEqualsShort() {
  69         testEquals("arraysEqualsShort", new Class<?>[]{short[].class, short[].class}, ArraysSubstitutionsTestBase::shortArray);
  70     }
  71 
  72     @Test
  73     public void testEqualsInt() {
  74         testEquals("arraysEqualsInt", new Class<?>[]{int[].class, int[].class}, ArraysSubstitutionsTestBase::intArray);
  75     }
  76 
  77     @Test
  78     public void testEqualsLong() {
  79         testEquals("arraysEqualsLong", new Class<?>[]{long[].class, long[].class}, ArraysSubstitutionsTestBase::longArray);
  80     }
  81 
  82     private Object[] constantArgs;
  83 
  84     @Override
  85     protected GraphBuilderConfiguration editGraphBuilderConfiguration(GraphBuilderConfiguration conf) {
  86         if (constantArgs != null) {
  87             ConstantBindingParameterPlugin constantBinding = new ConstantBindingParameterPlugin(constantArgs, this.getMetaAccess(), this.getSnippetReflection());
  88             conf.getPlugins().appendParameterPlugin(constantBinding);
  89         }
  90         return super.editGraphBuilderConfiguration(conf);
  91     }
  92 
  93     @Override
  94     protected void testSubstitution(String testMethodName, Class<?> intrinsicClass, Class<?> holder, String methodName, Class<?>[] parameterTypes, boolean optional, boolean forceCompilation,
  95                     Object[] args1, Object[] args2) {
  96         constantArgs = new Object[]{args1[0], null};
  97         super.testSubstitution(testMethodName, intrinsicClass, holder, methodName, parameterTypes, optional, forceCompilation, args1, args2);
  98     }
  99 }