use of java8.util.Spliterator 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());
}
use of java8.util.Spliterator 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());
}
use of java8.util.Spliterator 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));
}
}
}
}
use of java8.util.Spliterator 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));
}
}
}
}
use of java8.util.Spliterator in project streamsupport by stefan-zobel.
the class Collection8Test method testDetectRaces.
/**
* Motley crew of threads concurrently randomly hammer the collection.
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test(dataProvider = "Source")
public void testDetectRaces(String description, Supplier<CollectionImplementation> sci) throws Throwable {
CollectionImplementation impl = sci.get();
if (!impl.isConcurrent())
return;
if (HAS_JAVA8_SPLITERATOR_BUG && LinkedBlockingDeque.class.equals(impl.klazz())) {
// https://bugs.openjdk.java.net/browse/JDK-8169739
return;
}
if (CopyOnWriteArraySet.class.equals(impl.klazz())) {
return;
}
final ThreadLocalRandom rnd = ThreadLocalRandom.current();
final Collection c = impl.emptyCollection();
final long testDurationMillis = expensiveTests ? LONG_DELAY_MS : timeoutMillis();
final AtomicBoolean done = new AtomicBoolean(false);
final Object one = impl.makeElement(1);
final Object two = impl.makeElement(2);
final Consumer checkSanity = x -> assertTrue(x == one || x == two);
final Consumer<Object[]> checkArraySanity = array -> {
// assertTrue(array.length <= 2); // duplicates are permitted
for (Object x : array) assertTrue(x == one || x == two);
};
final Object[] emptyArray = (Object[]) java.lang.reflect.Array.newInstance(one.getClass(), 0);
final List<Future<?>> futures;
// register this thread
final Phaser threadsStarted = new Phaser(1);
final Runnable[] frobbers = { () -> Iterables.forEach(c, checkSanity), () -> StreamSupport.stream(c).forEach(checkSanity), () -> StreamSupport.parallelStream(c).forEach(checkSanity), () -> Spliterators.spliterator(c).trySplit(), () -> {
Spliterator<?> s = Spliterators.spliterator(c);
s.tryAdvance(checkSanity);
s.trySplit();
}, () -> {
Spliterator<?> s = Spliterators.spliterator(c);
do {
} while (s.tryAdvance(checkSanity));
}, () -> {
for (Object x : c) checkSanity.accept(x);
}, () -> checkArraySanity.accept(c.toArray()), () -> checkArraySanity.accept(c.toArray(emptyArray)), () -> {
Object[] a = new Object[5];
Object three = impl.makeElement(3);
Arrays.fill(a, 0, a.length, three);
Object[] x = c.toArray(a);
if (x == a)
for (int i = 0; i < a.length && a[i] != null; i++) checkSanity.accept(a[i]);
else
// A careful reading of the spec does not support:
// for (i++; i < a.length; i++) assertSame(three, a[i]);
checkArraySanity.accept(x);
}, adderRemover(c, one), adderRemover(c, two) };
final List<Runnable> tasks = J8Arrays.stream(frobbers).filter(// random subset
task -> rnd.nextBoolean()).map(task -> (Runnable) () -> {
threadsStarted.arriveAndAwaitAdvance();
while (!done.get()) task.run();
}).collect(Collectors.<Runnable>toList());
final ExecutorService pool = Executors.newCachedThreadPool();
PoolCleaner cleaner = null;
try {
cleaner = cleaner(pool, done);
threadsStarted.bulkRegister(tasks.size());
futures = StreamSupport.stream(tasks).map(pool::submit).collect(Collectors.toList());
threadsStarted.arriveAndDeregister();
Thread.sleep(testDurationMillis);
} finally {
if (cleaner != null) {
cleaner.close();
}
}
for (Future<?> future : futures) assertNull(future.get(0L, MILLISECONDS));
}
Aggregations