1 /*
   2  * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright (c) 2019, Arm Limited. All rights reserved.
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * This code is free software; you can redistribute it and/or modify it
   7  * under the terms of the GNU General Public License version 2 only, as
   8  * published by the Free Software Foundation.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have
  22  * questions.
  23  */
  24 
  25 /*
  26  * @test
  27  * @modules jdk.incubator.vector
  28  * @run testng Float64VectorExceptionTests
  29  *
  30  */
  31 
  32 import jdk.incubator.vector.Vector.Shape;
  33 import jdk.incubator.vector.Vector;
  34 import jdk.incubator.vector.FloatVector;
  35 
  36 import org.testng.Assert;
  37 import org.testng.annotations.DataProvider;
  38 import org.testng.annotations.Test;
  39 
  40 import java.util.List;
  41 import java.util.function.IntFunction;
  42 
  43 @Test
  44 public class Float64VectorExceptionTests extends AbstractVectorTest {
  45     static final FloatVector.FloatSpecies SPECIES =
  46                 FloatVector.species(Shape.S_64_BIT);
  47 
  48     static final int LOAD_STORE_ARRAY_LENGTH = SPECIES.length() / 2;
  49     static final int GATHER_SCATTER_ARRAY_LENGTH = SPECIES.length();
  50 
  51     static final Vector.Mask<Float> LOAD_STORE_MASK = FloatVector.maskFromValues(
  52         SPECIES, fill_boolean(SPECIES.length(), i -> (i < LOAD_STORE_ARRAY_LENGTH)));
  53 
  54     static final Vector.Mask<Float> GATHER_SCATTER_MASK = FloatVector.maskFromValues(
  55         SPECIES, fill_boolean(SPECIES.length(), i -> ((i * 2 + 1) < GATHER_SCATTER_ARRAY_LENGTH)));
  56 
  57     static final Vector.Mask<Float> ALL_TRUE_MASK = FloatVector.maskFromValues(
  58         SPECIES, fill_boolean(SPECIES.length(), i -> true));
  59 
  60     interface ToFloatF {
  61         float apply(int i);
  62     }
  63 
  64     static float[] fill(int s , ToFloatF f) {
  65         return fill(new float[s], f);
  66     }
  67 
  68     static float[] fill(float[] a, ToFloatF f) {
  69         for (int i = 0; i < a.length; i++) {
  70             a[i] = f.apply(i);
  71         }
  72         return a;
  73     }
  74 
  75     static final List<IntFunction<float[]>> FLOAT_GENERATORS = List.of(
  76             withToString("float[i + 1]", (int s) -> {
  77                 return fill(s,
  78                     i -> (((float)(i + 1) == 0) ? 1 : (float)(i + 1)));
  79             })
  80     );
  81 
  82     static final List<IntFunction<int[]>> INDEX_GENERATORS = List.of(
  83             withToString("index[i * 2 + 1]", (int s) -> {
  84                 return fillInts(s, i -> (i * 2 + 1));
  85             })
  86     );
  87 
  88     @DataProvider
  89     public Object[][] floatProvider() {
  90         return FLOAT_GENERATORS.stream().
  91                 map(f -> new Object[]{f}).
  92                 toArray(Object[][]::new);
  93     }
  94 
  95     @DataProvider
  96     public Object[][] floatIndexMapProvider() {
  97         return INDEX_GENERATORS.stream().
  98                 flatMap(fim -> FLOAT_GENERATORS.stream().map(fa -> {
  99                     return new Object[] {fa, fim};
 100                 })).
 101                 toArray(Object[][]::new);
 102     }
 103 
 104     @Test(dataProvider = "floatProvider")
 105     static void loadStoreArrayWithMask(IntFunction<float[]> fa) {
 106         float[] a = fa.apply(SPECIES.length() / 2);
 107         float[] r = new float[a.length];
 108 
 109         // case 1: No IndexOutOfBoundsException for unset mask lanes.
 110         FloatVector av = FloatVector.fromArray(SPECIES, a, 0, LOAD_STORE_MASK);
 111         av.intoArray(r, 0, LOAD_STORE_MASK);
 112         Assert.assertEquals(a, r);
 113 
 114         // case 2:
 115         float[] new_a = new float[a.length];
 116         av = FloatVector.fromArray(SPECIES, new_a, 0, LOAD_STORE_MASK);
 117         try {
 118             // Result in IndexOutOfBoundsException for intoArray().
 119             av.intoArray(r, 0, ALL_TRUE_MASK);
 120             Assert.fail("Expected IndexOutOfBoundsException not thrown");
 121         } catch(IndexOutOfBoundsException e) {
 122         }
 123 
 124         // case 3:
 125         try {
 126             // Result in IndexOutOfBoundsException for fromArray().
 127             av = FloatVector.fromArray(SPECIES, a, 0, ALL_TRUE_MASK);
 128             Assert.fail("Expected IndexOutOfBoundsException not thrown");
 129         } catch(IndexOutOfBoundsException e) {
 130         }
 131     }
 132 
 133     @Test(dataProvider = "floatIndexMapProvider")
 134     static void gatherScatterArrayWithMask(IntFunction<float[]> fa,
 135                                            IntFunction<int[]> fb) {
 136         float[] a = fa.apply(SPECIES.length());
 137         int[] indexMap = fb.apply(SPECIES.length());
 138         float[] r = new float[a.length];
 139 
 140         // case 1: No IndexOutOfBoundsException for unset mask lanes.
 141         FloatVector av = FloatVector.fromArray(SPECIES, a, 0);
 142         av.intoArray(r, 0, GATHER_SCATTER_MASK, indexMap, 0);
 143 
 144         av = FloatVector.fromArray(SPECIES, a, 0, GATHER_SCATTER_MASK, indexMap, 0);
 145 
 146         // case 2:
 147         float[] r1 = new float[a.length];
 148         float[] r2 = new float[a.length];
 149         av = FloatVector.fromArray(SPECIES, a, 0);
 150         try {
 151             // Result in IndexOutOfBoundsException for scatter.
 152             av.intoArray(r2, 0, ALL_TRUE_MASK, indexMap, 0);
 153             Assert.fail("Expected IndexOutOfBoundsException not thrown");
 154         } catch(IndexOutOfBoundsException e) {
 155         }
 156 
 157         // case 3:
 158         try {
 159             // Result in IndexOutOfBoundsException for gather.
 160             av = FloatVector.fromArray(SPECIES, a, 0, ALL_TRUE_MASK, indexMap, 0);
 161             Assert.fail("Expected IndexOutOfBoundsException not thrown");
 162         } catch(IndexOutOfBoundsException e) {
 163         }
 164     }
 165 
 166     @Test(dataProvider = "floatProvider")
 167     static void loadStoreArray(IntFunction<float[]> fa) {
 168         float[] a = fa.apply(SPECIES.length() / 2);
 169         float[] r = new float[a.length];
 170 
 171         // case 1:
 172         try {
 173             // Result in IndexOutOfBoundsException for fromArray().
 174             FloatVector av = FloatVector.fromArray(SPECIES, a, 0);
 175             Assert.fail("Expected IndexOutOfBoundsException not thrown");
 176         } catch(IndexOutOfBoundsException e) {
 177         }
 178 
 179         // case 2:
 180         FloatVector av = FloatVector.fromArray(SPECIES, a, 0, LOAD_STORE_MASK);
 181         try {
 182             // Result in IndexOutOfBoundsException for intoArray().
 183             av.intoArray(r, 0);
 184             Assert.fail("Expected IndexOutOfBoundsException not thrown");
 185         } catch(IndexOutOfBoundsException e) {
 186         }
 187     }
 188 
 189     @Test(dataProvider = "floatIndexMapProvider")
 190     static void gatherScatterArray(IntFunction<float[]> fa,
 191                                    IntFunction<int[]> fb) {
 192         float[] a = fa.apply(SPECIES.length());
 193         int[] indexMap = fb.apply(SPECIES.length());
 194         float[] r = new float[a.length];
 195 
 196         // case 1:
 197         try {
 198             // Result in IndexOutOfBoundsException for gather.
 199             FloatVector av = FloatVector.fromArray(SPECIES, a, 0, indexMap, 0);
 200             Assert.fail("Expected IndexOutOfBoundsException not thrown");
 201         } catch(IndexOutOfBoundsException e) {
 202         }
 203 
 204         // case 2:
 205         FloatVector av = FloatVector.fromArray(SPECIES, a, 0, GATHER_SCATTER_MASK, indexMap, 0);
 206         try {
 207             // Result in IndexOutOfBoundsException for scatter.
 208             av.intoArray(r, 0, indexMap, 0);
 209             Assert.fail("Expected IndexOutOfBoundsException not thrown");
 210         } catch(IndexOutOfBoundsException e) {
 211         }
 212     }
 213 }