/* * 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 Int64VectorExceptionTests * */ import jdk.incubator.vector.Vector.Shape; import jdk.incubator.vector.Vector; import jdk.incubator.vector.IntVector; 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 Int64VectorExceptionTests extends AbstractVectorTest { static final IntVector.IntSpecies SPECIES = IntVector.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 = IntVector.maskFromValues( SPECIES, fill_boolean(SPECIES.length(), i -> (i < LOAD_STORE_ARRAY_LENGTH))); static final Vector.Mask GATHER_SCATTER_MASK = IntVector.maskFromValues( SPECIES, fill_boolean(SPECIES.length(), i -> ((i * 2 + 1) < GATHER_SCATTER_ARRAY_LENGTH))); static final Vector.Mask ALL_TRUE_MASK = IntVector.maskFromValues( SPECIES, fill_boolean(SPECIES.length(), i -> true)); interface ToIntF { int apply(int i); } static int[] fill(int s , ToIntF f) { return fill(new int[s], f); } static int[] fill(int[] a, ToIntF f) { for (int i = 0; i < a.length; i++) { a[i] = f.apply(i); } return a; } static final List> INT_GENERATORS = List.of( withToString("int[i + 1]", (int s) -> { return fill(s, i -> (((int)(i + 1) == 0) ? 1 : (int)(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[][] intProvider() { return INT_GENERATORS.stream(). map(f -> new Object[]{f}). toArray(Object[][]::new); } @DataProvider public Object[][] intIndexMapProvider() { return INDEX_GENERATORS.stream(). flatMap(fim -> INT_GENERATORS.stream().map(fa -> { return new Object[] {fa, fim}; })). toArray(Object[][]::new); } @Test(dataProvider = "intProvider") static void loadStoreArrayWithMask(IntFunction fa) { int[] a = fa.apply(SPECIES.length() / 2); int[] r = new int[a.length]; // case 1: No IndexOutOfBoundsException for unset mask lanes. IntVector av = IntVector.fromArray(SPECIES, a, 0, LOAD_STORE_MASK); av.intoArray(r, 0, LOAD_STORE_MASK); Assert.assertEquals(a, r); // case 2: int[] new_a = new int[a.length]; av = IntVector.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 = IntVector.fromArray(SPECIES, a, 0, ALL_TRUE_MASK); Assert.fail("Expected IndexOutOfBoundsException not thrown"); } catch(IndexOutOfBoundsException e) { } } @Test(dataProvider = "intIndexMapProvider") static void gatherScatterArrayWithMask(IntFunction fa, IntFunction fb) { int[] a = fa.apply(SPECIES.length()); int[] indexMap = fb.apply(SPECIES.length()); int[] r = new int[a.length]; // case 1: No IndexOutOfBoundsException for unset mask lanes. IntVector av = IntVector.fromArray(SPECIES, a, 0); av.intoArray(r, 0, GATHER_SCATTER_MASK, indexMap, 0); av = IntVector.fromArray(SPECIES, a, 0, GATHER_SCATTER_MASK, indexMap, 0); // case 2: int[] r1 = new int[a.length]; int[] r2 = new int[a.length]; av = IntVector.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 = IntVector.fromArray(SPECIES, a, 0, ALL_TRUE_MASK, indexMap, 0); Assert.fail("Expected IndexOutOfBoundsException not thrown"); } catch(IndexOutOfBoundsException e) { } } @Test(dataProvider = "intProvider") static void loadStoreArray(IntFunction fa) { int[] a = fa.apply(SPECIES.length() / 2); int[] r = new int[a.length]; // case 1: try { // Result in IndexOutOfBoundsException for fromArray(). IntVector av = IntVector.fromArray(SPECIES, a, 0); Assert.fail("Expected IndexOutOfBoundsException not thrown"); } catch(IndexOutOfBoundsException e) { } // case 2: IntVector av = IntVector.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 = "intIndexMapProvider") static void gatherScatterArray(IntFunction fa, IntFunction fb) { int[] a = fa.apply(SPECIES.length()); int[] indexMap = fb.apply(SPECIES.length()); int[] r = new int[a.length]; // case 1: try { // Result in IndexOutOfBoundsException for gather. IntVector av = IntVector.fromArray(SPECIES, a, 0, indexMap, 0); Assert.fail("Expected IndexOutOfBoundsException not thrown"); } catch(IndexOutOfBoundsException e) { } // case 2: IntVector av = IntVector.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) { } } }