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
26  * @bug 8037857
27  * @summary tests for stream and spliterator factory methods
28  * @run testng StreamAndSpliterator
29  */
30 
31 package test.java.util.Arrays;
32 
33 import org.testng.annotations.Test;
34 
35 import java.util.Arrays;
36 import java.util.Spliterators;
37 
38 import org.testng.Assert.ThrowingRunnable;
39 
40 import static org.testng.Assert.assertThrows;
41 
42 public class StreamAndSpliterator {
43     @Test
testStreamNPEs()44     public void testStreamNPEs() {
45         assertThrowsNPE(() -> Arrays.stream((int[]) null, 0, 0));
46         assertThrowsNPE(() -> Arrays.stream((long[]) null, 0, 0));
47         assertThrowsNPE(() -> Arrays.stream((double[]) null, 0, 0));
48         assertThrowsNPE(() -> Arrays.stream((String[]) null, 0, 0));
49     }
50 
51     @Test
testStreamAIOBEs()52     public void testStreamAIOBEs() {
53         // origin > fence
54         assertThrowsAIOOB(() -> Arrays.stream(new int[]{}, 1, 0));
55         assertThrowsAIOOB(() -> Arrays.stream(new long[]{}, 1, 0));
56         assertThrowsAIOOB(() -> Arrays.stream(new double[]{}, 1, 0));
57         assertThrowsAIOOB(() -> Arrays.stream(new String[]{}, 1, 0));
58 
59         // bad origin
60         assertThrowsAIOOB(() -> Arrays.stream(new int[]{}, -1, 0));
61         assertThrowsAIOOB(() -> Arrays.stream(new long[]{}, -1, 0));
62         assertThrowsAIOOB(() -> Arrays.stream(new double[]{}, -1, 0));
63         assertThrowsAIOOB(() -> Arrays.stream(new String[]{}, -1, 0));
64 
65         // bad fence
66         assertThrowsAIOOB(() -> Arrays.stream(new int[]{}, 0, 1));
67         assertThrowsAIOOB(() -> Arrays.stream(new long[]{}, 0, 1));
68         assertThrowsAIOOB(() -> Arrays.stream(new double[]{}, 0, 1));
69         assertThrowsAIOOB(() -> Arrays.stream(new String[]{}, 0, 1));
70     }
71 
72 
73     @Test
testSpliteratorNPEs()74     public void testSpliteratorNPEs() {
75         assertThrowsNPE(() -> Arrays.spliterator((int[]) null, 0, 0));
76         assertThrowsNPE(() -> Arrays.spliterator((long[]) null, 0, 0));
77         assertThrowsNPE(() -> Arrays.spliterator((double[]) null, 0, 0));
78         assertThrowsNPE(() -> Arrays.spliterator((String[]) null, 0, 0));
79     }
80 
81     @Test
testSpliteratorAIOBEs()82     public void testSpliteratorAIOBEs() {
83         // origin > fence
84         assertThrowsAIOOB(() -> Arrays.spliterator(new int[]{}, 1, 0));
85         assertThrowsAIOOB(() -> Arrays.spliterator(new long[]{}, 1, 0));
86         assertThrowsAIOOB(() -> Arrays.spliterator(new double[]{}, 1, 0));
87         assertThrowsAIOOB(() -> Arrays.spliterator(new String[]{}, 1, 0));
88 
89         // bad origin
90         assertThrowsAIOOB(() -> Arrays.spliterator(new int[]{}, -1, 0));
91         assertThrowsAIOOB(() -> Arrays.spliterator(new long[]{}, -1, 0));
92         assertThrowsAIOOB(() -> Arrays.spliterator(new double[]{}, -1, 0));
93         assertThrowsAIOOB(() -> Arrays.spliterator(new String[]{}, -1, 0));
94 
95         // bad fence
96         assertThrowsAIOOB(() -> Arrays.spliterator(new int[]{}, 0, 1));
97         assertThrowsAIOOB(() -> Arrays.spliterator(new long[]{}, 0, 1));
98         assertThrowsAIOOB(() -> Arrays.spliterator(new double[]{}, 0, 1));
99         assertThrowsAIOOB(() -> Arrays.spliterator(new String[]{}, 0, 1));
100     }
101 
102 
103     @Test
testSpliteratorNPEsFromSpliterators()104     public void testSpliteratorNPEsFromSpliterators() {
105         assertThrowsNPE(() -> Spliterators.spliterator((int[]) null, 0, 0, 0));
106         assertThrowsNPE(() -> Spliterators.spliterator((long[]) null, 0, 0, 0));
107         assertThrowsNPE(() -> Spliterators.spliterator((double[]) null, 0, 0, 0));
108         assertThrowsNPE(() -> Spliterators.spliterator((String[]) null, 0, 0, 0));
109     }
110 
111     @Test
testSpliteratorAIOBEsFromSpliterators()112     public void testSpliteratorAIOBEsFromSpliterators() {
113         // origin > fence
114         assertThrowsAIOOB(() -> Spliterators.spliterator(new int[]{}, 1, 0, 0));
115         assertThrowsAIOOB(() -> Spliterators.spliterator(new long[]{}, 1, 0, 0));
116         assertThrowsAIOOB(() -> Spliterators.spliterator(new double[]{}, 1, 0, 0));
117         assertThrowsAIOOB(() -> Spliterators.spliterator(new String[]{}, 1, 0, 0));
118 
119         // bad origin
120         assertThrowsAIOOB(() -> Spliterators.spliterator(new int[]{}, -1, 0, 0));
121         assertThrowsAIOOB(() -> Spliterators.spliterator(new long[]{}, -1, 0, 0));
122         assertThrowsAIOOB(() -> Spliterators.spliterator(new double[]{}, -1, 0, 0));
123         assertThrowsAIOOB(() -> Spliterators.spliterator(new String[]{}, -1, 0, 0));
124 
125         // bad fence
126         assertThrowsAIOOB(() -> Spliterators.spliterator(new int[]{}, 0, 1, 0));
127         assertThrowsAIOOB(() -> Spliterators.spliterator(new long[]{}, 0, 1, 0));
128         assertThrowsAIOOB(() -> Spliterators.spliterator(new double[]{}, 0, 1, 0));
129         assertThrowsAIOOB(() -> Spliterators.spliterator(new String[]{}, 0, 1, 0));
130     }
131 
assertThrowsNPE(ThrowingRunnable r)132     void assertThrowsNPE(ThrowingRunnable r) {
133         assertThrows(NullPointerException.class, r);
134     }
135 
assertThrowsAIOOB(ThrowingRunnable r)136     void assertThrowsAIOOB(ThrowingRunnable r) {
137         assertThrows(ArrayIndexOutOfBoundsException.class, r);
138     }
139 }
140