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. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 package org.openjdk.tests.java.util.stream; 26 27 import java.util.*; 28 import java.util.function.BiConsumer; 29 import java.util.stream.*; 30 31 import org.testng.annotations.Test; 32 33 import java.util.function.Function; 34 35 import static java.util.stream.LambdaTestHelpers.*; 36 37 38 /** 39 * FindAnyOpTest 40 */ 41 @Test 42 public class FindAnyOpTest extends OpTestCase { 43 44 public void testFindAny() { 45 assertFalse(Collections.emptySet().stream().findAny().isPresent(), "no result"); 46 assertFalse(countTo(10).stream().filter(x -> x > 10).findAny().isPresent(), "no result"); 47 assertTrue(countTo(10).stream().filter(pEven).findAny().isPresent(), "with result"); 48 } 49 50 public void testFindAnyParallel() { 51 assertFalse(Collections.emptySet().parallelStream().findAny().isPresent(), "no result"); 52 assertFalse(countTo(1000).parallelStream().filter(x -> x > 1000).findAny().isPresent(), "no result"); 53 assertTrue(countTo(1000).parallelStream().filter(pEven).findAny().isPresent(), "with result"); 54 } 55 56 @Test(dataProvider = "StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class) 57 public void testStream(String name, TestData.OfRef<Integer> data) { 58 exerciseStream(data, s -> s); 59 exerciseStream(data, s -> s.filter(pTrue)); 60 exerciseStream(data, s -> s.filter(pFalse)); 61 exerciseStream(data, s -> s.filter(pEven)); 62 } 63 64 void exerciseStream(TestData.OfRef<Integer> data, Function<Stream<Integer>, Stream<Integer>> fs) { 65 Optional<Integer> or = withData(data).terminal(fs, s -> s.findAny()).equalator(VALID_ANSWER).exercise(); 66 if (or.isPresent()) { 67 Integer r = or.get(); 68 Iterator<Integer> it = fs.apply(data.stream()).iterator(); 69 boolean contained = false; 70 while (!contained && it.hasNext()) { 71 contained = Objects.equals(r, it.next()); 72 } 73 assertTrue(contained); 74 } 75 else { 76 assertFalse(fs.apply(data.stream()).iterator().hasNext()); 77 } 78 } 79 80 @Test(dataProvider = "IntStreamTestData", dataProviderClass = IntStreamTestDataProvider.class) 81 public void testIntStream(String name, TestData.OfInt data) { 82 exerciseIntStream(data, s -> s); 83 exerciseIntStream(data, s -> s.filter(ipTrue)); 84 exerciseIntStream(data, s -> s.filter(ipFalse)); 85 exerciseIntStream(data, s -> s.filter(ipEven)); 86 } 87 88 void exerciseIntStream(TestData.OfInt data, Function<IntStream, IntStream> fs) { 89 OptionalInt or = withData(data).terminal(fs, s -> s.findAny()).equalator(INT_VALID_ANSWER).exercise(); 90 if (or.isPresent()) { 91 int r = or.getAsInt(); 92 PrimitiveIterator.OfInt it = fs.apply(data.stream()).iterator(); 93 boolean contained = false; 94 while (!contained && it.hasNext()) { 95 contained = r == it.nextInt(); 96 } 97 assertTrue(contained); 98 } 99 else { 100 assertFalse(fs.apply(data.stream()).iterator().hasNext()); 101 } 102 } 103 104 @Test(dataProvider = "LongStreamTestData", dataProviderClass = LongStreamTestDataProvider.class) 105 public void testLongStream(String name, TestData.OfLong data) { 106 exerciseLongStream(data, s -> s); 107 exerciseLongStream(data, s -> s.filter(lpTrue)); 108 exerciseLongStream(data, s -> s.filter(lpFalse)); 109 exerciseLongStream(data, s -> s.filter(lpEven)); 110 } 111 112 void exerciseLongStream(TestData.OfLong data, Function<LongStream, LongStream> fs) { 113 OptionalLong or = withData(data).terminal(fs, s -> s.findAny()).equalator(LONG_VALID_ANSWER).exercise(); 114 if (or.isPresent()) { 115 long r = or.getAsLong(); 116 PrimitiveIterator.OfLong it = fs.apply(data.stream()).iterator(); 117 boolean contained = false; 118 while (!contained && it.hasNext()) { 119 contained = r == it.nextLong(); 120 } 121 assertTrue(contained); 122 } 123 else { 124 assertFalse(fs.apply(data.stream()).iterator().hasNext()); 125 } 126 } 127 128 @Test(dataProvider = "DoubleStreamTestData", dataProviderClass = DoubleStreamTestDataProvider.class) 129 public void testDoubleStream(String name, TestData.OfDouble data) { 130 exerciseDoubleStream(data, s -> s); 131 exerciseDoubleStream(data, s -> s.filter(dpTrue)); 132 exerciseDoubleStream(data, s -> s.filter(dpEven)); 133 exerciseDoubleStream(data, s -> s.filter(dpFalse)); 134 } 135 136 void exerciseDoubleStream(TestData.OfDouble data, Function<DoubleStream, DoubleStream> fs) { 137 OptionalDouble or = withData(data).terminal(fs, s -> s.findAny()).equalator(DOUBLE_VALID_ANSWER).exercise(); 138 if (or.isPresent()) { 139 double r = or.getAsDouble(); 140 PrimitiveIterator.OfDouble it = fs.apply(data.stream()).iterator(); 141 boolean contained = false; 142 while (!contained && it.hasNext()) { 143 contained = r == it.nextDouble(); 144 } 145 assertTrue(contained); 146 } 147 else { 148 assertFalse(fs.apply(data.stream()).iterator().hasNext()); 149 } 150 } 151 152 static final BiConsumer<Optional<Integer>, Optional<Integer>> VALID_ANSWER = (a, b) -> assertEquals(a.isPresent(), b.isPresent()); 153 154 static final BiConsumer<OptionalInt, OptionalInt> INT_VALID_ANSWER = (a, b) -> assertEquals(a.isPresent(), b.isPresent()); 155 156 static final BiConsumer<OptionalLong, OptionalLong> LONG_VALID_ANSWER = (a, b) -> assertEquals(a.isPresent(), b.isPresent()); 157 158 static final BiConsumer<OptionalDouble, OptionalDouble> DOUBLE_VALID_ANSWER = (a, b) -> assertEquals(a.isPresent(), b.isPresent()); 159 }