1 /*
  2  * Copyright (c) 2014, 2026, 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 8037857
 27  * @summary tests for stream and spliterator factory methods
 28  * @run junit StreamAndSpliterator
 29  */
 30 
 31 
 32 import java.util.Arrays;
 33 import java.util.Spliterators;
 34 
 35 import static org.junit.jupiter.api.Assertions.assertThrows;
 36 import org.junit.jupiter.api.Test;
 37 import org.junit.jupiter.api.function.Executable;
 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));
 58         assertThrowsAIOOB(() -> Arrays.stream(new long[]{}, -1, 0));
 59         assertThrowsAIOOB(() -> Arrays.stream(new double[]{}, -1, 0));
 60         assertThrowsAIOOB(() -> Arrays.stream(new String[]{}, -1, 0));
 61 
 62         // bad fence
 63         assertThrowsAIOOB(() -> Arrays.stream(new int[]{}, 0, 1));
 64         assertThrowsAIOOB(() -> Arrays.stream(new long[]{}, 0, 1));
 65         assertThrowsAIOOB(() -> Arrays.stream(new double[]{}, 0, 1));
 66         assertThrowsAIOOB(() -> Arrays.stream(new String[]{}, 0, 1));
 67     }
 68 
 69 
 70     @Test
 71     public void testSpliteratorNPEs() {
 72         assertThrowsNPE(() -> Arrays.spliterator((int[]) null, 0, 0));
 73         assertThrowsNPE(() -> Arrays.spliterator((long[]) null, 0, 0));
 74         assertThrowsNPE(() -> Arrays.spliterator((double[]) null, 0, 0));
 75         assertThrowsNPE(() -> Arrays.spliterator((String[]) null, 0, 0));
 76     }
 77 
 78     @Test
 79     public void testSpliteratorAIOBEs() {
 80         // origin > fence
 81         assertThrowsAIOOB(() -> Arrays.spliterator(new int[]{}, 1, 0));
 82         assertThrowsAIOOB(() -> Arrays.spliterator(new long[]{}, 1, 0));
 83         assertThrowsAIOOB(() -> Arrays.spliterator(new double[]{}, 1, 0));
 84         assertThrowsAIOOB(() -> Arrays.spliterator(new String[]{}, 1, 0));
 85 
 86         // bad origin
 87         assertThrowsAIOOB(() -> Arrays.spliterator(new int[]{}, -1, 0));
 88         assertThrowsAIOOB(() -> Arrays.spliterator(new long[]{}, -1, 0));
 89         assertThrowsAIOOB(() -> Arrays.spliterator(new double[]{}, -1, 0));
 90         assertThrowsAIOOB(() -> Arrays.spliterator(new String[]{}, -1, 0));
 91 
 92         // bad fence
 93         assertThrowsAIOOB(() -> Arrays.spliterator(new int[]{}, 0, 1));
 94         assertThrowsAIOOB(() -> Arrays.spliterator(new long[]{}, 0, 1));
 95         assertThrowsAIOOB(() -> Arrays.spliterator(new double[]{}, 0, 1));
 96         assertThrowsAIOOB(() -> Arrays.spliterator(new String[]{}, 0, 1));
 97     }
 98 
 99 
100     @Test
101     public void testSpliteratorNPEsFromSpliterators() {
102         assertThrowsNPE(() -> Spliterators.spliterator((int[]) null, 0, 0, 0));
103         assertThrowsNPE(() -> Spliterators.spliterator((long[]) null, 0, 0, 0));
104         assertThrowsNPE(() -> Spliterators.spliterator((double[]) null, 0, 0, 0));
105         assertThrowsNPE(() -> Spliterators.spliterator((String[]) null, 0, 0, 0));
106     }
107 
108     @Test
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(Executable r) {
130         assertThrows(NullPointerException.class, r);
131     }
132 
133     void assertThrowsAIOOB(Executable r) {
134         assertThrows(ArrayIndexOutOfBoundsException.class, r);
135     }
136 }