use of java8.util.Spliterator 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));
}
}
}
}
use of java8.util.Spliterator in project streamsupport by stefan-zobel.
the class Collection8Test method testElementRemovalDuringTraversal.
/**
* All elements removed in the middle of CONCURRENT traversal.
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test(dataProvider = "Source")
public void testElementRemovalDuringTraversal(String description, Supplier<CollectionImplementation> sci) {
CollectionImplementation impl = sci.get();
if (HAS_JAVA8_SPLITERATOR_BUG && LinkedBlockingQueue.class.equals(impl.klazz())) {
// https://bugs.openjdk.java.net/browse/JDK-8171051
return;
}
Collection<Object> c = impl.emptyCollection();
ThreadLocalRandom rnd = ThreadLocalRandom.current();
int n = rnd.nextInt(6);
ArrayList copy = new ArrayList();
for (int i = 0; i < n; i++) {
Object x = impl.makeElement(i);
copy.add(x);
c.add(x);
}
ArrayList iterated = new ArrayList();
ArrayList spliterated = new ArrayList();
Spliterator<?> s = Spliterators.spliterator(c);
Iterator<?> it = c.iterator();
for (int i = rnd.nextInt(n + 1); --i >= 0; ) {
assertTrue(s.tryAdvance(spliterated::add));
if (rnd.nextBoolean())
assertTrue(it.hasNext());
iterated.add(it.next());
}
Consumer alwaysThrows = e -> {
throw new AssertionError();
};
if (s.hasCharacteristics(Spliterator.CONCURRENT)) {
// TODO: many more removal methods
c.clear();
if (testImplementationDetails && !(c instanceof java.util.concurrent.ArrayBlockingQueue)) {
if (rnd.nextBoolean())
assertFalse(s.tryAdvance(alwaysThrows));
else
s.forEachRemaining(alwaysThrows);
}
if (it.hasNext())
iterated.add(it.next());
if (rnd.nextBoolean())
assertIteratorExhausted(it);
}
assertTrue(copy.containsAll(iterated));
assertTrue(copy.containsAll(spliterated));
}
use of java8.util.Spliterator in project streamsupport by stefan-zobel.
the class Collection8Test method testTraversalEquivalence.
/**
* Various ways of traversing a collection yield same elements
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test(dataProvider = "Source")
public void testTraversalEquivalence(String description, Supplier<CollectionImplementation> sci) {
CollectionImplementation impl = sci.get();
Collection c = impl.emptyCollection();
ThreadLocalRandom rnd = ThreadLocalRandom.current();
int n = rnd.nextInt(6);
for (int i = 0; i < n; i++) c.add(impl.makeElement(i));
ArrayList iterated = new ArrayList();
ArrayList iteratedForEachRemaining = new ArrayList();
ArrayList tryAdvanced = new ArrayList();
ArrayList spliterated = new ArrayList();
ArrayList splitonced = new ArrayList();
ArrayList forEached = new ArrayList();
ArrayList streamForEached = new ArrayList();
ConcurrentLinkedQueue parallelStreamForEached = new ConcurrentLinkedQueue();
ArrayList removeIfed = new ArrayList();
for (Object x : c) iterated.add(x);
Iterators.forEachRemaining(c.iterator(), iteratedForEachRemaining::add);
for (Spliterator s = Spliterators.spliterator(c); s.tryAdvance(tryAdvanced::add); ) {
}
Spliterators.spliterator(c).forEachRemaining(spliterated::add);
{
// trySplit returns "strict prefix"
Spliterator<?> s1 = Spliterators.spliterator(c), s2 = s1.trySplit();
if (s2 != null)
s2.forEachRemaining(splitonced::add);
s1.forEachRemaining(splitonced::add);
}
Iterables.forEach(c, forEached::add);
StreamSupport.stream(c).forEach(streamForEached::add);
StreamSupport.parallelStream(c).forEach(parallelStreamForEached::add);
Iterables.removeIf(c, e -> {
removeIfed.add(e);
return false;
});
boolean ordered = Spliterators.spliterator(c).hasCharacteristics(Spliterator.ORDERED);
if (c instanceof List || c instanceof Deque)
assertTrue(ordered);
HashSet<?> cset = new HashSet<>(c);
assertEquals(cset, new HashSet<>(parallelStreamForEached));
if (ordered) {
assertEquals(iterated, iteratedForEachRemaining);
assertEquals(iterated, tryAdvanced);
assertEquals(iterated, spliterated);
assertEquals(iterated, splitonced);
assertEquals(iterated, forEached);
assertEquals(iterated, streamForEached);
assertEquals(iterated, removeIfed);
} else {
assertEquals(cset, new HashSet<>(iterated));
assertEquals(cset, new HashSet<>(iteratedForEachRemaining));
assertEquals(cset, new HashSet<>(tryAdvanced));
assertEquals(cset, new HashSet<>(spliterated));
assertEquals(cset, new HashSet<>(splitonced));
assertEquals(cset, new HashSet<>(forEached));
assertEquals(cset, new HashSet<>(streamForEached));
assertEquals(cset, new HashSet<>(removeIfed));
}
if (c instanceof Deque) {
Deque<?> d = (Deque<?>) c;
ArrayList descending = new ArrayList();
ArrayList descendingForEachRemaining = new ArrayList();
for (Iterator<?> it = d.descendingIterator(); it.hasNext(); ) descending.add(it.next());
Iterators.forEachRemaining(d.descendingIterator(), e -> descendingForEachRemaining.add(e));
Collections.reverse(descending);
Collections.reverse(descendingForEachRemaining);
assertEquals(iterated, descending);
assertEquals(iterated, descendingForEachRemaining);
}
}
use of java8.util.Spliterator in project streamsupport by stefan-zobel.
the class Collection8Test method testLateBindingStyle.
/**
* Spliterators are either IMMUTABLE or truly late-binding or, if
* concurrent, use the same "late-binding style" of returning
* elements added between creation and first use.
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test(dataProvider = "Source")
public void testLateBindingStyle(String description, Supplier<CollectionImplementation> sci) {
if (!testImplementationDetails)
return;
CollectionImplementation impl = sci.get();
// for jdk8
if (impl.klazz() == ArrayList.class)
return;
// Immutable (snapshot) spliterators are exempt
if (Spliterators.spliterator(impl.emptyCollection()).hasCharacteristics(Spliterator.IMMUTABLE))
return;
final Object one = impl.makeElement(1);
{
final Collection c = impl.emptyCollection();
final Spliterator<?> split = Spliterators.spliterator(c);
c.add(one);
assertTrue(split.tryAdvance(e -> {
assertSame(e, one);
}));
assertFalse(split.tryAdvance(e -> {
throw new AssertionError();
}));
assertTrue(c.contains(one));
}
{
final AtomicLong count = new AtomicLong(0);
final Collection c = impl.emptyCollection();
final Spliterator<?> split = Spliterators.spliterator(c);
c.add(one);
split.forEachRemaining(e -> {
assertSame(e, one);
count.getAndIncrement();
});
assertEquals(1L, count.get());
assertFalse(split.tryAdvance(e -> {
throw new AssertionError();
}));
assertTrue(c.contains(one));
}
}
use of java8.util.Spliterator in project streamsupport by stefan-zobel.
the class Collection8Test method testStickySpliteratorExhaustion.
/**
* Concurrent Spliterators, once exhausted, stay exhausted.
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test(dataProvider = "Source")
public void testStickySpliteratorExhaustion(String description, Supplier<CollectionImplementation> sci) throws Throwable {
CollectionImplementation impl = sci.get();
if (HAS_JAVA8_SPLITERATOR_BUG && PriorityBlockingQueue.class.equals(impl.klazz())) {
// https://bugs.openjdk.java.net/browse/JDK-8172023
return;
}
if (!impl.isConcurrent())
return;
if (!testImplementationDetails)
return;
final ThreadLocalRandom rnd = ThreadLocalRandom.current();
final Consumer<?> alwaysThrows = e -> {
throw new AssertionError();
};
final Collection c = impl.emptyCollection();
final Spliterator s = Spliterators.spliterator(c);
if (rnd.nextBoolean()) {
assertFalse(s.tryAdvance(alwaysThrows));
} else {
s.forEachRemaining(alwaysThrows);
}
final Object one = impl.makeElement(1);
// Spliterator should not notice added element
c.add(one);
if (rnd.nextBoolean()) {
assertFalse(s.tryAdvance(alwaysThrows));
} else {
s.forEachRemaining(alwaysThrows);
}
}
Aggregations