1 /* 2 * Copyright (c) 2014, 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 8037857 26 * @summary tests for stream and spliterator factory methods 27 * @run testng StreamAndSpliterator 28 */ 29 30 import org.testng.annotations.Test; 31 32 import java.util.Arrays; 33 import java.util.Spliterators; 34 35 import static org.testng.Assert.assertNotNull; 36 37 public class StreamAndSpliterator { 38 @Test 39 public void testStreamNPEs() { 40 assertThrowsNPE(() -> Arrays.stream((int[]) null, 0, 0)); 41 assertThrowsNPE(() -> Arrays.stream((long[]) null, 0, 0)); 42 assertThrowsNPE(() -> Arrays.stream((double[]) null, 0, 0)); 43 assertThrowsNPE(() -> Arrays.stream((String[]) null, 0, 0)); 44 } 45 46 @Test 47 public void testStreamAIOBEs() { 48 // origin > fence 49 assertThrowsAIOOB(() -> Arrays.stream(new int[]{}, 1, 0)); 50 assertThrowsAIOOB(() -> Arrays.stream(new long[]{}, 1, 0)); 51 assertThrowsAIOOB(() -> Arrays.stream(new double[]{}, 1, 0)); 52 assertThrowsAIOOB(() -> Arrays.stream(new String[]{}, 1, 0)); 53 54 // bad origin 55 assertThrowsAIOOB(() -> Arrays.stream(new int[]{}, -1, 0)); 107 public void testSpliteratorAIOBEsFromSpliterators() { 108 // origin > fence 109 assertThrowsAIOOB(() -> Spliterators.spliterator(new int[]{}, 1, 0, 0)); 110 assertThrowsAIOOB(() -> Spliterators.spliterator(new long[]{}, 1, 0, 0)); 111 assertThrowsAIOOB(() -> Spliterators.spliterator(new double[]{}, 1, 0, 0)); 112 assertThrowsAIOOB(() -> Spliterators.spliterator(new String[]{}, 1, 0, 0)); 113 114 // bad origin 115 assertThrowsAIOOB(() -> Spliterators.spliterator(new int[]{}, -1, 0, 0)); 116 assertThrowsAIOOB(() -> Spliterators.spliterator(new long[]{}, -1, 0, 0)); 117 assertThrowsAIOOB(() -> Spliterators.spliterator(new double[]{}, -1, 0, 0)); 118 assertThrowsAIOOB(() -> Spliterators.spliterator(new String[]{}, -1, 0, 0)); 119 120 // bad fence 121 assertThrowsAIOOB(() -> Spliterators.spliterator(new int[]{}, 0, 1, 0)); 122 assertThrowsAIOOB(() -> Spliterators.spliterator(new long[]{}, 0, 1, 0)); 123 assertThrowsAIOOB(() -> Spliterators.spliterator(new double[]{}, 0, 1, 0)); 124 assertThrowsAIOOB(() -> Spliterators.spliterator(new String[]{}, 0, 1, 0)); 125 } 126 127 void assertThrowsNPE(Runnable r) { 128 NullPointerException caught = null; 129 try { 130 r.run(); 131 } 132 catch (NullPointerException e) { 133 caught = e; 134 } 135 assertNotNull(caught, "NullPointerException not thrown"); 136 } 137 138 void assertThrowsAIOOB(Runnable r) { 139 ArrayIndexOutOfBoundsException caught = null; 140 try { 141 r.run(); 142 } 143 catch (ArrayIndexOutOfBoundsException e) { 144 caught = e; 145 } 146 assertNotNull(caught, "ArrayIndexOutOfBoundsException not thrown"); 147 } 148 } | 1 /* 2 * Copyright (c) 2014, 2017, 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 8037857 26 * @summary tests for stream and spliterator factory methods 27 * @run testng StreamAndSpliterator 28 */ 29 30 import org.testng.annotations.Test; 31 32 import java.util.Arrays; 33 import java.util.Spliterators; 34 35 import org.testng.Assert.ThrowingRunnable; 36 37 import static org.testng.Assert.assertThrows; 38 39 public class StreamAndSpliterator { 40 @Test 41 public void testStreamNPEs() { 42 assertThrowsNPE(() -> Arrays.stream((int[]) null, 0, 0)); 43 assertThrowsNPE(() -> Arrays.stream((long[]) null, 0, 0)); 44 assertThrowsNPE(() -> Arrays.stream((double[]) null, 0, 0)); 45 assertThrowsNPE(() -> Arrays.stream((String[]) null, 0, 0)); 46 } 47 48 @Test 49 public void testStreamAIOBEs() { 50 // origin > fence 51 assertThrowsAIOOB(() -> Arrays.stream(new int[]{}, 1, 0)); 52 assertThrowsAIOOB(() -> Arrays.stream(new long[]{}, 1, 0)); 53 assertThrowsAIOOB(() -> Arrays.stream(new double[]{}, 1, 0)); 54 assertThrowsAIOOB(() -> Arrays.stream(new String[]{}, 1, 0)); 55 56 // bad origin 57 assertThrowsAIOOB(() -> Arrays.stream(new int[]{}, -1, 0)); 109 public void testSpliteratorAIOBEsFromSpliterators() { 110 // origin > fence 111 assertThrowsAIOOB(() -> Spliterators.spliterator(new int[]{}, 1, 0, 0)); 112 assertThrowsAIOOB(() -> Spliterators.spliterator(new long[]{}, 1, 0, 0)); 113 assertThrowsAIOOB(() -> Spliterators.spliterator(new double[]{}, 1, 0, 0)); 114 assertThrowsAIOOB(() -> Spliterators.spliterator(new String[]{}, 1, 0, 0)); 115 116 // bad origin 117 assertThrowsAIOOB(() -> Spliterators.spliterator(new int[]{}, -1, 0, 0)); 118 assertThrowsAIOOB(() -> Spliterators.spliterator(new long[]{}, -1, 0, 0)); 119 assertThrowsAIOOB(() -> Spliterators.spliterator(new double[]{}, -1, 0, 0)); 120 assertThrowsAIOOB(() -> Spliterators.spliterator(new String[]{}, -1, 0, 0)); 121 122 // bad fence 123 assertThrowsAIOOB(() -> Spliterators.spliterator(new int[]{}, 0, 1, 0)); 124 assertThrowsAIOOB(() -> Spliterators.spliterator(new long[]{}, 0, 1, 0)); 125 assertThrowsAIOOB(() -> Spliterators.spliterator(new double[]{}, 0, 1, 0)); 126 assertThrowsAIOOB(() -> Spliterators.spliterator(new String[]{}, 0, 1, 0)); 127 } 128 129 void assertThrowsNPE(ThrowingRunnable r) { 130 assertThrows(NullPointerException.class, r); 131 } 132 133 void assertThrowsAIOOB(ThrowingRunnable r) { 134 assertThrows(ArrayIndexOutOfBoundsException.class, r); 135 } 136 } |