< prev index next >

test/jdk/java/util/Arrays/SetAllTest.java

Print this page

  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" }}

 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

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 }

  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" }}

 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

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 }
< prev index next >