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  * @run junit SetAllTest
 29  */
 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 org.junit.jupiter.api.Assertions;
 39 import static org.junit.jupiter.api.Assertions.assertEquals;
 40 import static org.junit.jupiter.api.Assertions.fail;
 41 import org.junit.jupiter.api.Test;
 42 import org.junit.jupiter.api.TestInstance;
 43 import org.junit.jupiter.params.ParameterizedTest;
 44 import org.junit.jupiter.params.provider.MethodSource;
 45 
 46 @TestInstance(TestInstance.Lifecycle.PER_CLASS)
 47 public class SetAllTest {
 48     private static final IntFunction<String> toString = i -> "N" + Integer.valueOf(i);
 49     private static final IntFunction<String> fillString = i -> "X";
 50     private static final String[] r0 = {};
 51     private static final String[] r1 = { "N0" };
 52     private static final String[] r10 = { "N0", "N1", "N2", "N3", "N4", "N5", "N6", "N7", "N8", "N9" };
 53 
 54     private Object[][] stringData = new Object[][] {
 55         { "empty", 0, toString, r0 },
 56         { "one", 1, toString, r1 },
 57         { "ten", 10, toString, r10 },
 58         { "fill", 3, fillString, new String[] { "X", "X", "X" }}
 59     };
 60 
 61     private static final IntUnaryOperator toInt = i -> i << 1;
 62     private static final IntUnaryOperator fillInt = i -> 99;
 63     private static final int[] ir0 = {};
 64     private static final int[] ir1 = { 0 };
 65     private static final int[] ir10 = { 0, 2, 4, 6, 8, 10, 12, 14, 16, 18 };
 66     private Object[][] intData = new Object[][] {
 67         { "empty", 0, toInt, ir0 },
 68         { "one", 1, toInt, ir1 },
 69         { "ten", 10, toInt, ir10 },
 70         { "fill", 3, fillInt, new int[] { 99, 99, 99 }}
 71     };
 72 
 73     private static final IntToLongFunction toLong = i -> i << 1;
 74     private static final IntToLongFunction fillLong = i -> 9999L;
 75     private static final long[] lr0 = {};
 76     private static final long[] lr1 = { 0L };
 77     private static final long[] lr10 = { 0L, 2L, 4L, 6L, 8L, 10L, 12L, 14L, 16L, 18L };
 78     private Object[][] longData = new Object[][] {
 79         { "empty", 0, toLong, lr0 },
 80         { "one", 1, toLong, lr1 },
 81         { "ten", 10, toLong, lr10 },
 82         { "fill", 3, fillLong, new long[] { 9999L, 9999L, 9999L }}
 83     };
 84 
 85     private static final IntToDoubleFunction toDouble = i -> i * 1.1;
 86     private static final IntToDoubleFunction fillDouble = i -> 3.14;
 87     private static final double[] dr0 = {};
 88     private static final double[] dr1 = { 0.0 };
 89     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 };
 90     private Object[][] doubleData = new Object[][] {
 91         { "empty", 0, toDouble, dr0 },
 92         { "one", 1, toDouble, dr1 },
 93         { "ten", 10, toDouble, dr10 },
 94         { "fill", 3, fillDouble, new double[] { 3.14, 3.14, 3.14 }}
 95     };
 96 
 97     public Object[][] stringTests() { return stringData; }
 98 
 99     public Object[][] intTests() { return intData; }
