1 /*
2  * Copyright (c) 2012, 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 package org.openjdk.tests.java.util.stream;
24 
25 import org.openjdk.testlib.java.util.stream.DoubleStreamTestDataProvider;
26 import org.openjdk.testlib.java.util.stream.IntStreamTestDataProvider;
27 import org.openjdk.testlib.java.util.stream.LambdaTestHelpers;
28 import org.openjdk.testlib.java.util.stream.LongStreamTestDataProvider;
29 import org.openjdk.testlib.java.util.stream.OpTestCase;
30 import org.openjdk.testlib.java.util.stream.StreamTestDataProvider;
31 import org.openjdk.testlib.java.util.stream.TestData;
32 
33 import java.util.Arrays;
34 import java.util.HashMap;
35 import java.util.Iterator;
36 import java.util.List;
37 import java.util.Map;
38 import java.util.PrimitiveIterator;
39 import java.util.Spliterators;
40 import java.util.function.DoublePredicate;
41 import java.util.function.Function;
42 import java.util.function.IntPredicate;
43 import java.util.function.LongPredicate;
44 import java.util.function.Predicate;
45 import java.util.function.Supplier;
46 import java.util.stream.DoubleStream;
47 import java.util.stream.IntStream;
48 import java.util.stream.LongStream;
49 import java.util.stream.Stream;
50 import java.util.stream.StreamSupport;
51 
52 import org.testng.annotations.Test;
53 
54 import static org.openjdk.testlib.java.util.stream.LambdaTestHelpers.countTo;
55 import static org.openjdk.testlib.java.util.stream.LambdaTestHelpers.dpEven;
56 import static org.openjdk.testlib.java.util.stream.LambdaTestHelpers.dpFalse;
57 import static org.openjdk.testlib.java.util.stream.LambdaTestHelpers.dpOdd;
58 import static org.openjdk.testlib.java.util.stream.LambdaTestHelpers.dpTrue;
59 import static org.openjdk.testlib.java.util.stream.LambdaTestHelpers.ipEven;
60 import static org.openjdk.testlib.java.util.stream.LambdaTestHelpers.ipFalse;
61 import static org.openjdk.testlib.java.util.stream.LambdaTestHelpers.ipOdd;
62 import static org.openjdk.testlib.java.util.stream.LambdaTestHelpers.ipTrue;
63 import static org.openjdk.testlib.java.util.stream.LambdaTestHelpers.lpEven;
64 import static org.openjdk.testlib.java.util.stream.LambdaTestHelpers.lpFalse;
65 import static org.openjdk.testlib.java.util.stream.LambdaTestHelpers.lpOdd;
66 import static org.openjdk.testlib.java.util.stream.LambdaTestHelpers.lpTrue;
67 import static org.openjdk.testlib.java.util.stream.LambdaTestHelpers.pEven;
68 import static org.openjdk.testlib.java.util.stream.LambdaTestHelpers.pFalse;
69 import static org.openjdk.testlib.java.util.stream.LambdaTestHelpers.pOdd;
70 import static org.openjdk.testlib.java.util.stream.LambdaTestHelpers.pTrue;
71 
72 /**
73  * MatchOpTest
74  *
75  * @author Brian Goetz
76  */
77 @Test
78 public class MatchOpTest extends OpTestCase {
79     private enum Kind { ANY, ALL, NONE }
80 
81     @SuppressWarnings("unchecked")
82     private static final Predicate<Integer>[] INTEGER_PREDICATES
83             = (Predicate<Integer>[]) new Predicate<?>[]{pTrue, pFalse, pEven, pOdd};
84 
85     @SuppressWarnings({"serial", "rawtypes"})
86     private final Map kinds
87             = new HashMap<Kind, Function<Predicate<Integer>, Function<Stream<Integer>, Boolean>>>() {{
88         put(Kind.ANY, p -> s -> s.anyMatch(p));
89         put(Kind.ALL, p -> s -> s.allMatch(p));
90         put(Kind.NONE, p -> s -> s.noneMatch(p));
91     }};
92 
93     @SuppressWarnings("unchecked")
kinds()94     private <T> Map<Kind, Function<Predicate<T>, Function<Stream<T>, Boolean>>> kinds() {
95         return (Map<Kind, Function<Predicate<T>, Function<Stream<T>, Boolean>>>) kinds;
96     }
97 
assertPredicates(List<T> source, Kind kind, Predicate<T>[] predicates, boolean... answers)98     private <T> void assertPredicates(List<T> source, Kind kind, Predicate<T>[] predicates, boolean... answers) {
99         for (int i = 0; i < predicates.length; i++) {
100             setContext("i", i);
101             boolean match = this.<T>kinds().get(kind).apply(predicates[i]).apply(source.stream());
102             assertEquals(answers[i], match, kind.toString() + predicates[i].toString());
103         }
104     }
105 
testStreamMatches()106     public void testStreamMatches() {
107         assertPredicates(countTo(0), Kind.ANY, INTEGER_PREDICATES, false, false, false, false);
108         assertPredicates(countTo(0), Kind.ALL, INTEGER_PREDICATES, true, true, true, true);
109         assertPredicates(countTo(0), Kind.NONE, INTEGER_PREDICATES, true, true, true, true);
110 
111         assertPredicates(countTo(1), Kind.ANY, INTEGER_PREDICATES, true, false, false, true);
112         assertPredicates(countTo(1), Kind.ALL, INTEGER_PREDICATES, true, false, false, true);
113         assertPredicates(countTo(1), Kind.NONE, INTEGER_PREDICATES, false, true, true, false);
114 
115         assertPredicates(countTo(5), Kind.ANY, INTEGER_PREDICATES, true, false, true, true);
116         assertPredicates(countTo(5), Kind.ALL, INTEGER_PREDICATES, true, false, false, false);
117         assertPredicates(countTo(5), Kind.NONE, INTEGER_PREDICATES, false, true, false, false);
118     }
119 
120     @Test(dataProvider = "StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class)
testStream(String name, TestData.OfRef<Integer> data)121     public void testStream(String name, TestData.OfRef<Integer> data) {
122         for (Predicate<Integer> p : INTEGER_PREDICATES) {
123             setContext("p", p);
124             for (Kind kind : Kind.values()) {
125                 setContext("kind", kind);
126                 exerciseTerminalOps(data, this.<Integer>kinds().get(kind).apply(p));
127                 exerciseTerminalOps(data, s -> s.filter(pFalse), this.<Integer>kinds().get(kind).apply(p));
128                 exerciseTerminalOps(data, s -> s.filter(pEven), this.<Integer>kinds().get(kind).apply(p));
129             }
130         }
131     }
132 
testInfinite()133     public void testInfinite() {
134         class CycleIterator implements Iterator<Integer> {
135             final Supplier<Iterator<Integer>> source;
136             Iterator<Integer> i = null;
137 
138             CycleIterator(Supplier<Iterator<Integer>> source) {
139                 this.source = source;
140             }
141 
142             @Override
143             public Integer next() {
144                 if (i == null || !i.hasNext()) {
145                     i = source.get();
146                 }
147                 return i.next();
148             }
149 
150             @Override
151             public boolean hasNext() {
152                 if (i == null || !i.hasNext()) {
153                     i = source.get();
154                 }
155                 return i.hasNext();
156             }
157         }
158 
159         Supplier<Iterator<Integer>> source = () -> Arrays.asList(1, 2, 3, 4).iterator();
160         Supplier<Stream<Integer>> s = () -> StreamSupport.stream(Spliterators.spliteratorUnknownSize(new CycleIterator(source), 0), false);
161 
162         assertFalse(s.get().allMatch(i -> i > 3));
163         assertTrue(s.get().anyMatch(i -> i > 3));
164         assertFalse(s.get().noneMatch(i -> i > 3));
165         assertFalse(s.get().parallel().allMatch(i -> i > 3));
166         assertTrue(s.get().parallel().anyMatch(i -> i > 3));
167         assertFalse(s.get().parallel().noneMatch(i -> i > 3));
168     }
169 
170     //
171 
172     private static final IntPredicate[] INT_PREDICATES
173             = new IntPredicate[]{ipTrue, ipFalse, ipEven, ipOdd};
174 
175     @SuppressWarnings("serial")
176     private final Map<Kind, Function<IntPredicate, Function<IntStream, Boolean>>> intKinds
177             = new HashMap<Kind, Function<IntPredicate, Function<IntStream, Boolean>>>() {{
178         put(Kind.ANY, p -> s -> s.anyMatch(p));
179         put(Kind.ALL, p -> s -> s.allMatch(p));
180         put(Kind.NONE, p -> s -> s.noneMatch(p));
181     }};
182 
assertIntPredicates(Supplier<IntStream> source, Kind kind, IntPredicate[] predicates, boolean... answers)183     private void assertIntPredicates(Supplier<IntStream> source, Kind kind, IntPredicate[] predicates, boolean... answers) {
184         for (int i = 0; i < predicates.length; i++) {
185             setContext("i", i);
186             boolean match = intKinds.get(kind).apply(predicates[i]).apply(source.get());
187             assertEquals(answers[i], match, kind.toString() + predicates[i].toString());
188         }
189     }
190 
testIntStreamMatches()191     public void testIntStreamMatches() {
192         assertIntPredicates(() -> IntStream.range(0, 0), Kind.ANY, INT_PREDICATES, false, false, false, false);
193         assertIntPredicates(() -> IntStream.range(0, 0), Kind.ALL, INT_PREDICATES, true, true, true, true);
194         assertIntPredicates(() -> IntStream.range(0, 0), Kind.NONE, INT_PREDICATES, true, true, true, true);
195 
196         assertIntPredicates(() -> IntStream.range(1, 2), Kind.ANY, INT_PREDICATES, true, false, false, true);
197         assertIntPredicates(() -> IntStream.range(1, 2), Kind.ALL, INT_PREDICATES, true, false, false, true);
198         assertIntPredicates(() -> IntStream.range(1, 2), Kind.NONE, INT_PREDICATES, false, true, true, false);
199 
200         assertIntPredicates(() -> IntStream.range(1, 6), Kind.ANY, INT_PREDICATES, true, false, true, true);
201         assertIntPredicates(() -> IntStream.range(1, 6), Kind.ALL, INT_PREDICATES, true, false, false, false);
202         assertIntPredicates(() -> IntStream.range(1, 6), Kind.NONE, INT_PREDICATES, false, true, false, false);
203     }
204 
205     @Test(dataProvider = "IntStreamTestData", dataProviderClass = IntStreamTestDataProvider.class)
testIntStream(String name, TestData.OfInt data)206     public void testIntStream(String name, TestData.OfInt data) {
207         for (IntPredicate p : INT_PREDICATES) {
208             setContext("p", p);
209             for (Kind kind : Kind.values()) {
210                 setContext("kind", kind);
211                 exerciseTerminalOps(data, intKinds.get(kind).apply(p));
212                 exerciseTerminalOps(data, s -> s.filter(ipFalse), intKinds.get(kind).apply(p));
213                 exerciseTerminalOps(data, s -> s.filter(ipEven), intKinds.get(kind).apply(p));
214             }
215         }
216     }
217 
testIntInfinite()218     public void testIntInfinite() {
219         class CycleIterator implements PrimitiveIterator.OfInt {
220             final Supplier<PrimitiveIterator.OfInt> source;
221             PrimitiveIterator.OfInt i = null;
222 
223             CycleIterator(Supplier<PrimitiveIterator.OfInt> source) {
224                 this.source = source;
225             }
226 
227             @Override
228             public int nextInt() {
229                 if (i == null || !i.hasNext()) {
230                     i = source.get();
231                 }
232                 return i.nextInt();
233             }
234 
235             @Override
236             public boolean hasNext() {
237                 if (i == null || !i.hasNext()) {
238                     i = source.get();
239                 }
240                 return i.hasNext();
241             }
242         }
243 
244         Supplier<PrimitiveIterator.OfInt> source = () -> Arrays.stream(new int[]{1, 2, 3, 4}).iterator();
245         Supplier<IntStream> s = () -> StreamSupport.intStream(Spliterators.spliteratorUnknownSize(new CycleIterator(source), 0), false);
246 
247         assertFalse(s.get().allMatch(i -> i > 3));
248         assertTrue(s.get().anyMatch(i -> i > 3));
249         assertFalse(s.get().noneMatch(i -> i > 3));
250         assertFalse(s.get().parallel().allMatch(i -> i > 3));
251         assertTrue(s.get().parallel().anyMatch(i -> i > 3));
252         assertFalse(s.get().parallel().noneMatch(i -> i > 3));
253     }
254 
255     //
256 
257     private static final LongPredicate[] LONG_PREDICATES
258             = new LongPredicate[]{lpTrue, lpFalse, lpEven, lpOdd};
259 
260     @SuppressWarnings("serial")
261     private final Map<Kind, Function<LongPredicate, Function<LongStream, Boolean>>> longKinds
262             = new HashMap<Kind, Function<LongPredicate, Function<LongStream, Boolean>>>() {{
263         put(Kind.ANY, p -> s -> s.anyMatch(p));
264         put(Kind.ALL, p -> s -> s.allMatch(p));
265         put(Kind.NONE, p -> s -> s.noneMatch(p));
266     }};
267 
assertLongPredicates(Supplier<LongStream> source, Kind kind, LongPredicate[] predicates, boolean... answers)268     private void assertLongPredicates(Supplier<LongStream> source, Kind kind, LongPredicate[] predicates, boolean... answers) {
269         for (int i = 0; i < predicates.length; i++) {
270             setContext("i", i);
271             boolean match = longKinds.get(kind).apply(predicates[i]).apply(source.get());
272             assertEquals(answers[i], match, kind.toString() + predicates[i].toString());
273         }
274     }
275 
testLongStreamMatches()276     public void testLongStreamMatches() {
277         assertLongPredicates(() -> LongStream.range(0, 0), Kind.ANY, LONG_PREDICATES, false, false, false, false);
278         assertLongPredicates(() -> LongStream.range(0, 0), Kind.ALL, LONG_PREDICATES, true, true, true, true);
279         assertLongPredicates(() -> LongStream.range(0, 0), Kind.NONE, LONG_PREDICATES, true, true, true, true);
280 
281         assertLongPredicates(() -> LongStream.range(1, 2), Kind.ANY, LONG_PREDICATES, true, false, false, true);
282         assertLongPredicates(() -> LongStream.range(1, 2), Kind.ALL, LONG_PREDICATES, true, false, false, true);
283         assertLongPredicates(() -> LongStream.range(1, 2), Kind.NONE, LONG_PREDICATES, false, true, true, false);
284 
285         assertLongPredicates(() -> LongStream.range(1, 6), Kind.ANY, LONG_PREDICATES, true, false, true, true);
286         assertLongPredicates(() -> LongStream.range(1, 6), Kind.ALL, LONG_PREDICATES, true, false, false, false);
287         assertLongPredicates(() -> LongStream.range(1, 6), Kind.NONE, LONG_PREDICATES, false, true, false, false);
288     }
289 
290     @Test(dataProvider = "LongStreamTestData", dataProviderClass = LongStreamTestDataProvider.class)
testLongStream(String name, TestData.OfLong data)291     public void testLongStream(String name, TestData.OfLong data) {
292         for (LongPredicate p : LONG_PREDICATES) {
293             setContext("p", p);
294             for (Kind kind : Kind.values()) {
295                 setContext("kind", kind);
296                 exerciseTerminalOps(data, longKinds.get(kind).apply(p));
297                 exerciseTerminalOps(data, s -> s.filter(lpFalse), longKinds.get(kind).apply(p));
298                 exerciseTerminalOps(data, s -> s.filter(lpEven), longKinds.get(kind).apply(p));
299             }
300         }
301     }
302 
testLongInfinite()303     public void testLongInfinite() {
304         class CycleIterator implements PrimitiveIterator.OfLong {
305             final Supplier<PrimitiveIterator.OfLong> source;
306             PrimitiveIterator.OfLong i = null;
307 
308             CycleIterator(Supplier<PrimitiveIterator.OfLong> source) {
309                 this.source = source;
310             }
311 
312             @Override
313             public long nextLong() {
314                 if (i == null || !i.hasNext()) {
315                     i = source.get();
316                 }
317                 return i.nextLong();
318             }
319 
320             @Override
321             public boolean hasNext() {
322                 if (i == null || !i.hasNext()) {
323                     i = source.get();
324                 }
325                 return i.hasNext();
326             }
327         }
328 
329         Supplier<PrimitiveIterator.OfLong> source = () -> Arrays.stream(new long[]{1, 2, 3, 4}).iterator();
330         Supplier<LongStream> s = () -> StreamSupport.longStream(Spliterators.spliteratorUnknownSize(new CycleIterator(source), 0), false);
331 
332         assertFalse(s.get().allMatch(i -> i > 3));
333         assertTrue(s.get().anyMatch(i -> i > 3));
334         assertFalse(s.get().noneMatch(i -> i > 3));
335         assertFalse(s.get().parallel().allMatch(i -> i > 3));
336         assertTrue(s.get().parallel().anyMatch(i -> i > 3));
337         assertFalse(s.get().parallel().noneMatch(i -> i > 3));
338     }
339 
340     //
341 
342     private static final DoublePredicate[] DOUBLE_PREDICATES
343             = new DoublePredicate[]{dpTrue, dpFalse, dpEven, dpOdd};
344 
345     @SuppressWarnings("serial")
346     private final Map<Kind, Function<DoublePredicate, Function<DoubleStream, Boolean>>> doubleKinds
347             = new HashMap<Kind, Function<DoublePredicate, Function<DoubleStream, Boolean>>>() {{
348         put(Kind.ANY, p -> s -> s.anyMatch(p));
349         put(Kind.ALL, p -> s -> s.allMatch(p));
350         put(Kind.NONE, p -> s -> s.noneMatch(p));
351     }};
352 
assertDoublePredicates(Supplier<DoubleStream> source, Kind kind, DoublePredicate[] predicates, boolean... answers)353     private void assertDoublePredicates(Supplier<DoubleStream> source, Kind kind, DoublePredicate[] predicates, boolean... answers) {
354         for (int i = 0; i < predicates.length; i++) {
355             setContext("i", i);
356             boolean match = doubleKinds.get(kind).apply(predicates[i]).apply(source.get());
357             assertEquals(answers[i], match, kind.toString() + predicates[i].toString());
358         }
359     }
360 
testDoubleStreamMatches()361     public void testDoubleStreamMatches() {
362         assertDoublePredicates(() -> LongStream.range(0, 0).asDoubleStream(), Kind.ANY, DOUBLE_PREDICATES, false, false, false, false);
363         assertDoublePredicates(() -> LongStream.range(0, 0).asDoubleStream(), Kind.ALL, DOUBLE_PREDICATES, true, true, true, true);
364         assertDoublePredicates(() -> LongStream.range(0, 0).asDoubleStream(), Kind.NONE, DOUBLE_PREDICATES, true, true, true, true);
365 
366         assertDoublePredicates(() -> LongStream.range(1, 2).asDoubleStream(), Kind.ANY, DOUBLE_PREDICATES, true, false, false, true);
367         assertDoublePredicates(() -> LongStream.range(1, 2).asDoubleStream(), Kind.ALL, DOUBLE_PREDICATES, true, false, false, true);
368         assertDoublePredicates(() -> LongStream.range(1, 2).asDoubleStream(), Kind.NONE, DOUBLE_PREDICATES, false, true, true, false);
369 
370         assertDoublePredicates(() -> LongStream.range(1, 6).asDoubleStream(), Kind.ANY, DOUBLE_PREDICATES, true, false, true, true);
371         assertDoublePredicates(() -> LongStream.range(1, 6).asDoubleStream(), Kind.ALL, DOUBLE_PREDICATES, true, false, false, false);
372         assertDoublePredicates(() -> LongStream.range(1, 6).asDoubleStream(), Kind.NONE, DOUBLE_PREDICATES, false, true, false, false);
373     }
374 
375     @Test(dataProvider = "DoubleStreamTestData", dataProviderClass = DoubleStreamTestDataProvider.class)
testDoubleStream(String name, TestData.OfDouble data)376     public void testDoubleStream(String name, TestData.OfDouble data) {
377         for (DoublePredicate p : DOUBLE_PREDICATES) {
378             setContext("p", p);
379             for (Kind kind : Kind.values()) {
380                 setContext("kind", kind);
381                 exerciseTerminalOps(data, doubleKinds.get(kind).apply(p));
382                 exerciseTerminalOps(data, s -> s.filter(dpFalse), doubleKinds.get(kind).apply(p));
383                 exerciseTerminalOps(data, s -> s.filter(dpEven), doubleKinds.get(kind).apply(p));
384             }
385         }
386     }
387 
testDoubleInfinite()388     public void testDoubleInfinite() {
389         class CycleIterator implements PrimitiveIterator.OfDouble {
390             final Supplier<PrimitiveIterator.OfDouble> source;
391             PrimitiveIterator.OfDouble i = null;
392 
393             CycleIterator(Supplier<PrimitiveIterator.OfDouble> source) {
394                 this.source = source;
395             }
396 
397             @Override
398             public double nextDouble() {
399                 if (i == null || !i.hasNext()) {
400                     i = source.get();
401                 }
402                 return i.nextDouble();
403             }
404 
405             @Override
406             public boolean hasNext() {
407                 if (i == null || !i.hasNext()) {
408                     i = source.get();
409                 }
410                 return i.hasNext();
411             }
412         }
413 
414         Supplier<PrimitiveIterator.OfDouble> source = () -> Arrays.stream(new double[]{1, 2, 3, 4}).iterator();
415         Supplier<DoubleStream> s = () -> StreamSupport.doubleStream(Spliterators.spliteratorUnknownSize(new CycleIterator(source), 0), false);
416 
417         assertFalse(s.get().allMatch(i -> i > 3));
418         assertTrue(s.get().anyMatch(i -> i > 3));
419         assertFalse(s.get().noneMatch(i -> i > 3));
420         assertFalse(s.get().parallel().allMatch(i -> i > 3));
421         assertTrue(s.get().parallel().anyMatch(i -> i > 3));
422         assertFalse(s.get().parallel().noneMatch(i -> i > 3));
423     }
424 }
425