Search in sources :

Example 1 with UnaryOperator

use of java8.util.function.UnaryOperator in project streamsupport by stefan-zobel.

the class StreamSpliteratorTest method testDoubleSplitting.

// 
public void testDoubleSplitting() {
    List<Consumer<DoubleStream>> terminalOps = Arrays.asList(s -> s.toArray(), s -> s.forEach(e -> {
    }), s -> s.reduce(java8.lang.Doubles::sum));
    List<UnaryOperator<DoubleStream>> intermediateOps = Arrays.asList(s -> s.parallel(), // The following ensures the wrapping spliterator is tested
    s -> s.map(i -> i).parallel());
    for (int i = 0; i < terminalOps.size(); i++) {
        Consumer<DoubleStream> terminalOp = terminalOps.get(i);
        setContext("termOpIndex", i);
        for (int j = 0; j < intermediateOps.size(); j++) {
            UnaryOperator<DoubleStream> intermediateOp = intermediateOps.get(j);
            setContext("intOpIndex", j);
            for (boolean proxyEstimateSize : new boolean[] { false, true }) {
                setContext("proxyEstimateSize", proxyEstimateSize);
                // Size is assumed to be larger than the target size for no splitting
                // @@@ Need way to obtain the target size
                Spliterator.OfDouble sp = intermediateOp.apply(IntStreams.range(0, 1000).asDoubleStream()).spliterator();
                ProxyNoExactSizeSpliterator.OfDouble psp = new ProxyNoExactSizeSpliterator.OfDouble(sp, proxyEstimateSize);
                DoubleStream s = StreamSupport.doubleStream(psp, true);
                terminalOp.accept(s);
                Assert.assertTrue(psp.splits > 0, String.format("Number of splits should be greater that zero when proxyEstimateSize is %s", proxyEstimateSize));
                Assert.assertTrue(psp.prefixSplits > 0, String.format("Number of non-null prefix splits should be greater that zero when proxyEstimateSize is %s", proxyEstimateSize));
                Assert.assertTrue(psp.sizeOnTraversal < 1000, String.format("Size on traversal of last split should be less than the size of the list, %d, when proxyEstimateSize is %s", 1000, proxyEstimateSize));
            }
        }
    }
}
Also used : LongConsumer(java8.util.function.LongConsumer) LambdaTestHelpers.irDoubler(java8.util.stream.LambdaTestHelpers.irDoubler) Arrays(java.util.Arrays) IntConsumer(java8.util.function.IntConsumer) LambdaTestHelpers.countTo(java8.util.stream.LambdaTestHelpers.countTo) LongStreamTestDataProvider(java8.util.stream.LongStreamTestDataProvider) UnaryOperator(java8.util.function.UnaryOperator) Test(org.testng.annotations.Test) LambdaTestHelpers.ipEven(java8.util.stream.LambdaTestHelpers.ipEven) DoubleStreamTestDataProvider(java8.util.stream.DoubleStreamTestDataProvider) TestData(java8.util.stream.TestData) LambdaTestHelpers.lpEven(java8.util.stream.LambdaTestHelpers.lpEven) LambdaTestHelpers.dpEven(java8.util.stream.LambdaTestHelpers.dpEven) Function(java8.util.function.Function) LambdaTestHelpers(java8.util.stream.LambdaTestHelpers) LongStreams(java8.util.stream.LongStreams) Assert(org.testng.Assert) Consumer(java8.util.function.Consumer) StreamTestDataProvider(java8.util.stream.StreamTestDataProvider) LambdaTestHelpers.pEven(java8.util.stream.LambdaTestHelpers.pEven) DoubleStreams(java8.util.stream.DoubleStreams) Spliterator(java8.util.Spliterator) IntStream(java8.util.stream.IntStream) OpTestCase(java8.util.stream.OpTestCase) SpliteratorTestHelper(java8.util.stream.SpliteratorTestHelper) Spliterators(java8.util.Spliterators) LongStream(java8.util.stream.LongStream) StreamSupport(java8.util.stream.StreamSupport) IntStreams(java8.util.stream.IntStreams) List(java.util.List) RefStreams(java8.util.stream.RefStreams) LambdaTestHelpers.mDoubler(java8.util.stream.LambdaTestHelpers.mDoubler) LambdaTestHelpers.permuteStreamFunctions(java8.util.stream.LambdaTestHelpers.permuteStreamFunctions) DoubleStream(java8.util.stream.DoubleStream) Comparator(java.util.Comparator) IntStreamTestDataProvider(java8.util.stream.IntStreamTestDataProvider) DoubleConsumer(java8.util.function.DoubleConsumer) Stream(java8.util.stream.Stream) LongConsumer(java8.util.function.LongConsumer) IntConsumer(java8.util.function.IntConsumer) Consumer(java8.util.function.Consumer) DoubleConsumer(java8.util.function.DoubleConsumer) DoubleStream(java8.util.stream.DoubleStream) UnaryOperator(java8.util.function.UnaryOperator) Spliterator(java8.util.Spliterator)

