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.replacements.ArraysSubstitutions;
  28 
  29 import java.util.Arrays;
  30 
  31 /**
  32  * Tests {@link ArraysSubstitutions}.
  33  */
  34 public class ArraysSubstitutionsTestBase extends MethodSubstitutionTest {
  35 
  36     @SuppressWarnings("all")
  37     public static boolean arraysEqualsBoolean(boolean[] a, boolean[] b) {
  38         return Arrays.equals(a, b);
  39     }
  40 
  41     @SuppressWarnings("all")
  42     public static boolean arraysEqualsByte(byte[] a, byte[] b) {
  43         return Arrays.equals(a, b);
  44     }
  45 
  46     @SuppressWarnings("all")
  47     public static boolean arraysEqualsChar(char[] a, char[] b) {
  48         return Arrays.equals(a, b);
  49     }
  50 
  51     @SuppressWarnings("all")
  52     public static boolean arraysEqualsShort(short[] a, short[] b) {
  53         return Arrays.equals(a, b);
  54     }
  55 
  56     @SuppressWarnings("all")
  57     public static boolean arraysEqualsInt(int[] a, int[] b) {
  58         return Arrays.equals(a, b);
  59     }
  60 
  61     @SuppressWarnings("all")
  62     public static boolean arraysEqualsLong(long[] a, long[] b) {
  63         return Arrays.equals(a, b);
  64     }
  65 
  66     interface ArrayBuilder {
  67         Object newArray(int length, int firstValue, int lastValue);
  68     }
  69 
  70     static boolean[] booleanArray(int length, int firstValue, int lastValue) {
  71         boolean[] arr = new boolean[length];
  72         for (int i = 0; i < length; i++) {
  73             arr[i] = (i & 1) == 0;
  74         }
  75         if (length > 0) {
  76             arr[0] = (firstValue & 1) == 0;
  77         }
  78         if (length > 1) {
  79             arr[length - 1] = (lastValue & 1) == 0;
  80         }
  81         return arr;
  82     }
  83 
  84     static byte[] byteArray(int length, int firstValue, int lastValue) {
  85         byte[] arr = new byte[length];
  86         for (int i = 0; i < length; i++) {
  87             arr[i] = (byte) i;
  88         }
  89         if (length > 0) {
  90             arr[0] = (byte) firstValue;
  91         }
  92         if (length > 1) {
  93             arr[length - 1] = (byte) lastValue;
  94         }
  95         return arr;
  96     }
  97 
  98     static char[] charArray(int length, int firstValue, int lastValue) {
  99         char[] arr = new char[length];
 100         for (int i = 0; i < length; i++) {
 101             arr[i] = (char) i;
 102         }
 103         if (length > 0) {
 104             arr[0] = (char) firstValue;
 105         }
 106         if (length > 1) {
 107             arr[length - 1] = (char) lastValue;
 108         }
 109         return arr;
 110     }
 111 
 112     static short[] shortArray(int length, int firstValue, int lastValue) {
 113         short[] arr = new short[length];
 114         for (int i = 0; i < length; i++) {
 115             arr[i] = (short) i;
 116         }
 117         if (length > 0) {
 118             arr[0] = (short) firstValue;
 119         }
 120         if (length > 1) {
 121             arr[length - 1] = (short) lastValue;
 122         }
 123         return arr;
 124     }
 125 
 126     static int[] intArray(int length, int firstValue, int lastValue) {
 127         int[] arr = new int[length];
 128         for (int i = 0; i < length; i++) {
 129             arr[i] = i;
 130         }
 131         if (length > 0) {
 132             arr[0] = firstValue;
 133         }
 134         if (length > 1) {
 135             arr[length - 1] = lastValue;
 136         }
 137         return arr;
 138     }
 139 
 140     static long[] longArray(int length, int firstValue, int lastValue) {
 141         long[] arr = new long[length];
 142         for (int i = 0; i < length; i++) {
 143             arr[i] = i;
 144         }
 145         if (length > 0) {
 146             arr[0] = firstValue;
 147         }
 148         if (length > 1) {
 149             arr[length - 1] = lastValue;
 150         }
 151         return arr;
 152     }
 153 }