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