Search in sources :

Example 1 with IntStream

use of java8.util.stream.IntStream in project streamsupport by stefan-zobel.

the class ConcatTest method assertIntConcat.

private void assertIntConcat(Stream<Integer> s1, Stream<Integer> s2, boolean parallel, boolean ordered) {
    IntStream result = IntStreams.concat(s1.mapToInt(Integer::intValue), s2.mapToInt(Integer::intValue));
    assertEquals(result.isParallel(), parallel);
    assertConcatContent(result.spliterator(), ordered, StreamSupport.stream(expected).mapToInt(Integer::intValue).spliterator());
}
Also used : IntStream(java8.util.stream.IntStream)

Example 2 with IntStream

use of java8.util.stream.IntStream in project streamsupport by stefan-zobel.

the class SplittableRandomTest method intsDataProvider.

@DataProvider(name = "ints")
public static Object[][] intsDataProvider() {
    List<Object[]> data = new ArrayList<>();
    // Function to create a stream using a RandomBoxedSpliterator
    Function<Function<SplittableRandom, Integer>, IntStream> rbsf = sf -> StreamSupport.stream(new RandomBoxedSpliterator<>(new SplittableRandom(), 0, SIZE, sf), false).mapToInt(i -> i);
    // Unbounded
    data.add(new Object[] { TestData.Factory.ofIntSupplier(String.format("new SplittableRandom().ints().limit(%d)", SIZE), () -> new SplittableRandom().ints().limit(SIZE)), randomAsserter(SIZE, Integer.MAX_VALUE, 0) });
    data.add(new Object[] { TestData.Factory.ofIntSupplier(String.format("new SplittableRandom().ints(%d)", SIZE), () -> new SplittableRandom().ints(SIZE)), randomAsserter(SIZE, Integer.MAX_VALUE, 0) });
    data.add(new Object[] { TestData.Factory.ofIntSupplier(String.format("new RandomBoxedSpliterator(0, %d, sr -> sr.nextInt())", SIZE), () -> rbsf.apply(sr -> sr.nextInt())), randomAsserter(SIZE, Integer.MAX_VALUE, 0) });
    for (int b : BOUNDS) {
        for (int o : ORIGINS) {
            final int origin = o;
            final int bound = b;
            data.add(new Object[] { TestData.Factory.ofIntSupplier(String.format("new SplittableRandom().ints(%d, %d).limit(%d)", origin, bound, SIZE), () -> new SplittableRandom().ints(origin, bound).limit(SIZE)), randomAsserter(SIZE, origin, bound) });
            data.add(new Object[] { TestData.Factory.ofIntSupplier(String.format("new SplittableRandom().ints(%d, %d, %d)", SIZE, origin, bound), () -> new SplittableRandom().ints(SIZE, origin, bound)), randomAsserter(SIZE, origin, bound) });
            if (origin == 0) {
                data.add(new Object[] { TestData.Factory.ofIntSupplier(String.format("new RandomBoxedSpliterator(0, %d, sr -> sr.nextInt(%d))", SIZE, bound), () -> rbsf.apply(sr -> sr.nextInt(bound))), randomAsserter(SIZE, origin, bound) });
            }
            data.add(new Object[] { TestData.Factory.ofIntSupplier(String.format("new RandomBoxedSpliterator(0, %d, sr -> sr.nextInt(%d, %d))", SIZE, origin, bound), () -> rbsf.apply(sr -> sr.nextInt(origin, bound))), randomAsserter(SIZE, origin, bound) });
        }
    }
    return data.toArray(new Object[0][]);
}
Also used : DataProvider(org.testng.annotations.DataProvider) Spliterator(java8.util.Spliterator) IntStream(java8.util.stream.IntStream) OpTestCase(java8.util.stream.OpTestCase) Set(java.util.Set) Test(org.testng.annotations.Test) Spliterators(java8.util.Spliterators) SplittableRandom(java8.util.SplittableRandom) LongStreamTestScenario(java8.util.stream.LongStreamTestScenario) LongStream(java8.util.stream.LongStream) StreamSupport(java8.util.stream.StreamSupport) TestData(java8.util.stream.TestData) Function(java8.util.function.Function) IntStreamTestScenario(java8.util.stream.IntStreamTestScenario) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) List(java.util.List) Consumer(java8.util.function.Consumer) DoubleStream(java8.util.stream.DoubleStream) Comparator(java.util.Comparator) DoubleStreamTestScenario(java8.util.stream.DoubleStreamTestScenario) Function(java8.util.function.Function) ArrayList(java.util.ArrayList) IntStream(java8.util.stream.IntStream) SplittableRandom(java8.util.SplittableRandom) DataProvider(org.testng.annotations.DataProvider)

Example 3 with IntStream

use of java8.util.stream.IntStream 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 IntStream

use of java8.util.stream.IntStream in project streamsupport by stefan-zobel.

the class WhileOpStatefulTest method testWhileMulti.

