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