Example 2 with UnaryOperator

use of java8.util.function.UnaryOperator in project streamsupport by stefan-zobel.

the class StreamSpliteratorTest method testLongSplitting.

// 
public void testLongSplitting() {
    List<Consumer<LongStream>> terminalOps = Arrays.asList(s -> s.toArray(), s -> s.forEach(e -> {
    }), s -> s.reduce(java8.lang.Longs::sum));
    List<UnaryOperator<LongStream>> intermediateOps = Arrays.asList(s -> s.parallel(), // The following ensures the wrapping spliterator is tested
    s -> s.map(i -> i).parallel());
    for (int i = 0; i < terminalOps.size(); i++) {
        Consumer<LongStream> terminalOp = terminalOps.get(i);
        setContext("termOpIndex", i);
        for (int j = 0; j < intermediateOps.size(); j++) {
            setContext("intOpIndex", j);
            UnaryOperator<LongStream> intermediateOp = intermediateOps.get(j);
            for (boolean proxyEstimateSize : new boolean[] { false, true }) {
                setContext("proxyEstimateSize", proxyEstimateSize);
                // Size is assumed to be larger than the target size for no splitting
                // @@@ Need way to obtain the target size
                Spliterator.OfLong sp = intermediateOp.apply(LongStreams.range(0, 1000)).spliterator();
                ProxyNoExactSizeSpliterator.OfLong psp = new ProxyNoExactSizeSpliterator.OfLong(sp, proxyEstimateSize);
                LongStream s = StreamSupport.longStream(psp, true);
                terminalOp.accept(s);
                Assert.assertTrue(psp.splits > 0, String.format("Number of splits should be greater that zero when proxyEstimateSize is %s", proxyEstimateSize));
                Assert.assertTrue(psp.prefixSplits > 0, String.format("Number of non-null prefix splits should be greater that zero when proxyEstimateSize is %s", proxyEstimateSize));
                Assert.assertTrue(psp.sizeOnTraversal < 1000, String.format("Size on traversal of last split should be less than the size of the list, %d, when proxyEstimateSize is %s", 1000, proxyEstimateSize));
            }
        }
    }
}
Also used : LongConsumer(java8.util.function.LongConsumer) LambdaTestHelpers.irDoubler(java8.util.stream.LambdaTestHelpers.irDoubler) Arrays(java.util.Arrays) IntConsumer(java8.util.function.IntConsumer) LambdaTestHelpers.countTo(java8.util.stream.LambdaTestHelpers.countTo) LongStreamTestDataProvider(java8.util.stream.LongStreamTestDataProvider) UnaryOperator(java8.util.function.UnaryOperator) Test(org.testng.annotations.Test) LambdaTestHelpers.ipEven(java8.util.stream.LambdaTestHelpers.ipEven) DoubleStreamTestDataProvider(java8.util.stream.DoubleStreamTestDataProvider) TestData(java8.util.stream.TestData) LambdaTestHelpers.lpEven(java8.util.stream.LambdaTestHelpers.lpEven) LambdaTestHelpers.dpEven(java8.util.stream.LambdaTestHelpers.dpEven) Function(java8.util.function.Function) LambdaTestHelpers(java8.util.stream.LambdaTestHelpers) LongStreams(java8.util.stream.LongStreams) Assert(org.testng.Assert) Consumer(java8.util.function.Consumer) StreamTestDataProvider(java8.util.stream.StreamTestDataProvider) LambdaTestHelpers.pEven(java8.util.stream.LambdaTestHelpers.pEven) DoubleStreams(java8.util.stream.DoubleStreams) Spliterator(java8.util.Spliterator) IntStream(java8.util.stream.IntStream) OpTestCase(java8.util.stream.OpTestCase) SpliteratorTestHelper(java8.util.stream.SpliteratorTestHelper) Spliterators(java8.util.Spliterators) LongStream(java8.util.stream.LongStream) StreamSupport(java8.util.stream.StreamSupport) IntStreams(java8.util.stream.IntStreams) List(java.util.List) RefStreams(java8.util.stream.RefStreams) LambdaTestHelpers.mDoubler(java8.util.stream.LambdaTestHelpers.mDoubler) LambdaTestHelpers.permuteStreamFunctions(java8.util.stream.LambdaTestHelpers.permuteStreamFunctions) DoubleStream(java8.util.stream.DoubleStream) Comparator(java.util.Comparator) IntStreamTestDataProvider(java8.util.stream.IntStreamTestDataProvider) DoubleConsumer(java8.util.function.DoubleConsumer) Stream(java8.util.stream.Stream) LongStream(java8.util.stream.LongStream) LongConsumer(java8.util.function.LongConsumer) IntConsumer(java8.util.function.IntConsumer) Consumer(java8.util.function.Consumer) DoubleConsumer(java8.util.function.DoubleConsumer) UnaryOperator(java8.util.function.UnaryOperator) Spliterator(java8.util.Spliterator)

