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