1 /*
  2  * Copyright (c) 2013, 2026, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  */
 23 
 24 /*
 25  * @test
 26  * @bug 8012650
 27  * @summary Unit test for setAll, parallelSetAll variants
 28  * @library /test/lib
 29  * @run junit SetAllTest
 30  */
 31 
 32 import java.util.Arrays;
 33 import java.util.function.IntFunction;
 34 import java.util.function.IntToDoubleFunction;
 35 import java.util.function.IntToLongFunction;
 36 import java.util.function.IntUnaryOperator;
 37 
 38 import jdk.test.lib.valueclass.AsValueClass;
 39 
 40 import org.junit.jupiter.api.Assertions;
 41 import static org.junit.jupiter.api.Assertions.assertThrows;
 42 import static org.junit.jupiter.api.Assertions.assertEquals;
 43 import static org.junit.jupiter.api.Assertions.fail;
 44 import org.junit.jupiter.api.Test;
 45 import org.junit.jupiter.api.TestInstance;
 46 import org.junit.jupiter.params.ParameterizedTest;
 47 import org.junit.jupiter.params.provider.MethodSource;
 48 
 49 @TestInstance(TestInstance.Lifecycle.PER_CLASS)
 50 public class SetAllTest {
 51     private static final IntFunction<String> toString = i -> "N" + Integer.valueOf(i);
 52     private static final IntFunction<String> fillString = i -> "X";
 53     private static final String[] r0 = {};
 54     private static final String[] r1 = { "N0" };
 55     private static final String[] r10 = { "N0", "N1", "N2", "N3", "N4", "N5", "N6", "N7", "N8", "N9" };
 56 
 57     private Object[][] stringData = new Object[][] {
 58         { "empty", 0, toString, r0 },
 59         { "one", 1, toString, r1 },
 60         { "ten", 10, toString, r10 },
 61         { "fill", 3, fillString, new String[] { "X", "X", "X" }}
 62     };
 63 
 64     private static final IntUnaryOperator toInt = i -> i << 1;
 65     private static final IntUnaryOperator fillInt = i -> 99;
 66     private static final int[] ir0 = {};
 67     private static final int[] ir1 = { 0 };
 68     private static final int[] ir10 = { 0, 2, 4, 6, 8, 10, 12, 14, 16, 18 };
 69     private Object[][] intData = new Object[][] {
 70         { "empty", 0, toInt, ir0 },
 71         { "one", 1, toInt, ir1 },
 72         { "ten", 10, toInt, ir10 },
 73         { "fill", 3, fillInt, new int[] { 99, 99, 99 }}
 74     };
 75 
 76     private static final IntToLongFunction toLong = i -> i << 1;
 77     private static final IntToLongFunction fillLong = i -> 9999L;
 78     private static final long[] lr0 = {};
 79     private static final long[] lr1 = { 0L };
 80     private static final long[] lr10 = { 0L, 2L, 4L, 6L, 8L, 10L, 12L, 14L, 16L, 18L };
 81     private Object[][] longData = new Object[][] {
 82         { "empty", 0, toLong, lr0 },
 83         { "one", 1, toLong, lr1 },
 84         { "ten", 10, toLong, lr10 },
 85         { "fill", 3, fillLong, new long[] { 9999L, 9999L, 9999L }}
 86     };
 87 
 88     private static final IntToDoubleFunction toDouble = i -> i * 1.1;
 89     private static final IntToDoubleFunction fillDouble = i -> 3.14;
 90     private static final double[] dr0 = {};
 91     private static final double[] dr1 = { 0.0 };
 92     private static final double[] dr10 = { 0.0, 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9 };
 93     private Object[][] doubleData = new Object[][] {
 94         { "empty", 0, toDouble, dr0 },
 95         { "one", 1, toDouble, dr1 },
 96         { "ten", 10, toDouble, dr10 },
 97         { "fill", 3, fillDouble, new double[] { 3.14, 3.14, 3.14 }}
 98     };
 99 
100     @AsValueClass
101     record Point(int x, int y) { }
102 
103     private static final IntFunction<Point> toPoint = i -> new Point(i, i * 2);
104     private static final IntFunction<Point> fillPoint = i -> new Point(0, 0);
105     private static final Point[] pr0  = {};
106     private static final Point[] pr1  = { new Point(0, 0) };
107     private static final Point[] pr10 = { new Point(0,0), new Point(1, 2), new Point(2, 4),
108             new Point(3, 6), new Point(4, 8), new Point(5, 10), new Point(6, 12),
109             new Point(7, 14), new Point(8, 16), new Point(9, 18) };
110 
111     private Object[][] pointData = new Object[][] {
112             { "empty", 0, toPoint, pr0 },
113             { "one",   1, toPoint, pr1 },
114             { "ten",  10, toPoint, pr10 },
115             { "fill",  3, fillPoint, new Point[] { new Point(0,0), new Point(0,0), new Point(0,0) }}
116     };
117 
118     public Object[][] stringTests() { return stringData; }
119 
120     public Object[][] intTests() { return intData; }
121 
122     public Object[][] longTests() { return longData; }
123 
124     public Object[][] doubleTests() { return doubleData; }
125 
126     public Object[][] pointTests() { return pointData; }
127 
128     @ParameterizedTest(name = "{0}, size={1}, data={3}")
129     @MethodSource("stringTests")
130     public void testSetAllString(String name, int size, IntFunction<String> generator, String[] expected) {
131         String[] result = new String[size];
132         Arrays.setAll(result, generator);
133         Assertions.assertArrayEquals(expected, result, "setAll(String[], IntFunction<String>) case " + name + " failed.");
134 
135         // ensure fresh array
136         result = new String[size];
137         Arrays.parallelSetAll(result, generator);
138         Assertions.assertArrayEquals(expected, result, "parallelSetAll(String[], IntFunction<String>) case " + name + " failed.");
139     }
140 
141     @ParameterizedTest(name = "{0}, size={1}, data={3}")
142     @MethodSource("intTests")
143     public void testSetAllInt(String name, int size, IntUnaryOperator generator, int[] expected) {
144         int[] result = new int[size];
145         Arrays.setAll(result, generator);
146         Assertions.assertArrayEquals(expected, result, "setAll(int[], IntUnaryOperator) case " + name + " failed.");
147 
148         // ensure fresh array
149         result = new int[size];
150         Arrays.parallelSetAll(result, generator);
151         Assertions.assertArrayEquals(expected, result, "parallelSetAll(int[], IntUnaryOperator) case " + name + " failed.");
152     }
153 
154     @ParameterizedTest(name = "{0}, size={1}, data={3}")
155     @MethodSource("longTests")
156     public void testSetAllLong(String name, int size, IntToLongFunction generator, long[] expected) {
157         long[] result = new long[size];
158         Arrays.setAll(result, generator);
159         Assertions.assertArrayEquals(expected, result, "setAll(long[], IntToLongFunction) case " + name + " failed.");
160 
161         // ensure fresh array
162         result = new long[size];
163         Arrays.parallelSetAll(result, generator);
164         Assertions.assertArrayEquals(expected, result, "parallelSetAll(long[], IntToLongFunction) case " + name + " failed.");
165     }
166 
167     private void assertDoubleArrayEquals(double[] actual, double[] expected, double delta, String msg) {
168         if (actual.length != expected.length) {
169             fail(msg + ": length mismatch, expected " + expected.length + ", got " + actual.length);
170         }
171 
172         for (int i = 0; i < actual.length; i++) {
173             assertEquals(actual[i], expected[i], delta, msg + "(mismatch at index " + i + ")");
174         }
175     }
176 
177     @ParameterizedTest(name = "{0}, size={1}, data={3}")
178     @MethodSource("doubleTests")
179     public void testSetAllDouble(String name, int size, IntToDoubleFunction generator, double[] expected) {
180         double[] result = new double[size];
181         Arrays.setAll(result, generator);
182         assertDoubleArrayEquals(result, expected, 0.05, "setAll(double[], IntToDoubleFunction) case " + name + " failed.");
183 
184         // ensure fresh array
185         result = new double[size];
186         Arrays.parallelSetAll(result, generator);
187         assertDoubleArrayEquals(result, expected, 0.05, "setAll(double[], IntToDoubleFunction) case " + name + " failed.");
188     }
189 
190     @ParameterizedTest(name = "{0}, size={1}, data={3}")
191     @MethodSource("pointTests")
192     public void testSetAllPoint(String name, int size, IntFunction<Point> generator, Point[] expected) {
193         Point[] result = new Point[size];
194         Arrays.setAll(result, generator);
195         Assertions.assertArrayEquals(expected, result, "setAll(Point[], IntFunction<Point>) case " + name + " failed.");
196 
197         result = new Point[size];
198         Arrays.parallelSetAll(result, generator);
199         Assertions.assertArrayEquals(expected, result, "parallelSetAll(Point[], IntFunction<Point>) case " + name + " failed.");
200     }
201 
202     @Test
203     public void testStringSetNulls() {
204         String[] ar = new String[2];
205         try {
206             Arrays.setAll(null, (IntFunction<String>) i -> "X");
207             fail("Arrays.setAll(null, foo) should throw NPE");
208         } catch (NullPointerException npe) {
209             // expected
210         }
211         try {
212             Arrays.parallelSetAll(null, (IntFunction<String>) i -> "X");
213             fail("Arrays.parallelSetAll(null, foo) should throw NPE");
214         } catch (NullPointerException npe) {
215             // expected
216         }
217         try {
218             Arrays.setAll(ar, null);
219             fail("Arrays.setAll(array, null) should throw NPE");
220         } catch (NullPointerException npe) {
221             // expected
222         }
223         try {
224             Arrays.parallelSetAll(ar, null);
225             fail("Arrays.parallelSetAll(array, null) should throw NPE");
226         } catch (NullPointerException npe) {
227             // expected
228         }
229     }
230 
231     @Test
232     public void testIntSetNulls() {
233         int[] ar = new int[2];
234         try {
235             Arrays.setAll(null, (IntUnaryOperator) i -> i);
236             fail("Arrays.setAll(null, foo) should throw NPE");
237         } catch (NullPointerException npe) {
238             // expected
239         }
240         try {
241             Arrays.parallelSetAll(null, (IntUnaryOperator) i -> i);
242             fail("Arrays.parallelSetAll(null, foo) should throw NPE");
243         } catch (NullPointerException npe) {
244             // expected
245         }
246         try {
247             Arrays.setAll(ar, null);
248             fail("Arrays.setAll(array, null) should throw NPE");
249         } catch (NullPointerException npe) {
250             // expected
251         }
252         try {
253             Arrays.parallelSetAll(ar, null);
254             fail("Arrays.parallelSetAll(array, null) should throw NPE");
255         } catch (NullPointerException npe) {
256             // expected
257         }
258     }
259 
260     @Test
261     public void testLongSetNulls() {
262         long[] ar = new long[2];
263         try {
264             Arrays.setAll(null, (IntToLongFunction) i -> Long.MAX_VALUE);
265             fail("Arrays.setAll(null, foo) should throw NPE");
266         } catch (NullPointerException npe) {
267             // expected
268         }
269         try {
270             Arrays.parallelSetAll(null, (IntToLongFunction) i -> Long.MAX_VALUE);
271             fail("Arrays.parallelSetAll(null, foo) should throw NPE");
272         } catch (NullPointerException npe) {
273             // expected
274         }
275         try {
276             Arrays.setAll(ar, null);
277             fail("Arrays.setAll(array, null) should throw NPE");
278         } catch (NullPointerException npe) {
279             // expected
280         }
281         try {
282             Arrays.parallelSetAll(ar, null);
283             fail("Arrays.parallelSetAll(array, null) should throw NPE");
284         } catch (NullPointerException npe) {
285             // expected
286         }
287     }
288 
289     @Test
290     public void testDoubleSetNulls() {
291         double[] ar = new double[2];
292         try {
293             Arrays.setAll(null, (IntToDoubleFunction) i -> Math.E);
294             fail("Arrays.setAll(null, foo) should throw NPE");
295         } catch (NullPointerException npe) {
296             // expected
297         }
298         try {
299             Arrays.parallelSetAll(null, (IntToDoubleFunction) i -> Math.E);
300             fail("Arrays.parallelSetAll(null, foo) should throw NPE");
301         } catch (NullPointerException npe) {
302             // expected
303         }
304         try {
305             Arrays.setAll(ar, null);
306             fail("Arrays.setAll(array, null) should throw NPE");
307         } catch (NullPointerException npe) {
308             // expected
309         }
310         try {
311             Arrays.parallelSetAll(ar, null);
312             fail("Arrays.parallelSetAll(array, null) should throw NPE");
313         } catch (NullPointerException npe) {
314             // expected
315         }
316     }
317 
318     @Test
319     public void testPointSetNulls() {
320         Point[] ar = new Point[2];
321 
322         assertThrows(NullPointerException.class, () -> Arrays.setAll(
323             (Point[]) null, (IntFunction<Point>) i -> new Point(i, i)));
324         assertThrows(NullPointerException.class, () -> Arrays.parallelSetAll(
325             (Point[]) null, (IntFunction<Point>) i -> new Point(i, i)));
326         assertThrows(NullPointerException.class, () -> Arrays.setAll(
327             ar, (IntFunction<Point>) null));
328         assertThrows(NullPointerException.class, () -> Arrays.parallelSetAll(
329             ar, (IntFunction<Point>) null));
330     }
331 }