diff a/test/jdk/java/util/Arrays/SetAllTest.java b/test/jdk/java/util/Arrays/SetAllTest.java --- a/test/jdk/java/util/Arrays/SetAllTest.java +++ b/test/jdk/java/util/Arrays/SetAllTest.java @@ -23,21 +23,24 @@ /* * @test * @bug 8012650 * @summary Unit test for setAll, parallelSetAll variants + * @library /test/lib * @run junit SetAllTest */ - import java.util.Arrays; import java.util.function.IntFunction; import java.util.function.IntToDoubleFunction; import java.util.function.IntToLongFunction; import java.util.function.IntUnaryOperator; +import jdk.test.lib.valueclass.AsValueClass; + import org.junit.jupiter.api.Assertions; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.fail; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.TestInstance; import org.junit.jupiter.params.ParameterizedTest; @@ -92,19 +95,39 @@ { "one", 1, toDouble, dr1 }, { "ten", 10, toDouble, dr10 }, { "fill", 3, fillDouble, new double[] { 3.14, 3.14, 3.14 }} }; + @AsValueClass + record Point(int x, int y) { } + + private static final IntFunction toPoint = i -> new Point(i, i * 2); + private static final IntFunction fillPoint = i -> new Point(0, 0); + private static final Point[] pr0 = {}; + private static final Point[] pr1 = { new Point(0, 0) }; + private static final Point[] pr10 = { new Point(0,0), new Point(1, 2), new Point(2, 4), + new Point(3, 6), new Point(4, 8), new Point(5, 10), new Point(6, 12), + new Point(7, 14), new Point(8, 16), new Point(9, 18) }; + + private Object[][] pointData = new Object[][] { + { "empty", 0, toPoint, pr0 }, + { "one", 1, toPoint, pr1 }, + { "ten", 10, toPoint, pr10 }, + { "fill", 3, fillPoint, new Point[] { new Point(0,0), new Point(0,0), new Point(0,0) }} + }; + public Object[][] stringTests() { return stringData; } public Object[][] intTests() { return intData; } public Object[][] longTests() { return longData; } public Object[][] doubleTests() { return doubleData; } - @ParameterizedTest + public Object[][] pointTests() { return pointData; } + + @ParameterizedTest(name = "{0}, size={1}, data={3}") @MethodSource("stringTests") public void testSetAllString(String name, int size, IntFunction generator, String[] expected) { String[] result = new String[size]; Arrays.setAll(result, generator); Assertions.assertArrayEquals(expected, result, "setAll(String[], IntFunction) case " + name + " failed."); @@ -113,11 +136,11 @@ result = new String[size]; Arrays.parallelSetAll(result, generator); Assertions.assertArrayEquals(expected, result, "parallelSetAll(String[], IntFunction) case " + name + " failed."); } - @ParameterizedTest + @ParameterizedTest(name = "{0}, size={1}, data={3}") @MethodSource("intTests") public void testSetAllInt(String name, int size, IntUnaryOperator generator, int[] expected) { int[] result = new int[size]; Arrays.setAll(result, generator); Assertions.assertArrayEquals(expected, result, "setAll(int[], IntUnaryOperator) case " + name + " failed."); @@ -126,11 +149,11 @@ result = new int[size]; Arrays.parallelSetAll(result, generator); Assertions.assertArrayEquals(expected, result, "parallelSetAll(int[], IntUnaryOperator) case " + name + " failed."); } - @ParameterizedTest + @ParameterizedTest(name = "{0}, size={1}, data={3}") @MethodSource("longTests") public void testSetAllLong(String name, int size, IntToLongFunction generator, long[] expected) { long[] result = new long[size]; Arrays.setAll(result, generator); Assertions.assertArrayEquals(expected, result, "setAll(long[], IntToLongFunction) case " + name + " failed."); @@ -149,11 +172,11 @@ for (int i = 0; i < actual.length; i++) { assertEquals(actual[i], expected[i], delta, msg + "(mismatch at index " + i + ")"); } } - @ParameterizedTest + @ParameterizedTest(name = "{0}, size={1}, data={3}") @MethodSource("doubleTests") public void testSetAllDouble(String name, int size, IntToDoubleFunction generator, double[] expected) { double[] result = new double[size]; Arrays.setAll(result, generator); assertDoubleArrayEquals(result, expected, 0.05, "setAll(double[], IntToDoubleFunction) case " + name + " failed."); @@ -162,10 +185,22 @@ result = new double[size]; Arrays.parallelSetAll(result, generator); assertDoubleArrayEquals(result, expected, 0.05, "setAll(double[], IntToDoubleFunction) case " + name + " failed."); } + @ParameterizedTest(name = "{0}, size={1}, data={3}") + @MethodSource("pointTests") + public void testSetAllPoint(String name, int size, IntFunction generator, Point[] expected) { + Point[] result = new Point[size]; + Arrays.setAll(result, generator); + Assertions.assertArrayEquals(expected, result, "setAll(Point[], IntFunction) case " + name + " failed."); + + result = new Point[size]; + Arrays.parallelSetAll(result, generator); + Assertions.assertArrayEquals(expected, result, "parallelSetAll(Point[], IntFunction) case " + name + " failed."); + } + @Test public void testStringSetNulls() { String[] ar = new String[2]; try { Arrays.setAll(null, (IntFunction) i -> "X"); @@ -277,6 +312,20 @@ fail("Arrays.parallelSetAll(array, null) should throw NPE"); } catch (NullPointerException npe) { // expected } } + + @Test + public void testPointSetNulls() { + Point[] ar = new Point[2]; + + assertThrows(NullPointerException.class, () -> Arrays.setAll( + (Point[]) null, (IntFunction) i -> new Point(i, i))); + assertThrows(NullPointerException.class, () -> Arrays.parallelSetAll( + (Point[]) null, (IntFunction) i -> new Point(i, i))); + assertThrows(NullPointerException.class, () -> Arrays.setAll( + ar, (IntFunction) null)); + assertThrows(NullPointerException.class, () -> Arrays.parallelSetAll( + ar, (IntFunction) null)); + } }