Search in sources :

Example 1 with DoubleStream

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

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

the class TestDoubleSumAverage method testForCompensation.

/**
 * Compute the sum and average of a sequence of double values in
 * various ways and report an error if naive summation is used.
 */
private static int testForCompensation() {
    int failures = 0;
    /*
         * The exact sum of the test stream is 1 + 1e6*ulp(1.0) but a
         * naive summation algorithm will return 1.0 since (1.0 +
         * ulp(1.0)/2) will round to 1.0 again.
         */
    final double base = 1.0;
    final double increment = Math.ulp(base) / 2.0;
    final int count = 1000001;
    double expectedSum = base + (increment * (count - 1));
    double expectedAvg = expectedSum / count;
    // Factory for double a stream of [base, increment, ..., increment] limited to a size of count
    Supplier<DoubleStream> ds = new Supplier<DoubleStream>() {

        @Override
        public DoubleStream get() {
            return DoubleStreams.iterate(base, new DoubleUnaryOperator() {

                @Override
                public double applyAsDouble(double e) {
                    return increment;
                }
            }).limit(count);
        }
    };
    DoubleSummaryStatistics stats = ds.get().collect(new Supplier<DoubleSummaryStatistics>() {

        @Override
        public DoubleSummaryStatistics get() {
            return new DoubleSummaryStatistics();
        }
    }, (ObjDoubleConsumer<DoubleSummaryStatistics>) new ObjDoubleConsumer<DoubleSummaryStatistics>() {

        @Override
        public void accept(DoubleSummaryStatistics doubleSummaryStatistics, double v) {
            doubleSummaryStatistics.accept(v);
        }
    }, (BiConsumer<DoubleSummaryStatistics, DoubleSummaryStatistics>) new BiConsumer<DoubleSummaryStatistics, DoubleSummaryStatistics>() {

        @Override
        public void accept(DoubleSummaryStatistics doubleSummaryStatistics, DoubleSummaryStatistics doubleSummaryStatistics2) {
            doubleSummaryStatistics.combine(doubleSummaryStatistics2);
        }
    });
    failures += compareUlpDifference(expectedSum, stats.getSum(), 3);
    failures += compareUlpDifference(expectedAvg, stats.getAverage(), 3);
    failures += compareUlpDifference(expectedSum, ds.get().sum(), 3);
    failures += compareUlpDifference(expectedAvg, ds.get().average().getAsDouble(), 3);
    failures += compareUlpDifference(expectedSum, ds.get().boxed().collect(Collectors.summingDouble(new ToDoubleFunction<Double>() {

        @Override
        public double applyAsDouble(Double d) {
            return d;
        }
    })), 3);
    failures += compareUlpDifference(expectedAvg, ds.get().boxed().collect(Collectors.averagingDouble(new ToDoubleFunction<Double>() {

        @Override
        public double applyAsDouble(Double d) {
            return d;
        }
    })), 3);
    return failures;
}
Also used : DoubleSummaryStatistics(java8.util.DoubleSummaryStatistics) Double(java.lang.Double)

Example 3 with DoubleStream

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

the class ConcatTest method assertDoubleConcat.

private void assertDoubleConcat(Stream<Integer> s1, Stream<Integer> s2, boolean parallel, boolean ordered) {
    DoubleStream result = DoubleStreams.concat(s1.mapToDouble(Integer::doubleValue), s2.mapToDouble(Integer::doubleValue));
    assertEquals(result.isParallel(), parallel);
    assertConcatContent(result.spliterator(), ordered, StreamSupport.stream(expected).mapToDouble(Integer::doubleValue).spliterator());
}
Also used : DoubleStream(java8.util.stream.DoubleStream)

Example 4 with DoubleStream

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

the class SplittableRandomTest method doublesDataProvider.

