Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/util/Arrays/ParallelPrefix.java
38811 views
/*1* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test 8014076 802506725* @summary unit test for Arrays.ParallelPrefix().26* @author Tristan Yan27* @run testng ParallelPrefix28*/2930import java.util.Arrays;31import java.util.function.BinaryOperator;32import java.util.function.DoubleBinaryOperator;33import java.util.function.Function;34import java.util.function.IntBinaryOperator;35import java.util.function.LongBinaryOperator;36import java.util.stream.IntStream;37import java.util.stream.LongStream;38import static org.testng.Assert.*;39import org.testng.annotations.DataProvider;40import org.testng.annotations.Test;4142public class ParallelPrefix {43//Array size less than MIN_PARTITION44private final static int SMALL_ARRAY_SIZE = 1 << 3;4546//Array size equals MIN_PARTITION47private final static int THRESHOLD_ARRAY_SIZE = 1 << 4;4849//Array size greater than MIN_PARTITION50private final static int MEDIUM_ARRAY_SIZE = 1 << 8;5152//Array size much greater than MIN_PARTITION53private final static int LARGE_ARRAY_SIZE = 1 << 12;5455private final static int[] ARRAY_SIZE_COLLECTION = new int[]{56SMALL_ARRAY_SIZE,57THRESHOLD_ARRAY_SIZE,58MEDIUM_ARRAY_SIZE,59LARGE_ARRAY_SIZE60};6162@DataProvider63public static Object[][] intSet(){64return genericData(size -> IntStream.range(0, size).toArray(),65new IntBinaryOperator[]{66Integer::sum,67Integer::min});68}6970@DataProvider71public static Object[][] longSet(){72return genericData(size -> LongStream.range(0, size).toArray(),73new LongBinaryOperator[]{74Long::sum,75Long::min});76}7778@DataProvider79public static Object[][] doubleSet(){80return genericData(size -> IntStream.range(0, size).mapToDouble(i -> (double)i).toArray(),81new DoubleBinaryOperator[]{82Double::sum,83Double::min});84}8586@DataProvider87public static Object[][] stringSet(){88Function<Integer, String[]> stringsFunc = size ->89IntStream.range(0, size).mapToObj(Integer::toString).toArray(String[]::new);90BinaryOperator<String> concat = String::concat;91return genericData(stringsFunc,92(BinaryOperator<String>[]) new BinaryOperator[]{93concat });94}9596private static <T, OPS> Object[][] genericData(Function<Integer, T> generateFunc, OPS[] ops) {97//test arrays which size is equals n-1, n, n+1, test random data98Object[][] data = new Object[ARRAY_SIZE_COLLECTION.length * 3 * ops.length][4];99for(int n = 0; n < ARRAY_SIZE_COLLECTION.length; n++ ) {100for(int testValue = -1 ; testValue <= 1; testValue++) {101int array_size = ARRAY_SIZE_COLLECTION[n] + testValue;102for(int opsN = 0; opsN < ops.length; opsN++) {103int index = n * 3 * ops.length + (testValue + 1) * ops.length + opsN;104data[index][0] = generateFunc.apply(array_size);105data[index][1] = array_size / 3;106data[index][2] = 2 * array_size / 3;107data[index][3] = ops[opsN];108}109}110}111return data;112}113114@Test(dataProvider="intSet")115public void testParallelPrefixForInt(int[] data, int fromIndex, int toIndex, IntBinaryOperator op) {116int[] sequentialResult = data.clone();117for (int index = fromIndex + 1; index < toIndex; index++) {118sequentialResult[index ] = op.applyAsInt(sequentialResult[index - 1], sequentialResult[index]);119}120121int[] parallelResult = data.clone();122Arrays.parallelPrefix(parallelResult, fromIndex, toIndex, op);123assertEquals(parallelResult, sequentialResult);124125int[] parallelRangeResult = Arrays.copyOfRange(data, fromIndex, toIndex);126Arrays.parallelPrefix(parallelRangeResult, op);127assertEquals(parallelRangeResult, Arrays.copyOfRange(sequentialResult, fromIndex, toIndex));128}129130@Test(dataProvider="longSet")131public void testParallelPrefixForLong(long[] data, int fromIndex, int toIndex, LongBinaryOperator op) {132long[] sequentialResult = data.clone();133for (int index = fromIndex + 1; index < toIndex; index++) {134sequentialResult[index ] = op.applyAsLong(sequentialResult[index - 1], sequentialResult[index]);135}136137long[] parallelResult = data.clone();138Arrays.parallelPrefix(parallelResult, fromIndex, toIndex, op);139assertEquals(parallelResult, sequentialResult);140141long[] parallelRangeResult = Arrays.copyOfRange(data, fromIndex, toIndex);142Arrays.parallelPrefix(parallelRangeResult, op);143assertEquals(parallelRangeResult, Arrays.copyOfRange(sequentialResult, fromIndex, toIndex));144}145146@Test(dataProvider="doubleSet")147public void testParallelPrefixForDouble(double[] data, int fromIndex, int toIndex, DoubleBinaryOperator op) {148double[] sequentialResult = data.clone();149for (int index = fromIndex + 1; index < toIndex; index++) {150sequentialResult[index ] = op.applyAsDouble(sequentialResult[index - 1], sequentialResult[index]);151}152153double[] parallelResult = data.clone();154Arrays.parallelPrefix(parallelResult, fromIndex, toIndex, op);155assertEquals(parallelResult, sequentialResult);156157double[] parallelRangeResult = Arrays.copyOfRange(data, fromIndex, toIndex);158Arrays.parallelPrefix(parallelRangeResult, op);159assertEquals(parallelRangeResult, Arrays.copyOfRange(sequentialResult, fromIndex, toIndex));160}161162@Test(dataProvider="stringSet")163public void testParallelPrefixForStringr(String[] data , int fromIndex, int toIndex, BinaryOperator<String> op) {164String[] sequentialResult = data.clone();165for (int index = fromIndex + 1; index < toIndex; index++) {166sequentialResult[index ] = op.apply(sequentialResult[index - 1], sequentialResult[index]);167}168169String[] parallelResult = data.clone();170Arrays.parallelPrefix(parallelResult, fromIndex, toIndex, op);171assertEquals(parallelResult, sequentialResult);172173String[] parallelRangeResult = Arrays.copyOfRange(data, fromIndex, toIndex);174Arrays.parallelPrefix(parallelRangeResult, op);175assertEquals(parallelRangeResult, Arrays.copyOfRange(sequentialResult, fromIndex, toIndex));176}177178@Test179public void testNPEs() {180// null array181assertThrows( () -> Arrays.parallelPrefix((int[]) null, Integer::max),182NullPointerException.class, "should throw NPE");183assertThrows( () -> Arrays.parallelPrefix((long []) null, Long::max),184NullPointerException.class, "should throw NPE");185assertThrows( () -> Arrays.parallelPrefix((double []) null, Double::max),186NullPointerException.class, "should throw NPE");187assertThrows( () -> Arrays.parallelPrefix((String []) null, String::concat),188NullPointerException.class, "should throw NPE");189190// null array w/ range191assertThrows( () -> Arrays.parallelPrefix((int[]) null, 0, 0, Integer::max),192NullPointerException.class, "should throw NPE");193assertThrows( () -> Arrays.parallelPrefix((long []) null, 0, 0, Long::max),194NullPointerException.class, "should throw NPE");195assertThrows( () -> Arrays.parallelPrefix((double []) null, 0, 0, Double::max),196NullPointerException.class, "should throw NPE");197assertThrows( () -> Arrays.parallelPrefix((String []) null, 0, 0, String::concat),198NullPointerException.class, "should throw NPE");199200// null op201assertThrows( () -> Arrays.parallelPrefix(new int[] {}, null),202NullPointerException.class, "should throw NPE");203assertThrows( () -> Arrays.parallelPrefix(new long[] {}, null),204NullPointerException.class, "should throw NPE");205assertThrows( () -> Arrays.parallelPrefix(new double[] {}, null),206NullPointerException.class, "should throw NPE");207assertThrows( () -> Arrays.parallelPrefix(new String[] {}, null),208NullPointerException.class, "should throw NPE");209210// null op w/ range211assertThrows( () -> Arrays.parallelPrefix(new int[] {}, 0, 0, null),212NullPointerException.class, "should throw NPE");213assertThrows( () -> Arrays.parallelPrefix(new long[] {}, 0, 0, null),214NullPointerException.class, "should throw NPE");215assertThrows( () -> Arrays.parallelPrefix(new double[] {}, 0, 0, null),216NullPointerException.class, "should throw NPE");217assertThrows( () -> Arrays.parallelPrefix(new String[] {}, 0, 0, null),218NullPointerException.class, "should throw NPE");219}220221@Test222public void testIAEs() {223assertThrows( () -> Arrays.parallelPrefix(new int[] {}, 1, 0, Integer::max),224IllegalArgumentException.class, "should throw IAE");225assertThrows( () -> Arrays.parallelPrefix(new long[] {}, 1, 0, Long::max),226IllegalArgumentException.class, "should throw IAE");227assertThrows( () -> Arrays.parallelPrefix(new double[] {}, 1, 0, Double::max),228IllegalArgumentException.class, "should throw IAE");229assertThrows( () -> Arrays.parallelPrefix(new String[] {}, 1, 0, String::concat),230IllegalArgumentException.class, "should throw IAE");231}232233@Test234public void testAIOBEs() {235// bad "fromIndex"236assertThrows( () -> Arrays.parallelPrefix(new int[] {}, -1, 0, Integer::max),237ArrayIndexOutOfBoundsException.class, "should throw AIOBE");238assertThrows( () -> Arrays.parallelPrefix(new long[] {}, -1, 0, Long::max),239ArrayIndexOutOfBoundsException.class, "should throw AIOBE");240assertThrows( () -> Arrays.parallelPrefix(new double[] {}, -1, 0, Double::max),241ArrayIndexOutOfBoundsException.class, "should throw AIOBE");242assertThrows( () -> Arrays.parallelPrefix(new String[] {}, -1, 0, String::concat),243ArrayIndexOutOfBoundsException.class, "should throw AIOBE");244245// bad "toIndex"246assertThrows( () -> Arrays.parallelPrefix(new int[] {}, 0, 1, Integer::max),247ArrayIndexOutOfBoundsException.class, "should throw AIOBE");248assertThrows( () -> Arrays.parallelPrefix(new long[] {}, 0, 1, Long::max),249ArrayIndexOutOfBoundsException.class, "should throw AIOBE");250assertThrows( () -> Arrays.parallelPrefix(new double[] {}, 0, 1, Double::max),251ArrayIndexOutOfBoundsException.class, "should throw AIOBE");252assertThrows( () -> Arrays.parallelPrefix(new String[] {}, 0, 1, String::concat),253ArrayIndexOutOfBoundsException.class, "should throw AIOBE");254}255256// "library" code257258public interface Thrower<T extends Throwable> {259260public void run() throws T;261}262263264public static <T extends Throwable> void assertThrows(Thrower<T> thrower, Class<T> throwable) {265assertThrows(thrower, throwable, null);266}267268public static <T extends Throwable> void assertThrows(Thrower<T> thrower, Class<T> throwable, String message) {269Throwable thrown;270try {271thrower.run();272thrown = null;273} catch (Throwable caught) {274thrown = caught;275}276277assertInstance(thrown, throwable,278((null != message) ? message : "") +279" Failed to throw " + throwable.getCanonicalName());280}281282public static <T extends Throwable> void assertThrows(Class<T> throwable, String message, Thrower<T>... throwers) {283for(Thrower<T> thrower : throwers) {284assertThrows(thrower, throwable, message);285}286}287288public static void assertInstance(Object actual, Class<?> expected) {289assertInstance(expected.isInstance(actual), null);290}291292public static void assertInstance(Object actual, Class<?> expected, String message) {293assertTrue(expected.isInstance(actual), message);294}295}296297298299