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