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