Example 3 with UnaryOperator

use of java8.util.function.UnaryOperator in project streamsupport by stefan-zobel.

the class StreamSpliteratorTest method testIntSplitting.

// 
public void testIntSplitting() {
    List<Consumer<IntStream>> terminalOps = Arrays.asList(s -> s.toArray(), s -> s.forEach(e -> {
    }), s -> s.reduce(java8.lang.Integers::sum));
    List<UnaryOperator<IntStream>> intermediateOps = Arrays.asList(s -> s.parallel(), // The following ensures the wrapping spliterator is tested
    s -> s.map(i -> i).parallel());
    for (int i = 0; i < terminalOps.size(); i++) {
        setContext("termOpIndex", i);
        Consumer<IntStream> terminalOp = terminalOps.get(i);
        for (int j = 0; j < intermediateOps.size(); j++) {
            setContext("intOpIndex", j);
            UnaryOperator<IntStream> intermediateOp = intermediateOps.get(j);
            for (boolean proxyEstimateSize : new boolean[] { false, true }) {
                setContext("proxyEstimateSize", proxyEstimateSize);
                // Size is assumed to be larger than the target size for no splitting
                // @@@ Need way to obtain the target size
                Spliterator.OfInt sp = intermediateOp.apply(IntStreams.range(0, 1000)).spliterator();
                ProxyNoExactSizeSpliterator.OfInt psp = new ProxyNoExactSizeSpliterator.OfInt(sp, proxyEstimateSize);
                IntStream s = StreamSupport.intStream(psp, true);
                terminalOp.accept(s);
                Assert.assertTrue(psp.splits > 0, String.format("Number of splits should be greater that zero when proxyEstimateSize is %s", proxyEstimateSize));
                Assert.assertTrue(psp.prefixSplits > 0, String.format("Number of non-null prefix splits should be greater that zero when proxyEstimateSize is %s", proxyEstimateSize));
                Assert.assertTrue(psp.sizeOnTraversal < 1000, String.format("Size on traversal of last split should be less than the size of the list, %d, when proxyEstimateSize is %s", 1000, proxyEstimateSize));
            }
        }
    }
}
Also used : LongConsumer(java8.util.function.LongConsumer) LambdaTestHelpers.irDoubler(java8.util.stream.LambdaTestHelpers.irDoubler) Arrays(java.util.Arrays) IntConsumer(java8.util.function.IntConsumer) LambdaTestHelpers.countTo(java8.util.stream.LambdaTestHelpers.countTo) LongStreamTestDataProvider(java8.util.stream.LongStreamTestDataProvider) UnaryOperator(java8.util.function.UnaryOperator) Test(org.testng.annotations.Test) LambdaTestHelpers.ipEven(java8.util.stream.LambdaTestHelpers.ipEven) DoubleStreamTestDataProvider(java8.util.stream.DoubleStreamTestDataProvider) TestData(java8.util.stream.TestData) LambdaTestHelpers.lpEven(java8.util.stream.LambdaTestHelpers.lpEven) LambdaTestHelpers.dpEven(java8.util.stream.LambdaTestHelpers.dpEven) Function(java8.util.function.Function) LambdaTestHelpers(java8.util.stream.LambdaTestHelpers) LongStreams(java8.util.stream.LongStreams) Assert(org.testng.Assert) Consumer(java8.util.function.Consumer) StreamTestDataProvider(java8.util.stream.StreamTestDataProvider) LambdaTestHelpers.pEven(java8.util.stream.LambdaTestHelpers.pEven) DoubleStreams(java8.util.stream.DoubleStreams) Spliterator(java8.util.Spliterator) IntStream(java8.util.stream.IntStream) OpTestCase(java8.util.stream.OpTestCase) SpliteratorTestHelper(java8.util.stream.SpliteratorTestHelper) Spliterators(java8.util.Spliterators) LongStream(java8.util.stream.LongStream) StreamSupport(java8.util.stream.StreamSupport) IntStreams(java8.util.stream.IntStreams) List(java.util.List) RefStreams(java8.util.stream.RefStreams) LambdaTestHelpers.mDoubler(java8.util.stream.LambdaTestHelpers.mDoubler) LambdaTestHelpers.permuteStreamFunctions(java8.util.stream.LambdaTestHelpers.permuteStreamFunctions) DoubleStream(java8.util.stream.DoubleStream) Comparator(java.util.Comparator) IntStreamTestDataProvider(java8.util.stream.IntStreamTestDataProvider) DoubleConsumer(java8.util.function.DoubleConsumer) Stream(java8.util.stream.Stream) LongConsumer(java8.util.function.LongConsumer) IntConsumer(java8.util.function.IntConsumer) Consumer(java8.util.function.Consumer) DoubleConsumer(java8.util.function.DoubleConsumer) UnaryOperator(java8.util.function.UnaryOperator) IntStream(java8.util.stream.IntStream) Spliterator(java8.util.Spliterator)

