Path: blob/master/test/jdk/java/util/Arrays/SetAllTest.java
41149 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* @test25* @bug 801265026* @summary Unit test for setAll, parallelSetAll variants27* @run testng SetAllTest28*/2930import org.testng.annotations.DataProvider;31import org.testng.annotations.Test;3233import java.util.Arrays;34import java.util.function.IntFunction;35import java.util.function.IntToDoubleFunction;36import java.util.function.IntToLongFunction;37import java.util.function.IntUnaryOperator;3839import static org.testng.Assert.assertEquals;40import static org.testng.Assert.assertTrue;41import static org.testng.Assert.assertSame;42import static org.testng.Assert.fail;4344@Test45public class SetAllTest {46private static final IntFunction<String> toString = i -> "N" + Integer.valueOf(i);47private static final IntFunction<String> fillString = i -> "X";48private static final String[] r0 = {};49private static final String[] r1 = { "N0" };50private static final String[] r10 = { "N0", "N1", "N2", "N3", "N4", "N5", "N6", "N7", "N8", "N9" };5152private Object[][] stringData = new Object[][] {53{ "empty", 0, toString, r0 },54{ "one", 1, toString, r1 },55{ "ten", 10, toString, r10 },56{ "fill", 3, fillString, new String[] { "X", "X", "X" }}57};5859private static final IntUnaryOperator toInt = i -> i << 1;60private static final IntUnaryOperator fillInt = i -> 99;61private static final int[] ir0 = {};62private static final int[] ir1 = { 0 };63private static final int[] ir10 = { 0, 2, 4, 6, 8, 10, 12, 14, 16, 18 };64private Object[][] intData = new Object[][] {65{ "empty", 0, toInt, ir0 },66{ "one", 1, toInt, ir1 },67{ "ten", 10, toInt, ir10 },68{ "fill", 3, fillInt, new int[] { 99, 99, 99 }}69};7071private static final IntToLongFunction toLong = i -> i << 1;72private static final IntToLongFunction fillLong = i -> 9999L;73private static final long[] lr0 = {};74private static final long[] lr1 = { 0L };75private static final long[] lr10 = { 0L, 2L, 4L, 6L, 8L, 10L, 12L, 14L, 16L, 18L };76private Object[][] longData = new Object[][] {77{ "empty", 0, toLong, lr0 },78{ "one", 1, toLong, lr1 },79{ "ten", 10, toLong, lr10 },80{ "fill", 3, fillLong, new long[] { 9999L, 9999L, 9999L }}81};8283private static final IntToDoubleFunction toDouble = i -> i * 1.1;84private static final IntToDoubleFunction fillDouble = i -> 3.14;85private static final double[] dr0 = {};86private static final double[] dr1 = { 0.0 };87private static final double[] dr10 = { 0.0, 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9 };88private Object[][] doubleData = new Object[][] {89{ "empty", 0, toDouble, dr0 },90{ "one", 1, toDouble, dr1 },91{ "ten", 10, toDouble, dr10 },92{ "fill", 3, fillDouble, new double[] { 3.14, 3.14, 3.14 }}93};9495@DataProvider(name="string")96public Object[][] stringTests() { return stringData; }9798@DataProvider(name="int")99public Object[][] intTests() { return intData; }100101@DataProvider(name="long")102public Object[][] longTests() { return longData; }103104@DataProvider(name="double")105public Object[][] doubleTests() { return doubleData; }106107@Test(dataProvider = "string")108public void testSetAllString(String name, int size, IntFunction<String> generator, String[] expected) {109String[] result = new String[size];110Arrays.setAll(result, generator);111assertEquals(result, expected, "setAll(String[], IntFunction<String>) case " + name + " failed.");112113// ensure fresh array114result = new String[size];115Arrays.parallelSetAll(result, generator);116assertEquals(result, expected, "parallelSetAll(String[], IntFunction<String>) case " + name + " failed.");117}118119@Test(dataProvider = "int")120public void testSetAllInt(String name, int size, IntUnaryOperator generator, int[] expected) {121int[] result = new int[size];122Arrays.setAll(result, generator);123assertEquals(result, expected, "setAll(int[], IntUnaryOperator) case " + name + " failed.");124125// ensure fresh array126result = new int[size];127Arrays.parallelSetAll(result, generator);128assertEquals(result, expected, "parallelSetAll(int[], IntUnaryOperator) case " + name + " failed.");129}130131@Test(dataProvider = "long")132public void testSetAllLong(String name, int size, IntToLongFunction generator, long[] expected) {133long[] result = new long[size];134Arrays.setAll(result, generator);135assertEquals(result, expected, "setAll(long[], IntToLongFunction) case " + name + " failed.");136137// ensure fresh array138result = new long[size];139Arrays.parallelSetAll(result, generator);140assertEquals(result, expected, "parallelSetAll(long[], IntToLongFunction) case " + name + " failed.");141}142143private void assertDoubleArrayEquals(double[] actual, double[] expected, double delta, String msg) {144if (actual.length != expected.length) {145fail(msg + ": length mismatch, expected " + expected.length + ", got " + actual.length);146}147148for (int i = 0; i < actual.length; i++) {149assertEquals(actual[i], expected[i], delta, msg + "(mismatch at index " + i + ")");150}151}152153@Test(dataProvider = "double")154public void testSetAllDouble(String name, int size, IntToDoubleFunction generator, double[] expected) {155double[] result = new double[size];156Arrays.setAll(result, generator);157assertDoubleArrayEquals(result, expected, 0.05, "setAll(double[], IntToDoubleFunction) case " + name + " failed.");158159// ensure fresh array160result = new double[size];161Arrays.parallelSetAll(result, generator);162assertDoubleArrayEquals(result, expected, 0.05, "setAll(double[], IntToDoubleFunction) case " + name + " failed.");163}164165@Test166public void testStringSetNulls() {167String[] ar = new String[2];168try {169Arrays.setAll(null, (IntFunction<String>) i -> "X");170fail("Arrays.setAll(null, foo) should throw NPE");171} catch (NullPointerException npe) {172// expected173}174try {175Arrays.parallelSetAll(null, (IntFunction<String>) i -> "X");176fail("Arrays.parallelSetAll(null, foo) should throw NPE");177} catch (NullPointerException npe) {178// expected179}180try {181Arrays.setAll(ar, null);182fail("Arrays.setAll(array, null) should throw NPE");183} catch (NullPointerException npe) {184// expected185}186try {187Arrays.parallelSetAll(ar, null);188fail("Arrays.parallelSetAll(array, null) should throw NPE");189} catch (NullPointerException npe) {190// expected191}192}193194@Test195public void testIntSetNulls() {196int[] ar = new int[2];197try {198Arrays.setAll(null, (IntUnaryOperator) i -> i);199fail("Arrays.setAll(null, foo) should throw NPE");200} catch (NullPointerException npe) {201// expected202}203try {204Arrays.parallelSetAll(null, (IntUnaryOperator) i -> i);205fail("Arrays.parallelSetAll(null, foo) should throw NPE");206} catch (NullPointerException npe) {207// expected208}209try {210Arrays.setAll(ar, null);211fail("Arrays.setAll(array, null) should throw NPE");212} catch (NullPointerException npe) {213// expected214}215try {216Arrays.parallelSetAll(ar, null);217fail("Arrays.parallelSetAll(array, null) should throw NPE");218} catch (NullPointerException npe) {219// expected220}221}222223@Test224public void testLongSetNulls() {225long[] ar = new long[2];226try {227Arrays.setAll(null, (IntToLongFunction) i -> Long.MAX_VALUE);228fail("Arrays.setAll(null, foo) should throw NPE");229} catch (NullPointerException npe) {230// expected231}232try {233Arrays.parallelSetAll(null, (IntToLongFunction) i -> Long.MAX_VALUE);234fail("Arrays.parallelSetAll(null, foo) should throw NPE");235} catch (NullPointerException npe) {236// expected237}238try {239Arrays.setAll(ar, null);240fail("Arrays.setAll(array, null) should throw NPE");241} catch (NullPointerException npe) {242// expected243}244try {245Arrays.parallelSetAll(ar, null);246fail("Arrays.parallelSetAll(array, null) should throw NPE");247} catch (NullPointerException npe) {248// expected249}250}251252@Test253public void testDoubleSetNulls() {254double[] ar = new double[2];255try {256Arrays.setAll(null, (IntToDoubleFunction) i -> Math.E);257fail("Arrays.setAll(null, foo) should throw NPE");258} catch (NullPointerException npe) {259// expected260}261try {262Arrays.parallelSetAll(null, (IntToDoubleFunction) i -> Math.E);263fail("Arrays.parallelSetAll(null, foo) should throw NPE");264} catch (NullPointerException npe) {265// expected266}267try {268Arrays.setAll(ar, null);269fail("Arrays.setAll(array, null) should throw NPE");270} catch (NullPointerException npe) {271// expected272}273try {274Arrays.parallelSetAll(ar, null);275fail("Arrays.parallelSetAll(array, null) should throw NPE");276} catch (NullPointerException npe) {277// expected278}279}280}281282283