private void testWhileMulti(Map<String, Supplier<Stream<Integer>>> sources, Consumer<Stream<Integer>> mRef, Consumer<IntStream> mInt, Consumer<LongStream> mLong, Consumer<DoubleStream> mDouble) {
    Map<String, Function<Stream<Integer>, Stream<Integer>>> transforms = new HashMap<>();
    transforms.put("Stream.sequential()", s -> {
        BooleanSupplier isWithinExecutionPeriod = within(System.currentTimeMillis(), EXECUTION_TIME_LIMIT);
        return s.peek(e -> {
            if (!isWithinExecutionPeriod.getAsBoolean()) {
                throw new RuntimeException();
            }
        });
    });
    transforms.put("Stream.parallel()", s -> {
        BooleanSupplier isWithinExecutionPeriod = within(System.currentTimeMillis(), EXECUTION_TIME_LIMIT);
        return s.parallel().peek(e -> {
            if (!isWithinExecutionPeriod.getAsBoolean()) {
                throw new RuntimeException();
            }
        });
    });
    Map<String, Consumer<Stream<Integer>>> actions = new HashMap<>();
    actions.put("Ref", mRef);
    actions.put("Int", s -> mInt.accept(s.mapToInt(e -> e)));
    actions.put("Long", s -> mLong.accept(s.mapToLong(e -> e)));
    actions.put("Double", s -> mDouble.accept(s.mapToDouble(e -> e)));
    actions.put("Ref using defaults", s -> mRef.accept(DefaultMethodStreams.delegateTo(s)));
    actions.put("Int using defaults", s -> mInt.accept(DefaultMethodStreams.delegateTo(s.mapToInt(e -> e))));
    actions.put("Long using defaults", s -> mLong.accept(DefaultMethodStreams.delegateTo(s.mapToLong(e -> e))));
    actions.put("Double using defaults", s -> mDouble.accept(DefaultMethodStreams.delegateTo(s.mapToDouble(e -> e))));
    for (Map.Entry<String, Supplier<Stream<Integer>>> s : sources.entrySet()) {
        setContext("source", s.getKey());
        for (Map.Entry<String, Function<Stream<Integer>, Stream<Integer>>> t : transforms.entrySet()) {
            setContext("transform", t.getKey());
            for (Map.Entry<String, Consumer<Stream<Integer>>> a : actions.entrySet()) {
                setContext("shape", a.getKey());
                Stream<Integer> stream = s.getValue().get();
                stream = t.getValue().apply(stream);
                a.getValue().accept(stream);
            }
        }
    }
}
Also used : Collector(java8.util.stream.Collector) IntStream(java8.util.stream.IntStream) OpTestCase(java8.util.stream.OpTestCase) Test(org.testng.annotations.Test) HashMap(java.util.HashMap) LongStream(java8.util.stream.LongStream) StreamSupport(java8.util.stream.StreamSupport) Function(java8.util.function.Function) Supplier(java8.util.function.Supplier) IntStreams(java8.util.stream.IntStreams) TimeUnit(java.util.concurrent.TimeUnit) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) RefStreams(java8.util.stream.RefStreams) Collectors.toCollection(java8.util.stream.Collectors.toCollection) Consumer(java8.util.function.Consumer) Map(java.util.Map) DefaultMethodStreams(java8.util.stream.DefaultMethodStreams) BooleanSupplier(java8.util.function.BooleanSupplier) DoubleStream(java8.util.stream.DoubleStream) LinkedList(java.util.LinkedList) Stream(java8.util.stream.Stream) HashMap(java.util.HashMap) Function(java8.util.function.Function) Consumer(java8.util.function.Consumer) Supplier(java8.util.function.Supplier) BooleanSupplier(java8.util.function.BooleanSupplier) BooleanSupplier(java8.util.function.BooleanSupplier) HashMap(java.util.HashMap) Map(java.util.Map)

Example 5 with IntStream

use of java8.util.stream.IntStream in project streamsupport by stefan-zobel.

the class WhileOpTest method testIntDefaultClose.

@Test(groups = { "serialization-hostile" })
public void testIntDefaultClose() {
    AtomicBoolean isClosed = new AtomicBoolean();
    IntStream s = IntStreams.of(1, 2, 3).onClose(() -> isClosed.set(true));
    IntStream ds = null;
    try {
        ds = DefaultMethodStreams.delegateTo(s).takeWhile(e -> e < 3);
        ds.count();
    } finally {
        if (ds != null) {
            ds.close();
        }
    }
    assertTrue(isClosed.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.testng.annotations.Test) HashMap(java.util.HashMap) Iterables(java8.lang.Iterables) Predicate(java8.util.function.Predicate) TestData(java8.util.stream.TestData) Function(java8.util.function.Function) ArrayList(java.util.ArrayList) LambdaTestHelpers(java8.util.stream.LambdaTestHelpers) LongStreams(java8.util.stream.LongStreams) Map(java.util.Map) StreamTestDataProvider(java8.util.stream.StreamTestDataProvider) LinkedHashSet(java.util.LinkedHashSet) DoubleStreams(java8.util.stream.DoubleStreams) Collection(java.util.Collection) IntStream(java8.util.stream.IntStream) OpTestCase(java8.util.stream.OpTestCase) Set(java.util.Set) LongStream(java8.util.stream.LongStream) IntStreams(java8.util.stream.IntStreams) List(java.util.List) RefStreams(java8.util.stream.RefStreams) DefaultMethodStreams(java8.util.stream.DefaultMethodStreams) DoubleStream(java8.util.stream.DoubleStream) Stream(java8.util.stream.Stream) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IntStream(java8.util.stream.IntStream) Test(org.testng.annotations.Test)

Aggregations

IntStream (java8.util.stream.IntStream)6 OpTestCase (java8.util.stream.OpTestCase)5 Test (org.testng.annotations.Test)5 List (java.util.List)4 Function (java8.util.function.Function)4 DoubleStream (java8.util.stream.DoubleStream)4 IntStreams (java8.util.stream.IntStreams)4 LongStream (java8.util.stream.LongStream)4 TestData (java8.util.stream.TestData)4 Consumer (java8.util.function.Consumer)3 LambdaTestHelpers (java8.util.stream.LambdaTestHelpers)3 RefStreams (java8.util.stream.RefStreams)3 Stream (java8.util.stream.Stream)3 StreamSupport (java8.util.stream.StreamSupport)3 ArrayList (java.util.ArrayList)2 Comparator (java.util.Comparator)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Set (java.util.Set)2 OptionalInt (java8.util.OptionalInt)2