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