Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
8255174: Vector API unit tests for missed public api code coverage
Reviewed-by: psandoz
  • Loading branch information
Sandhya Viswanathan committed Oct 22, 2020
1 parent b9186be commit 5d26229
Show file tree
Hide file tree
Showing 67 changed files with 19,354 additions and 100 deletions.
74 changes: 74 additions & 0 deletions test/jdk/jdk/incubator/vector/AbstractVectorTest.java
Expand Up @@ -31,6 +31,11 @@
import java.util.Set;
import java.util.function.BiFunction;
import java.util.function.IntFunction;
import java.util.function.IntUnaryOperator;
import java.util.stream.Stream;
import java.util.stream.Collectors;

import org.testng.Assert;

public class AbstractVectorTest {

Expand Down Expand Up @@ -149,12 +154,53 @@ static int[] fillInts(int[] a, IntOp f) {
withToString("mask[false]", boolean[]::new)
);

static final List<List<IntFunction<boolean[]>>>
BOOLEAN_MASK_COMPARE_GENERATOR_PAIRS =
Stream.of(BOOLEAN_MASK_GENERATORS.get(0)).
flatMap(fa -> BOOLEAN_MASK_GENERATORS.stream().skip(1).map(
fb -> List.of(fa, fb))).collect(Collectors.toList());

static final List<BiFunction<Integer,Integer,int[]>> INT_SHUFFLE_GENERATORS = List.of(
withToStringBi("shuffle[random]", (Integer l, Integer m) -> {
return RAND.ints(l, 0, m).toArray();
})
);

interface RangeIntOp {
int apply(int i, int min, int max);
}

static int[] fillRangeInts(int s, int min, int max, RangeIntOp f) {
return fillRangeInts(new int[s], min, max, f);
}

static int[] fillRangeInts(int[] a, int min, int max, RangeIntOp f) {
for (int i = 0; i < a.length; i++) {
a[i] = f.apply(i, min, max);
}
return a;
}

static final List<List<BiFunction<Integer, Integer, int[]>>>
INT_SHUFFLE_COMPARE_GENERATOR_PAIRS = List.of(
List.of(
withToStringBi("shuffle[i]", (Integer l, Integer m) -> {
return fillRangeInts(l, 0, m, (i, _min, _max) -> (i % _max));
}),
withToStringBi("shuffle[random]", (Integer l, Integer m) -> {
return RAND.ints(l, 0, m).toArray();
})
),
List.of(
withToStringBi("shuffle[i]", (Integer l, Integer m) -> {
return fillRangeInts(l, 0, m, (i, _min, _max) -> (i % _max));
}),
withToStringBi("shuffle[random]", (Integer l, Integer m) -> {
return RAND.ints(l, 0, m).toArray();
})
)
);

static final List<BiFunction<Integer,Integer,int[]>> INT_INDEX_GENERATORS = List.of(
withToStringBi("index[random]", (Integer l, Integer m) -> {
return RAND.ints(l, 0, m).toArray();
Expand Down Expand Up @@ -187,4 +233,32 @@ static boolean isIndexOutOfBounds(int size, int offset, int length) {
int upperBound = offset + size;
return upperBound < size || upperBound > length;
}

public static int[] expectedShuffle(int length, IntUnaryOperator fn) {
int [] a = new int[length];
for (int i = 0; i < length; i++) {
int elem = fn.applyAsInt(i);
int wrapElem = Math.floorMod(elem, length);
if (elem != wrapElem) {
elem = wrapElem - length;
}
a[i] = elem;
}
return a;
}

interface FBooleanBinOp {
boolean apply(boolean a, boolean b);
}

static void assertArraysEquals(boolean[] a, boolean[] b, boolean[] r, FBooleanBinOp f) {
int i = 0;
try {
for (; i < a.length; i++) {
Assert.assertEquals(r[i], f.apply(a[i], b[i]));
}
} catch (AssertionError e) {
Assert.assertEquals(r[i], f.apply(a[i], b[i]), "(" + a[i] + ", " + b[i] + ") at index #" + i);
}
}
}
36 changes: 35 additions & 1 deletion test/jdk/jdk/incubator/vector/Byte128VectorLoadStoreTests.java
Expand Up @@ -33,6 +33,7 @@
import jdk.incubator.vector.ByteVector;
import jdk.incubator.vector.VectorMask;
import jdk.incubator.vector.VectorSpecies;
import jdk.incubator.vector.VectorShuffle;
import jdk.internal.vm.annotation.DontInline;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
Expand All @@ -42,7 +43,7 @@
import java.nio.ByteOrder;
import java.nio.ReadOnlyBufferException;
import java.util.List;
import java.util.function.IntFunction;
import java.util.function.*;

@Test
public class Byte128VectorLoadStoreTests extends AbstractVectorTest {
Expand Down Expand Up @@ -164,6 +165,13 @@ public Object[][] byteProvider() {
toArray(Object[][]::new);
}

@DataProvider
public Object[][] maskProvider() {
return BOOLEAN_MASK_GENERATORS.stream().
map(f -> new Object[]{f}).
toArray(Object[][]::new);
}

@DataProvider
public Object[][] byteProviderForIOOBE() {
var f = BYTE_GENERATORS.get(0);
Expand Down Expand Up @@ -936,4 +944,30 @@ static void storeByteArrayMaskIOOBE(IntFunction<byte[]> fa, IntFunction<Integer>
}
}
}

@Test(dataProvider = "maskProvider")
static void loadStoreMask(IntFunction<boolean[]> fm) {
boolean[] a = fm.apply(SPECIES.length());
boolean[] r = new boolean[a.length];

for (int ic = 0; ic < INVOC_COUNT; ic++) {
for (int i = 0; i < a.length; i += SPECIES.length()) {
VectorMask<Byte> vmask = SPECIES.loadMask(a, i);
vmask.intoArray(r, i);
}
}
Assert.assertEquals(a, r);
}

@Test
static void loadStoreShuffle() {
IntUnaryOperator fn = a -> a + 5;
for (int ic = 0; ic < INVOC_COUNT; ic++) {
var shuffle = VectorShuffle.fromOp(SPECIES, fn);
int [] r = shuffle.toArray();

int [] a = expectedShuffle(SPECIES.length(), fn);
Assert.assertEquals(a, r);
}
}
}

1 comment on commit 5d26229

@bridgekeeper
Copy link

@bridgekeeper bridgekeeper bot commented on 5d26229 Oct 22, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.