Search in sources :

Example 21 with Supplier

use of java8.util.function.Supplier 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

Test (org.testng.annotations.Test)16 Collection (java.util.Collection)14 LinkedList (java.util.LinkedList)13 ThreadLocalRandom (java8.util.concurrent.ThreadLocalRandom)13 Supplier (java8.util.function.Supplier)13 ArrayList (java.util.ArrayList)12 List (java.util.List)12 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)12 ArrayDeque (java.util.ArrayDeque)11 Deque (java.util.Deque)11 HashSet (java.util.HashSet)11 BlockingDeque (java.util.concurrent.BlockingDeque)11 LinkedBlockingDeque (java.util.concurrent.LinkedBlockingDeque)11 Spliterator (java8.util.Spliterator)11 StreamSupport (java8.util.stream.StreamSupport)11 Arrays (java.util.Arrays)10 ConcurrentModificationException (java.util.ConcurrentModificationException)10 Iterator (java.util.Iterator)10 Set (java.util.Set)10 ConcurrentLinkedQueue (java.util.concurrent.ConcurrentLinkedQueue)10