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