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)); 56 assertThrowsAIOOB(() -> Arrays.stream(new long[]{}, -1, 0)); 57 assertThrowsAIOOB(() -> Arrays.stream(new double[]{}, -1, 0)); 58 assertThrowsAIOOB(() -> Arrays.stream(new String[]{}, -1, 0)); 59 60 // bad fence 61 assertThrowsAIOOB(() -> Arrays.stream(new int[]{}, 0, 1)); 62 assertThrowsAIOOB(() -> Arrays.stream(new long[]{}, 0, 1)); 63 assertThrowsAIOOB(() -> Arrays.stream(new double[]{}, 0, 1)); 64 assertThrowsAIOOB(() -> Arrays.stream(new String[]{}, 0, 1)); 65 } 66 67 68 @Test 69 public void testSpliteratorNPEs() { 70 assertThrowsNPE(() -> Arrays.spliterator((int[]) null, 0, 0)); 71 assertThrowsNPE(() -> Arrays.spliterator((long[]) null, 0, 0)); 72 assertThrowsNPE(() -> Arrays.spliterator((double[]) null, 0, 0)); 73 assertThrowsNPE(() -> Arrays.spliterator((String[]) null, 0, 0)); 74 } 75 76 @Test 77 public void testSpliteratorAIOBEs() { 78 // origin > fence 79 assertThrowsAIOOB(() -> Arrays.spliterator(new int[]{}, 1, 0)); 80 assertThrowsAIOOB(() -> Arrays.spliterator(new long[]{}, 1, 0)); 81 assertThrowsAIOOB(() -> Arrays.spliterator(new double[]{}, 1, 0)); 82 assertThrowsAIOOB(() -> Arrays.spliterator(new String[]{}, 1, 0)); 83 84 // bad origin 85 assertThrowsAIOOB(() -> Arrays.spliterator(new int[]{}, -1, 0)); 86 assertThrowsAIOOB(() -> Arrays.spliterator(new long[]{}, -1, 0)); 87 assertThrowsAIOOB(() -> Arrays.spliterator(new double[]{}, -1, 0)); 88 assertThrowsAIOOB(() -> Arrays.spliterator(new String[]{}, -1, 0)); 89 90 // bad fence 91 assertThrowsAIOOB(() -> Arrays.spliterator(new int[]{}, 0, 1)); 92 assertThrowsAIOOB(() -> Arrays.spliterator(new long[]{}, 0, 1)); 93 assertThrowsAIOOB(() -> Arrays.spliterator(new double[]{}, 0, 1)); 94 assertThrowsAIOOB(() -> Arrays.spliterator(new String[]{}, 0, 1)); 95 } 96 97 98 @Test 99 public void testSpliteratorNPEsFromSpliterators() { 100 assertThrowsNPE(() -> Spliterators.spliterator((int[]) null, 0, 0, 0)); 101 assertThrowsNPE(() -> Spliterators.spliterator((long[]) null, 0, 0, 0)); 102 assertThrowsNPE(() -> Spliterators.spliterator((double[]) null, 0, 0, 0)); 103 assertThrowsNPE(() -> Spliterators.spliterator((String[]) null, 0, 0, 0)); 104 } 105 106 @Test 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 }