use of java8.util.stream.Stream in project streamsupport by stefan-zobel.
the class CollectorsTest method testFlatMappingClose.
@Test
public void testFlatMappingClose() {
Function<Integer, Integer> classifier = i -> i;
AtomicInteger ai = new AtomicInteger();
Function<Integer, Stream<Integer>> flatMapper = i -> RefStreams.of(i, i).onClose(ai::getAndIncrement);
Map<Integer, List<Integer>> m = RefStreams.of(1, 2).collect(groupingBy(classifier, flatMapping(flatMapper, toList())));
assertEquals(m.size(), ai.get());
}
use of java8.util.stream.Stream in project streamsupport by stefan-zobel.
the class ConcatTest method assertLongConcat.
private void assertLongConcat(Stream<Integer> s1, Stream<Integer> s2, boolean parallel, boolean ordered) {
LongStream result = LongStreams.concat(s1.mapToLong(Integer::longValue), s2.mapToLong(Integer::longValue));
assertEquals(result.isParallel(), parallel);
assertConcatContent(result.spliterator(), ordered, StreamSupport.stream(expected).mapToLong(Integer::longValue).spliterator());
}
use of java8.util.stream.Stream in project streamsupport by stefan-zobel.
the class SequentialOpTest method testLazy.
@SuppressWarnings("unchecked")
@Test(dataProvider = "StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class, groups = { "serialization-hostile" })
public void testLazy(String name, TestData.OfRef<Integer> data) {
Function<Integer, Integer> id = LambdaTestHelpers.identity();
AtomicInteger counter = new AtomicInteger();
Supplier<Stream<Integer>>[] suppliers = new Supplier[] { () -> data.stream(), () -> data.parallelStream() };
UnaryOperator<Stream<Integer>>[] configs = new UnaryOperator[] { (UnaryOperator<Stream<Integer>>) s -> s.peek(e -> {
counter.incrementAndGet();
}), (UnaryOperator<Stream<Integer>>) s -> s.map(id).peek(e -> {
counter.incrementAndGet();
}).sequential().map(id), (UnaryOperator<Stream<Integer>>) s -> s.map(id).peek(e -> {
counter.incrementAndGet();
}).parallel().map(id), (UnaryOperator<Stream<Integer>>) s -> s.sequential().map(id).peek(e -> {
counter.incrementAndGet();
}).map(id), (UnaryOperator<Stream<Integer>>) s -> s.parallel().map(id).peek(e -> {
counter.incrementAndGet();
}).map(id) };
for (int i = 0; i < suppliers.length; i++) {
setContext("supplierIndex", i);
Supplier<Stream<Integer>> supp = suppliers[i];
for (int j = 0; j < configs.length; j++) {
setContext("configIndex", j);
UnaryOperator<Stream<Integer>> config = configs[j];
counter.set(0);
Stream<Integer> stream = config.apply(supp.get());
assertEquals(0, counter.get());
Iterator<Integer> iterator = stream.iterator();
assertEquals(0, counter.get());
if (iterator.hasNext())
iterator.next();
assertTrue(data.size() == 0 || counter.get() > 0);
counter.set(0);
stream = config.apply(supp.get());
Spliterator<Integer> spliterator = stream.spliterator();
assertEquals(0, counter.get());
spliterator.forEachRemaining(e -> {
});
assertTrue(data.size() == 0 || counter.get() > 0);
}
}
}
use of java8.util.stream.Stream in project streamsupport by stefan-zobel.
the class SplittableRandomTest method testUnsizedIntsCount.
/**
* 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(size, counter.sum());
}
use of java8.util.stream.Stream in project streamsupport by stefan-zobel.
the class SplittableRandomTest method testBoundedLongs.
/**
* 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(0, fails.get());
}
Aggregations