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.*; 26 import java.util.*; 27 28 import java.util.stream.BaseStream; 29 import java.util.stream.Stream; 30 import java.util.stream.IntStream; 31 import java.util.stream.LongStream; 32 import java.util.stream.DoubleStream; 33 34 import org.testng.annotations.Test; 35 36 import java.util.function.Function; 37 38 import static org.openjdk.testlib.java.util.stream.LambdaTestHelpers.*; 39 40 /** 41 * @test 42 * @bug 8148115 43 */ 44 @Test 45 public class FindFirstOpTest extends OpTestCase { 46 testFindFirst()47 public void testFindFirst() { 48 assertFalse(Collections.emptySet().stream().findFirst().isPresent(), "no result"); 49 assertFalse(countTo(10).stream().filter(x -> x > 10).findFirst().isPresent(), "no result"); 50 51 exerciseOps(countTo(1000), s -> Arrays.asList(new Integer[]{s.filter(pEven).findFirst().get()}).stream(), Arrays.asList(2)); 52 exerciseOps(countTo(1000), s -> Arrays.asList(new Integer[]{s.findFirst().get()}).stream(), Arrays.asList(1)); 53 exerciseOps(countTo(1000), s -> Arrays.asList(new Integer[]{s.filter(e -> e == 499).findFirst().get()}).stream(), Arrays.asList(499)); 54 exerciseOps(countTo(1000), s -> Arrays.asList(new Integer[]{s.filter(e -> e == 999).findFirst().get()}).stream(), Arrays.asList(999)); 55 exerciseOps(countTo(0), s -> Arrays.asList(new Integer[]{s.findFirst().orElse(-1)}).stream(), Arrays.asList(-1)); 56 exerciseOps(countTo(1000), s -> Arrays.asList(new Integer[]{s.filter(e -> e == 1499).findFirst().orElse(-1)}).stream(), Arrays.asList(-1)); 57 } 58 59 @Test(dataProvider = "StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class) testStream(String name, TestData.OfRef<Integer> data)60 public void testStream(String name, TestData.OfRef<Integer> data) { 61 exerciseStream(data, s -> s); 62 exerciseStream(data, s -> s.filter(pTrue)); 63 exerciseStream(data, s -> s.filter(pFalse)); 64 exerciseStream(data, s -> s.filter(pEven)); 65 } 66 exerciseStream(TestData.OfRef<Integer> data, Function<Stream<Integer>, Stream<Integer>> fs)67 void exerciseStream(TestData.OfRef<Integer> data, Function<Stream<Integer>, Stream<Integer>> fs) { 68 Iterator<Integer> i = fs.apply(data.stream()).iterator(); 69 Optional<Integer> expected = i.hasNext() ? Optional.of(i.next()) : Optional.empty(); 70 withData(data).terminal(fs, s -> s.findFirst()) 71 .expectedResult(expected) 72 .resultAsserter((act, exp, ord, par) -> { 73 if (par & !ord) { 74 assertContains(act, fs.apply(data.stream()).iterator()); 75 } 76 else { 77 assertEquals(act, exp); 78 } 79 }) 80 .exercise(); 81 } 82 83 @Test(dataProvider = "IntStreamTestData", dataProviderClass = IntStreamTestDataProvider.class) testIntStream(String name, TestData.OfInt data)84 public void testIntStream(String name, TestData.OfInt data) { 85 exerciseIntStream(data, s -> s); 86 exerciseIntStream(data, s -> s.filter(ipTrue)); 87 exerciseIntStream(data, s -> s.filter(ipFalse)); 88 exerciseIntStream(data, s -> s.filter(ipEven)); 89 } 90 exerciseIntStream(TestData.OfInt data, Function<IntStream, IntStream> fs)91 void exerciseIntStream(TestData.OfInt data, Function<IntStream, IntStream> fs) { 92 OptionalInt r = exerciseTerminalOps(data, fs, s -> s.findFirst()); 93 if (r.isPresent()) { 94 PrimitiveIterator.OfInt i = fs.apply(data.stream()).iterator(); 95 assertTrue(i.hasNext()); 96 assertEquals(i.nextInt(), r.getAsInt()); 97 } 98 else { 99 assertFalse(fs.apply(data.stream()).iterator().hasNext()); 100 } 101 } 102 103 @Test(dataProvider = "LongStreamTestData", dataProviderClass = LongStreamTestDataProvider.class) testLongStream(String name, TestData.OfLong data)104 public void testLongStream(String name, TestData.OfLong data) { 105 exerciseLongStream(data, s -> s); 106 exerciseLongStream(data, s -> s.filter(lpTrue)); 107 exerciseLongStream(data, s -> s.filter(lpFalse)); 108 exerciseLongStream(data, s -> s.filter(lpEven)); 109 } 110 exerciseLongStream(TestData.OfLong data, Function<LongStream, LongStream> fs)111 void exerciseLongStream(TestData.OfLong data, Function<LongStream, LongStream> fs) { 112 OptionalLong r = exerciseTerminalOps(data, fs, s -> s.findFirst()); 113 if (r.isPresent()) { 114 PrimitiveIterator.OfLong i = fs.apply(data.stream()).iterator(); 115 assertTrue(i.hasNext()); 116 assertEquals(i.nextLong(), r.getAsLong()); 117 } 118 else { 119 assertFalse(fs.apply(data.stream()).iterator().hasNext()); 120 } 121 } 122 123 @Test(dataProvider = "DoubleStreamTestData", dataProviderClass = DoubleStreamTestDataProvider.class) testDoubleStream(String name, TestData.OfDouble data)124 public void testDoubleStream(String name, TestData.OfDouble data) { 125 exerciseDoubleStream(data, s -> s); 126 exerciseDoubleStream(data, s -> s.filter(dpTrue)); 127 exerciseDoubleStream(data, s -> s.filter(dpFalse)); 128 exerciseDoubleStream(data, s -> s.filter(dpEven)); 129 } 130 exerciseDoubleStream(TestData.OfDouble data, Function<DoubleStream, DoubleStream> fs)131 void exerciseDoubleStream(TestData.OfDouble data, Function<DoubleStream, DoubleStream> fs) { 132 OptionalDouble r = exerciseTerminalOps(data, fs, s -> s.findFirst()); 133 if (r.isPresent()) { 134 PrimitiveIterator.OfDouble i = fs.apply(data.stream()).iterator(); 135 assertTrue(i.hasNext()); 136 assertEquals(i.nextDouble(), r.getAsDouble()); 137 } 138 else { 139 assertFalse(fs.apply(data.stream()).iterator().hasNext()); 140 } 141 } 142 } 143