100 
101     public Object[][] longTests() { return longData; }
102 
103     public Object[][] doubleTests() { return doubleData; }
104 
105     @ParameterizedTest
106     @MethodSource("stringTests")
107     public void testSetAllString(String name, int size, IntFunction<String> generator, String[] expected) {
108         String[] result = new String[size];
109         Arrays.setAll(result, generator);
110         Assertions.assertArrayEquals(expected, result, "setAll(String[], IntFunction<String>) case " + name + " failed.");
111 
112         // ensure fresh array
113         result = new String[size];
114         Arrays.parallelSetAll(result, generator);
115         Assertions.assertArrayEquals(expected, result, "parallelSetAll(String[], IntFunction<String>) case " + name + " failed.");
116     }
117 
118     @ParameterizedTest
119     @MethodSource("intTests")
120     public void testSetAllInt(String name, int size, IntUnaryOperator generator, int[] expected) {
121         int[] result = new int[size];
122         Arrays.setAll(result, generator);
123         Assertions.assertArrayEquals(expected, result, "setAll(int[], IntUnaryOperator) case " + name + " failed.");
124 
125         // ensure fresh array
126         result = new int[size];
127         Arrays.parallelSetAll(result, generator);
128         Assertions.assertArrayEquals(expected, result, "parallelSetAll(int[], IntUnaryOperator) case " + name + " failed.");
129     }
130 
131     @ParameterizedTest
132     @MethodSource("longTests")
133     public void testSetAllLong(String name, int size, IntToLongFunction generator, long[] expected) {
134         long[] result = new long[size];
135         Arrays.setAll(result, generator);
136         Assertions.assertArrayEquals(expected, result, "setAll(long[], IntToLongFunction) case " + name + " failed.");
137 
138         // ensure fresh array
139         result = new long[size];
140         Arrays.parallelSetAll(result, generator);
141         Assertions.assertArrayEquals(expected, result, "parallelSetAll(long[], IntToLongFunction) case " + name + " failed.");
142     }
143 
144     private void assertDoubleArrayEquals(double[] actual, double[] expected, double delta, String msg) {
145         if (actual.length != expected.length) {
146             fail(msg + ": length mismatch, expected " + expected.length + ", got " + actual.length);
147         }
148 
149         for (int i = 0; i < actual.length; i++) {
150             assertEquals(actual[i], expected[i], delta, msg + "(mismatch at index " + i + ")");
151         }
152     }
153 
154     @ParameterizedTest
155     @MethodSource("doubleTests")
156     public void testSetAllDouble(String name, int size, IntToDoubleFunction generator, double[] expected) {
157         double[] result = new double[size];
158         Arrays.setAll(result, generator);
159         assertDoubleArrayEquals(result, expected, 0.05, "setAll(double[], IntToDoubleFunction) case " + name + " failed.");
160 
161         // ensure fresh array
162         result = new double[size];
163         Arrays.parallelSetAll(result, generator);
164         assertDoubleArrayEquals(result, expected, 0.05, "setAll(double[], IntToDoubleFunction) case " + name + " failed.");
165     }
166 
167     @Test
168     public void testStringSetNulls() {
169         String[] ar = new String[2];
170         try {
171             Arrays.setAll(null, (IntFunction<String>) i -> "X");
172             fail("Arrays.setAll(null, foo) should throw NPE");
173         } catch (NullPointerException npe) {
174             // expected
175         }
176         try {
177             Arrays.parallelSetAll(null, (IntFunction<String>) i -> "X");
178             fail("Arrays.parallelSetAll(null, foo) should throw NPE");
179         } catch (NullPointerException npe) {
180             // expected
181         }
182         try {
183             Arrays.setAll(ar, null);
184             fail("Arrays.setAll(array, null) should throw NPE");
185         } catch (NullPointerException npe) {
186             // expected
187         }
188         try {
189             Arrays.parallelSetAll(ar, null);
190             fail("Arrays.parallelSetAll(array, null) should throw NPE");
191         } catch (NullPointerException npe) {
192             // expected
193         }
194     }
195 
196     @Test
197     public void testIntSetNulls() {
198         int[] ar = new int[2];
199         try {
200             Arrays.setAll(null, (IntUnaryOperator) i -> i);
201             fail("Arrays.setAll(null, foo) should throw NPE");
202         } catch (NullPointerException npe) {
203             // expected
204         }
205         try {
206             Arrays.parallelSetAll(null, (IntUnaryOperator) i -> i);
207             fail("Arrays.parallelSetAll(null, foo) should throw NPE");
208         } catch (NullPointerException npe) {
209             // expected
210         }
211         try {
212             Arrays.setAll(ar, null);
213             fail("Arrays.setAll(array, null) should throw NPE");
214         } catch (NullPointerException npe) {
215             // expected
216         }
217         try {
218             Arrays.parallelSetAll(ar, null);
219             fail("Arrays.parallelSetAll(array, null) should throw NPE");
220         } catch (NullPointerException npe) {
221             // expected
222         }
223     }
224 
225     @Test
226     public void testLongSetNulls() {
227         long[] ar = new long[2];
228         try {
229             Arrays.setAll(null, (IntToLongFunction) i -> Long.MAX_VALUE);
230             fail("Arrays.setAll(null, foo) should throw NPE");
231         } catch (NullPointerException npe) {
232             // expected
233         }
234         try {
235             Arrays.parallelSetAll(null, (IntToLongFunction) i -> Long.MAX_VALUE);
236             fail("Arrays.parallelSetAll(null, foo) should throw NPE");
237         } catch (NullPointerException npe) {
238             // expected
239         }
240         try {
241             Arrays.setAll(ar, null);
242             fail("Arrays.setAll(array, null) should throw NPE");
243         } catch (NullPointerException npe) {
244             // expected
245         }
246         try {
247             Arrays.parallelSetAll(ar, null);
248             fail("Arrays.parallelSetAll(array, null) should throw NPE");
249         } catch (NullPointerException npe) {
250             // expected
251         }
252     }
253 
254     @Test
255     public void testDoubleSetNulls() {
256         double[] ar = new double[2];
257         try {
258             Arrays.setAll(null, (IntToDoubleFunction) i -> Math.E);
259             fail("Arrays.setAll(null, foo) should throw NPE");
260         } catch (NullPointerException npe) {
261             // expected
262         }
263         try {
264             Arrays.parallelSetAll(null, (IntToDoubleFunction) i -> Math.E);
265             fail("Arrays.parallelSetAll(null, foo) should throw NPE");
266         } catch (NullPointerException npe) {
267             // expected
268         }
269         try {
270             Arrays.setAll(ar, null);
271             fail("Arrays.setAll(array, null) should throw NPE");
272         } catch (NullPointerException npe) {
273             // expected
274         }
275         try {
276             Arrays.parallelSetAll(ar, null);
277             fail("Arrays.parallelSetAll(array, null) should throw NPE");
278         } catch (NullPointerException npe) {
279             // expected
280         }
281     }
282 }