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 java.util.*;
26 import java.util.stream.*;
27 
28 import org.testng.annotations.Test;
29 
30 import java.util.function.Function;
31 
32 import static java.util.stream.LambdaTestHelpers.*;
33 
34 
35 /**
36  * FindFirstOpTest
37  */
38 @Test
39 public class FindFirstOpTest extends OpTestCase {
40 
testFindFirst()41     public void testFindFirst() {
42         assertFalse(Collections.emptySet().stream().findFirst().isPresent(), "no result");
43         assertFalse(countTo(10).stream().filter(x -> x > 10).findFirst().isPresent(), "no result");
44 
45         exerciseOps(countTo(1000), s -> Arrays.asList(new Integer[]{s.filter(pEven).findFirst().get()}).stream(), Arrays.asList(2));
46         exerciseOps(countTo(1000), s -> Arrays.asList(new Integer[]{s.findFirst().get()}).stream(), Arrays.asList(1));
47         exerciseOps(countTo(1000), s -> Arrays.asList(new Integer[]{s.filter(e -> e == 499).findFirst().get()}).stream(), Arrays.asList(499));
48         exerciseOps(countTo(1000), s -> Arrays.asList(new Integer[]{s.filter(e -> e == 999).findFirst().get()}).stream(), Arrays.asList(999));
49         exerciseOps(countTo(0), s -> Arrays.asList(new Integer[]{s.findFirst().orElse(-1)}).stream(), Arrays.asList(-1));
50         exerciseOps(countTo(1000), s -> Arrays.asList(new Integer[]{s.filter(e -> e == 1499).findFirst().orElse(-1)}).stream(), Arrays.asList(-1));
51     }
52 
53     @Test(dataProvider = "StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class)
testStream(String name, TestData.OfRef<Integer> data)54     public void testStream(String name, TestData.OfRef<Integer> data) {
55         exerciseStream(data, s -> s);
56         exerciseStream(data, s -> s.filter(pTrue));
57         exerciseStream(data, s -> s.filter(pFalse));
58         exerciseStream(data, s -> s.filter(pEven));
59     }
60 
exerciseStream(TestData.OfRef<Integer> data, Function<Stream<Integer>, Stream<Integer>> fs)61     void exerciseStream(TestData.OfRef<Integer> data, Function<Stream<Integer>, Stream<Integer>> fs) {
62         Optional<Integer> r = exerciseTerminalOps(data, fs, s -> s.findFirst());
63         if (r.isPresent()) {
64             Iterator<Integer> i = fs.apply(data.stream()).iterator();
65             assertTrue(i.hasNext());
66             assertEquals(i.next(), r.get());
67         }
68         else {
69             assertFalse(fs.apply(data.stream()).iterator().hasNext());
70         }
71     }
72 
73     @Test(dataProvider = "IntStreamTestData", dataProviderClass = IntStreamTestDataProvider.class)
testIntStream(String name, TestData.OfInt data)74     public void testIntStream(String name, TestData.OfInt data) {
75         exerciseIntStream(data, s -> s);
76         exerciseIntStream(data, s -> s.filter(ipTrue));
77         exerciseIntStream(data, s -> s.filter(ipFalse));
78         exerciseIntStream(data, s -> s.filter(ipEven));
79     }
80 
exerciseIntStream(TestData.OfInt data, Function<IntStream, IntStream> fs)81     void exerciseIntStream(TestData.OfInt data, Function<IntStream, IntStream> fs) {
82         OptionalInt r = exerciseTerminalOps(data, fs, s -> s.findFirst());
83         if (r.isPresent()) {
84             PrimitiveIterator.OfInt i = fs.apply(data.stream()).iterator();
85             assertTrue(i.hasNext());
86             assertEquals(i.nextInt(), r.getAsInt());
87         }
88         else {
89             assertFalse(fs.apply(data.stream()).iterator().hasNext());
90         }
91     }
92 
93     @Test(dataProvider = "LongStreamTestData", dataProviderClass = LongStreamTestDataProvider.class)
testLongStream(String name, TestData.OfLong data)94     public void testLongStream(String name, TestData.OfLong data) {
95         exerciseLongStream(data, s -> s);
96         exerciseLongStream(data, s -> s.filter(lpTrue));
97         exerciseLongStream(data, s -> s.filter(lpFalse));
98         exerciseLongStream(data, s -> s.filter(lpEven));
99     }
100 
exerciseLongStream(TestData.OfLong data, Function<LongStream, LongStream> fs)101     void exerciseLongStream(TestData.OfLong data, Function<LongStream, LongStream> fs) {
102         OptionalLong r = exerciseTerminalOps(data, fs, s -> s.findFirst());
103         if (r.isPresent()) {
104             PrimitiveIterator.OfLong i = fs.apply(data.stream()).iterator();
105             assertTrue(i.hasNext());
106             assertEquals(i.nextLong(), r.getAsLong());
107         }
108         else {
109             assertFalse(fs.apply(data.stream()).iterator().hasNext());
110         }
111     }
112 
113     @Test(dataProvider = "DoubleStreamTestData", dataProviderClass = DoubleStreamTestDataProvider.class)
testDoubleStream(String name, TestData.OfDouble data)114     public void testDoubleStream(String name, TestData.OfDouble data) {
115         exerciseDoubleStream(data, s -> s);
116         exerciseDoubleStream(data, s -> s.filter(dpTrue));
117         exerciseDoubleStream(data, s -> s.filter(dpFalse));
118         exerciseDoubleStream(data, s -> s.filter(dpEven));
119     }
120 
exerciseDoubleStream(TestData.OfDouble data, Function<DoubleStream, DoubleStream> fs)121     void exerciseDoubleStream(TestData.OfDouble data, Function<DoubleStream, DoubleStream> fs) {
122         OptionalDouble r = exerciseTerminalOps(data, fs, s -> s.findFirst());
123         if (r.isPresent()) {
124             PrimitiveIterator.OfDouble i = fs.apply(data.stream()).iterator();
125             assertTrue(i.hasNext());
126             assertEquals(i.nextDouble(), r.getAsDouble());
127         }
128         else {
129             assertFalse(fs.apply(data.stream()).iterator().hasNext());
130         }
131     }
132 }
133