提交 7f860593 编写于 作者: P psandoz
上级 b4fbda77
此差异已折叠。
/*
* Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
import org.testng.Assert;
import org.testng.annotations.Test;
import java.util.SplittableRandom;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.LongAdder;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotNull;
import static org.testng.AssertJUnit.assertTrue;
/**
* @test
* @run testng SplittableRandomTest
* @run testng/othervm -Djava.util.secureRandomSeed=true SplittableRandomTest
* @summary test methods on SplittableRandom
*/
@Test
public class SplittableRandomTest {
// Note: this test was copied from the 166 TCK SplittableRandomTest test
// and modified to be a TestNG test
/*
* Testing coverage notes:
*
* 1. Many of the test methods are adapted from ThreadLocalRandomTest.
*
* 2. These tests do not check for random number generator quality.
* But we check for minimal API compliance by requiring that
* repeated calls to nextX methods, up to NCALLS tries, produce at
* least two distinct results. (In some possible universe, a
* "correct" implementation might fail, but the odds are vastly
* less than that of encountering a hardware failure while running
* the test.) For bounded nextX methods, we sample various
* intervals across multiples of primes. In other tests, we repeat
* under REPS different values.
*/
// max numbers of calls to detect getting stuck on one value
static final int NCALLS = 10000;
// max sampled int bound
static final int MAX_INT_BOUND = (1 << 28);
// max sampled long bound
static final long MAX_LONG_BOUND = (1L << 42);
// Number of replications for other checks
static final int REPS = 20;
/**
* Repeated calls to nextInt produce at least two distinct results
*/
public void testNextInt() {
SplittableRandom sr = new SplittableRandom();
int f = sr.nextInt();
int i = 0;
while (i < NCALLS && sr.nextInt() == f)
++i;
assertTrue(i < NCALLS);
}
/**
* Repeated calls to nextLong produce at least two distinct results
*/
public void testNextLong() {
SplittableRandom sr = new SplittableRandom();
long f = sr.nextLong();
int i = 0;
while (i < NCALLS && sr.nextLong() == f)
++i;
assertTrue(i < NCALLS);
}
/**
* Repeated calls to nextDouble produce at least two distinct results
*/
public void testNextDouble() {
SplittableRandom sr = new SplittableRandom();
double f = sr.nextDouble();
int i = 0;
while (i < NCALLS && sr.nextDouble() == f)
++i;
assertTrue(i < NCALLS);
}
/**
* Two SplittableRandoms created with the same seed produce the
* same values for nextLong.
*/
public void testSeedConstructor() {
for (long seed = 2; seed < MAX_LONG_BOUND; seed += 15485863) {
SplittableRandom sr1 = new SplittableRandom(seed);
SplittableRandom sr2 = new SplittableRandom(seed);
for (int i = 0; i < REPS; ++i)
assertEquals(sr1.nextLong(), sr2.nextLong());
}
}
/**
* A SplittableRandom produced by split() of a default-constructed
* SplittableRandom generates a different sequence
*/
public void testSplit1() {
SplittableRandom sr = new SplittableRandom();
for (int reps = 0; reps < REPS; ++reps) {
SplittableRandom sc = sr.split();
int i = 0;
while (i < NCALLS && sr.nextLong() == sc.nextLong())
++i;
assertTrue(i < NCALLS);
}
}
/**
* A SplittableRandom produced by split() of a seeded-constructed
* SplittableRandom generates a different sequence
*/
public void testSplit2() {
SplittableRandom sr = new SplittableRandom(12345);
for (int reps = 0; reps < REPS; ++reps) {
SplittableRandom sc = sr.split();
int i = 0;
while (i < NCALLS && sr.nextLong() == sc.nextLong())
++i;
assertTrue(i < NCALLS);
}
}
/**
* nextInt(negative) throws IllegalArgumentException
*/
@Test(expectedExceptions = IllegalArgumentException.class)
public void testNextIntBoundedNeg() {
SplittableRandom sr = new SplittableRandom();
int f = sr.nextInt(-17);
}
/**
* nextInt(least >= bound) throws IllegalArgumentException
*/
@Test(expectedExceptions = IllegalArgumentException.class)
public void testNextIntBadBounds() {
SplittableRandom sr = new SplittableRandom();
int f = sr.nextInt(17, 2);
}
/**
* nextInt(bound) returns 0 <= value < bound;
* repeated calls produce at least two distinct results
*/
public void testNextIntBounded() {
SplittableRandom sr = new SplittableRandom();
// sample bound space across prime number increments
for (int bound = 2; bound < MAX_INT_BOUND; bound += 524959) {
int f = sr.nextInt(bound);
assertTrue(0 <= f && f < bound);
int i = 0;
int j;
while (i < NCALLS &&
(j = sr.nextInt(bound)) == f) {
assertTrue(0 <= j && j < bound);
++i;
}
assertTrue(i < NCALLS);
}
}
/**
* nextInt(least, bound) returns least <= value < bound;
* repeated calls produce at least two distinct results
*/
public void testNextIntBounded2() {
SplittableRandom sr = new SplittableRandom();
for (int least = -15485863; least < MAX_INT_BOUND; least += 524959) {
for (int bound = least + 2; bound > least && bound < MAX_INT_BOUND; bound += 49979687) {
int f = sr.nextInt(least, bound);
assertTrue(least <= f && f < bound);
int i = 0;
int j;
while (i < NCALLS &&
(j = sr.nextInt(least, bound)) == f) {
assertTrue(least <= j && j < bound);
++i;
}
assertTrue(i < NCALLS);
}
}
}
/**
* nextLong(negative) throws IllegalArgumentException
*/
@Test(expectedExceptions = IllegalArgumentException.class)
public void testNextLongBoundedNeg() {
SplittableRandom sr = new SplittableRandom();
long f = sr.nextLong(-17);
}
/**
* nextLong(least >= bound) throws IllegalArgumentException
*/
@Test(expectedExceptions = IllegalArgumentException.class)
public void testNextLongBadBounds() {
SplittableRandom sr = new SplittableRandom();
long f = sr.nextLong(17, 2);
}
/**
* nextLong(bound) returns 0 <= value < bound;
* repeated calls produce at least two distinct results
*/
public void testNextLongBounded() {
SplittableRandom sr = new SplittableRandom();
for (long bound = 2; bound < MAX_LONG_BOUND; bound += 15485863) {
long f = sr.nextLong(bound);
assertTrue(0 <= f && f < bound);
int i = 0;
long j;
while (i < NCALLS &&
(j = sr.nextLong(bound)) == f) {
assertTrue(0 <= j && j < bound);
++i;
}
assertTrue(i < NCALLS);
}
}
/**
* nextLong(least, bound) returns least <= value < bound;
* repeated calls produce at least two distinct results
*/
public void testNextLongBounded2() {
SplittableRandom sr = new SplittableRandom();
for (long least = -86028121; least < MAX_LONG_BOUND; least += 982451653L) {
for (long bound = least + 2; bound > least && bound < MAX_LONG_BOUND; bound += Math.abs(bound * 7919)) {
long f = sr.nextLong(least, bound);
assertTrue(least <= f && f < bound);
int i = 0;
long j;
while (i < NCALLS &&
(j = sr.nextLong(least, bound)) == f) {
assertTrue(least <= j && j < bound);
++i;
}
assertTrue(i < NCALLS);
}
}
}
/**
* nextDouble(least, bound) returns least <= value < bound;
* repeated calls produce at least two distinct results
*/
public void testNextDoubleBounded2() {
SplittableRandom sr = new SplittableRandom();
for (double least = 0.0001; least < 1.0e20; least *= 8) {
for (double bound = least * 1.001; bound < 1.0e20; bound *= 16) {
double f = sr.nextDouble(least, bound);
assertTrue(least <= f && f < bound);
int i = 0;
double j;
while (i < NCALLS &&
(j = sr.nextDouble(least, bound)) == f) {
assertTrue(least <= j && j < bound);
++i;
}
assertTrue(i < NCALLS);
}
}
}
/**
* Invoking sized ints, long, doubles, with negative sizes throws
* IllegalArgumentException
*/
public void testBadStreamSize() {
SplittableRandom r = new SplittableRandom();
executeAndCatchIAE(() -> r.ints(-1L));
executeAndCatchIAE(() -> r.ints(-1L, 2, 3));
executeAndCatchIAE(() -> r.longs(-1L));
executeAndCatchIAE(() -> r.longs(-1L, -1L, 1L));
executeAndCatchIAE(() -> r.doubles(-1L));
executeAndCatchIAE(() -> r.doubles(-1L, .5, .6));
}
/**
* Invoking bounded ints, long, doubles, with illegal bounds throws
* IllegalArgumentException
*/
public void testBadStreamBounds() {
SplittableRandom r = new SplittableRandom();
executeAndCatchIAE(() -> r.ints(2, 1));
executeAndCatchIAE(() -> r.ints(10, 42, 42));
executeAndCatchIAE(() -> r.longs(-1L, -1L));
executeAndCatchIAE(() -> r.longs(10, 1L, -2L));
executeAndCatchIAE(() -> r.doubles(0.0, 0.0));
executeAndCatchIAE(() -> r.doubles(10, .5, .4));
}
private void executeAndCatchIAE(Runnable r) {
executeAndCatch(IllegalArgumentException.class, r);
}
private void executeAndCatch(Class<? extends Exception> expected, Runnable r) {
Exception caught = null;
try {
r.run();
}
catch (Exception e) {
caught = e;
}
assertNotNull(caught,
String.format("No Exception was thrown, expected an Exception of %s to be thrown",
expected.getName()));
Assert.assertTrue(expected.isInstance(caught),
String.format("Exception thrown %s not an instance of %s",
caught.getClass().getName(), expected.getName()));
}
/**
* A parallel sized stream of ints generates the given number of values
*/
public void testIntsCount() {
LongAdder counter = new LongAdder();
SplittableRandom r = new SplittableRandom();
long size = 0;
for (int reps = 0; reps < REPS; ++reps) {
counter.reset();
r.ints(size).parallel().forEach(x -> {counter.increment();});
assertEquals(counter.sum(), size);
size += 524959;
}
}
/**
* A parallel sized stream of longs generates the given number of values
*/
public void testLongsCount() {
LongAdder counter = new LongAdder();
SplittableRandom r = new SplittableRandom();
long size = 0;
for (int reps = 0; reps < REPS; ++reps) {
counter.reset();
r.longs(size).parallel().forEach(x -> {counter.increment();});
assertEquals(counter.sum(), size);
size += 524959;
}
}
/**
* A parallel sized stream of doubles generates the given number of values
*/
public void testDoublesCount() {
LongAdder counter = new LongAdder();
SplittableRandom r = new SplittableRandom();
long size = 0;
for (int reps = 0; reps < REPS; ++reps) {
counter.reset();
r.doubles(size).parallel().forEach(x -> {counter.increment();});
assertEquals(counter.sum(), size);
size += 524959;
}
}
/**
* Each of a parallel sized stream of bounded ints is within bounds
*/
public void testBoundedInts() {
AtomicInteger fails = new AtomicInteger(0);
SplittableRandom r = new SplittableRandom();
long size = 12345L;
for (int least = -15485867; least < MAX_INT_BOUND; least += 524959) {
for (int bound = least + 2; bound > least && bound < MAX_INT_BOUND; bound += 67867967) {
final int lo = least, hi = bound;
r.ints(size, lo, hi).parallel().
forEach(x -> {if (x < lo || x >= hi)
fails.getAndIncrement(); });
}
}
assertEquals(fails.get(), 0);
}
/**
* Each of a parallel sized stream of bounded longs is within bounds
*/
public void testBoundedLongs() {
AtomicInteger fails = new AtomicInteger(0);
SplittableRandom r = new SplittableRandom();
long size = 123L;
for (long least = -86028121; least < MAX_LONG_BOUND; least += 1982451653L) {
for (long bound = least + 2; bound > least && bound < MAX_LONG_BOUND; bound += Math.abs(bound * 7919)) {
final long lo = least, hi = bound;
r.longs(size, lo, hi).parallel().
forEach(x -> {if (x < lo || x >= hi)
fails.getAndIncrement(); });
}
}
assertEquals(fails.get(), 0);
}
/**
* Each of a parallel sized stream of bounded doubles is within bounds
*/
public void testBoundedDoubles() {
AtomicInteger fails = new AtomicInteger(0);
SplittableRandom r = new SplittableRandom();
long size = 456;
for (double least = 0.00011; least < 1.0e20; least *= 9) {
for (double bound = least * 1.0011; bound < 1.0e20; bound *= 17) {
final double lo = least, hi = bound;
r.doubles(size, lo, hi).parallel().
forEach(x -> {if (x < lo || x >= hi)
fails.getAndIncrement(); });
}
}
assertEquals(fails.get(), 0);
}
/**
* A parallel unsized stream of ints generates at least 100 values
*/
public void testUnsizedIntsCount() {
LongAdder counter = new LongAdder();
SplittableRandom r = new SplittableRandom();
long size = 100;
r.ints().limit(size).parallel().forEach(x -> {counter.increment();});
assertEquals(counter.sum(), size);
}
/**
* A parallel unsized stream of longs generates at least 100 values
*/
public void testUnsizedLongsCount() {
LongAdder counter = new LongAdder();
SplittableRandom r = new SplittableRandom();
long size = 100;
r.longs().limit(size).parallel().forEach(x -> {counter.increment();});
assertEquals(counter.sum(), size);
}
/**
* A parallel unsized stream of doubles generates at least 100 values
*/
public void testUnsizedDoublesCount() {
LongAdder counter = new LongAdder();
SplittableRandom r = new SplittableRandom();
long size = 100;
r.doubles().limit(size).parallel().forEach(x -> {counter.increment();});
assertEquals(counter.sum(), size);
}
/**
* A sequential unsized stream of ints generates at least 100 values
*/
public void testUnsizedIntsCountSeq() {
LongAdder counter = new LongAdder();
SplittableRandom r = new SplittableRandom();
long size = 100;
r.ints().limit(size).forEach(x -> {counter.increment();});
assertEquals(counter.sum(), size);
}
/**
* A sequential unsized stream of longs generates at least 100 values
*/
public void testUnsizedLongsCountSeq() {
LongAdder counter = new LongAdder();
SplittableRandom r = new SplittableRandom();
long size = 100;
r.longs().limit(size).forEach(x -> {counter.increment();});
assertEquals(counter.sum(), size);
}
/**
* A sequential unsized stream of doubles generates at least 100 values
*/
public void testUnsizedDoublesCountSeq() {
LongAdder counter = new LongAdder();
SplittableRandom r = new SplittableRandom();
long size = 100;
r.doubles().limit(size).forEach(x -> {counter.increment();});
assertEquals(counter.sum(), size);
}
}
/*
* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package org.openjdk.tests.java.util;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.Spliterator;
import java.util.SplittableRandom;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.DoubleStream;
import java.util.stream.DoubleStreamTestScenario;
import java.util.stream.IntStream;
import java.util.stream.IntStreamTestScenario;
import java.util.stream.LongStream;
import java.util.stream.LongStreamTestScenario;
import java.util.stream.OpTestCase;
import java.util.stream.StreamSupport;
import java.util.stream.TestData;
@Test
public class SplittableRandomTest extends OpTestCase {
static class RandomBoxedSpliterator<T> implements Spliterator<T> {
final SplittableRandom rng;
long index;
final long fence;
final Function<SplittableRandom, T> rngF;
RandomBoxedSpliterator(SplittableRandom rng, long index, long fence, Function<SplittableRandom, T> rngF) {
this.rng = rng;
this.index = index;
this.fence = fence;
this.rngF = rngF;
}
public RandomBoxedSpliterator<T> trySplit() {
long i = index, m = (i + fence) >>> 1;
return (m <= i) ? null :
new RandomBoxedSpliterator<>(rng.split(), i, index = m, rngF);
}
public long estimateSize() {
return fence - index;
}
public int characteristics() {
return (Spliterator.SIZED | Spliterator.SUBSIZED |
Spliterator.NONNULL | Spliterator.IMMUTABLE);
}
@Override
public boolean tryAdvance(Consumer<? super T> consumer) {
if (consumer == null) throw new NullPointerException();
long i = index, f = fence;
if (i < f) {
consumer.accept(rngF.apply(rng));
index = i + 1;
return true;
}
return false;
}
}
static final int SIZE = 1 << 16;
// Ensure there is a range of a power of 2
static final int[] BOUNDS = {256};
static final int[] ORIGINS = {-16, 0, 16};
static <T extends Comparable<T>> ResultAsserter<Iterable<T>> randomAsserter(int size, T origin, T bound) {
return (act, exp, ord, par) -> {
int count = 0;
Set<Comparable<T>> values = new HashSet<>();
for (Comparable<T> t : act) {
if (origin.compareTo(bound) < 0) {
assertTrue(t.compareTo(origin) >= 0);
assertTrue(t.compareTo(bound) < 0);
}
values.add(t);
count++;
}
assertEquals(count, size);
// Assert that at least one different result is produced
// For the size of the data it is highly improbable that this
// will cause a false negative (i.e. a false failure)
assertTrue(values.size() > 1);
};
}
@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)
});
// Bounded
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][]);
}
@Test(dataProvider = "ints")
public void testInts(TestData.OfInt data, ResultAsserter<Iterable<Integer>> ra) {
withData(data).
stream(s -> s).
without(IntStreamTestScenario.PAR_STREAM_TO_ARRAY_CLEAR_SIZED).
resultAsserter(ra).
exercise();
}
@DataProvider(name = "longs")
public static Object[][] longsDataProvider() {
List<Object[]> data = new ArrayList<>();
// Function to create a stream using a RandomBoxedSpliterator
Function<Function<SplittableRandom, Long>, LongStream> rbsf =
sf -> StreamSupport.stream(new RandomBoxedSpliterator<>(new SplittableRandom(), 0, SIZE, sf), false).
mapToLong(i -> i);
// Unbounded
data.add(new Object[]{
TestData.Factory.ofLongSupplier(
String.format("new SplittableRandom().longs().limit(%d)", SIZE),
() -> new SplittableRandom().longs().limit(SIZE)),
randomAsserter(SIZE, Long.MAX_VALUE, 0L)
});
data.add(new Object[]{
TestData.Factory.ofLongSupplier(
String.format("new SplittableRandom().longs(%d)", SIZE),
() -> new SplittableRandom().longs(SIZE)),
randomAsserter(SIZE, Long.MAX_VALUE, 0L)
});
data.add(new Object[]{
TestData.Factory.ofLongSupplier(
String.format("new RandomBoxedSpliterator(0, %d, sr -> sr.nextLong())", SIZE),
() -> rbsf.apply(sr -> sr.nextLong())),
randomAsserter(SIZE, Long.MAX_VALUE, 0L)
});
// Bounded
for (int b : BOUNDS) {
for (int o : ORIGINS) {
final long origin = o;
final long bound = b;
data.add(new Object[]{
TestData.Factory.ofLongSupplier(
String.format("new SplittableRandom().longs(%d, %d).limit(%d)", origin, bound, SIZE),
() -> new SplittableRandom().longs(origin, bound).limit(SIZE)),
randomAsserter(SIZE, origin, bound)
});
data.add(new Object[]{
TestData.Factory.ofLongSupplier(
String.format("new SplittableRandom().longs(%d, %d, %d)", SIZE, origin, bound),
() -> new SplittableRandom().longs(SIZE, origin, bound)),
randomAsserter(SIZE, origin, bound)
});
if (origin == 0) {
data.add(new Object[]{
TestData.Factory.ofLongSupplier(
String.format("new RandomBoxedSpliterator(0, %d, sr -> sr.nextLong(%d))", SIZE, bound),
() -> rbsf.apply(sr -> sr.nextLong(bound))),
randomAsserter(SIZE, origin, bound)
});
}
data.add(new Object[]{
TestData.Factory.ofLongSupplier(
String.format("new RandomBoxedSpliterator(0, %d, sr -> sr.nextLong(%d, %d))", SIZE, origin, bound),
() -> rbsf.apply(sr -> sr.nextLong(origin, bound))),
randomAsserter(SIZE, origin, bound)
});
}
}
return data.toArray(new Object[0][]);
}
@Test(dataProvider = "longs")
public void testLongs(TestData.OfLong data, ResultAsserter<Iterable<Long>> ra) {
withData(data).
stream(s -> s).
without(LongStreamTestScenario.PAR_STREAM_TO_ARRAY_CLEAR_SIZED).
resultAsserter(ra).
exercise();
}
@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)
});
// Bounded
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][]);
}
@Test(dataProvider = "doubles")
public void testDoubles(TestData.OfDouble data, ResultAsserter<Iterable<Double>> ra) {
withData(data).
stream(s -> s).
without(DoubleStreamTestScenario.PAR_STREAM_TO_ARRAY_CLEAR_SIZED).
resultAsserter(ra).
exercise();
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册