• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.*;
34 import java.util.function.BiConsumer;
35 import java.util.function.Function;
36 import java.util.stream.BaseStream;
37 import java.util.stream.Stream;
38 import java.util.stream.IntStream;
39 import java.util.stream.LongStream;
40 import java.util.stream.DoubleStream;
41 
42 import org.testng.annotations.Test;
43 
44 
45 import static org.openjdk.testlib.java.util.stream.LambdaTestHelpers.*;
46 
47 
48 /**
49  * FindAnyOpTest
50  */
51 @Test
52 public class FindAnyOpTest extends OpTestCase {
53 
testFindAny()54     public void testFindAny() {
55         assertFalse(Collections.emptySet().stream().findAny().isPresent(), "no result");
56         assertFalse(countTo(10).stream().filter(x -> x > 10).findAny().isPresent(), "no result");
57         assertTrue(countTo(10).stream().filter(pEven).findAny().isPresent(), "with result");
58     }
59 
testFindAnyParallel()60     public void testFindAnyParallel() {
61         assertFalse(Collections.emptySet().parallelStream().findAny().isPresent(), "no result");
62         assertFalse(countTo(1000).parallelStream().filter(x -> x > 1000).findAny().isPresent(), "no result");
63         assertTrue(countTo(1000).parallelStream().filter(pEven).findAny().isPresent(), "with result");
64     }
65 
66     @Test(dataProvider = "StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class)
testStream(String name, TestData.OfRef<Integer> data)67     public void testStream(String name, TestData.OfRef<Integer> data) {
68         exerciseStream(data, s -> s);
69         exerciseStream(data, s -> s.filter(pTrue));
70         exerciseStream(data, s -> s.filter(pFalse));
71         exerciseStream(data, s -> s.filter(pEven));
72     }
73 
exerciseStream(TestData.OfRef<Integer> data, Function<Stream<Integer>, Stream<Integer>> fs)74     void exerciseStream(TestData.OfRef<Integer> data, Function<Stream<Integer>, Stream<Integer>> fs) {
75         Optional<Integer> or = withData(data).terminal(fs, s -> s.findAny()).equalator(VALID_ANSWER).exercise();
76         if (or.isPresent()) {
77             Integer r = or.get();
78             Iterator<Integer> it = fs.apply(data.stream()).iterator();
79             boolean contained = false;
80             while (!contained && it.hasNext()) {
81                 contained = Objects.equals(r, it.next());
82             }
83             assertTrue(contained);
84         }
85         else {
86             assertFalse(fs.apply(data.stream()).iterator().hasNext());
87         }
88     }
89 
90     @Test(dataProvider = "IntStreamTestData", dataProviderClass = IntStreamTestDataProvider.class)
testIntStream(String name, TestData.OfInt data)91     public void testIntStream(String name, TestData.OfInt data) {
92         exerciseIntStream(data, s -> s);
93         exerciseIntStream(data, s -> s.filter(ipTrue));
94         exerciseIntStream(data, s -> s.filter(ipFalse));
95         exerciseIntStream(data, s -> s.filter(ipEven));
96     }
97 
exerciseIntStream(TestData.OfInt data, Function<IntStream, IntStream> fs)98     void exerciseIntStream(TestData.OfInt data, Function<IntStream, IntStream> fs) {
99         OptionalInt or = withData(data).terminal(fs, s -> s.findAny()).equalator(INT_VALID_ANSWER).exercise();
100         if (or.isPresent()) {
101             int r = or.getAsInt();
102             PrimitiveIterator.OfInt it = fs.apply(data.stream()).iterator();
103             boolean contained = false;
104             while (!contained && it.hasNext()) {
105                 contained = r == it.nextInt();
106             }
107             assertTrue(contained);
108         }
109         else {
110             assertFalse(fs.apply(data.stream()).iterator().hasNext());
111         }
112     }
113 
114     @Test(dataProvider = "LongStreamTestData", dataProviderClass = LongStreamTestDataProvider.class)
testLongStream(String name, TestData.OfLong data)115     public void testLongStream(String name, TestData.OfLong data) {
116         exerciseLongStream(data, s -> s);
117         exerciseLongStream(data, s -> s.filter(lpTrue));
118         exerciseLongStream(data, s -> s.filter(lpFalse));
119         exerciseLongStream(data, s -> s.filter(lpEven));
120     }
121 
exerciseLongStream(TestData.OfLong data, Function<LongStream, LongStream> fs)122     void exerciseLongStream(TestData.OfLong data, Function<LongStream, LongStream> fs) {
123         OptionalLong or = withData(data).terminal(fs, s -> s.findAny()).equalator(LONG_VALID_ANSWER).exercise();
124         if (or.isPresent()) {
125             long r = or.getAsLong();
126             PrimitiveIterator.OfLong it = fs.apply(data.stream()).iterator();
127             boolean contained = false;
128             while (!contained && it.hasNext()) {
129                 contained = r == it.nextLong();
130             }
131             assertTrue(contained);
132         }
133         else {
134             assertFalse(fs.apply(data.stream()).iterator().hasNext());
135         }
136     }
137 
138     @Test(dataProvider = "DoubleStreamTestData", dataProviderClass = DoubleStreamTestDataProvider.class)
testDoubleStream(String name, TestData.OfDouble data)139     public void testDoubleStream(String name, TestData.OfDouble data) {
140         exerciseDoubleStream(data, s -> s);
141         exerciseDoubleStream(data, s -> s.filter(dpTrue));
142         exerciseDoubleStream(data, s -> s.filter(dpEven));
143         exerciseDoubleStream(data, s -> s.filter(dpFalse));
144     }
145 
exerciseDoubleStream(TestData.OfDouble data, Function<DoubleStream, DoubleStream> fs)146     void exerciseDoubleStream(TestData.OfDouble data, Function<DoubleStream, DoubleStream> fs) {
147         OptionalDouble or = withData(data).terminal(fs, s -> s.findAny()).equalator(DOUBLE_VALID_ANSWER).exercise();
148         if (or.isPresent()) {
149             double r = or.getAsDouble();
150             PrimitiveIterator.OfDouble it = fs.apply(data.stream()).iterator();
151             boolean contained = false;
152             while (!contained && it.hasNext()) {
153                 contained = r == it.nextDouble();
154             }
155             assertTrue(contained);
156         }
157         else {
158             assertFalse(fs.apply(data.stream()).iterator().hasNext());
159         }
160     }
161 
162     static final BiConsumer<Optional<Integer>, Optional<Integer>> VALID_ANSWER = (a, b) -> assertEquals(a.isPresent(), b.isPresent());
163 
164     static final BiConsumer<OptionalInt, OptionalInt> INT_VALID_ANSWER = (a, b) -> assertEquals(a.isPresent(), b.isPresent());
165 
166     static final BiConsumer<OptionalLong, OptionalLong> LONG_VALID_ANSWER = (a, b) -> assertEquals(a.isPresent(), b.isPresent());
167 
168     static final BiConsumer<OptionalDouble, OptionalDouble> DOUBLE_VALID_ANSWER = (a, b) -> assertEquals(a.isPresent(), b.isPresent());
169 }
170