Example 4 with UnaryOperator

use of java8.util.function.UnaryOperator in project streamsupport by stefan-zobel.

the class SequentialOpTest method testLazy.

@SuppressWarnings("unchecked")
@Test(dataProvider = "StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class, groups = { "serialization-hostile" })
public void testLazy(String name, TestData.OfRef<Integer> data) {
    Function<Integer, Integer> id = LambdaTestHelpers.identity();
    AtomicInteger counter = new AtomicInteger();
    Supplier<Stream<Integer>>[] suppliers = new Supplier[] { () -> data.stream(), () -> data.parallelStream() };
    UnaryOperator<Stream<Integer>>[] configs = new UnaryOperator[] { (UnaryOperator<Stream<Integer>>) s -> s.peek(e -> {
        counter.incrementAndGet();
    }), (UnaryOperator<Stream<Integer>>) s -> s.map(id).peek(e -> {
        counter.incrementAndGet();
    }).sequential().map(id), (UnaryOperator<Stream<Integer>>) s -> s.map(id).peek(e -> {
        counter.incrementAndGet();
    }).parallel().map(id), (UnaryOperator<Stream<Integer>>) s -> s.sequential().map(id).peek(e -> {
        counter.incrementAndGet();
    }).map(id), (UnaryOperator<Stream<Integer>>) s -> s.parallel().map(id).peek(e -> {
        counter.incrementAndGet();
    }).map(id) };
    for (int i = 0; i < suppliers.length; i++) {
        setContext("supplierIndex", i);
        Supplier<Stream<Integer>> supp = suppliers[i];
        for (int j = 0; j < configs.length; j++) {
            setContext("configIndex", j);
            UnaryOperator<Stream<Integer>> config = configs[j];
            counter.set(0);
            Stream<Integer> stream = config.apply(supp.get());
            assertEquals(0, counter.get());
            Iterator<Integer> iterator = stream.iterator();
            assertEquals(0, counter.get());
            if (iterator.hasNext())
                iterator.next();
            assertTrue(data.size() == 0 || counter.get() > 0);
            counter.set(0);
            stream = config.apply(supp.get());
            Spliterator<Integer> spliterator = stream.spliterator();
            assertEquals(0, counter.get());
            spliterator.forEachRemaining(e -> {
            });
            assertTrue(data.size() == 0 || counter.get() > 0);
        }
    }
}
Also used : Iterator(java.util.Iterator) Spliterator(java8.util.Spliterator) OpTestCase(java8.util.stream.OpTestCase) Test(org.testng.annotations.Test) UnaryOperator(java8.util.function.UnaryOperator) TestData(java8.util.stream.TestData) Function(java8.util.function.Function) Supplier(java8.util.function.Supplier) LambdaTestHelpers(java8.util.stream.LambdaTestHelpers) Comparators(java8.util.Comparators) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) StreamTestDataProvider(java8.util.stream.StreamTestDataProvider) Stream(java8.util.stream.Stream) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Supplier(java8.util.function.Supplier) Stream(java8.util.stream.Stream) UnaryOperator(java8.util.function.UnaryOperator) Test(org.testng.annotations.Test)

Aggregations

Spliterator (java8.util.Spliterator)4 Function (java8.util.function.Function)4 UnaryOperator (java8.util.function.UnaryOperator)4 LambdaTestHelpers (java8.util.stream.LambdaTestHelpers)4 OpTestCase (java8.util.stream.OpTestCase)4 Stream (java8.util.stream.Stream)4 StreamTestDataProvider (java8.util.stream.StreamTestDataProvider)4 TestData (java8.util.stream.TestData)4 Test (org.testng.annotations.Test)4 Arrays (java.util.Arrays)3 Comparator (java.util.Comparator)3 List (java.util.List)3 Spliterators (java8.util.Spliterators)3 Consumer (java8.util.function.Consumer)3 DoubleConsumer (java8.util.function.DoubleConsumer)3 IntConsumer (java8.util.function.IntConsumer)3 LongConsumer (java8.util.function.LongConsumer)3 DoubleStream (java8.util.stream.DoubleStream)3 DoubleStreamTestDataProvider (java8.util.stream.DoubleStreamTestDataProvider)3 DoubleStreams (java8.util.stream.DoubleStreams)3