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.Collection; 26 import java.util.stream.*; 27 28 import org.testng.annotations.Test; 29 30 import java.util.Arrays; 31 32 import static java.util.stream.LambdaTestHelpers.*; 33 34 /** 35 * FilterOpTest 36 * 37 * @author Brian Goetz 38 */ 39 @Test 40 public class FilterOpTest extends OpTestCase { testFilter()41 public void testFilter() { 42 assertCountSum(countTo(0).stream().filter(pTrue), 0, 0); 43 assertCountSum(countTo(10).stream().filter(pFalse), 0, 0); 44 assertCountSum(countTo(10).stream().filter(pEven), 5, 30); 45 assertCountSum(countTo(10).stream().filter(pOdd), 5, 25); 46 assertCountSum(countTo(10).stream().filter(pTrue), 10, 55); 47 assertCountSum(countTo(10).stream().filter(pEven).filter(pOdd), 0, 0); 48 49 exerciseOps(countTo(1000), s -> s.filter(pTrue), countTo(1000)); 50 exerciseOps(countTo(1000), s -> s.filter(pFalse), countTo(0)); 51 exerciseOps(countTo(1000), s -> s.filter(e -> e > 100), range(101, 1000)); 52 exerciseOps(countTo(1000), s -> s.filter(e -> e < 100), countTo(99)); 53 exerciseOps(countTo(1000), s -> s.filter(e -> e == 100), Arrays.asList(100)); 54 } 55 56 @Test(dataProvider = "StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class) testOps(String name, TestData.OfRef<Integer> data)57 public void testOps(String name, TestData.OfRef<Integer> data) { 58 Collection<Integer> result = exerciseOps(data, s -> s.filter(pTrue)); 59 assertEquals(result.size(), data.size()); 60 61 result = exerciseOps(data, s -> s.filter(pFalse)); 62 assertEquals(result.size(), 0); 63 64 exerciseOps(data, s -> s.filter(pEven)); 65 exerciseOps(data, s -> s.filter(pOdd)); 66 67 result = exerciseOps(data, s -> s.filter(pOdd.and(pEven))); 68 assertEquals(result.size(), 0); 69 70 result = exerciseOps(data, s -> s.filter(pOdd.or(pEven))); 71 assertEquals(result.size(), data.size()); 72 } 73 74 @Test(dataProvider = "IntStreamTestData", dataProviderClass = IntStreamTestDataProvider.class) testOps(String name, TestData.OfInt data)75 public void testOps(String name, TestData.OfInt data) { 76 Collection<Integer> result = exerciseOps(data, s -> s.filter(i -> true)); 77 assertEquals(result.size(), data.size()); 78 79 result = exerciseOps(data, s -> s.filter(i -> false)); 80 assertEquals(result.size(), 0); 81 82 exerciseOps(data, s -> s.filter(i -> 0 == i % 2)); 83 exerciseOps(data, s -> s.filter(i -> 1 == i % 2)); 84 } 85 86 @Test(dataProvider = "LongStreamTestData", dataProviderClass = LongStreamTestDataProvider.class) testOps(String name, TestData.OfLong data)87 public void testOps(String name, TestData.OfLong data) { 88 Collection<Long> result = exerciseOps(data, s -> s.filter(i -> true)); 89 assertEquals(result.size(), data.size()); 90 91 result = exerciseOps(data, s -> s.filter(i -> false)); 92 assertEquals(result.size(), 0); 93 94 exerciseOps(data, s -> s.filter(i -> 0 == i % 2)); 95 exerciseOps(data, s -> s.filter(i -> 1 == i % 2)); 96 } 97 98 @Test(dataProvider = "DoubleStreamTestData", dataProviderClass = DoubleStreamTestDataProvider.class) testOps(String name, TestData.OfDouble data)99 public void testOps(String name, TestData.OfDouble data) { 100 Collection<Double> result = exerciseOps(data, s -> s.filter(i -> true)); 101 assertEquals(result.size(), data.size()); 102 103 result = exerciseOps(data, s -> s.filter(i -> false)); 104 assertEquals(result.size(), 0); 105 106 exerciseOps(data, s -> s.filter(i -> 0 == ((long) i) % 2)); 107 exerciseOps(data, s -> s.filter(i -> 1 == ((long) i) % 2)); 108 } 109 } 110