@DataProvider(name = "doubles")
public static Object[][] doublesDataProvider() {
    List<Object[]> data = new ArrayList<>();
    // Function to create a stream using a RandomBoxedSpliterator
    Function<Function<SplittableRandom, Double>, DoubleStream> rbsf = sf -> StreamSupport.stream(new RandomBoxedSpliterator<>(new SplittableRandom(), 0, SIZE, sf), false).mapToDouble(i -> i);
    // Unbounded
    data.add(new Object[] { TestData.Factory.ofDoubleSupplier(String.format("new SplittableRandom().doubles().limit(%d)", SIZE), () -> new SplittableRandom().doubles().limit(SIZE)), randomAsserter(SIZE, Double.MAX_VALUE, 0d) });
    data.add(new Object[] { TestData.Factory.ofDoubleSupplier(String.format("new SplittableRandom().doubles(%d)", SIZE), () -> new SplittableRandom().doubles(SIZE)), randomAsserter(SIZE, Double.MAX_VALUE, 0d) });
    data.add(new Object[] { TestData.Factory.ofDoubleSupplier(String.format("new RandomBoxedSpliterator(0, %d, sr -> sr.nextDouble())", SIZE), () -> rbsf.apply(sr -> sr.nextDouble())), randomAsserter(SIZE, Double.MAX_VALUE, 0d) });
    for (int b : BOUNDS) {
        for (int o : ORIGINS) {
            final double origin = o;
            final double bound = b;
            data.add(new Object[] { TestData.Factory.ofDoubleSupplier(String.format("new SplittableRandom().doubles(%f, %f).limit(%d)", origin, bound, SIZE), () -> new SplittableRandom().doubles(origin, bound).limit(SIZE)), randomAsserter(SIZE, origin, bound) });
            data.add(new Object[] { TestData.Factory.ofDoubleSupplier(String.format("new SplittableRandom().doubles(%d, %f, %f)", SIZE, origin, bound), () -> new SplittableRandom().doubles(SIZE, origin, bound)), randomAsserter(SIZE, origin, bound) });
            if (origin == 0) {
                data.add(new Object[] { TestData.Factory.ofDoubleSupplier(String.format("new RandomBoxedSpliterator(0, %d, sr -> sr.nextDouble(%f))", SIZE, bound), () -> rbsf.apply(sr -> sr.nextDouble(bound))), randomAsserter(SIZE, origin, bound) });
            }
            data.add(new Object[] { TestData.Factory.ofDoubleSupplier(String.format("new RandomBoxedSpliterator(0, %d, sr -> sr.nextDouble(%f, %f))", SIZE, origin, bound), () -> rbsf.apply(sr -> sr.nextDouble(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) DoubleStream(java8.util.stream.DoubleStream) SplittableRandom(java8.util.SplittableRandom) DataProvider(org.testng.annotations.DataProvider)

Example 5 with DoubleStream

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

the class TestDoubleSumAverage method testNonfiniteSum.

private static int testNonfiniteSum() {
    int failures = 0;
    Map<Supplier<DoubleStream>, Double> testCases = new LinkedHashMap<Supplier<DoubleStream>, Double>();
    testCases.put(new Supplier<DoubleStream>() {

        @Override
        public DoubleStream get() {
            return DoubleStreams.of(MAX_VALUE, MAX_VALUE);
        }
    }, POSITIVE_INFINITY);
    testCases.put(new Supplier<DoubleStream>() {

        @Override
        public DoubleStream get() {
            return DoubleStreams.of(-MAX_VALUE, -MAX_VALUE);
        }
    }, NEGATIVE_INFINITY);
    testCases.put(new Supplier<DoubleStream>() {

        @Override
        public DoubleStream get() {
            return DoubleStreams.of(1.0d, POSITIVE_INFINITY, 1.0d);
        }
    }, POSITIVE_INFINITY);
    testCases.put(new Supplier<DoubleStream>() {

        @Override
        public DoubleStream get() {
            return DoubleStreams.of(POSITIVE_INFINITY);
        }
    }, POSITIVE_INFINITY);
    testCases.put(new Supplier<DoubleStream>() {

        @Override
        public DoubleStream get() {
            return DoubleStreams.of(POSITIVE_INFINITY, POSITIVE_INFINITY);
        }
    }, POSITIVE_INFINITY);
    testCases.put(new Supplier<DoubleStream>() {

        @Override
        public DoubleStream get() {
            return DoubleStreams.of(POSITIVE_INFINITY, POSITIVE_INFINITY, 0.0);
        }
    }, POSITIVE_INFINITY);
    testCases.put(new Supplier<DoubleStream>() {

        @Override
        public DoubleStream get() {
            return DoubleStreams.of(1.0d, NEGATIVE_INFINITY, 1.0d);
        }
    }, NEGATIVE_INFINITY);
    testCases.put(new Supplier<DoubleStream>() {

        @Override
        public DoubleStream get() {
            return DoubleStreams.of(NEGATIVE_INFINITY);
        }
    }, NEGATIVE_INFINITY);
    testCases.put(new Supplier<DoubleStream>() {

        @Override
        public DoubleStream get() {
            return DoubleStreams.of(NEGATIVE_INFINITY, NEGATIVE_INFINITY);
        }
    }, NEGATIVE_INFINITY);
    testCases.put(new Supplier<DoubleStream>() {

        @Override
        public DoubleStream get() {
            return DoubleStreams.of(NEGATIVE_INFINITY, NEGATIVE_INFINITY, 0.0);
        }
    }, NEGATIVE_INFINITY);
    testCases.put(new Supplier<DoubleStream>() {

        @Override
        public DoubleStream get() {
            return DoubleStreams.of(1.0d, NaN, 1.0d);
        }
    }, NaN);
    testCases.put(new Supplier<DoubleStream>() {

        @Override
        public DoubleStream get() {
            return DoubleStreams.of(NaN);
        }
    }, NaN);
    testCases.put(new Supplier<DoubleStream>() {

        @Override
        public DoubleStream get() {
            return DoubleStreams.of(1.0d, NEGATIVE_INFINITY, POSITIVE_INFINITY, 1.0d);
        }
    }, NaN);
    testCases.put(new Supplier<DoubleStream>() {

        @Override
        public DoubleStream get() {
            return DoubleStreams.of(1.0d, POSITIVE_INFINITY, NEGATIVE_INFINITY, 1.0d);
        }
    }, NaN);
    testCases.put(new Supplier<DoubleStream>() {

        @Override
        public DoubleStream get() {
            return DoubleStreams.of(POSITIVE_INFINITY, NaN);
        }
    }, NaN);
    testCases.put(new Supplier<DoubleStream>() {

        @Override
        public DoubleStream get() {
            return DoubleStreams.of(NEGATIVE_INFINITY, NaN);
        }
    }, NaN);
    testCases.put(new Supplier<DoubleStream>() {

        @Override
        public DoubleStream get() {
            return DoubleStreams.of(NaN, POSITIVE_INFINITY);
        }
    }, NaN);
    testCases.put(new Supplier<DoubleStream>() {

        @Override
        public DoubleStream get() {
            return DoubleStreams.of(NaN, NEGATIVE_INFINITY);
        }
    }, NaN);
    for (Map.Entry<Supplier<DoubleStream>, Double> testCase : testCases.entrySet()) {
        Supplier<DoubleStream> ds = testCase.getKey();
        double expected = testCase.getValue();
        DoubleSummaryStatistics stats = ds.get().collect(new Supplier<DoubleSummaryStatistics>() {

            @Override
            public DoubleSummaryStatistics get() {
                return new DoubleSummaryStatistics();
            }
        }, (ObjDoubleConsumer<DoubleSummaryStatistics>) new ObjDoubleConsumer<DoubleSummaryStatistics>() {

            @Override
            public void accept(DoubleSummaryStatistics doubleSummaryStatistics, double v) {
                doubleSummaryStatistics.accept(v);
            }
        }, (BiConsumer<DoubleSummaryStatistics, DoubleSummaryStatistics>) new BiConsumer<DoubleSummaryStatistics, DoubleSummaryStatistics>() {

            @Override
            public void accept(DoubleSummaryStatistics doubleSummaryStatistics, DoubleSummaryStatistics doubleSummaryStatistics2) {
                doubleSummaryStatistics.combine(doubleSummaryStatistics2);
            }
        });
        failures += compareUlpDifference(expected, stats.getSum(), 0);
        failures += compareUlpDifference(expected, stats.getAverage(), 0);
        failures += compareUlpDifference(expected, ds.get().sum(), 0);
        failures += compareUlpDifference(expected, ds.get().average().getAsDouble(), 0);
        failures += compareUlpDifference(expected, ds.get().boxed().collect(Collectors.summingDouble(new ToDoubleFunction<Double>() {

            @Override
            public double applyAsDouble(Double d) {
                return d;
            }
        })), 0);
        failures += compareUlpDifference(expected, ds.get().boxed().collect(Collectors.averagingDouble(new ToDoubleFunction<Double>() {

            @Override
            public double applyAsDouble(Double d) {
                return d;
            }
        })), 0);
    }
    return failures;
}
Also used : DoubleSummaryStatistics(java8.util.DoubleSummaryStatistics) Double(java.lang.Double)

Aggregations

DoubleStream (java8.util.stream.DoubleStream)5 List (java.util.List)4 Function (java8.util.function.Function)4 IntStream (java8.util.stream.IntStream)4 LongStream (java8.util.stream.LongStream)4 OpTestCase (java8.util.stream.OpTestCase)4 Test (org.testng.annotations.Test)4 Consumer (java8.util.function.Consumer)3 IntStreams (java8.util.stream.IntStreams)3 StreamSupport (java8.util.stream.StreamSupport)3 TestData (java8.util.stream.TestData)3 Double (java.lang.Double)2 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 DoubleSummaryStatistics (java8.util.DoubleSummaryStatistics)2 Spliterator (java8.util.Spliterator)2 Spliterators (java8.util.Spliterators)2