/* * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2019, Arm Limited. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have * questions. */ /* * @test * @modules jdk.incubator.vector * @run testng Float64VectorExceptionTests * */ import jdk.incubator.vector.Vector.Shape; import jdk.incubator.vector.Vector; import jdk.incubator.vector.FloatVector; import org.testng.Assert; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; import java.util.List; import java.util.function.IntFunction; @Test public class Float64VectorExceptionTests extends AbstractVectorTest { static final FloatVector.FloatSpecies SPECIES = FloatVector.species(Shape.S_64_BIT); static final int LOAD_STORE_ARRAY_LENGTH = SPECIES.length() / 2; static final int GATHER_SCATTER_ARRAY_LENGTH = SPECIES.length(); static final Vector.Mask LOAD_STORE_MASK = FloatVector.maskFromValues( SPECIES, fill_boolean(SPECIES.length(), i -> (i < LOAD_STORE_ARRAY_LENGTH))); static final Vector.Mask GATHER_SCATTER_MASK = FloatVector.maskFromValues( SPECIES, fill_boolean(SPECIES.length(), i -> ((i * 2 + 1) < GATHER_SCATTER_ARRAY_LENGTH))); static final Vector.Mask ALL_TRUE_MASK = FloatVector.maskFromValues( SPECIES, fill_boolean(SPECIES.length(), i -> true)); interface ToFloatF { float apply(int i); } static float[] fill(int s , ToFloatF f) { return fill(new float[s], f); } static float[] fill(float[] a, ToFloatF f) { for (int i = 0; i < a.length; i++) { a[i] = f.apply(i); } return a; } static final List> FLOAT_GENERATORS = List.of( withToString("float[i + 1]", (int s) -> { return fill(s, i -> (((float)(i + 1) == 0) ? 1 : (float)(i + 1))); }) ); static final List> INDEX_GENERATORS = List.of( withToString("index[i * 2 + 1]", (int s) -> { return fillInts(s, i -> (i * 2 + 1)); }) ); @DataProvider public Object[][] floatProvider() { return FLOAT_GENERATORS.stream(). map(f -> new Object[]{f}). toArray(Object[][]::new); } @DataProvider public Object[][] floatIndexMapProvider() { return INDEX_GENERATORS.stream(). flatMap(fim -> FLOAT_GENERATORS.stream().map(fa -> { return new Object[] {fa, fim}; })). toArray(Object[][]::new); } @Test(dataProvider = "floatProvider") static void loadStoreArrayWithMask(IntFunction fa) { float[] a = fa.apply(SPECIES.length() / 2); float[] r = new float[a.length]; // case 1: No IndexOutOfBoundsException for unset mask lanes. FloatVector av = FloatVector.fromArray(SPECIES, a, 0, LOAD_STORE_MASK); av.intoArray(r, 0, LOAD_STORE_MASK); Assert.assertEquals(a, r); // case 2: float[] new_a = new float[a.length]; av = FloatVector.fromArray(SPECIES, new_a, 0, LOAD_STORE_MASK); try { // Result in IndexOutOfBoundsException for intoArray(). av.intoArray(r, 0, ALL_TRUE_MASK); Assert.fail("Expected IndexOutOfBoundsException not thrown"); } catch(IndexOutOfBoundsException e) { } // case 3: try { // Result in IndexOutOfBoundsException for fromArray(). av = FloatVector.fromArray(SPECIES, a, 0, ALL_TRUE_MASK); Assert.fail("Expected IndexOutOfBoundsException not thrown"); } catch(IndexOutOfBoundsException e) { } } @Test(dataProvider = "floatIndexMapProvider") static void gatherScatterArrayWithMask(IntFunction fa, IntFunction fb) { float[] a = fa.apply(SPECIES.length()); int[] indexMap = fb.apply(SPECIES.length()); float[] r = new float[a.length]; // case 1: No IndexOutOfBoundsException for unset mask lanes. FloatVector av = FloatVector.fromArray(SPECIES, a, 0); av.intoArray(r, 0, GATHER_SCATTER_MASK, indexMap, 0); av = FloatVector.fromArray(SPECIES, a, 0, GATHER_SCATTER_MASK, indexMap, 0); // case 2: float[] r1 = new float[a.length]; float[] r2 = new float[a.length]; av = FloatVector.fromArray(SPECIES, a, 0); try { // Result in IndexOutOfBoundsException for scatter. av.intoArray(r2, 0, ALL_TRUE_MASK, indexMap, 0); Assert.fail("Expected IndexOutOfBoundsException not thrown"); } catch(IndexOutOfBoundsException e) { } // case 3: try { // Result in IndexOutOfBoundsException for gather. av = FloatVector.fromArray(SPECIES, a, 0, ALL_TRUE_MASK, indexMap, 0); Assert.fail("Expected IndexOutOfBoundsException not thrown"); } catch(IndexOutOfBoundsException e) { } } @Test(dataProvider = "floatProvider") static void loadStoreArray(IntFunction fa) { float[] a = fa.apply(SPECIES.length() / 2); float[] r = new float[a.length]; // case 1: try { // Result in IndexOutOfBoundsException for fromArray(). FloatVector av = FloatVector.fromArray(SPECIES, a, 0); Assert.fail("Expected IndexOutOfBoundsException not thrown"); } catch(IndexOutOfBoundsException e) { } // case 2: FloatVector av = FloatVector.fromArray(SPECIES, a, 0, LOAD_STORE_MASK); try { // Result in IndexOutOfBoundsException for intoArray(). av.intoArray(r, 0); Assert.fail("Expected IndexOutOfBoundsException not thrown"); } catch(IndexOutOfBoundsException e) { } } @Test(dataProvider = "floatIndexMapProvider") static void gatherScatterArray(IntFunction fa, IntFunction fb) { float[] a = fa.apply(SPECIES.length()); int[] indexMap = fb.apply(SPECIES.length()); float[] r = new float[a.length]; // case 1: try { // Result in IndexOutOfBoundsException for gather. FloatVector av = FloatVector.fromArray(SPECIES, a, 0, indexMap, 0); Assert.fail("Expected IndexOutOfBoundsException not thrown"); } catch(IndexOutOfBoundsException e) { } // case 2: FloatVector av = FloatVector.fromArray(SPECIES, a, 0, GATHER_SCATTER_MASK, indexMap, 0); try { // Result in IndexOutOfBoundsException for scatter. av.intoArray(r, 0, indexMap, 0); Assert.fail("Expected IndexOutOfBoundsException not thrown"); } catch(IndexOutOfBoundsException e) { } } }