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 java.util.*; 26 import java.util.function.BiConsumer; 27 import java.util.stream.*; 28 29 import org.testng.annotations.Test; 30 31 import java.util.function.Function; 32 33 import static java.util.stream.LambdaTestHelpers.*; 34 35 36 /** 37 * FindAnyOpTest 38 */ 39 @Test 40 public class FindAnyOpTest extends OpTestCase { 41 testFindAny()42 public void testFindAny() { 43 assertFalse(Collections.emptySet().stream().findAny().isPresent(), "no result"); 44 assertFalse(countTo(10).stream().filter(x -> x > 10).findAny().isPresent(), "no result"); 45 assertTrue(countTo(10).stream().filter(pEven).findAny().isPresent(), "with result"); 46 } 47 testFindAnyParallel()48 public void testFindAnyParallel() { 49 assertFalse(Collections.emptySet().parallelStream().findAny().isPresent(), "no result"); 50 assertFalse(countTo(1000).parallelStream().filter(x -> x > 1000).findAny().isPresent(), "no result"); 51 assertTrue(countTo(1000).parallelStream().filter(pEven).findAny().isPresent(), "with result"); 52 } 53 54 @Test(dataProvider = "StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class) testStream(String name, TestData.OfRef<Integer> data)55 public void testStream(String name, TestData.OfRef<Integer> data) { 56 exerciseStream(data, s -> s); 57 exerciseStream(data, s -> s.filter(pTrue)); 58 exerciseStream(data, s -> s.filter(pFalse)); 59 exerciseStream(data, s -> s.filter(pEven)); 60 } 61 exerciseStream(TestData.OfRef<Integer> data, Function<Stream<Integer>, Stream<Integer>> fs)62 void exerciseStream(TestData.OfRef<Integer> data, Function<Stream<Integer>, Stream<Integer>> fs) { 63 Optional<Integer> or = withData(data).terminal(fs, s -> s.findAny()).equalator(VALID_ANSWER).exercise(); 64 if (or.isPresent()) { 65 Integer r = or.get(); 66 Iterator<Integer> it = fs.apply(data.stream()).iterator(); 67 boolean contained = false; 68 while (!contained && it.hasNext()) { 69 contained = Objects.equals(r, it.next()); 70 } 71 assertTrue(contained); 72 } 73 else { 74 assertFalse(fs.apply(data.stream()).iterator().hasNext()); 75 } 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